~usn-tool/usn-tool/trunk

« back to all changes in this revision

Viewing changes to test.py

  • Committer: Steve Beattie
  • Date: 2019-02-19 07:48:48 UTC
  • Revision ID: sbeattie@ubuntu.com-20190219074848-2hmbpko59tlrzeav
The usn-tool repository has been converted to git.

To get the converted repository, please use:
  git clone https://git.launchpad.net/usn-tool

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
#!/usr/bin/env python
2
 
# -*- encoding: utf-8 -*-
3
 
from StringIO import StringIO
4
 
import tempfile
5
 
import unittest
6
 
import time
7
 
import sys
8
 
import os
9
 
 
10
 
import yaml
11
 
 
12
 
import usn
13
 
 
14
 
 
15
 
class SaveLoadDatabaseTest(unittest.TestCase):
16
 
 
17
 
    def setUp(self):
18
 
        self.filename = tempfile.mktemp()
19
 
 
20
 
    def tearDown(self):
21
 
        os.unlink(self.filename)
22
 
 
23
 
    def test_save_and_load_database(self):
24
 
        data = {"Hello": "world!"}
25
 
        usn.save_database(data, self.filename)
26
 
        self.assertTrue(os.path.isfile(self.filename))
27
 
        self.assertEquals(usn.load_database(self.filename), data)
28
 
 
29
 
    def test_save_and_load_database_with_filename(self):
30
 
        data = {"Hello": "world!"}
31
 
        usn.save_database(data, self.filename)
32
 
        self.assertTrue(os.path.isfile(self.filename))
33
 
        self.assertEquals(usn.load_database(self.filename), data)
34
 
 
35
 
 
36
 
class GetRestrictionsTest(unittest.TestCase):
37
 
 
38
 
    def setUp(self):
39
 
        self.original_filename = usn.CONFIG_FILENAME
40
 
        self.filename = usn.CONFIG_FILENAME = tempfile.mktemp()
41
 
 
42
 
    def tearDown(self):
43
 
        usn.CONFIG_FILENAME = self.original_filename
44
 
        if os.path.isfile(self.filename):
45
 
            os.unlink(self.filename)
46
 
 
47
 
    def test_unexistent(self):
48
 
        self.assertEquals(usn.get_restrictions(), {})
49
 
 
50
 
    def test_empty(self):
51
 
        file = open(self.filename, "w")
52
 
        file.write("[restrictions]\n")
53
 
        file.close()
54
 
        self.assertEquals(usn.get_restrictions(), {})
55
 
 
56
 
    def test_with_values(self):
57
 
        file = open(self.filename, "w")
58
 
        file.write("[restrictions]\n"
59
 
                   "option1 = value1 value2\n"
60
 
                   "option2 = value3 value4\n")
61
 
        file.close()
62
 
        self.assertEquals(usn.get_restrictions(),
63
 
                          {"option1": set(["value1", "value2"]),
64
 
                           "option2": set(["value3", "value4"])})
65
 
 
66
 
 
67
 
class USNMapTest(unittest.TestCase):
68
 
 
69
 
    def setUp(self):
70
 
        self.usn_map = usn.USNMap()
71
 
 
72
 
    def test_process_option_without_path(self):
73
 
        self.usn_map.process_option(None, "--foo", "bar", None)
74
 
        self.assertEquals(self.usn_map, {})
75
 
 
76
 
    def test_process_option_with_path(self):
77
 
        self.usn_map.process_option(None, "--foo", "bar", None, ("key",))
78
 
        self.assertEquals(self.usn_map, {"key": "bar"})
79
 
 
80
 
    def test_process_option_with_path_composed(self):
81
 
        self.usn_map.process_option(None, "--foo", "bar", None,
82
 
                                    ("key", "subkey"))
83
 
        self.assertEquals(self.usn_map, {"key": {"subkey": "bar"}})
84
 
 
85
 
    def test_process_option_with_two_paths(self):
86
 
        self.usn_map.process_option(None, "--foo", "bar", None,
87
 
                                    ("key", "subkey1"))
88
 
        self.usn_map.process_option(None, "--foo", "baz", None,
89
 
                                    ("key", "subkey2"))
90
 
        self.assertEquals(self.usn_map,
91
 
                          {"key": {"subkey1": "bar", "subkey2": "baz"}})
