~sil2100/ubuntu-system-image/server_si-nondestructive

« back to all changes in this revision

Viewing changes to tests/test_diff.py

  • Committer: StĂ©phane Graber
  • Date: 2013-05-29 14:09:57 UTC
  • Revision ID: stgraber@ubuntu.com-20130529140957-hbfysxvgebu6fqza
Add testsuite

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
from phablet.diff import ImageDiff
 
2
import tarfile
 
3
import tempfile
 
4
from io import StringIO
 
5
import unittest
 
6
import os
 
7
 
 
8
 
 
9
class DiffTests(unittest.TestCase):
 
10
    def setUp(self):
 
11
        fd, source_tarball_path = tempfile.mkstemp()
 
12
        os.close(fd)
 
13
        fd, target_tarball_path = tempfile.mkstemp()
 
14
        os.close(fd)
 
15
 
 
16
        source_tarball = tarfile.open(source_tarball_path, "w")
 
17
        target_tarball = tarfile.open(target_tarball_path, "w")
 
18
 
 
19
        a = tarfile.TarInfo()
 
20
        a.name = "a"
 
21
        a.size = 4
 
22
 
 
23
        b = tarfile.TarInfo()
 
24
        b.name = "b"
 
25
        b.size = 4
 
26
 
 
27
        c_dir = tarfile.TarInfo()
 
28
        c_dir.name = "c"
 
29
        c_dir.type = tarfile.DIRTYPE
 
30
        c_dir.mode = 0o755
 
31
 
 
32
        c = tarfile.TarInfo()
 
33
        c.name = "c/c"
 
34
        c.size = 4
 
35
 
 
36
        d_source = tarfile.TarInfo()
 
37
        d_source.name = "c/d"
 
38
        d_source.size = 8
 
39
        d_source.mtime = 1000
 
40
 
 
41
        d_target = tarfile.TarInfo()
 
42
        d_target.name = "c/d"
 
43
        d_target.size = 8
 
44
        d_target.mtime = 1234
 
45
 
 
46
        source_tarball.addfile(a, StringIO(u"test"))
 
47
        source_tarball.addfile(b, StringIO(u"test"))
 
48
        source_tarball.addfile(c_dir, StringIO(u"test"))
 
49
        source_tarball.addfile(d_source, StringIO(u"test-abc"))
 
50
 
 
51
        target_tarball.addfile(a, StringIO(u"test"))
 
52
        target_tarball.addfile(c_dir, StringIO(u"test"))
 
53
        target_tarball.addfile(c, StringIO(u"test"))
 
54
        target_tarball.addfile(d_target, StringIO(u"test-def"))
 
55
 
 
56
        source_tarball.close()
 
57
        target_tarball.close()
 
58
 
 
59
        self.imagediff = ImageDiff(source_tarball_path, target_tarball_path)
 
60
        self.source_tarball_path = source_tarball_path
 
61
        self.target_tarball_path = target_tarball_path
 
62
 
 
63
    def tearDown(self):
 
64
#        os.remove(self.source_tarball_path)
 
65
#        os.remove(self.target_tarball_path)
 
66
        pass
 
67
 
 
68
    def test_content(self):
 
69
        content_set, content_dict = self.imagediff.scan_content("source")
 
70
        self.assertEquals(sorted(content_dict.keys()),
 
71
                          ['a', 'b', 'c', 'c/d'])
 
72
 
 
73
        content_set, content_dict = self.imagediff.scan_content("target")
 
74
        self.assertEquals(sorted(content_dict.keys()),
 
75
                          ['a', 'c', 'c/c', 'c/d'])
 
76
 
 
77
    def test_content_invalid_image(self):
 
78
        self.assertRaises(KeyError, self.imagediff.scan_content, "invalid")
 
79
 
 
80
    def test_compare(self):
 
81
        self.imagediff.compare_images()
 
82
        self.imagediff.print_changes()