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_collect_volumes_ssh_auth_socket(self, os_environ_mock: Mock): ssh_auth_path = 'DUMMY' env = {'SSH_AUTH_SOCK': ssh_auth_path} 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 = module.Command({}) expected = OrderedDict({ # Source code also has to be mounted in: ProjectStub.SRC_FOLDER: { 'bind': CONTAINER_SRC_PATH, 'mode': 'rw' }, # SSH_AUTH_SOCK: ssh_auth_path: { 'bind': ssh_auth_path, 'mode': 'rw' } }) # The project contains NO services matching the defined roles cmd.parent_doc = ProjectStub({}, set_parent_to_self=True) actual = cmd.collect_volumes() self.assertEqual(expected, actual) self.assertIsInstance(actual, OrderedDict)
def test_volume_path(self, meta_folder_mock: Mock, os_makedirs_mock: Mock): cmd = module.Command({'$name': 'hello_world'}) cmd.parent_doc = ProjectStub({}, set_parent_to_self=True) expected_path = os.path.join('META', 'cmd_data', 'hello_world') self.assertEqual(expected_path, cmd.volume_path()) meta_folder_mock.assert_called_once_with(ProjectStub.FOLDER) os_makedirs_mock.assert_called_once_with(expected_path, exist_ok=True)
def test_volume_path(self, get_project_meta_folder_mock: Mock): service = module.Service({'$name': 'TEST'}, parent=ProjectStub({}, set_parent_to_self=True)) self.assertEqual( os.path.join(ProjectStub.FOLDER + '~PROCESSED', 'data', 'TEST'), service.volume_path())
def test_collect_volumes_no_src(self): service = module.Service({"roles": ["something"]}) expected = {} service.parent_doc = ProjectStub({}, set_parent_to_self=True) ## OVERALL ASSERTIONS self.assertEqual(expected, service.collect_volumes())
def test_before_start_absolute_workdir_no_workdir(self, makedirs_mock: Mock): project_stub = ProjectStub({"src": "SRC"}, set_parent_to_self=True) service = module.Service({}, parent=project_stub) service.before_start() # Assert NO creation of working directory makedirs_mock.assert_not_called()
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_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_only_stdere(self, get_logging_path_for_mock: Mock, create_logging_path_mock: Mock): service = module.Service({ "roles": ["something"], "logging": { "stderr": True } }) expected = { 'stderr~PROCESSED2': { 'bind': module.LOGGING_CONTAINER_STDERR, 'mode': 'rw' } } service.parent_doc = ProjectStub({}, set_parent_to_self=True) ## OVERALL ASSERTIONS self.assertEqual(expected, service.collect_volumes()) get_logging_path_for_mock.assert_called_once_with(service, 'stderr') create_logging_path_mock.assert_called_once()
def test_before_start(self, get_additional_port_mock: Mock, makedirs_mock: Mock): project_stub = ProjectStub({"src": "SRC"}, set_parent_to_self=True) service = module.Service( { "working_directory": "WORKDIR", "additional_ports": { "one": { "container": 1, "host_start": 2 }, "two": { "container": 2, "host_start": 3 }, "three": { "container": 3, "host_start": 4 }, } }, parent=project_stub) service.before_start() self.assertEqual({1: 12, 2: 13, 3: 14}, service._loaded_port_mappings) get_additional_port_mock.assert_has_calls([ call(project_stub, service, 2), call(project_stub, service, 3), call(project_stub, service, 4) ], any_order=True) # Assert creation of working directory makedirs_mock.assert_called_with(os.path.join(ProjectStub.FOLDER, "SRC", "WORKDIR"), exist_ok=True)
def test_init_data_after_merge_config_has_project(self, exist_mock: Mock): service = module.Service( { "config": { "one": { "from": "config1/path", "to": "doesnt matter" }, "two": { "from": "config2/path2/blub", "to": "doesnt matter2" } } }, parent=ProjectStub({}, set_parent_to_self=True)) service._initialize_data_after_merge() self.assertEqual( { "one": { "from": "config1/path", "to": "doesnt matter", "$source": os.path.join(ProjectStub.FOLDER, "config1/path") }, "two": { "from": "config2/path2/blub", "to": "doesnt matter2", "$source": os.path.join(ProjectStub.FOLDER, "config2/path2/blub") } }, service['config']) exist_mock.assert_has_calls([ call(os.path.join(ProjectStub.FOLDER, "config1/path")), call(os.path.join(ProjectStub.FOLDER, "config2/path2/blub")) ])
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)
def test_get_project(self): cmd = module.Command({}) project = ProjectStub({}, set_parent_to_self=True) cmd.parent_doc = project self.assertEqual(project, cmd.get_project())
def test_collect_volumes(self, process_additional_volumes_mock: Mock, process_config_mock: Mock, makedirs_mock: Mock, get_logging_path_for_mock: Mock, get_command_logging_container_path_mock: Mock, create_logging_path_mock: Mock): config1 = {'to': '/TO_1', 'from': '/FROM_1'} config2 = {'to': '/TO_2', 'from': '/FROM_2'} config3 = {'to': '/TO_3', 'from': '/FROM_3'} service = module.Service({ "roles": ["src"], "config": { "config1": config1, "config2": config2, "config3": config3 }, "logging": { "stdout": True, "stderr": True, "paths": { "one": "one_path", "two": "two_path", "three": "three_path" }, "commands": { "four": "not used here" } }, "additional_volumes": { "one": { "host": "~/hometest", "container": "/vol1", "mode": "rw" }, "2": { "host": "./reltest1", "container": "/vol2", "mode": "rw" }, "three": { "host": "reltest2", "container": "/vol3", "mode": "rw" }, "FOUR": { "host": "reltestc", "container": "reltest_container", "mode": "rw" }, "faive": { "host": "/absolute_with_ro", "container": "/vol4", "mode": "ro" }, "xis": { "host": "/absolute_no_mode", "container": "/vol5" } } }) expected = OrderedDict({ # SRC ProjectStub.SRC_FOLDER: { 'bind': CONTAINER_SRC_PATH, 'mode': 'rw' }, # CONFIG 'config1~/FROM_1~PROCESSED': { 'bind': '/TO_1', 'mode': 'rw' }, 'config2~/FROM_2~PROCESSED': { 'bind': '/TO_2', 'mode': 'rw' }, 'config3~/FROM_3~PROCESSED': { 'bind': '/TO_3', 'mode': 'rw' }, # LOGGING 'stdout~PROCESSED2': { 'bind': module.LOGGING_CONTAINER_STDOUT, 'mode': 'rw' }, 'stderr~PROCESSED2': { 'bind': module.LOGGING_CONTAINER_STDERR, 'mode': 'rw' }, 'one~PROCESSED2': { 'bind': 'one_path', 'mode': 'rw' }, 'two~PROCESSED2': { 'bind': 'two_path', 'mode': 'rw' }, 'three~PROCESSED2': { 'bind': 'three_path', 'mode': 'rw' }, 'four~PROCESSED2': { 'bind': 'four~PROCESSED3', 'mode': 'rw' }, # DB DRIVER 'FROM_DB_DRIVER': 'VALUE', # ADDITIONAL VOLUMES # process_additional_volumes has to be called STUB_PAV__KEY: STUB_PAV__VAL }) service.parent_doc = ProjectStub({}, set_parent_to_self=True) ## DB DRIVER SETUP with patch_mock_db_driver( 'riptide.config.document.service.db_driver_for_service.get' ) as (_, driver): service._db_driver = driver driver.collect_volumes.return_value = {'FROM_DB_DRIVER': 'VALUE'} ## OVERALL ASSERTIONS actual = service.collect_volumes() self.assertEqual(expected, actual) self.assertIsInstance(actual, OrderedDict) ## CONFIG ASSERTIONS process_config_mock.assert_has_calls([ call("config1", config1, service), call("config2", config2, service), call("config3", config3, service), ], any_order=True) ## LOGGING ASSERTIONS get_logging_path_for_mock.assert_has_calls([ call(service, "stdout"), call(service, "stderr"), call(service, "one"), call(service, "two"), call(service, "three"), call(service, "four"), ], any_order=True) get_command_logging_container_path_mock.assert_has_calls( [ call("four"), ], any_order=True) create_logging_path_mock.assert_called_once() ## ADDITIONAL VOLUMES ASSERTIONS process_additional_volumes_mock.assert_called_with( list(service['additional_volumes'].values()), ProjectStub.FOLDER) ## DB DRIVER ASSERTIONS makedirs_mock.assert_has_calls( [ # DB DRIVER call('FROM_DB_DRIVER', exist_ok=True) ], any_order=True)
def test_get_project(self): service = module.Service({}) project = ProjectStub({}, set_parent_to_self=True) service.parent_doc = project self.assertEqual(project, service.get_project())