92
 
 
93
 
    def test_process_option_with_variable(self):
94
 
        self.usn_map.process_option(None, "--hey", "dude", None)
95
 
        self.usn_map.process_option(None, "--foo", "bar", None,
96
 
                                    ("key", usn.Var("hey"), "subkey"))
97
 
        self.assertEquals(self.usn_map, {"key": {"dude": {"subkey": "bar"}}})
98
 
 
99
 
    def test_process_option_append(self):
100
 
        self.usn_map.process_option(None, "--foo", "bar", None,
101
 
                                    ("key", "subkey"), append=True)
102
 
        self.assertEquals(self.usn_map, {"key": {"subkey": ["bar"]}})
103
 
        self.usn_map.process_option(None, "--foo", "baz", None,
104
 
                                    ("key", "subkey"), append=True)
105
 
        self.assertEquals(self.usn_map, {"key": {"subkey": ["bar", "baz"]}})
106
 
 
107
 
    def test_process_option_numeric(self):
108
 
        self.usn_map.process_option(None, "--foo", "123", None,
109
 
                                    ("key", "subkey"), numeric=True)
110
 
        self.assertEquals(self.usn_map, {"key": {"subkey": 123}})
111
 
 
112
 
    def test_allow_unrestricted(self):
113
 
        usn_map = usn.USNMap({"foo": set(["bar"])})
114
 
        usn_map.process_option(None, "--foo", "bar", None)
115
 
 
116
 
    def test_disallow_restricted(self):
117
 
        usn_map = usn.USNMap({"foo": set(["bar"])})
118
 
        self.assertRaises(usn.Error, usn_map.process_option,
119
 
                          None, "--foo", ("baz",), None)
120
 
 
121
 
    def test_process_option_from_stdin(self):
122
 
        sio = StringIO("STDIN")
123
 
        old_stdin = sys.stdin
124
 
        sys.stdin = sio
125
 
        try:
126
 
            self.usn_map.process_option(None, "--foo", "-", None, ("key",))
127
 
            self.assertEquals(self.usn_map, {"key": "STDIN"})
128
 
        finally:
129
 
            sys.stdin = old_stdin
130
 
 
131
 
    def test_out_of_order_error(self):
132
 
        try:
133
 
            self.usn_map.process_option(None, "--foo", "bar", None,
134
 
                                        (usn.Var("baz"),))
135
 
        except usn.Error as e:
136
 
            pass
137
 
        else:
138
 
            self.fail("Error not raised")
139
 
        self.assertEquals(str(e), "option --baz is needed before --foo")
140
 
 
141
 
 
142
 
class ParseOptionsTest(unittest.TestCase):
143
 
 
144
 
    def test_bad_args(self):
145
 
        self.assertRaises(usn.Error, usn.parse_options, ["123-4", "foo"])
146
 
 
147
 
    def test_lack_of_usn(self):
148
 
        self.assertRaises(usn.Error, usn.parse_options, ["--title", "foo"])
149
 
 
150
 
    def test_defaults(self):
151
 
        options = usn.parse_options([])
152
 
        self.assertEquals(options.usn, None)
153
 
        self.assertEquals(options.usn_map, {})
154
 
 
155
 
    def test_usn_map_is_dict(self):
156
 
        options = usn.parse_options(["123-4", "--title", "foo"])
157
 
        self.assertEquals(type(options.usn_map), dict)
158
 
 
159
 
    def test_usn(self):
160
 
        options = usn.parse_options(["123-4"])
161
 
        self.assertEquals(options.usn, "123-4")
162
 
 
163
 
    def test_template(self):
164
 
        options = usn.parse_options(["123-4", "--template", "TEMPL"])
165
 
        self.assertEquals(options.template, "TEMPL")
166
 
 
167
 
    def test_import(self):
168
 
        options = usn.parse_options(["--import"])
169
 
        self.assertTrue(options.import_)
170
 
 
171
 
    def test_export(self):
172
 
        options = usn.parse_options(["--export"])
173
 
        self.assertTrue(options.export)
174
 
 
175
 
    def test_title(self):
176
 
        options = usn.parse_options(["123-4", "--title", "TITLE"])
177
 
        self.assertEquals(options.usn_map, {"title": "TITLE"})
