|
1
|
|
|
#!/usr/bin/env python |
|
2
|
|
|
import io |
|
3
|
|
|
import os |
|
4
|
|
|
import re |
|
5
|
|
|
import sys |
|
6
|
|
|
import unittest |
|
7
|
|
|
from stakkr import package_utils |
|
8
|
|
|
from stakkr.configreader import Config |
|
9
|
|
|
base_dir = os.path.abspath(os.path.dirname(__file__)) |
|
10
|
|
|
sys.path.insert(0, base_dir + '/../') |
|
11
|
|
|
|
|
12
|
|
|
|
|
13
|
|
|
# https://docs.python.org/3/library/unittest.html#assert-methods |
|
14
|
|
|
class ConfigReaderTest(unittest.TestCase): |
|
15
|
|
|
def test_bad_config(self): |
|
16
|
|
|
"""Test a non existing configuration file""" |
|
17
|
|
|
c = Config('/does/not/exists') |
|
18
|
|
|
with self.assertRaisesRegex(IOError, "Config file /does/not/exists does not exist"): |
|
19
|
|
|
c.read() |
|
20
|
|
|
|
|
21
|
|
|
def test_default_config(self): |
|
22
|
|
|
"""Test the default config (exit if it exists)""" |
|
23
|
|
|
if os.path.isfile(package_utils.get_venv_basedir() + '/conf/compose.ini'): |
|
24
|
|
|
return |
|
25
|
|
|
|
|
26
|
|
|
c = Config() |
|
27
|
|
|
with self.assertRaisesRegex(IOError, "Config file .*compose.ini does not exist"): |
|
28
|
|
|
c.read() |
|
29
|
|
|
|
|
30
|
|
|
def test_invalid_config(self): |
|
31
|
|
|
"""Test an existing configuration file but invalid""" |
|
32
|
|
|
c = Config(base_dir + '/static/config_invalid.ini') |
|
33
|
|
|
self.assertFalse(c.read()) |
|
34
|
|
|
self.assertGreater(len(c.errors), 0) |
|
35
|
|
|
self.assertTrue('project_name' in c.errors) |
|
36
|
|
|
self.assertEqual('Missing', c.errors['project_name']) |
|
37
|
|
|
self.assertTrue('php.version' in c.errors) |
|
38
|
|
|
self.assertEqual('the value "8.0" is unacceptable.', c.errors['php.version']) |
|
39
|
|
|
|
|
40
|
|
|
# Don't go further with python < 3.5 |
|
41
|
|
|
try: |
|
42
|
|
|
from contextlib import redirect_stderr |
|
43
|
|
|
except Exception: |
|
44
|
|
|
return |
|
45
|
|
|
|
|
46
|
|
|
f = io.StringIO() |
|
47
|
|
|
with redirect_stderr(f): |
|
48
|
|
|
c.display_errors() |
|
49
|
|
|
res = f.getvalue() |
|
50
|
|
|
|
|
51
|
|
|
regex = re.compile('Failed validating .*config_invalid.ini', re.MULTILINE) |
|
52
|
|
|
self.assertRegex(res, regex) |
|
53
|
|
|
|
|
54
|
|
|
regex = re.compile('the value ".*8.0.*" is unacceptable', re.MULTILINE) |
|
55
|
|
|
self.assertRegex(res, regex) |
|
56
|
|
|
|
|
57
|
|
|
def test_valid_config(self): |
|
58
|
|
|
"""Test an existing and valid configuration file""" |
|
59
|
|
|
from configobj import ConfigObj |
|
60
|
|
|
|
|
61
|
|
|
c = Config(base_dir + '/static/config_valid.ini') |
|
62
|
|
|
config = c.read() |
|
63
|
|
|
self.assertIs(ConfigObj, type(config)) |
|
64
|
|
|
self.assertTrue('main' in config) |
|
65
|
|
|
self.assertTrue('services' in config['main']) |
|
66
|
|
|
self.assertTrue('php' in config['main']['services']) |
|
67
|
|
|
self.assertFalse('apache' in config['main']['services']) |
|
68
|
|
|
|
|
69
|
|
|
self.assertTrue('project_name' in config['main']) |
|
70
|
|
|
self.assertEqual('test', config['main']['project_name']) |
|
71
|
|
|
|
|
72
|
|
|
self.assertTrue('php.version' in config['main']) |
|
73
|
|
|
self.assertEqual('7.2', config['main']['php.version']) |
|
74
|
|
|
|
|
75
|
|
|
|
|
76
|
|
|
if __name__ == "__main__": |
|
77
|
|
|
unittest.main() |
|
78
|
|
|
|