TestPlumdConfig.test_config_conf_get_normal()   A
last analyzed

Complexity

Conditions 2

Size

Total Lines 21

Duplication

Lines 0
Ratio 0 %

Importance

Changes 3
Bugs 2 Features 1
Metric Value
cc 2
c 3
b 2
f 1
dl 0
loc 21
rs 9.3142
1
import sys
2
import os.path
3
import unittest
4
5
6
MAX_DIFF = 8192
7
FILE_PATH = os.path.realpath(__file__)
8
PATH = os.path.dirname(FILE_PATH)
9
DAT_PATH = os.path.join(PATH, "dat")
10
DAT_EXT = "yaml"
11
sys.path.insert(0, os.path.realpath(os.path.join(PATH, "../../../")))
12
import plumd.config
13
14
15
class TestPlumdConfig(unittest.TestCase):
16
17
    def test_config_find_normal(self):
18
        """ensure config find functioning normally"""
19
        expect = ['test.main.yaml', 'plugins/cpu.yaml', 'plugins/disk.yaml',
20
                  'plugins/diskio.yaml', 'plugins/graphitetcp.yaml',
21
                  'plugins/mem.yaml', 'plugins/net.yaml', 'plugins/proc.yaml',
22
                  'plugins/swap.yaml', 'plugins/test.writer.yaml',
23
                  'plugins/test/constreader.yaml',
24
                  'plugins/test/randreader.yaml']
25
        expect = sorted([ os.path.join(DAT_PATH, fname) for fname in expect ])
0 ignored issues
show
Coding Style introduced by
No space allowed after bracket
expect = sorted([ os.path.join(DAT_PATH, fname) for fname in expect ])
^
Loading history...
Coding Style introduced by
No space allowed before bracket
expect = sorted([ os.path.join(DAT_PATH, fname) for fname in expect ])
^
Loading history...
26
        found = sorted([i for i in plumd.config.find(DAT_PATH, DAT_EXT) ])
0 ignored issues
show
Coding Style introduced by
No space allowed before bracket
found = sorted([i for i in plumd.config.find(DAT_PATH, DAT_EXT) ])
^
Loading history...
27
        self.assertEqual(expect, found)
28
29
30
    def test_config_conf_get_normal(self):
31
        """ensure config conf functioning normally"""
32
        fname = os.path.join(DAT_PATH, 'test.main.yaml')
33
        expect = {
34
            'log.level': 'debug',
35
            'log.format': "[%(asctime)s] %(levelname)s %(process)d %(message)s",
36
            'config.plugins': '/home/kf/code/plumd/conf/plugins',
37
            'delay.startup': 1,
38
            'delay.poll': 1,
39
            'poll.interval': 10,
40
            'max.queue': 8,
41
            'meta': [{'env': 'test_env'},{'region': 'test_region'}],
0 ignored issues
show
Coding Style introduced by
Exactly one space required after comma
'meta': ['env': 'test_env','region': 'test_region'],
^
Loading history...
42
            'meta.hostname': True,
43
            'shutdown_timeout': 10
44
        }
45
        config = plumd.config.Conf(fname)
46
        fmt = "key: {0}, expected: {1}, actual: {2}"
47
        for key, val in expect.items():
48
            actual = config.get(key)
49
            msg = fmt.format(key, val, actual)
50
            self.assertEqual(val, actual, msg=msg)
51
52
53
    def test_config_conf_set_normal(self):
54
        """ensure config conf functioning normally"""
55
        fname = os.path.join(DAT_PATH, 'test.main.yaml')
56
        expect = {
57
            'log.level': 'debug',
58
            'log.format': "[%(asctime)s] %(levelname)s %(process)d %(message)s",
59
            'config.plugins': '/home/kf/code/plumd/conf/plugins',
60
            'delay.startup': 10,
61
            'delay.poll': 1,
62
            'poll.interval': 30,
63
            'max.queue': 8,
64
            'meta': [{'env': 'test_env'},{'region': 'test_region'}],
0 ignored issues
show
Coding Style introduced by
Exactly one space required after comma
'meta': ['env': 'test_env','region': 'test_region'],
^
Loading history...
65
            'meta.hostname': True,
66
            'shutdown_timeout': 10
67
        }
68
        config = plumd.config.Conf(fname)
69
        config.set_conf('delay.startup', 10, overwrite=True)
70
        config.set_conf('poll.interval', 30, overwrite=True)
71
        fmt = "key: {0}, expected: {1}, actual: {2}"
72
        for key, val in expect.items():
73
            actual = config.get(key)
74
            msg = fmt.format(key, val, actual)
75
            self.assertEqual(val, actual, msg=msg)
76
77
78
    def test_config_conf_defaults_normal(self):
79
        """ensure config conf functioning normally"""
80
        fname = os.path.join(DAT_PATH, 'test.main.yaml')
81
        defaults = {
82
            'config_test1': "test1",
83
            'config_test2': "test2",
84
            'config_test3': "test3",
85
            'config_Test4': "test4"
86
        }
87
        expect = {
88
            'log.level': 'debug',
89
            'log.format': "[%(asctime)s] %(levelname)s %(process)d %(message)s",
90
            'config.plugins': '/home/kf/code/plumd/conf/plugins',
91
            'delay.startup': 1,
92
            'delay.poll': 1,
93
            'poll.interval': 10,
94
            'max.queue': 8,
95
            'meta': [{'env': 'test_env'},{'region': 'test_region'}],
0 ignored issues
show
Coding Style introduced by
Exactly one space required after comma
'meta': ['env': 'test_env','region': 'test_region'],
^
Loading history...
96
            'meta.hostname': True,
97
            'shutdown_timeout': 10,
98
            'config_test1': "test1",
99
            'config_test2': "test2",
100
            'config_test3': "test3",
101
            'config_Test4': "test4"
102
        }
103
        config = plumd.config.Conf(fname)
104
        config.defaults(defaults)
105
        fmt = "key: {0}, expected: {1}, actual: {2}"
106
        for key, val in expect.items():
107
            actual = config.get(key)
108
            msg = fmt.format(key, val, actual)
109
            self.assertEqual(val, config.get(key), msg=msg)
110
111
if __name__ == '__main__':
112
    unittest.main()
113