178
 
 
179
 
    def test_summary(self):
180
 
        options = usn.parse_options(["123-4", "--summary", "SUMMARY"])
181
 
        self.assertEquals(options.usn_map, {"summary": "SUMMARY"})
182
 
 
183
 
    def test_description(self):
184
 
        options = usn.parse_options(["123-4", "--description", "DESCR"])
185
 
        self.assertEquals(options.usn_map, {"description": "DESCR"})
186
 
 
187
 
    def test_action(self):
188
 
        options = usn.parse_options(["123-4", "--action", "ACTION"])
189
 
        self.assertEquals(options.usn_map, {"action": "ACTION"})
190
 
 
191
 
    def test_cve_one(self):
192
 
        options = usn.parse_options(["123-4", "--cve", "CVE"])
193
 
        self.assertEquals(options.usn_map, {"cves": ["CVE"]})
194
 
 
195
 
    def test_cve_two(self):
196
 
        options = usn.parse_options(["123-4", "--cve", "CVE1", "--cve", "CVE2"])
197
 
        self.assertEquals(options.usn_map, {"cves": ["CVE1", "CVE2"]})
198
 
 
199
 
    def test_timestamp(self):
200
 
        options = usn.parse_options(["123-4", "--timestamp", "123"])
201
 
        self.assertEquals(options.usn_map, {"timestamp": 123})
202
 
 
203
 
    def test_binary_version(self):
204
 
        options = usn.parse_options(["123-4", "--release", "REL",
205
 
                                     "--package", "PKG",
206
 
                                     "--binary-version", "VER"])
207
 
        self.assertEquals(options.usn_map,
208
 
                          {"releases":
209
 
                              {"REL":
210
 
                                  {"binaries": {"PKG": {"version": "VER"}}}}})
211
 
 
212
 
    def test_source_version(self):
213
 
        options = usn.parse_options(["123-4", "--release", "REL",
214
 
                                     "--package", "PKG",
215
 
                                     "--source-version", "VER"])
216
 
        self.assertEquals(options.usn_map,
217
 
                          {"releases":
218
 
                              {"REL":
219
 
                                  {"sources": {"PKG": {"version": "VER"}}}}})
220
 
 
221
 
    def test_url_size(self):
222
 
        options = usn.parse_options(["123-4", "--release", "REL",
223
 
                                     "--arch", "ARCH", "--url", "URL",
224
 
                                     "--url-size", "123"])
225
 
        self.assertEquals(options.usn_map,
226
 
                          {"releases":
227
 
                              {"REL":
228
 
                                  {"archs":
229
 
                                      {"ARCH":
230
 
                                          {"urls":
231
 
                                              {"URL": {"size": 123}}}}}}})
232
 
 
233
 
    def test_url_size(self):
234
 
        options = usn.parse_options(["123-4", "--release", "REL",
235
 
                                     "--arch", "ARCH", "--url", "URL",
236
 
                                     "--url-md5", "MD5"])
237
 
        self.assertEquals(options.usn_map,
238
 
                          {"releases":
239
 
                              {"REL":
240
 
                                  {"archs":
241
 
                                      {"ARCH":
242
 
                                          {"urls":
243
 
                                              {"URL": {"md5": "MD5"}}}}}}})
244
 
 
245
 
    def test_restriction(self):
246
 
        original_filename = usn.CONFIG_FILENAME
247
 
        filename = usn.CONFIG_FILENAME = tempfile.mktemp()
248
 
        file = open(filename, "w")
249
 
        file.write("[restrictions]\n"
250
 
                   "title = one two\n")
251
 
        file.close()
252
 
        try:
253
 
            options = usn.parse_options(["123-4", "--title", "one"])
254
 
            self.assertEquals(options.usn_map, {"title": "one"})
255
 
            options = usn.parse_options(["123-4", "--title", "two"])
256
 
            self.assertEquals(options.usn_map, {"title": "two"})
257
 
            self.assertRaises(usn.Error,
258
 
                              usn.parse_options, ["123-4", "--title", "three"])
259
 
            self.assertRaises(usn.Error,
260
 
                              usn.parse_options, ["123-4", "--title", "four"])
261
 
        finally:
262
 
            usn.CONFIG_FILENAME = original_filename
263
 
            os.unlink(filename)
