def test_config_get_service_by_name(self): self.mock_safe_load.return_value = self.conf_data conf = Config() for service in iter(self.conf_data['services']): self.assertDictEqual(self.conf_data['services'][service], conf.get_service_by_name(service))
def test_config_get_service_by_name_empty(self): self.mock_safe_load.return_value = {} conf = Config() for service in iter(self.conf_data['services']): self.assertIsNone(conf.get_service_by_name(service))
def test_config_get_all_services_empty(self): self.mock_safe_load.return_value = {} conf = Config() self.assertListEqual(list({}), conf.get_all_services())
def test_config_get_all_services(self): self.mock_safe_load.return_value = self.conf_data conf = Config() self.assertEqual(list(self.conf_data['services'].keys()), conf.get_all_services())
def test_config_init(self): conf = Config() self.assertIsInstance(conf, Config)
from flask import jsonify from mocksi.api import api from mocksi.utils.config import Config config = Config() @api.route('/services') def get_services(): return jsonify(config.get_all_services())
def __init__(self): self.client = APIClient('unix://var/run/docker.sock') self.filtered_statuses = ('running', 'restarting', 'paused', 'exited') self.config = Config()
class DockerCli: def __init__(self): self.client = APIClient('unix://var/run/docker.sock') self.filtered_statuses = ('running', 'restarting', 'paused', 'exited') self.config = Config() def _get_containers(self, filters=None): filters = filters if filters else dict() for status in self.filtered_statuses: filters.update({'status': status}) for container in self.client.containers( all=True, filters=filters ): img_name, _, img_version = container['Image'].partition(':') service = self.config.get_service_by_name(img_name) if service: instance = dict() instance['created'] = container['Created'] instance['id'] = container['Id'] instance['image'] = img_name for con_port in container['Ports']: if service['port'] is con_port['PrivatePort']: instance['port'] = con_port.get('PublicPort') else: instance['port'] = None instance['state'] = container['State'] instance['status'] = container['Status'] instance['version'] = img_version yield instance return def get_all_containers(self): containers = [] for container in self._get_containers(): if container: containers.append(container) return containers def get_container(self, by_id): for container in self._get_containers({'id': by_id}): return container raise NotFoundContainerException( 'Container was not found: {}'.format(by_id) ) def create_container(self, image): service = self.config.get_service_by_name(image) if service: container = self.client.create_container( image='{0}:{1}'.format(image, service['version']), ports=[service['port']], detach=True, host_config=self.client.create_host_config( port_bindings={service['port']: None} ) ) self.client.start(container=container['Id']) return self.get_container(container['Id']) raise NotFoundImageException('Image was not found: {}'.format(image)) def remove_container(self, by_id): try: self.client.remove_container( container=by_id, force=True, v=True ) except errors.NotFound as e: raise NotFoundContainerException(e) return {'status': 'OK'}