Пример #1
0
    def test_path_config_invalid_setting_in_file(self):
        """Tests detecting invalid settings defined in a TOML configuration
        file.

        Sets the SAWTOOTH_HOME environment variable to a temporary directory,
        writes a rest_api.toml config file with an invalid setting inside, then
        loads that config and verifies an exception is thrown.

        The test also attempts to avoid environment variables from interfering
        with the test by clearing os.environ and restoring it after the test.
        """
        orig_environ = dict(os.environ)
        os.environ.clear()
        directory = tempfile.mkdtemp(prefix="test-path-config-")
        try:
            os.environ['SAWTOOTH_HOME'] = directory

            config_dir = os.path.join(directory, 'etc')
            os.mkdir(config_dir)
            filename = os.path.join(config_dir, 'rest_api.toml')
            with open(filename, 'w') as fd:
                fd.write('invalid = "a value"')
                fd.write(os.linesep)

            with self.assertRaises(RestApiConfigurationError):
                load_toml_rest_api_config(filename)
        finally:
            os.environ.clear()
            os.environ.update(orig_environ)
            shutil.rmtree(directory)
Пример #2
0
    def test_path_config_invalid_setting_in_file(self):
        """Tests detecting invalid settings defined in a TOML configuration
        file.

        Sets the SAWTOOTH_HOME environment variable to a temporary directory,
        writes a rest_api.toml config file with an invalid setting inside, then
        loads that config and verifies an exception is thrown.

        The test also attempts to avoid environment variables from interfering
        with the test by clearing os.environ and restoring it after the test.
        """
        orig_environ = dict(os.environ)
        os.environ.clear()
        directory = tempfile.mkdtemp(prefix="test-path-config-")
        try:
            os.environ['SAWTOOTH_HOME'] = directory

            config_dir = os.path.join(directory, 'etc')
            os.mkdir(config_dir)
            filename = os.path.join(config_dir, 'rest_api.toml')
            with open(filename, 'w') as fd:
                fd.write('invalid = "a value"')
                fd.write(os.linesep)

            with self.assertRaises(RestApiConfigurationError):
                load_toml_rest_api_config(filename)
        finally:
            os.environ.clear()
            os.environ.update(orig_environ)
            shutil.rmtree(directory)
Пример #3
0
    def test_rest_api_config_load_from_file(self):
        """Tests loading config settings from a TOML configuration file.

        Sets the SAWTOOTH_HOME environment variable to a temporary directory,
        writes a rest_api.toml config file, then loads that config and verifies
        all the rest_api settings are their expected values.

        The test also attempts to avoid environment variables from interfering
        with the test by clearing os.environ and restoring it after the test.
        """
        orig_environ = dict(os.environ)
        os.environ.clear()
        directory = tempfile.mkdtemp(prefix="test-path-config-")
        try:
            os.environ['SAWTOOTH_HOME'] = directory

            config_dir = os.path.join(directory, 'etc')
            os.mkdir(config_dir)
            filename = os.path.join(config_dir, 'rest_api.toml')
            with open(filename, 'w') as fd:
                fd.write('bind = ["test:1234"]')
                fd.write(os.linesep)
                fd.write('connect = "tcp://test:4004"')
                fd.write(os.linesep)
                fd.write('timeout = 10')

            config = load_toml_rest_api_config(filename)
            self.assertEqual(config.bind, ["test:1234"])
            self.assertEqual(config.connect, "tcp://test:4004")
            self.assertEqual(config.timeout, 10)

        finally:
            os.environ.clear()
            os.environ.update(orig_environ)
            shutil.rmtree(directory)
Пример #4
0
    def test_rest_api_config_load_from_file(self):
        """Tests loading config settings from a TOML configuration file.

        Sets the SAWTOOTH_HOME environment variable to a temporary directory,
        writes a rest_api.toml config file, then loads that config and verifies
        all the rest_api settings are their expected values.

        The test also attempts to avoid environment variables from interfering
        with the test by clearing os.environ and restoring it after the test.
        """
        orig_environ = dict(os.environ)
        os.environ.clear()
        directory = tempfile.mkdtemp(prefix="test-path-config-")
        try:
            os.environ['SAWTOOTH_HOME'] = directory

            config_dir = os.path.join(directory, 'etc')
            os.mkdir(config_dir)
            filename = os.path.join(config_dir, 'rest_api.toml')
            with open(filename, 'w') as fd:
                fd.write('bind = ["test:1234"]')
                fd.write(os.linesep)
                fd.write('connect = "tcp://test:40000"')
                fd.write(os.linesep)
                fd.write('timeout = 10')

            config = load_toml_rest_api_config(filename)
            self.assertEqual(config.bind, ["test:1234"])
            self.assertEqual(config.connect, "tcp://test:40000")
            self.assertEqual(config.timeout, 10)

        finally:
            os.environ.clear()
            os.environ.update(orig_environ)
            shutil.rmtree(directory)
Пример #5
0
def load_rest_api_config(first_config):
    default_config = load_default_rest_api_config()
    config_dir = get_config_dir()
    conf_file = os.path.join(config_dir, 'rest_api.toml')

    toml_config = load_toml_rest_api_config(conf_file)
    return merge_rest_api_config(
        configs=[first_config, toml_config, default_config])
Пример #6
0
def load_rest_api_config(first_config):
    default_config = load_default_rest_api_config()
    config_dir = get_config_dir()
    conf_file = os.path.join(config_dir, 'rest_api.toml')

    toml_config = load_toml_rest_api_config(conf_file)
    return merge_rest_api_config(
        configs=[first_config, toml_config, default_config])