Skip to content

Commit

Permalink
Merge 80ed0d3 into ee6dc0a
Browse files Browse the repository at this point in the history
  • Loading branch information
Yaroslav Nikonorov committed May 11, 2017
2 parents ee6dc0a + 80ed0d3 commit cdde30a
Show file tree
Hide file tree
Showing 18 changed files with 648 additions and 56 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -61,3 +61,4 @@ target/
#Ipython Notebook
.ipynb_checkpoints
.pypirc
.DS_Store
18 changes: 7 additions & 11 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -3,20 +3,16 @@ python:
- "2.7"

install:
- pushd package
- pip install --extra-index-url https://testpypi.python.org/pypi -r requirements.txt
- 'if [ ${TRAVIS_BRANCH} = "master" ]; then
pip install --extra-index-url https://testpypi.python.org/pypi -r src/requirements.txt;
else
pip install --extra-index-url https://testpypi.python.org/pypi -r dev_requirements.txt;
fi'
- pip install -r test_requirements.txt
- popd
- pushd breaking_point_shell/src
- pip install -r test_requirements.txt
- popd
- pip install coveralls

script:
- pushd package
- python setup.py develop
- popd
- python runtests.py --with-coverage --cover-package=package
- nosetests --with-coverage --cover-package=src --where=tests

