~ubuntu-core-dev/ubuntu-release-upgrader/trunk

2519.1.2 by Michael Terry
add a (disabled for now) pep8 test and make some initial pep8 fixups in other tests
1
#!/usr/bin/python3
2
# -*- Mode: Python; indent-tabs-mode: nil; tab-width: 4; coding: utf-8 -*-
1133 by Michael Vogt
* DistUpgrade/DistUpgradeQuirks.py:
3
4
import apt
2075 by Michael Vogt
* DistUpgrade/DistUpgradeQuirks.py:
5
import apt_pkg
2063 by Michael Vogt
add native ed implementation
6
import hashlib
2004 by Michael Vogt
* debian/control:
7
import mock
2519.1.1 by Michael Terry
use nosetests3 for tests
8
import os
1133 by Michael Vogt
* DistUpgrade/DistUpgradeQuirks.py:
9
import unittest
10
import shutil
2075 by Michael Vogt
* DistUpgrade/DistUpgradeQuirks.py:
11
import tempfile
1133 by Michael Vogt
* DistUpgrade/DistUpgradeQuirks.py:
12
13
from DistUpgrade.DistUpgradeQuirks import DistUpgradeQuirks
14
2519.1.1 by Michael Terry
use nosetests3 for tests
15
CURDIR = os.path.dirname(os.path.abspath(__file__))
16
17
1133 by Michael Vogt
* DistUpgrade/DistUpgradeQuirks.py:
18
class MockController(object):
19
    def __init__(self):
20
        self._view = None
21
2519.1.1 by Michael Terry
use nosetests3 for tests
22
1133 by Michael Vogt
* DistUpgrade/DistUpgradeQuirks.py:
23
class MockConfig(object):
24
    pass
25
2519.1.1 by Michael Terry
use nosetests3 for tests
26
27
class TestPatches(unittest.TestCase):
28
29
    orig_chdir = ''
30
31
    def setUp(self):
32
        # To patch, we need to be in the same directory as the patched files
33
        self.orig_chdir = os.getcwd()
34
        os.chdir(CURDIR)
35
36
    def tearDown(self):
37
        os.chdir(self.orig_chdir)
38
39
    def _verify_result_checksums(self):
40
        """ helper for test_patch to verify that we get the expected result """
41
        # simple case is foo
42
        patchdir = CURDIR + "/patchdir/"
43
        self.assertFalse("Hello" in open(patchdir + "foo").read())
44
        self.assertTrue("Hello" in open(patchdir + "foo_orig").read())
45
        md5 = hashlib.md5()
46
        with open(patchdir + "foo", "rb") as patch:
47
            md5.update(patch.read())
48
        self.assertEqual(md5.hexdigest(), "52f83ff6877e42f613bcd2444c22528c")
49
        # more complex example fstab
50
        md5 = hashlib.md5()
51
        with open(patchdir + "fstab", "rb") as patch:
52
            md5.update(patch.read())
53
        self.assertEqual(md5.hexdigest(), "c56d2d038afb651920c83106ec8dfd09")
54
        # most complex example
55
        md5 = hashlib.md5()
56
        with open(patchdir + "pycompile", "rb") as patch:
57
            md5.update(patch.read())
58
        self.assertEqual(md5.hexdigest(), "97c07a02e5951cf68cb3f86534f6f917")
59
        # with ".\n"
60
        md5 = hashlib.md5()
61
        with open(patchdir + "dotdot", "rb") as patch:
62
            md5.update(patch.read())
63
        self.assertEqual(md5.hexdigest(), "cddc4be46bedd91db15ddb9f7ddfa804")
64
        # test that incorrect md5sum after patching rejects the patch
65
        self.assertEqual(open(patchdir + "fail").read(),
66
                         open(patchdir + "fail_orig").read())
67
68
    def test_patch(self):
69
        q = DistUpgradeQuirks(MockController(), MockConfig)
70
        # create patch environment
71
        patchdir = CURDIR + "/patchdir/"
72
        shutil.copy(patchdir + "foo_orig", patchdir + "foo")
73
        shutil.copy(patchdir + "fstab_orig", patchdir + "fstab")
74
        shutil.copy(patchdir + "pycompile_orig", patchdir + "pycompile")
75
        shutil.copy(patchdir + "dotdot_orig", patchdir + "dotdot")
76
        shutil.copy(patchdir + "fail_orig", patchdir + "fail")
77
        # apply patches
78
        q._applyPatches(patchdir=patchdir)
79
        self._verify_result_checksums()
80
        # now apply patches again and ensure we don't patch twice
81
        q._applyPatches(patchdir=patchdir)
82
        self._verify_result_checksums()
83
84
    def test_patch_lowlevel(self):
