Пример #1
0
    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))
Пример #2
0
 def test_validate_invalid_ports(self):
     service = module.Service.from_yaml(
         get_fixture_path(FIXTURE_BASE_PATH +
                          'invalid_additional_ports.yml'))
     with self.assertRaisesRegex(SchemaError,
                                 "should be instance of 'int'"):
         service.validate()
Пример #3
0
 def test_validate_invalid_additional_volumes(self):
     service = module.Service.from_yaml(
         get_fixture_path(FIXTURE_BASE_PATH +
                          'invalid_additional_volumes.yml'))
     with self.assertRaisesRegex(SchemaError,
                                 r"Or\('rw', 'ro'\) did not validate"):
         service.validate()
Пример #4
0
 def test_validate_valids(self):
     valid_names = ['valid.yml', 'integration_app.yml']
     for name in valid_names:
         with self.subTest(name=name):
             app = module.App.from_yaml(
                 get_fixture_path(FIXTURE_BASE_PATH + name))
             self.assertTrue(app.validate())
Пример #5
0
 def test_validate_extra_db_but_no_db_driver(self):
     service = module.Service.from_yaml(
         get_fixture_path(FIXTURE_BASE_PATH +
                          'invalid_db_but_no_db_driver.yml'))
     with self.assertRaisesRegex(
             ConfigcrunchError,
             "If a service has the role 'db' it has to have "
             "a valid 'driver' entry with a driver that "
             "is available."):
         service.validate()
Пример #6
0
    def test_validate_extra_db_driver(self):
        service = module.Service.from_yaml(
            get_fixture_path(FIXTURE_BASE_PATH + 'valid_db_driver.yml'))

        with patch_mock_db_driver(
                'riptide.config.document.service.db_driver_for_service.get'
        ) as (_, driver):
            service._db_driver = driver
            service.validate()
            driver.validate_service.assert_called_once()
Пример #7
0
 def test_validate_valids(self):
     valid_names = [
         'valid.yml', 'integration_all.yml', 'integration_no_command.yml',
         'integration_no_service.yml'
     ]
     for name in valid_names:
         with self.subTest(name=name):
             project = module.Project.from_yaml(
                 get_fixture_path(FIXTURE_BASE_PATH + name))
             self.assertTrue(project.validate())
Пример #8
0
    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)
Пример #9
0
 def test_validate_valids(self):
     valid_names = [
         'valid_regular.yml', 'valid_alias.yml',
         'valid_regular_with_some_optionals.yml'
     ]
     for name in valid_names:
         with self.subTest(name=name):
             command = module.Command.from_yaml(
                 get_fixture_path(FIXTURE_BASE_PATH + name))
             self.assertTrue(command.validate())
Пример #10
0
    def test_service_initialize_data_correct_config_file_exists_in_referenced_only(
            self):
        """Tests that Services load the correct config file for a document based on merging hierarchy"""
        base_path = get_fixture_path(
            os.path.join('service', 'test_config_paths'))
        doc = Service({'$ref': 'not_exist_one/config'})
        doc.resolve_and_merge_references([base_path])

        self.assertEqual(
            os.path.realpath(os.path.join(base_path, 'two', 'config.txt')),
            os.path.realpath(doc['config']["test"]["$source"]))
Пример #11
0
 def test_validate_valids(self):
     valid_names = [
         'valid.yml', 'valid_auto_perf.yml', 'integration_perf_dont_sync_unimportant_src.yml',
         'integration_perf_dont_sync_named_volumes_with_host.yml'
     ]
     for name in valid_names:
         with self.subTest(name=name):
             config = module.Config.from_yaml(get_fixture_path(
                 FIXTURE_BASE_PATH + name
             ))
             self.assertTrue(config.validate())
Пример #12
0
    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)
Пример #13
0
    def test_service_initialize_data_correct_config_file_exists_in_both(self):
        """Tests that Services load the correct config file for a document based on merging hierarchy"""
        base_path = get_fixture_path(
            os.path.join('service', 'test_config_paths'))
        doc = Service({'$ref': 'one/config'})
        doc.resolve_and_merge_references([base_path])

        self.assertEqual(
            {
                "test": {
                    "from": "config.txt",
                    "to": "doesnotmatter",
                    "$source": os.path.join(base_path, 'one', 'config.txt')
                }
            }, doc['config'])
