Beispiel #1
0
    def test_should_resolve_to_os_env_variable_when_property_value_is_None(self):
        # when
        os.environ['bf_b'] = 'x'
        config = Config('dev', {'a': 1, 'b': None})

        # then
        self.assertEqual(config.resolve('dev'), {'a':1, 'b':'x'})
Beispiel #2
0
    def test_should_use_os_environment_variable_prefix_if_given(self):
        # when
        os.environ['my_namespace_b'] = 'x'
        config = Config('dev', {'b': None}, environment_variables_prefix='my_namespace_')

        # then
        self.assertEqual(config.resolve('dev'), {'b':'x'})
Beispiel #3
0
    def test_should_resolve_from_master_config_when_property_is_missing(self):
        # when
        config = Config('dev', {'a': 1})\
            .add_configuration('prod', {})

        # expect
        self.assertEqual(config.resolve('dev'),  {'a': 1})
        self.assertEqual(config.resolve('prod'), {'a': 1})
Beispiel #4
0
    def test_should_support_hierarchical_configuration(self):
        # when
        config = Config('dev', {'a': 1, 'b': 2})\
            .add_configuration('prod', {'a': 5, 'b':10, 'c':20})

        # then
        self.assertEqual(config.resolve('dev'), {'a': 1, 'b': 2})
        self.assertEqual(config.resolve('prod'), {'a': 5, 'b': 10, 'c': 20})
Beispiel #5
0
    def test_should_resolve_config_given_by_os_env_variable(self):
        # when
        self._set_os_env('dev')

        config = Config('prod', {'bb': 1}).add_configuration('dev', {'bb': 2})

        # then
        self.assertEqual(config.resolve(), {'bb': 2})
Beispiel #6
0
    def test_should_use_bg_as_the_default_environment_variables_prefix(self):
        self._set_os_env('prod', 'bf_env')
        # when
        config = Config('dev', {'a': 'dev1'})\
            .add_configuration('prod', {'a': 'prod2'})

        #expect
        self.assertEqual(config.resolve(), {'a': 'prod2'})
Beispiel #7
0
    def test_should_resolve_config_given_by_os_env_variable_with_prefix(self):
        # when
        self._set_os_env('test', 'my_namespace_env')

        config = Config('prod', {'bb': 'prod'}, environment_variables_prefix='my_namespace_')\
            .add_configuration('test', {'bb': 'test'})

        # then
        self.assertEqual(config.resolve(), {'bb': 'test'})
Beispiel #8
0
    def test_should_resolve_explicit_properties_in_simple_config(self):
        # when
        config = Config('dev', {'a': 1, 'b': '2'})

        #expect
        self.assertEqual(config.resolve('dev'), {
            'a': 1,
            'b': '2',
            'env': 'dev'
        })
Beispiel #9
0
    def test_should_use_default_env_from_secondary_config_when_no_env_is_given(self):
        # when
        config = Config('dev', {'a': 'dev1'}, is_default=False)\
            .add_configuration('prod', {'a': 'prod2'}, is_default=True)\
            .add_configuration('test', {'a': 'test3'})

        # expect
        self.assertEqual(config.resolve(), {'a': 'prod2'})
        self.assertEqual(config.resolve('dev'), {'a': 'dev1'})
        self.assertEqual(config.resolve('prod'), {'a': 'prod2'})
        self.assertEqual(config.resolve('test'), {'a': 'test3'})
Beispiel #10
0
 def test_should_smartly_resolve_properties_with_placeholders(self):
     # when
     config = Config('dev', {
                                'project_id': 'dev_project',
                                'topic': 'topic/{project_id}/my_topic}'
                            }) \
         .add_configuration('prod',
                            {
                                'project_id': 'prod_project',
                            })
     # then
     self.assertEqual(config.resolve_property('topic', 'dev'),  'topic/dev_project/my_topic}')
     self.assertEqual(config.resolve_property('topic', 'prod'), 'topic/prod_project/my_topic}')
Beispiel #11
0
    def test_should_raise_Error_when_default_config_requested_but_not_exists(
            self):
        # when
        _set_os_env('prod')

        with self.assertRaises(ValueError):
            Config('dev', {}, is_master=False).resolve()