264
 
 
265
 
 
266
 
class RenderTemplateTest(unittest.TestCase):
267
 
 
268
 
    def test_text_template(self):
269
 
        # We include some HTML to make sure that it's using the text markup.
270
 
        output = usn.render_template("Hello, $name!"
271
 
                                     '<p py:content="foo"></p>',
272
 
                                     {"name": "Bob"})
273
 
        self.assertEquals(output, 'Hello, Bob!<p py:content="foo"></p>')
274
 
 
275
 
    def test_html_template(self):
276
 
        # We include some text to make sure that it's using the HTML markup.
277
 
        output = usn.render_template("<p>{# hide #}\n"
278
 
                                     "#for name in names\n"
279
 
                                     "Hello, $name!\n"
280
 
                                     "#end\n"
281
 
                                     "</p>\n",
282
 
                                     {"name": "Bob", "names": ["Joe"]})
283
 
        self.assertEquals(output,
284
 
                          "<p>{# hide #}\n#for name in names\n"
285
 
                          "Hello, Bob!\n#end\n</p>")
286
 
 
287
 
 
288
 
class UpdateMapTest(unittest.TestCase):
289
 
 
290
 
    def test_do_nothing(self):
291
 
        map = {}
292
 
        changes = {}
293
 
        usn.update_map(map, changes)
294
 
        self.assertEquals(map, {})
295
 
 
296
 
    def test_add_key(self):
297
 
        map = {}
298
 
        changes = {"key": "value"}
299
 
        usn.update_map(map, changes)
300
 
        self.assertEquals(map, {"key": "value"})
301
 
 
302
 
    def test_add_key_and_subkey(self):
303
 
        map = {}
304
 
        changes = {"key": {"subkey": "value"}}
305
 
        usn.update_map(map, changes)
306
 
        self.assertEquals(map, {"key": {"subkey": "value"}})
307
 
 
308
 
    def test_add_subkey(self):
309
 
        map = {"key": {}}
310
 
        changes = {"key": {"subkey": "value"}}
311
 
        usn.update_map(map, changes)
312
 
        self.assertEquals(map, {"key": {"subkey": "value"}})
313
 
 
314
 
    def test_change_key(self):
315
 
        map = {"key": "value1"}
316
 
        changes = {"key": "value2"}
317
 
        usn.update_map(map, changes)
318
 
        self.assertEquals(map, {"key": "value2"})
319
 
 
320
 
    def test_change_subkey(self):
321
 
        map = {"key": {"subkey": "value1"}}
322
 
        changes = {"key": {"subkey": "value2"}}
323
 
        usn.update_map(map, changes)
324
 
        self.assertEquals(map, {"key": {"subkey": "value2"}})
325
 
 
326
 
    def test_extend_list(self):
327
 
        map = {"key": ["value1"]}
328
 
        changes = {"key": ["value2"]}
329
 
        usn.update_map(map, changes)
330
 
        self.assertEquals(map, {"key": ["value1", "value2"]})
331
 
 
332
 
 
333
 
class DecodeStringsTest(unittest.TestCase):
334
 
 
335
 
    def test_decode_strings(self):
336
 
        map = usn.decode_strings({"a": "\xc3\xa4", "b": 1, "c": {"d": "e"},
337
 
                              "f": ["g", "h", "i"]})
338
 
        self.assertEquals(map, {"a": u"ä", "b": 1, "c": {"d": "e"},
339
 
                                "f": ["g", "h", "i"]})
340
 
        self.assertEquals(type(map["a"]), unicode)
341
 
        self.assertEquals(type(map["c"]["d"]), unicode)
342
 
        self.assertEquals(type(map["f"][0]), unicode)
343
 
        self.assertEquals(type(map["f"][1]), unicode)
344
 
        self.assertEquals(type(map["f"][2]), unicode)
345
 
 
346
 
 
347
 
class MainTest(unittest.TestCase):
348
 
 
349
 
    def setUp(self):
350
 
        self.original_database_filename = usn.DATABASE_FILENAME
351
 
        self.original_config_filename = usn.CONFIG_FILENAME
352
 
        self.original_time = time.time
353
 
        self.database_filename = usn.DATABASE_FILENAME = tempfile.mktemp()