Пример #14
0
 def test_validate_valids(self):
     valid_names = [
         'valid_minimum.yml', 'valid_everything.yml',
         'integration_additional_volumes.yml', 'integration_configs.yml',
         'integration_custom_command.yml', 'integration_env.yml',
         'integration_simple.yml', 'integration_simple_with_src.yml',
         'integration_src_working_directory.yml',
         'integration_working_directory_absolute.yml'
     ]
     for name in valid_names:
         with self.subTest(name=name):
             service = module.Service.from_yaml(
                 get_fixture_path(FIXTURE_BASE_PATH + name))
             service._initialize_data_after_merge()
             self.assertTrue(service.validate())
Пример #15
0
 def test_validate_via_service_no_command(self):
     command = module.Command.from_yaml(
         get_fixture_path(FIXTURE_BASE_PATH +
                          'invalid_via_service_no_command.yml'))
     with self.assertRaises(SchemaError):
         command.validate()
Пример #16
0
 def test_validate_invalid_regular_no_image(self):
     command = module.Command.from_yaml(
         get_fixture_path(FIXTURE_BASE_PATH +
                          'invalid_regular_no_image.yml'))
     with self.assertRaises(SchemaError):
         command.validate()
Пример #17
0
 def test_validate_invalid_weird_mixup(self):
     command = module.Command.from_yaml(
         get_fixture_path(FIXTURE_BASE_PATH + 'invalid_weird_mixup.yml'))
     with self.assertRaises(SchemaError):
         command.validate()
Пример #18
0
 def test_validate_invalid_alias_no_aliases(self):
     command = module.Command.from_yaml(
         get_fixture_path(FIXTURE_BASE_PATH +
                          'invalid_alias_no_aliases.yml'))
     with self.assertRaisesRegex(SchemaError, "Missing key:"):
         command.validate()
Пример #19
0
 def test_validate_invalid_no_name(self):
     app = module.App.from_yaml(
         get_fixture_path(FIXTURE_BASE_PATH + 'invalid_no_name.yml'))
     with self.assertRaisesRegex(SchemaError, "Missing key: 'name'"):
         app.validate()
