def test_get_service_valid(self): test_service = YamlConfigDocumentStub({'roles': ['rolename']}) app = YamlConfigDocumentStub({'services': {'test': test_service}}) command = module.Command.from_yaml( get_fixture_path(FIXTURE_BASE_PATH + 'valid_via_service.yml')) self.assertEqual('test', command.get_service(app))
def test_get_service_not_via_service(self): test_service = YamlConfigDocumentStub({'roles': ['rolename']}) app = YamlConfigDocumentStub({'services': {'test': test_service}}) command = module.Command.from_yaml( get_fixture_path(FIXTURE_BASE_PATH + 'valid_regular.yml')) with self.assertRaisesRegex( TypeError, 'get_service can only be used on "in service" commands.'): command.get_service(app)
def test_get_service_no_service_with_role(self): test_service = YamlConfigDocumentStub({'roles': []}) app = YamlConfigDocumentStub({'services': {'test': test_service}}) command = module.Command.from_yaml( get_fixture_path(FIXTURE_BASE_PATH + 'valid_via_service.yml')) with self.assertRaisesRegex( ValueError, 'Command .* can not run in service with role rolename: No service with this role found in the app.' ): command.get_service(app)
def test_domain_main(self): system = YamlConfigDocumentStub({'proxy': {'url': 'TEST-URL'}}) project = ProjectStub({'name': 'TEST-PROJECT'}, parent=system) app = YamlConfigDocumentStub({}, parent=project) service = module.Service({ '$name': 'TEST-SERVICE', 'roles': ['main'] }, parent=app) self.assertEqual('TEST-PROJECT.TEST-URL', service.domain())
def test_create_logging_path(self, get_project_meta_folder_mock: Mock, makedirs_mock: Mock): service_name = '__unit_test' service = YamlConfigDocumentStub({'$name': service_name}) service.get_project = MagicMock(return_value=ProjectStub({})) create_logging_path(service) makedirs_mock.assert_called_once_with( '/META' + os.sep + FOLDER_FOR_LOGGING + os.sep + service_name, exist_ok=True) get_project_meta_folder_mock.assert_called_once_with( ProjectStub.FOLDER)
def test_collect_volumes_no_roles(self, process_additional_volumes_mock: Mock, os_environ_mock: Mock): env = {} os_environ_mock.__getitem__.side_effect = env.__getitem__ os_environ_mock.__iter__.side_effect = env.__iter__ os_environ_mock.__contains__.side_effect = env.__contains__ cmd = self.fix_with_volumes expected = OrderedDict({ # Source code also has to be mounted in: ProjectStub.SRC_FOLDER: { 'bind': CONTAINER_SRC_PATH, 'mode': 'rw' }, # process_additional_volumes has to be called STUB_PAV__KEY: STUB_PAV__VAL }) # The project contains NO services matching the defined roles cmd.parent_doc = YamlConfigDocumentStub({"services": {}}, parent=ProjectStub({})) def get_services_by_role_mock(role): return [] cmd.parent_doc.get_services_by_role = get_services_by_role_mock actual = cmd.collect_volumes() self.assertEqual(expected, actual) self.assertIsInstance(actual, OrderedDict) process_additional_volumes_mock.assert_called_with( list(self.fix_with_volumes['additional_volumes'].values()), ProjectStub.FOLDER)
def test_resolve_alias_something_to_alias(self): # hello world command hello_world_command = YamlConfigDocumentStub({'hello': 'world'}) # The command we want to test cmd = module.Command({'aliases': 'hello_world'}) # The parent app of the command we want to test, that contains both commands cmd.parent_doc = YamlConfigDocumentStub({ 'commands': { 'hello_world': hello_world_command, 'our_test': cmd } }) # Make the mocked command's resolve_alias return itself. setattr(hello_world_command, 'resolve_alias', MagicMock(return_value=hello_world_command)) # Assert that we get the hello world command self.assertEqual(hello_world_command, cmd.resolve_alias()) # Make sure resolve_alias was actually called on our mocked command hello_world_command.resolve_alias.assert_called_once()
def test_get_logging_path_for(self, open_mock: Mock, chmod_mock: Mock, get_project_meta_folder_mock: Mock, rasc_mock: Mock): service_name = '__unit_test' log_name = 'xyzxyzxyz' service = YamlConfigDocumentStub({'$name': service_name}) service.get_project = MagicMock(return_value=ProjectStub({})) expected = '/META' + os.sep + FOLDER_FOR_LOGGING + os.sep + service_name + os.sep + 'SPECIAL_CHARS_REMOVED.log' self.assertEqual(expected, get_logging_path_for(service, log_name)) get_project_meta_folder_mock.assert_called_once_with( ProjectStub.FOLDER) rasc_mock.assert_called_once_with(log_name) open_mock.assert_called_once_with(expected, 'a') chmod_mock.assert_called_once_with(expected, 0o666)
def test_collect_volumes(self, process_config_mock: Mock, process_additional_volumes_mock: Mock, os_environ_mock: Mock): env = {} os_environ_mock.__getitem__.side_effect = env.__getitem__ os_environ_mock.__iter__.side_effect = env.__iter__ os_environ_mock.__contains__.side_effect = env.__contains__ # Fixture also wants config_from_roles A and B cmd = self.fix_with_volumes # Dict order is not checked here, because it can be arbitrary for config_from_roles. Order # is checked in the other collect_volume tests. expected = dict({ # Source code also has to be mounted in: ProjectStub.SRC_FOLDER: { 'bind': CONTAINER_SRC_PATH, 'mode': 'rw' }, # process_additional_volumes has to be called STUB_PAV__KEY: STUB_PAV__VAL, # config_from_roles : 'config1~/FROM_1~serviceRoleA1~False': { 'bind': '/TO_1', 'mode': 'STUB' }, 'config2~/FROM_2~serviceRoleA1~False': { 'bind': '/TO_2', 'mode': 'STUB' }, 'config2~/FROM_2~serviceRoleA2B1~False': { 'bind': '/TO_2', 'mode': 'STUB' }, 'config3~/FROM_3~serviceRoleA2B1~True': { 'bind': '/TO_3', 'mode': 'STUB' }, 'config3~/FROM_3~serviceRoleB2~True': { 'bind': '/TO_3', 'mode': 'STUB' }, }) # Config entries config1 = {'to': '/TO_1', 'from': '/FROM_1'} config2 = {'to': '/TO_2', 'from': '/FROM_2'} config3 = {'to': '/TO_3', 'from': '/FROM_3', 'force_recreate': True} # Services serviceRoleA1 = { "__UNIT_TEST_NAME": "serviceRoleA1", "roles": ["A"], "config": { "config1": config1, "config2": config2 } } # Is in two searched rules, must only be included once: serviceRoleA2B1 = { "__UNIT_TEST_NAME": "serviceRoleA2B1", "roles": ["A", "B"], "config": { "config2": config2, "config3": config3 } } serviceRoleB2 = { "__UNIT_TEST_NAME": "serviceRoleB2", "roles": ["B"], "config": { "config3": config3 } } # Has role C, should not be used: serviceRoleC1 = { "__UNIT_TEST_NAME": "serviceRoleC1", "roles": ["C"], "config": { "config1": config1, "config2": config2 } } # The project contains some services matching the defined roles cmd.parent_doc = YamlConfigDocumentStub( { 'services': { 'serviceRoleA1': serviceRoleA1, 'serviceRoleA2B1': serviceRoleA2B1, 'serviceRoleB2': serviceRoleB2, 'serviceRoleC1': serviceRoleC1 } }, parent=ProjectStub({})) def get_services_by_role_mock(role): if role == "A": return [serviceRoleA1, serviceRoleA2B1] if role == "B": return [serviceRoleA2B1, serviceRoleB2] if role == "C": return [serviceRoleC1] return [] cmd.parent_doc.get_services_by_role = get_services_by_role_mock actual = cmd.collect_volumes() self.assertEqual(expected, actual) self.assertIsInstance(actual, OrderedDict) process_additional_volumes_mock.assert_called_with( list(self.fix_with_volumes['additional_volumes'].values()), ProjectStub.FOLDER)