/
test_conf.py
274 lines (236 loc) · 9.61 KB
/
test_conf.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
import unittest
import tempfile
import shutil
import json
import yaml
import os
import sys
import stat
from mock import patch
from zkfarmer import conf
class TempDirectoryTestCase(unittest.TestCase):
def setUp(self):
self.tmpdir = tempfile.mkdtemp()
def tearDown(self):
shutil.rmtree(self.tmpdir)
class TestConf(unittest.TestCase):
def test_bad_format(self):
"""Check if we get the right exception if we specify a bad format."""
with self.assertRaises(ValueError):
conf.Conf("dunno.txt", "inexistant")
def test_bad_extension(self):
"""Check we get the right exception if we specify a bad extension."""
with self.assertRaises(ValueError):
conf.Conf("dunno.dunno")
class TestConfJSON(TempDirectoryTestCase):
def test_json_write_from_extension(self):
"""Check we write JSON output when specifying `.json`."""
name = "%s/test.json" % self.tmpdir
a = conf.Conf(name)
a.write({1: "cc"})
with open(name) as f:
self.assertEqual(json.load(f), {"1": "cc"})
def test_json_write_from_format(self):
"""Check we write JSON output when specifying `json` format."""
name = "%s/test.php" % self.tmpdir
a = conf.Conf(name, "json")
a.write({1: "cc"})
with open(name) as f:
self.assertEqual(json.load(f), {"1": "cc"})
def test_json_write_nested(self):
"""Check we can write a more complex JSON output."""
name = "%s/test.json" % self.tmpdir
a = conf.Conf(name)
a.write({1: "cc", "ccc": 14, "ddd": {3: 4}})
with open(name) as f:
self.assertEqual(json.load(f),
{"1": "cc", "ccc": 14,
"ddd": {"3": 4}})
def test_json_read(self):
"""Check we can read from JSON file."""
name = "%s/test.json" % self.tmpdir
with open(name, "w") as f:
json.dump({1: "cc"}, f)
a = conf.Conf(name)
self.assertEqual(a.read(), {"1": "cc"})
def test_json_read_not_exist(self):
"""Check we get `None` when asking for an inexistant file."""
name = "%s/test.json" % self.tmpdir
a = conf.Conf(name)
self.assertEqual(a.read(), None)
def test_json_dont_update_if_no_change(self):
"""Check the file is not updated when there is no change."""
name = "%s/test.json" % self.tmpdir
with open(name, "w") as f:
f.write('{ "1": "2"}')
a = conf.Conf(name)
a.write({"1": "2"})
with open(name) as f:
self.assertEqual(f.read(), '{ "1": "2"}')
def test_json_write_to_stdout(self):
"""Check we can write the result to stdout."""
with patch("sys.stdout", new=open("%s/out" % self.tmpdir, "w")) as mock:
a = conf.Conf("-", "json")
a.write({"1": "2"})
with open("%s/out" % self.tmpdir) as f:
self.assertEqual(json.load(f),
{"1": "2"})
def test_json_no_overwrite_on_failure(self):
"""Check we don't overwrite an existing file in case of failure."""
name = "%s/test.json" % self.tmpdir
a = conf.Conf(name)
a.write({"1": "2"})
# Write something invalid
self.assertRaises(TypeError, a.write, json)
self.assertEqual(a.read(), {"1": "2"})
def test_json_appropriate_rights(self):
"""Check if a file is created with the appropriate rights"""
os.umask(022)
name = "%s/test.json" % self.tmpdir
a = conf.Conf(name)
a.write({"1": "2"})
del a
a = os.stat(name)
self.assertEqual(a.st_mode & 0777, 0644)
def test_json_appropriate_rights_umask(self):
"""Check if a file is created with appropriate rights using non standard umask."""
os.umask(027)
name = "%s/test.json" % self.tmpdir
a = conf.Conf(name)
a.write({"1": "2"})
del a
a = os.stat(name)
self.assertEqual(a.st_mode & 0777, 0640)
class TestConfYAML(TempDirectoryTestCase):
def test_yaml_write_from_extension(self):
"""Check we write YAML output when specifying `.yaml`."""
name = "%s/test.yaml" % self.tmpdir
a = conf.Conf(name)
a.write({1: "cc"})
with open(name) as f:
self.assertEqual(yaml.load(f), {1: "cc"})
def test_yaml_write_from_format(self):
"""Check we write YAML output when specifying `yaml` format."""
name = "%s/test.php" % self.tmpdir
a = conf.Conf(name, "yaml")
a.write({1: "cc"})
with open(name) as f:
self.assertEqual(yaml.load(f), {1: "cc"})
def test_yaml_write_nested(self):
"""Check we can write a more complex YAML output."""
name = "%s/test.yaml" % self.tmpdir
a = conf.Conf(name)
a.write({1: "cc", "ccc": 14, "ddd": {"3": 4}})
with open(name) as f:
self.assertEqual(yaml.load(f),
{1: "cc", "ccc": 14,
"ddd": {"3": 4}})
def test_yaml_read(self):
"""Check we can read from YAML file."""
name = "%s/test.yaml" % self.tmpdir
with open(name, "w") as f:
yaml.dump({1: "cc"}, f)
a = conf.Conf(name)
self.assertEqual(a.read(), {1: "cc"})
def test_yaml_read_not_exist(self):
"""Check we get `None` when asking for an inexistant file."""
name = "%s/test.yaml" % self.tmpdir
a = conf.Conf(name)
self.assertEqual(a.read(), None)
def test_yaml_dont_update_if_no_change(self):
"""Check the file is not updated when there is no change."""
name = "%s/test.yaml" % self.tmpdir
with open(name, "w") as f:
f.write('{ "1": "2"}')
a = conf.Conf(name)
a.write({"1": "2"})
with open(name) as f:
self.assertEqual(f.read(), '{ "1": "2"}')
class TestConfPHP(TempDirectoryTestCase):
def test_php_write_from_extension(self):
"""Check we write PHP output when specifying `.php`."""
name = "%s/test.php" % self.tmpdir
a = conf.Conf(name)
a.write({"1": "cc"})
with open(name) as f:
self.assertIn('"1" => "cc"', f.read())
def test_php_write_from_format(self):
"""Check we write PHP output when specifying `php` format."""
name = "%s/test.yaml" % self.tmpdir
a = conf.Conf(name, "php")
a.write({"1": "cc"})
with open(name) as f:
self.assertIn('"1" => "cc"', f.read())
def test_php_write_nested(self):
"""Check we can write a more complex PHP output."""
name = "%s/test.php" % self.tmpdir
a = conf.Conf(name)
a.write({"1": "cc", "ccc": "14", "ddd": {"3": "4"}})
with open(name) as f:
result = f.read()
self.assertIn('"1" => "cc"', result)
self.assertIn('"3" => "4"', result)
self.assertIn('"ddd" => array', result)
def test_php_read(self):
"""Check we can't read PHP"""
name = "%s/test.php" % self.tmpdir
with self.assertRaises(NotImplementedError):
conf.Conf(name).read()
def test_php_no_overwrite_on_failure(self):
"""Check we don't overwrite an existing file in case of failure."""
name = "%s/test.json" % self.tmpdir
a = conf.Conf(name)
a.write({"1": "2"})
# Write something invalid
self.assertRaises(TypeError, a.write, json)
self.assertEqual(a.read(), {"1": "2"})
class TestConfDir(TempDirectoryTestCase):
def test_dir_from_existence(self):
"""Check we use directory as output because the directory exists."""
a = conf.Conf(self.tmpdir)
a.write({"1": "cc"})
with open("%s/1" % self.tmpdir) as f:
self.assertEqual(f.read(), "cc")
def test_dir_from_format(self):
"""Check we create the directory if we force the format."""
name = "%s/test.php" % self.tmpdir
a = conf.Conf(name, "dir")
os.makedirs(name)
a.write({"1": "cc"})
with open("%s/1" % name) as f:
self.assertEqual(f.read(), "cc")
def test_dir_read(self):
"""Check we can read data from a directory."""
with open("%s/stuff" % self.tmpdir, "w") as f:
f.write("12")
with open("%s/otherstuff" % self.tmpdir, "w") as f:
f.write("13")
a = conf.Conf(self.tmpdir)
self.assertEqual(a.read(),
{"stuff": "12",
"otherstuff": "13"})
def test_dir_read_nested(self):
"""Check we can read nested data in a directory."""
os.makedirs("%s/otherstuff" % self.tmpdir)
with open("%s/stuff" % self.tmpdir, "w") as f:
f.write("12")
with open("%s/otherstuff/stuff" % self.tmpdir, "w") as f:
f.write("13")
a = conf.Conf(self.tmpdir)
self.assertEqual(a.read(),
{"stuff": "12",
"otherstuff": {"stuff": "13"}})
def test_dir_write_nested(self):
"""Check we can write nested data in a directory."""
a = conf.Conf(self.tmpdir)
a.write({"stuff": "12",
"otherstuff": {"1": "1221",
"2": "1111"}})
with open("%s/stuff" % self.tmpdir) as f:
self.assertEqual(f.read(), "12")
with open("%s/otherstuff/1" % self.tmpdir) as f:
self.assertEqual(f.read(), "1221")
with open("%s/otherstuff/2" % self.tmpdir) as f:
self.assertEqual(f.read(), "1111")
if __name__ == '__main__':
unittest.main()