Пример #20
0
def load(
    testsuite, project_file_names: List[str], srcs: List[str]
) -> Generator[ContextManager[ProjectLoadResult], None, None]:
    """
    Generator that returns context managers

    Loads the project files with the given names (no path, just names in tests/fixtures/project) for each engine, for each entry in srcs,
    sets the project name to (name of the calling method or name)--(project_file_names)--(current engine)--(current src)
    sets the src to each entry in srcs
    creates a system config for each engine and places the project in it

    yields for each test_project_file_name x srcs entry:
        ContextManager[ProjectLoadResult]

    Cleans up (=asks EngineTester to clean up and make sure it's cleaned up) after each test

    Repositories (riptide.config.loader.repositories.collect) are mocked and set to the fixtures directory.
    riptide.config.files.user_config_dir() is mocked and set to a temporary directory.

    The context manager starts a sub test with the appropriate values

    USAGE:
        for project_ctx in load(...):
            with project_ctx as loaded:
                print(loaded.project_file_name)
    """
    calframe = inspect.getouterframes(inspect.currentframe(), 2)
    caller_name = calframe[1][3]

    with mock.patch("riptide.config.loader.repositories.collect",
                    return_value=[get_fixture_paths()]):
        for (engine_name, engine, engine_tester) in load_engines():
            for src in srcs:
                for project_name in project_file_names:
                    # Create temporary config directory
                    with TemporaryDirectory() as config_directory:
                        with mock.patch("riptide.config.files.user_config_dir",
                                        return_value=config_directory):
                            # Copy system config file
                            shutil.copy2(
                                get_fixture_path('config' + os.sep +
                                                 'valid.yml'),
                                os.path.join(config_directory, 'config.yml'))
                            # Create temporary project directory
                            with TemporaryDirectory() as project_directory:
                                # Copy project file
                                shutil.copy2(
                                    get_fixture_path('project' + os.sep +
                                                     project_name),
                                    os.path.join(project_directory,
                                                 'riptide.yml'))

                                name = (caller_name + '--' + project_name +
                                        '--' + engine_name + '--' + src)

                                @contextmanager
                                def ctx_manager(
                                ) -> ContextManager[ProjectLoadResult]:
                                    # replace dots with _, PyCharm seems to have parsing issues with .
                                    with testsuite.subTest(
                                            project=project_name.replace(
                                                '.', '_'),
                                            src=src.replace('.', '_'),
                                            engine=engine_name):
                                        old_dir = os.getcwd()
                                        try:
                                            os.chdir(project_directory)
                                            # LOAD
                                            system_config = load_config()
                                            # Sanity assertions / first function checks
                                            assert isinstance(
                                                system_config, Config)
                                            assert "project" in system_config
                                            assert isinstance(
                                                system_config["project"],
                                                Project)
                                            # set engine name
                                            system_config[
                                                "engine"] = engine_name
                                            # set project src
                                            system_config["project"][
                                                "src"] = src
                                            # set project name
                                            system_config["project"][
                                                "name"] = name

                                            yield ProjectLoadResult(
                                                project_file_name=project_name,
                                                config=system_config,
                                                src=src,
                                                engine_name=engine_name,
                                                engine=engine,
                                                engine_tester=engine_tester,
                                                temp_dir=project_directory,
                                                temp_system_dir=config_directory
                                            )
                                        finally:
                                            os.chdir(old_dir)
                                            engine_tester.reset(engine)

                                yield ctx_manager()
Пример #21
0
 def test_validate_invalid_no_src(self):
     project = module.Project.from_yaml(
         get_fixture_path(FIXTURE_BASE_PATH + 'invalid_no_src.yml'))
     with self.assertRaisesRegex(SchemaError, "Missing key: 'src'"):
         project.validate()
Пример #22
0
 def test_validate_invalid_missing_repos(self):
     config = module.Config.from_yaml(get_fixture_path(
         FIXTURE_BASE_PATH + 'invalid_missing_repos.yml'
     ))
     with self.assertRaisesRegex(SchemaError, "Missing key: 'repos'"):
         config.validate()
Пример #23
0
 def test_validate_valid(self):
     config = module.Config.from_yaml(get_fixture_path(
         FIXTURE_BASE_PATH + 'valid.yml'
     ))
     self.assertTrue(config.validate())
Пример #24
0
 def test_validate_invalid_proxy(self):
     config = module.Config.from_yaml(get_fixture_path(
         FIXTURE_BASE_PATH + 'invalid_proxy.yml'
     ))
     with self.assertRaisesRegex(SchemaError, "should be instance of 'dict'"):
         config.validate()
Пример #25
0
 def test_validate_invalid_config(self):
     with self.assertRaises(ConfigcrunchError):
         service = module.Service.from_yaml(
             get_fixture_path(FIXTURE_BASE_PATH + 'invalid_config.yml'))
         service._initialize_data_after_merge()
Пример #26
0
 def test_validate_invalid_logging(self):
     service = module.Service.from_yaml(
         get_fixture_path(FIXTURE_BASE_PATH + 'invalid_logging.yml'))
     with self.assertRaisesRegex(SchemaError,
                                 "should be instance of 'bool'"):
         service.validate()
Пример #27
0
 def test_validate_invalid_no_image(self):
     service = module.Service.from_yaml(
         get_fixture_path(FIXTURE_BASE_PATH + 'invalid_no_image.yml'))
     with self.assertRaisesRegex(SchemaError, "Missing key: 'image'"):
         service.validate()
Пример #28
0
 def test_validate_valid_with_some_optionals(self):
     app = module.App.from_yaml(
         get_fixture_path(FIXTURE_BASE_PATH +
                          'valid_with_some_optionals.yml'))
     self.assertTrue(app.validate())