354
 
        self.config_filename = usn.CONFIG_FILENAME = tempfile.mktemp()
355
 
        self.time = 1187379812
356
 
        time.time = lambda: self.time
357
 
 
358
 
    def tearDown(self):
359
 
        usn.DATABASE_FILENAME = self.original_database_filename
360
 
        usn.CONFIG_FILENAME = self.original_config_filename
361
 
        time.time = self.original_time
362
 
        if os.path.isfile(self.database_filename):
363
 
            os.unlink(self.database_filename)
364
 
        if os.path.isfile(self.config_filename):
365
 
            os.unlink(self.config_filename)
366
 
 
367
 
    def test_create_usn(self):
368
 
        usn.main(["123-4", "--title", "one"])
369
 
        data = usn.load_database(self.database_filename)
370
 
        self.assertEquals(data,
371
 
                          {"123-4": {"id": "123-4",
372
 
                                     "timestamp": 1187379812,
373
 
                                     "title": "one",
374
 
                                     "releases": {}}})
375
 
 
376
 
    def test_update_usn(self):
377
 
        usn.save_database({"123-4": {"cves": ["CVE1"], "timestamp": 0}},
378
 
                          self.database_filename)
379
 
        usn.main(["123-4", "--title", "TITLE", "--cve", "CVE2"])
380
 
        data = usn.load_database(self.database_filename)
381
 
        self.assertEquals(data,
382
 
                          {"123-4": {"id": "123-4",
383
 
                                     "timestamp": 0,
384
 
                                     "title": "TITLE",
385
 
                                     "cves": ["CVE1", "CVE2"],
386
 
                                     "releases": {}}})
387
 
 
388
 
    def test_template(self):
389
 
        filename = tempfile.mktemp()
390
 
        try:
391
 
            file = open(filename, "w")
392
 
            file.write("Title: $title")
393
 
            file.close()
394
 
            usn.main(["123-4", "--title", "oné"])
395
 
            usn.main(["123-45", "--title", "two"])
396
 
            output = usn.main(["123-4", "--template", filename])
397
 
            self.assertEquals(output, "Title: oné")
398
 
            output = usn.main(["123-45", "--template", filename])
399
 
            self.assertEquals(output, "Title: two")
400
 
            self.assertRaises(usn.Error,
401
 
                              usn.main, ["123-456", "--template", filename])
402
 
        finally:
403
 
            os.unlink(filename)
404
 
 
405
 
    def test_timestamp_is_datetime(self):
406
 
        filename = tempfile.mktemp()
407
 
        try:
408
 
            file = open(filename, "w")
409
 
            file.write("$timestamp.__class__.__name__")
410
 
            file.close()
411
 
            usn.main(["123-4", "--timestamp", "0"])
412
 
            output = usn.main(["123-4", "--template", filename])
413
 
            self.assertEquals(output, "datetime")
414
 
        finally:
415
 
            os.unlink(filename)
416
 
 
417
 
    def test_import(self):
418
 
        usn.save_database({"123-4": {"cves": ["CVE1"]}},
419
 
                          self.database_filename)
420
 
        old_stdin = sys.stdin
421
 
        sys.stdin = StringIO("123-4: {cves: [CVE2]}")
422
 
        try:
423
 
            usn.main(["--import"])
424
 
        finally:
425
 
            sys.stdin = old_stdin
426
 
        data = usn.load_database(self.database_filename)
427
 
        self.assertEquals(data, {"123-4": {"cves": ["CVE2"]}})
428
 
 
429
 
    def test_export_without_usn(self):
430
 
        usn.save_database({"123-4": {"cves": ["CVE1"]},
431
 
                           "123-5": {"cves": ["CVE2"]}},
432
 
                          self.database_filename)
433
 
        self.assertEquals(usn.main(["--export"]),
434
 
                          "123-4:\n  cves: [CVE1]\n123-5:\n  cves: [CVE2]\n")
435
 
 
436
 
    def test_export(self):
437
 
        usn.save_database({"123-4": {"cves": ["CVE1"]}},
438
 
                          self.database_filename)
439
 
        self.assertEquals(usn.main(["--export"]),
440
 
                          "123-4:\n  cves: [CVE1]\n")
441
 
 
442
 
 
443
 
if __name__ == "__main__":
444
 
        unittest.main()