Beispiel #12
0
    def test_should_give_priority_to_explicit_properties_rather_than_os_env_variables(self):
        # when
        os.environ['bf_bb'] = 'x'
        config = Config('dev', {'bb': 1})

        # then
        self.assertEqual(config.resolve('dev'), {'bb': 1})
        self.assertEqual(config.resolve(), {'bb': 1})
        self.assertEqual(config.resolve_property('bb', 'dev'), 1)
        self.assertEqual(config.resolve_property('bb'), 1)
Beispiel #13
0
    def test_should_use_default_env_from_master_config_when_no_env_is_given(self):
        # when
        config = Config('dev', {'a': 'dev1'})\
            .add_configuration('prod', {'a': 'prod2'})\
            .add_configuration('test', {'a': 'test3'})

        # expect
        self.assertEqual(config.resolve(), {'a': 'dev1'})
        self.assertEqual(config.resolve('dev'), {'a': 'dev1'})
        self.assertEqual(config.resolve('prod'), {'a': 'prod2'})
        self.assertEqual(config.resolve('test'), {'a': 'test3'})

        # when
        self._set_os_env('prod')
        self.assertEqual(config.resolve(), {'a': 'prod2'})
        self._set_os_env('dev')
        self.assertEqual(config.resolve(), {'a': 'dev1'})
        self._set_os_env('test')
        self.assertEqual(config.resolve(), {'a': 'test3'})
Beispiel #14
0
    def test_should_resolve_env_variables_via_resolve_method(self):
        # when
        config = Config('dev', {'a': 'dev1'})

        #then
        self.assertEqual(config.resolve(), {'a': 'dev1', 'env': 'dev'})
        self.assertEqual(config.resolve('dev'), {'a': 'dev1', 'env': 'dev'})

        # when
        os.environ['bf_b'] = 'b_from_env'

        # then
        self.assertEqual(config.resolve(), {
            'a': 'dev1',
            'b': 'b_from_env',
            'env': 'dev'
        })
        self.assertEqual(config.resolve('dev'), {
            'a': 'dev1',
            'b': 'b_from_env',
            'env': 'dev'
        })
Beispiel #15
0
 def test_should_raise_Error_no_explicit_env_is_given_nor_default_env_is_defined(self):
     with self.assertRaises(ValueError):
         Config('dev', {'a': 1}, is_default=False).resolve()
Beispiel #16
0
 def test_should_raise_Error_when_config_name_not_found(self):
     with self.assertRaises(ValueError):
         Config('dev', {}).resolve('prod')
from bigflow.configuration import Config

deployment_config = Config(name='dev',
                           properties={
                               'docker_repository': 'test_repository',
                           })
Beispiel #18
0
 def test_should_raise_an_error_when_docker_repository_not_in_lower_case(self):
     with self.assertRaises(ValueError):
         Config('dev', {'docker_repository': 'Docker_repository'})
     with self.assertRaises(ValueError):
         Config('dev', {'docker_repository': 'docker_repository'}).add_configuration('prod', {'docker_repository': 'Docker_repository'})
Beispiel #19
0
    def test_should_resolve_env_variables_laizyly_when_calling_resolve_property(self):
        # when
        config = Config('dev', {'a': 'dev1'})

        #then
        self.assertEqual(config.resolve(), {'a': 'dev1'})
        self.assertEqual(config.resolve_property('a'), 'dev1')
        self.assertEqual(config.resolve_property('a', 'dev'), 'dev1')
        with self.assertRaises(ValueError):
            config.resolve_property('b')

        # when
        os.environ['bf_b'] = 'b_from_env'

        # then
        self.assertEqual(config.resolve(), {'a': 'dev1'})
        self.assertEqual(config.resolve_property('b'), 'b_from_env')
        self.assertEqual(config.resolve_property('b', 'dev'), 'b_from_env')
        with self.assertRaises(ValueError):
            config.resolve_property('c')
Beispiel #20
0
 def test_should_raise_Error_when_required_os_env_variable_is_not_found(self):
     with self.assertRaises(ValueError):
         Config('dev', {'no_such_variable': None}).resolve('dev')
Beispiel #21
0
 def test_should_raise_Error_when_more_than_one_default_env_is_defined(self):
     with self.assertRaises(ValueError):
         Config('dev', {'a': 1}, is_default=True)\
             .add_configuration('prod', {'a': 2}, is_default=True)