85
        #test lowlevel too
86
        from DistUpgrade.DistUpgradePatcher import patch, PatchError
2519.1.2 by Michael Terry
add a (disabled for now) pep8 test and make some initial pep8 fixups in other tests
87
        self.assertRaises(PatchError, patch, CURDIR + "/patchdir/fail",
88
                          CURDIR + "/patchdir/patchdir_fail."
89
                          "ed04abbc6ee688ee7908c9dbb4b9e0a2."
90
                          "deadbeefdeadbeefdeadbeff",
91
                          "deadbeefdeadbeefdeadbeff")
2519.1.1 by Michael Terry
use nosetests3 for tests
92
93
2004 by Michael Vogt
* debian/control:
94
class TestQuirks(unittest.TestCase):
95
2519.1.1 by Michael Terry
use nosetests3 for tests
96
    orig_recommends = ''
97
    orig_status = ''
98
99
    def setUp(self):
100
        self.orig_recommends = apt_pkg.config.get("APT::Install-Recommends")
101
        self.orig_status = apt_pkg.config.get("Dir::state::status")
102
103
    def tearDown(self):
104
        apt_pkg.config.set("APT::Install-Recommends", self.orig_recommends)
105
        apt_pkg.config.set("Dir::state::status", self.orig_status)
106
2101 by Michael Vogt
* DistUpgrade/DistUpgradeQuirks.py, DistUpgrade/DistUpgradeController.py:
107
    def test_enable_recommends_during_upgrade(self):
108
        controller = mock.Mock()
109
110
        config = mock.Mock()
111
        q = DistUpgradeQuirks(controller, config)
112
        # server mode
113
        apt_pkg.config.set("APT::Install-Recommends", "0")
114
        controller.serverMode = True
115
        self.assertFalse(apt_pkg.config.find_b("APT::Install-Recommends"))
116
        q.ensure_recommends_are_installed_on_desktops()
117
        self.assertFalse(apt_pkg.config.find_b("APT::Install-Recommends"))
118
        # desktop mode
119
        apt_pkg.config.set("APT::Install-Recommends", "0")
120
        controller.serverMode = False
121
        self.assertFalse(apt_pkg.config.find_b("APT::Install-Recommends"))
122
        q.ensure_recommends_are_installed_on_desktops()
123
        self.assertTrue(apt_pkg.config.find_b("APT::Install-Recommends"))
124
2004 by Michael Vogt
* debian/control:
125
    def test_parse_from_modaliases_header(self):
2519.1.2 by Michael Terry
add a (disabled for now) pep8 test and make some initial pep8 fixups in other tests
126
        pkgrec = {
127
            "Package": "foo",
128
            "Modaliases": "modules1(pci:v00001002d00006700sv*sd*bc03sc*i*, "
129
                          "pci:v00001002d00006701sv*sd*bc03sc*i*), "
130
                          "module2(pci:v00001002d00006702sv*sd*bc03sc*i*, "
131
                          "pci:v00001001d00006702sv*sd*bc03sc*i*)"
132
        }
2004 by Michael Vogt
* debian/control:
133
        controller = mock.Mock()
134
        config = mock.Mock()
135
        q = DistUpgradeQuirks(controller, config)
136
        self.assertEqual(q._parse_modaliases_from_pkg_header({}), [])
137
        self.assertEqual(q._parse_modaliases_from_pkg_header(pkgrec),
138
                         [("modules1",
2519.1.2 by Michael Terry
add a (disabled for now) pep8 test and make some initial pep8 fixups in other tests
139
                           ["pci:v00001002d00006700sv*sd*bc03sc*i*",
140
                            "pci:v00001002d00006701sv*sd*bc03sc*i*"]),
2790 by Michael Vogt
* tests/test_pep8.py:
141
                          ("module2",
142
                           ["pci:v00001002d00006702sv*sd*bc03sc*i*",
143
                            "pci:v00001001d00006702sv*sd*bc03sc*i*"])])
1133 by Michael Vogt
* DistUpgrade/DistUpgradeQuirks.py:
144
1147 by Michael Vogt
* DistUpgrade/DistUpgradeQuirks.py:
145
    def testFglrx(self):
2557 by Brian Murray
releasing version 1:0.181
146
        mock_lspci_good = set(['1002:9990'])
1147 by Michael Vogt
* DistUpgrade/DistUpgradeQuirks.py:
147
        mock_lspci_bad = set(['8086:ac56'])
2004 by Michael Vogt
* debian/control:
148
        config = mock.Mock()
149
        cache = apt.Cache()
150
        controller = mock.Mock()
151
        controller.cache = cache
152
        q = DistUpgradeQuirks(controller, config)
