test_config.py 6.23 KB
Newer Older
1
import unittest
2 3
import agoclient.config as config
from agoclient.agoapp import AgoApp
4 5 6 7

import tempfile
import os, os.path, shutil

8 9 10
# Find checkedout code dir:
# We're in subdir shared/agoclient/
devdir = os.path.realpath(os.path.dirname(os.path.realpath(__file__)) + "/../..")
11

12
class AgoTest(AgoApp):
13 14 15 16 17 18 19 20 21 22 23
    pass

class ConfigTestBase(unittest.TestCase):
    def setUp(self):
        # Create dummy conf
        self.tempdir = tempfile.mkdtemp()

        os.mkdir("%s/conf.d" % self.tempdir)
        # Custom dir, we must place agocontrol.aug into this dir
        # TODO: or add some dir in loadpath?
        shutil.copyfile("%s/conf/agocontrol.aug" % devdir, "%s/agocontrol.aug" % self.tempdir)
24
        shutil.copyfile("%s/conf/conf.d/system.conf" % devdir, "%s/conf.d/system.conf" % self.tempdir)
25 26 27

        config.set_config_dir(self.tempdir)
        with open(config.get_config_path('conf.d/test.conf'), 'w') as f:
28 29 30 31 32 33 34 35 36 37 38 39
            f.write("[test]\n"+
                    "test_value_0=100\n"+
                    "test_value_blank=\n"+
                    "local=4\n"+
                    "units=inv\n"+

                    "[system]\n"+
                    "password=testpwd\n"+

                    "[sec2]\n"+
                    "some=value\n"+
                    "test_value_blank=not blank\n")
40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59

        # Kill augeas
        config.augeas = None

    def tearDown(self):
        shutil.rmtree(self.tempdir)

class ConfigTest(ConfigTestBase):
    def setUp(self):
        super(ConfigTest, self).setUp()
        global gco, sco
        gco = config.get_config_option
        sco = config.set_config_option

    def test_get_path(self):
        self.assertEqual(config.get_config_path("conf.d"), "%s/conf.d" % self.tempdir)
        self.assertEqual(config.get_config_path("conf.d/"), "%s/conf.d/" % self.tempdir)
        self.assertEqual(config.get_config_path("/conf.d/"), "%s/conf.d/" % self.tempdir)

    def test_get_basic(self):
60
        self.assertEqual(gco('system', 'broker'), 'localhost')
61 62
        self.assertEqual(gco('system', 'not_set'), None)

63
        self.assertEqual(gco('test', 'test_value_blank'), None)
64 65 66
        self.assertEqual(gco('test', 'local'), '4')

        # Test with defaults
67
        self.assertEqual(gco('system', 'broker', 'def'), 'localhost')
68 69 70
        self.assertEqual(gco('system', 'not_set', 'def'), 'def')

    def test_get_multiapp(self):
71 72 73
        self.assertEqual(gco('system', 'broker', app="test"), None)
        self.assertEqual(gco('system', 'broker', app=["test", "system"]), 'localhost')
        self.assertEqual(gco('system', 'password', app=["test", "system"]), 'testpwd')
74 75

    def test_get_multisection(self):
76
        # Must explicitly tell app=test, with differnet section name
77 78
        self.assertEqual(gco('sec2', 'some', app='test'), 'value')
        self.assertEqual(gco(['sec2', 'test'], 'test_value_blank', app='test'), 'not blank')
79 80
        self.assertEqual(gco(['system'], 'units'), 'SI')
        self.assertEqual(gco(['test', 'system'], 'units'), 'inv')
81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96


    def test_set_basic(self):
        self.assertEqual(sco('system', 'new_value', '666'), True)
        self.assertEqual(gco('system', 'new_value'), '666')

        self.assertEqual(sco('system', 'test_string', 'goodbye'), True)
        self.assertEqual(gco('system', 'test_string'), 'goodbye')

    def test_set_new(self):
        self.assertFalse(os.path.exists(config.get_config_path("conf.d/newfile.conf")))
        self.assertEqual(sco('newfile', 'new_value', '666'), True)
        self.assertTrue(os.path.exists(config.get_config_path("conf.d/newfile.conf")))

        self.assertEqual(gco('newfile', 'new_value'), '666')

97
    def test_unwritable(self):
98 99 100
        if os.getuid() == 0:
            # TODO: Fix builders so they dont run as root, then change to self.fail instead.
            #self.fail("You cannot run this test as. Also, do not develop as root!")
101
            print("You cannot run this test as. Also, do not develop as root!")
102 103
            return

104 105 106
        with open(config.get_config_path('conf.d/blocked.conf'), 'w') as f:
            f.write("[blocked]\nnop=nop\n")

107
        os.chmod(config.get_config_path('conf.d/blocked.conf'), 0o444)
108 109 110 111 112 113 114 115 116 117 118 119 120 121 122

        self.assertEqual(gco('blocked', 'nop'), 'nop')

        # This shall fail, and write log msg
        self.assertEqual(sco('blocked', 'new_value', '666'), False)

        os.chmod(config.get_config_path('conf.d/blocked.conf'), 0)

        # reload
        config.augeas = None
        self.assertEqual(gco('blocked', 'nop'), None)
        self.assertEqual(gco('blocked', 'nop', 'def'), 'def')



123 124 125 126 127 128 129 130 131 132 133 134
class AppConfigTest(ConfigTestBase):
    def setUp(self):
        super(AppConfigTest, self).setUp()
        self.app = AgoTest()

        global gco, sco
        gco = self.app.get_config_option
        sco = self.app.set_config_option

    def test_get_basic(self):
        self.assertEqual(gco('test_value_0'), '100')
        self.assertEqual(gco('test_value_1'), None)
135
        self.assertEqual(gco('units'), 'inv')
136 137

    def test_get_section(self):
138 139 140 141
        # from system.conf:
        self.assertEqual(gco('broker', section='system'), 'localhost')
        # from test.conf:
        self.assertEqual(gco('password', section='system'), 'testpwd')
142 143 144 145 146

    def test_get_app(self):
        # Will give None, since we're looking in section test, which is not in system.conf
        self.assertEqual(gco('test_value_0', app=['other', 'system']), None)

147 148
        # Will give system.confs password
        self.assertEqual(gco('password', app=['other', 'system'], section='system'), 'letmein')
149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168

    def test_set_basic(self):
        self.assertEqual(sco('new_value', '666'), True)
        self.assertEqual(gco('new_value'), '666')

        self.assertEqual(sco('test_string', 'goodbye'), True)
        self.assertEqual(gco('test_string'), 'goodbye')

    def test_set_section(self):
        self.assertEqual(sco('some_value', '0', 'sec2'), True)
        self.assertEqual(gco('some_value'), None)
        self.assertEqual(gco('some_value', section='sec2'), '0')

    def test_set_new_file(self):
        self.assertFalse(os.path.exists(config.get_config_path("conf.d/newfile.conf")))
        self.assertEqual(sco('new_value', '666', section='newfile'), True)
        self.assertTrue(os.path.exists(config.get_config_path("conf.d/newfile.conf")))

        self.assertEqual(gco('new_value', section='newfile'), '666')