コード例 #1
0
    def test_save_to_file(self):
        """

        1. create temp file with desired config (use default format)
        2. create a ConfigurableAPI using that file as a config
        3. delete the temp file
        4. save the config
        5. load the tempfile and check it's equal to the config

        """
        temp_file = tempfile.NamedTemporaryFile(delete=False)

        formatter = FormatFactory.get_formatter()
        formatter.dump(self.test_config, temp_file)
        temp_file.close()

        conf_api = Configurable(local_file=temp_file.name)
        os.unlink(temp_file.name)

        conf_api.save()

        with open(temp_file.name, 'rb') as infile:
            conf_from_file = formatter.load(infile)

        self.assertEquals(self.test_config, conf_from_file)
        os.unlink(temp_file.name)
コード例 #2
0
    def test_set(self):

        test_conf = {
            'a': 1,
            'b': {
                'c': '3',
            }
        }

        conf_api = Configurable(defaults=test_conf)
        conf_api['b']['e'] = 10
        conf_api['d'] = {'r': {'a': [True]}}

        expected_conf = {
            'a': 1,
            'b': {
                'c': '3',
                'e': 10
            },
            'd': {
                'r': {
                    'a': [True]
                }
            }
        }
        self.assertEquals(expected_conf, conf_api.config)
コード例 #3
0
    def test_get_with_flask_config(self):
        test_config = {'a': 1, 'b': {'c': 3, 'd': [True]}}

        conf_api = Configurable(defaults=test_config)
        # let's inject a flask config without having to make a whole app
        conf_api._config = {'DEBUG': True}

        # test flask _config existing get
        self.assertTrue(conf_api['DEBUG'])
コード例 #4
0
    def test_get(self):
        test_config = {'a': 1, 'b': {'c': 3, 'd': [True]}}

        conf_api = Configurable(defaults=test_config)

        # test existing get
        self.assertEquals(test_config['a'], conf_api['a'])

        # test non-existing get
        with self.assertRaises(Exception):
            doesnt_exist = conf_api['e']

        # test nested value get
        self.assertEquals([True], conf_api['b']['d'])
コード例 #5
0
    def test_no_merge(self):
        base_conf = {
            'a': 1,
        }

        merge_in = {}

        conf_api = Configurable(defaults=base_conf)
        changes = conf_api.merge_in_dict(merge_in)

        # Check the changes: there shouldn't be any
        self.assertEquals(0, len(changes))

        self.assertEquals(base_conf, conf_api.config)
コード例 #6
0
    def test_nested_merge(self):
        base_conf = {'a': 1, 'b': {'c': [True]}}

        merge_in = {'b': {'d': {'f': {1, 2, 3}}}}

        conf_api = Configurable(defaults=base_conf)
        changes = conf_api.merge_in_dict(merge_in)

        expected = {'a': 1, 'b': {'c': [True], 'd': {'f': {1, 2, 3}}}}

        # Check the changes: 1 change where 'b.d' was set to {'f': {1, 2, 3}}
        self.assertEquals(1, len(changes))
        self.assertEquals({'b.d': ({'f': {1, 2, 3}}, None)}, changes)

        self.assertEquals(expected, conf_api.config)
コード例 #7
0
    def test_top_level_merge(self):
        base_conf = {'a': 1}

        merge_in = {'b': 2}

        conf_api = Configurable(defaults=base_conf)
        changes = conf_api.merge_in_dict(merge_in)

        expected = {'a': 1, 'b': 2}

        # Check the changes: 1 change where 'b' went from None -> 2
        self.assertEquals(1, len(changes))
        self.assertEquals({'b': (2, None)}, changes)

        self.assertEquals(expected, conf_api.config)
コード例 #8
0
    def test_get_default(self):
        test_config = {'a': 1, 'b': {'c': 3, 'd': [True]}}

        conf_api = Configurable(defaults=test_config)

        # test existing get
        self.assertEquals(test_config['a'], conf_api.get('a', 10))

        # test non-existing get
        self.assertEquals(10, conf_api.get('e', 10))

        # test nested value get
        self.assertEquals([True], conf_api.get('b').get('d'))

        # test nested value get defaulted
        self.assertEquals([False], conf_api.get('b').get('r', [False]))
コード例 #9
0
    def test_load_from_file(self):
        """

        1. create temp file with desired config (use default format)
        2. create a ConfigurableAPI using that file as a config
        TEST desired config is equal to the APIs config

        """

        temp_file = tempfile.NamedTemporaryFile(delete=False)

        formatter = FormatFactory.get_formatter()
        formatter.dump(self.test_config, temp_file)
        temp_file.close()

        conf_api = Configurable(local_file=temp_file.name)
        os.unlink(temp_file.name)

        self.assertEquals(self.test_config, conf_api.config)
コード例 #10
0
ファイル: test_api.py プロジェクト: adityanatraj/configurable
    def test_app_wrap(self):

        test_config = {'a': 1, 'b': {'c': [True], 'd': 'wowowow'}}

        Configurable(flask_app=self.test_app, defaults=test_config)

        # 1. can we still get what the API delivers ?
        client = self.test_app.test_client()
        response = client.get('/hello')
        self.assertEquals("hello", response.data)

        # 2. can we access the /config/ route ?
        response = client.get('/config/')
        response_json = json.loads(response.data)
        self.assertEquals(response_json, test_config)

        # 3. can the underlying application use the .configuration ?
        response = client.get('/test_config')
        response_json = json.loads(response.data)
        self.assertEquals(response_json, test_config)
コード例 #11
0
ファイル: configurables.py プロジェクト: Sampru/tipulapi
from configurable import Configurable

DHCPD_CONF = """
subnet %NETWORK% netmask %MASK% {
 range %STARTADDR% %ENDADDR%;
 option broadcast-address %BROADCAST%;
 option routers %GATEWAY%;
 default-lease-time 600;
 max-lease-time 7200;
 option domain-name "local";
 option domain-name-servers %DNSSERVER%;
}
"""

DHCPD = Configurable(DHCPD_CONF, [
    "NETWORK", "MASK", "STARTADDR", "ENDADDR", "BROADCAST", "GATEWAY",
    "DNSSERVER"
], "/etc/dhcp/dhcpd.conf", "a")

DHCP_ENABLE = Configurable('INTERFACES="%INT%"', ["INT"],
                           "/etc/default/isc-dhcp-server", "a")

INT_CONF = """
iface %INTERFACE% inet static
  address %IPADDR%
  netmask %MASK%
"""

INTERFACES = Configurable(INT_CONF, ["INTERFACE", "IPADDR", "MASK"],
                          "/etc/network/interfaces", "a")

HOSTAPD_CONF = """