2730 by Brian Murray
Only run fglrx tests on i386 and amd64
153
        if q.arch not in ['i386', 'amd64']:
2790 by Michael Vogt
* tests/test_pep8.py:
154
            return self.skipTest("Not on an arch with fglrx package")
2416 by Colin Watson
Modernise use of unittest methods.
155
        self.assertTrue(q._supportInModaliases("fglrx", mock_lspci_good))
156
        self.assertFalse(q._supportInModaliases("fglrx", mock_lspci_bad))
1147 by Michael Vogt
* DistUpgrade/DistUpgradeQuirks.py:
157
1936 by Michael Vogt
* DistUpgrade/DistUpgradeQuirks.py:
158
    def test_cpu_is_i686(self):
159
        q = DistUpgradeQuirks(MockController(), MockConfig)
160
        q.arch = "i386"
2519.1.1 by Michael Terry
use nosetests3 for tests
161
        testdir = CURDIR + "/test-data/"
2519.1.2 by Michael Terry
add a (disabled for now) pep8 test and make some initial pep8 fixups in other tests
162
        self.assertTrue(
163
            q._cpu_is_i686_and_has_cmov(testdir + "cpuinfo-with-sse"))
164
        self.assertFalse(
165
            q._cpu_is_i686_and_has_cmov(testdir + "cpuinfo-without-cmov"))
166
        self.assertFalse(
167
            q._cpu_is_i686_and_has_cmov(testdir + "cpuinfo-i586"))
168
        self.assertFalse(
169
            q._cpu_is_i686_and_has_cmov(testdir + "cpuinfo-i486"))
170
        self.assertTrue(
171
            q._cpu_is_i686_and_has_cmov(testdir + "cpuinfo-via-c7m"))
1523 by Michael Vogt
switch patching feature from patch to sed and add test, also apply patch for the debconf gnome frontend (LP: #387112)
172
2075 by Michael Vogt
* DistUpgrade/DistUpgradeQuirks.py:
173
    def test_kde_card_games_transition(self):
174
        # fake nothing is installed
175
        empty_status = tempfile.NamedTemporaryFile()
176
        apt_pkg.config.set("Dir::state::status", empty_status.name)
177
178
        # create quirks class
179
        controller = mock.Mock()
180
        config = mock.Mock()
181
        quirks = DistUpgradeQuirks(controller, config)
182
        # add cache to the quirks class
183
        cache = quirks.controller.cache = apt.Cache()
184
        # add mark_install to the cache (this is part of mycache normally)
185
        cache.mark_install = lambda p, s: cache[p].mark_install()
186
187
        # test if the quirks handler works when kdegames-card is not installed
188
        # does not try to install it
189
        self.assertFalse(cache["kdegames-card-data-extra"].marked_install)
190
        quirks._add_kdegames_card_extra_if_installed()
191
        self.assertFalse(cache["kdegames-card-data-extra"].marked_install)
192
193
        # mark it for install
194
        cache["kdegames-card-data"].mark_install()
195
        self.assertFalse(cache["kdegames-card-data-extra"].marked_install)
196
        quirks._add_kdegames_card_extra_if_installed()
197
        # verify that the quirks handler is now installing it
2659 by Brian Murray
fix pep8 issues in tests
198
        self.assertTrue(cache["kdegames-card-data-extra"].marked_install)
2075 by Michael Vogt
* DistUpgrade/DistUpgradeQuirks.py:
199
2246.1.6 by Michael Vogt
* DistUpgrade/DistUpgradeQuirks.py:
200
    def test_screensaver_poke(self):
201
        # fake nothing is installed
202
        empty_status = tempfile.NamedTemporaryFile()
203
        apt_pkg.config.set("Dir::state::status", empty_status.name)
204
205
        # create quirks class
206
        controller = mock.Mock()
207
        config = mock.Mock()
208
        quirks = DistUpgradeQuirks(controller, config)
209
        quirks._pokeScreensaver()
210
        res = quirks._stopPokeScreensaver()
2519.1.2 by Michael Terry
add a (disabled for now) pep8 test and make some initial pep8 fixups in other tests
211
        res  # pyflakes
2246.1.6 by Michael Vogt
* DistUpgrade/DistUpgradeQuirks.py:
212
1133 by Michael Vogt
* DistUpgrade/DistUpgradeQuirks.py:
213
if __name__ == "__main__":
1523 by Michael Vogt
switch patching feature from patch to sed and add test, also apply patch for the debconf gnome frontend (LP: #387112)
214
    import logging
215
    logging.basicConfig(level=logging.DEBUG)
1133 by Michael Vogt
* DistUpgrade/DistUpgradeQuirks.py:
216
    unittest.main()