after_success:
- coveralls
- coveralls
7 changes: 6 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1 +1,6 @@
# BreakingPoint-Controller-Shell
# BreakingPoint-Controller-Shell
![alt tag](https://travis-ci.org/QualiSystems/BreakingPoint-Controller-Shell.svg?branch=dev)
[![Coverage Status](https://coveralls.io/repos/github/QualiSystems/BreakingPoint-Controller-Shell/badge.svg?branch=dev)](https://coveralls.io/github/QualiSystems/BreakingPoint-Controller-Shell?branch=dev)
[![PyPI version](https://badge.fury.io/py/BreakingPoint-Controller-Shell.svg)](https://badge.fury.io/py/BreakingPoint-Controller-Shell)
[![Dependency Status](https://dependencyci.com/github/QualiSystems/BreakingPoint-Controller-Shell/badge)](https://dependencyci.com/github/QualiSystems/BreakingPoint-Controller-Shell)
[![Stories in Ready](https://badge.waffle.io/QualiSystems/BreakingPoint-Controller-Shell.svg?label=ready&title=Ready)](http://waffle.io/QualiSystems/BreakingPoint-Controller-Shell)
15 changes: 0 additions & 15 deletions datamodel/shellconfig.xml

This file was deleted.

3 changes: 3 additions & 0 deletions dev_requirements.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
portalocker==1.1.0
cloudshell-shell-core>=3.1,<3.2
-e git+https://github.com/QualiSystems/cloudshell-tg-breaking-point.git@dev#egg=cloudshell-tg-breaking-point
17 changes: 0 additions & 17 deletions runtests.py

This file was deleted.

2 changes: 1 addition & 1 deletion src/bp_controller/runners/bp_runner_pool.py
Original file line number Diff line number Diff line change
Expand Up @@ -64,5 +64,5 @@ def actual_runner(self, context):
return runner_locker

def close_all_runners(self):
for runner in self._runners:
for runner in self._runners.values():
runner.close()
11 changes: 9 additions & 2 deletions src/bp_controller/runners/bp_test_runner.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ def __init__(self, context, logger, api):
self.__test_execution_flow = None
self.__test_statistics_flow = None
self.__test_results_flow = None
self.__test_configuration_file_flow = None
self.__reservation_details = None
self.__port_reservation_helper = None

Expand Down Expand Up @@ -96,6 +97,13 @@ def _test_results_flow(self):
self.__test_results_flow = BPResultsFlow(self._session_context_manager, self.logger)
return self.__test_results_flow

@property
def _test_configuration_file_flow(self):
if not self.__test_configuration_file_flow:
self.__test_configuration_file_flow = BPLoadConfigurationFileFlow(self._session_context_manager,
self.logger)
return self.__test_configuration_file_flow

@property
def _cs_reservation_details(self):
"""
Expand Down Expand Up @@ -149,8 +157,7 @@ def load_configuration(self, file_path):
:param file_path:
:return:
"""
self._test_name = BPLoadConfigurationFileFlow(self._session_context_manager,
self.logger).load_configuration(file_path)
self._test_name = self._test_configuration_file_flow.load_configuration(file_path)
test_model = ElementTree.parse(file_path).getroot().find('testmodel')
network_name = test_model.get('network')
interfaces = []
Expand Down
10 changes: 3 additions & 7 deletions src/bp_controller/utils/file_based_lock.py
Original file line number Diff line number Diff line change
@@ -1,16 +1,12 @@
import os
import portalocker


class FileBasedLock(object):
def __init__(self, lock_file_path):
if os.path.exists(lock_file_path):
self.__file_descriptor = open(lock_file_path, 'r')
else:
self.__file_descriptor = open(lock_file_path, 'w')
self._file_descriptor = open(lock_file_path, 'w')

def __enter__(self):
portalocker.lock(self.__file_descriptor, portalocker.LOCK_EX)
portalocker.lock(self._file_descriptor, portalocker.LOCK_EX)

def __exit__(self, exc_type, exc_val, exc_tb):
self.__file_descriptor.close()
self._file_descriptor.close()
3 changes: 1 addition & 2 deletions src/requirements.txt
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
portalocker==1.1.0
cloudshell-tg-breaking-point>=1.0,<1.1
cloudshell-shell-core>=3.1,<3.2
cloudshell-automation-api>=7.0,<7.1
cloudshell-shell-core>=3.1,<3.2
4 changes: 4 additions & 0 deletions test_requirements.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
nose
coverage
unittest2
mock
2 changes: 2 additions & 0 deletions tests/bp_controller/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
from pkgutil import extend_path
__path__ = extend_path(__path__, __name__)
2 changes: 2 additions & 0 deletions tests/bp_controller/runners/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
from pkgutil import extend_path
__path__ = extend_path(__path__, __name__)
96 changes: 96 additions & 0 deletions tests/bp_controller/runners/test_bp_runner_pool.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,96 @@
from mock import Mock, patch
from unittest2 import TestCase

from bp_controller.runners.bp_runner_pool import InstanceLocker, BPRunnersPool


class TestInstanceLocker(TestCase):
def setUp(self):
self._lock = Mock()
self._locked_instance = Mock()
self._instance = self._create_instance()

@patch('bp_controller.runners.bp_runner_pool.Lock')
def _create_instance(self, lock_class):
lock_class.return_value = self._lock
instance = InstanceLocker(self._locked_instance)
lock_class.assert_called_once_with()
return instance

def test_init(self):
self.assertIs(self._instance._instance, self._locked_instance)

def test_instance_prop(self):
self.assertIs(self._instance.instance, self._locked_instance)

def test_enter_exit(self):
with self._instance as instance:
self.assertIs(instance, self._locked_instance)
self._lock.acquire.assert_called_once_with()
self._lock.release.assert_called_once_with()


class TestBPRunnersPool(TestCase):
def setUp(self):
self._context = Mock()
self._instance = BPRunnersPool()

def test_init(self):
self.assertEqual(self._instance._runners, {})

@patch('bp_controller.runners.bp_runner_pool.InstanceLocker')
@patch('bp_controller.runners.bp_runner_pool.BPTestRunner')
@patch('bp_controller.runners.bp_runner_pool.get_logger_with_thread_id')
@patch('bp_controller.runners.bp_runner_pool.get_api')
def test_actual_runner_call_logger_and_api(self, get_api_func, get_logger_func, test_runner_class,
instance_locker_class):
actual_runner = self._instance.actual_runner(self._context)
get_api_func.assert_called_once_with(self._context)
get_logger_func.assert_called_once_with(self._context)

@patch('bp_controller.runners.bp_runner_pool.InstanceLocker')
@patch('bp_controller.runners.bp_runner_pool.BPTestRunner')
@patch('bp_controller.runners.bp_runner_pool.get_logger_with_thread_id')
@patch('bp_controller.runners.bp_runner_pool.get_api')
def test_actual_runner_new_instance(self, get_api_func, get_logger_func, test_runner_class,
instance_locker_class):
logger_instance = Mock()
api_instance = Mock()
get_logger_func.return_value = logger_instance
get_api_func.return_value = api_instance
instance_locker_inst = Mock()
test_runner_inst = Mock()
instance_locker_class.return_value = instance_locker_inst
test_runner_class.return_value = test_runner_inst
reservation_id = Mock()
self._context.reservation.reservation_id = reservation_id
self.assertIs(self._instance.actual_runner(self._context), instance_locker_inst)
instance_locker_class.assert_called_once_with(test_runner_inst)
test_runner_class.assert_called_once_with(self._context, logger_instance, api_instance)
self.assertIs(self._instance._runners[reservation_id], instance_locker_inst)

@patch('bp_controller.runners.bp_runner_pool.InstanceLocker')
@patch('bp_controller.runners.bp_runner_pool.BPTestRunner')
@patch('bp_controller.runners.bp_runner_pool.get_logger_with_thread_id')
@patch('bp_controller.runners.bp_runner_pool.get_api')
def test_actual_runner_existing_instance(self, get_api_func, get_logger_func, test_runner_class,
instance_locker_class):
logger_instance = Mock()
api_instance = Mock()
get_logger_func.return_value = logger_instance
get_api_func.return_value = api_instance
reservation_id = Mock()
self._context.reservation.reservation_id = reservation_id
runner_instance = Mock()
self._instance._runners[reservation_id] = runner_instance
runner_locker = self._instance.actual_runner(self._context)
self.assertIs(runner_locker, runner_instance)
self.assertIs(runner_locker.instance.logger, logger_instance)
self.assertIs(runner_locker.instance.api, api_instance)
self.assertIs(runner_locker.instance.context, self._context)

def test_class_all_runners(self):
runner = Mock()
self._instance._runners[Mock()] = runner
self._instance.close_all_runners()
runner.close.assert_called_once_with()

0 comments on commit cdde30a

Please sign in to comment.