1
|
|
|
import json |
2
|
|
|
import tempfile |
3
|
|
|
|
4
|
|
|
from configurator.cli import main |
5
|
|
|
from configurator.config import Configurable |
6
|
|
|
|
7
|
|
|
|
8
|
|
|
class O(object): |
9
|
|
|
""" |
10
|
|
|
test object |
11
|
|
|
""" |
12
|
|
|
|
13
|
|
|
|
14
|
|
|
def test_main(): |
15
|
|
|
assert main([]) == 0 |
16
|
|
|
|
17
|
|
|
|
18
|
|
|
def test_config_by_args(): |
19
|
|
|
class MyConfig(Configurable): |
20
|
|
|
timeout = 1 |
21
|
|
|
highwater = .4 |
22
|
|
|
watermark = 'tv' |
23
|
|
|
|
24
|
|
|
o = O() |
25
|
|
|
MyConfig.configure(o, timeouut=1, highwater=.7) |
26
|
|
View Code Duplication |
assert (o.watermark == MyConfig.watermark) |
|
|
|
|
27
|
|
|
assert (o.timeout == 1) |
28
|
|
|
assert (o.highwater == .7) |
29
|
|
|
|
30
|
|
|
|
31
|
|
|
def test_config_by_json(): |
32
|
|
|
class MyConfig(Configurable): |
33
|
|
|
timeout = 1 |
34
|
|
|
highwater = .4 |
35
|
|
|
watermark = 'tv' |
36
|
|
|
|
37
|
|
|
o = O() |
38
|
|
|
with tempfile.NamedTemporaryFile(suffix='.json', delete=False) as json_file: |
39
|
|
|
name = json_file.name |
40
|
|
|
json.dump(dict(timeouut=1, highwater=.7), json_file) |
41
|
|
|
MyConfig.configure(o, config_file=name) |
42
|
|
|
assert (o.watermark == MyConfig.watermark) |
43
|
|
|
assert (o.timeout == 1) |
44
|
|
|
assert (o.highwater == .7) |
45
|
|
|
|
46
|
|
|
|
47
|
|
|
def test_config_from_url(): |
48
|
|
|
class MyConfig(Configurable): |
49
|
|
|
only_backfill = False |
50
|
|
|
dont_backfill = False |
51
|
|
|
read_period = 1 |
52
|
|
|
config_url = 'http://107.21.150.202:5984/config/tailchaser-test' |
53
|
|
|
|
54
|
|
|
o = O() |
55
|
|
View Code Duplication |
MyConfig.configure(o) |
|
|
|
|
56
|
|
|
assert (o.dont_backfill is False) |
57
|
|
|
assert (o.read_period == 2) |
58
|
|
|
|
59
|
|
|
|
60
|
|
|
def test_config_from_python(): |
61
|
|
|
code = """ |
62
|
|
|
SZ=10 |
63
|
|
|
SIG_SZ=10*SZ |
64
|
|
|
class Tailer: |
65
|
|
|
READ_PERIOD = 1 |
66
|
|
|
""" |
67
|
|
|
|
68
|
|
|
class MyConfig(Configurable): |
69
|
|
|
body = 'hello' |
70
|
|
|
objectType = 'Nothing' |
71
|
|
|
|
72
|
|
|
with tempfile.NamedTemporaryFile(suffix='.py', delete=False) as python_file: |
73
|
|
|
name = python_file.name |
74
|
|
|
python_file.write(code) |
75
|
|
|
|
76
|
|
|
o = O() |
77
|
|
|
MyConfig.configure(o, config_file=name) |
78
|
|
|
assert (o.SIG_SZ == 100) |
79
|
|
|
assert (o.Tailer.READ_PERIOD == 1) |
80
|
|
|
|
81
|
|
|
|
82
|
|
|
def test_config_by_cfg(): |
83
|
|
|
class MyConfig(Configurable): |
84
|
|
|
timeout = 1 |
85
|
|
|
highwater = .4 |
86
|
|
|
watermark = 'tv' |
87
|
|
|
|
88
|
|
|
o = O() |
89
|
|
|
with tempfile.NamedTemporaryFile(suffix='.cfg', delete=False) as cfg_file: |
90
|
|
|
name = cfg_file.name |
91
|
|
|
cfg_file.write(""" |
92
|
|
|
[dev] |
93
|
|
|
timeout = 21 |
94
|
|
|
highwater = .7 |
95
|
|
|
""") |
96
|
|
|
|
97
|
|
|
MyConfig.configure(o, config_file=name) |
98
|
|
|
assert (o.watermark == MyConfig.watermark) |
99
|
|
|
assert (o.dev['timeout'] == '21') |
100
|
|
|
assert (o.dev['highwater'] == '.7') |
101
|
|
|
|