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

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
#!/usr/bin/python3
# -*- Mode: Python; indent-tabs-mode: nil; tab-width: 4; coding: utf-8 -*-

from __future__ import print_function

import apt_pkg
import os
import tempfile
import unittest

from mock import Mock

from DistUpgrade.DistUpgradeAptCdrom import AptCdrom

CURDIR = os.path.dirname(os.path.abspath(__file__))


class TestAptCdrom(unittest.TestCase):
    " this test the apt-cdrom implementation "

#    def testAdd(self):
#        p = CURDIR + "/test-data-cdrom"
#        apt_pkg.config.set("Dir::State::lists","/tmp")
#        cdrom = AptCdrom(None, p)
#        self.assertTrue(cdrom._doAdd())

    def testWriteDatabase(self):
        expect = \
            "CD::0380987599d9f666b749fbfe29d5b440-2 " \
            "\"Ubuntu 8.10 _Intrepid Ibex_ - Beta amd64 (20080930.4)\";\n" \
            "CD::0380987599d9f666b749fbfe29d5b440-2::Label " \
            "\"Ubuntu 8.10 _Intrepid Ibex_ - Beta amd64 (20080930.4)\";\n"
        p = CURDIR + "/test-data-cdrom/"
        database = CURDIR + "/test-data-cdrom/cdrom.list"
        apt_pkg.config.set("Dir::State::cdroms", database)
        apt_pkg.config.set("Acquire::cdrom::mount", p)
        apt_pkg.config.set("APT::CDROM::NoMount", "true")
        if os.path.exists(database):
            os.unlink(database)
        cdrom = AptCdrom(None, p)
        cdrom._writeDatabase()
        with open(database) as f:
            self.assertEqual(expect, f.read())

    def testScanCD(self):
        p = CURDIR + "/test-data-cdrom"
        cdrom = AptCdrom(None, p)
        (p, s, i18n) = cdrom._scanCD()
        self.assertTrue(len(p) > 0 and len(s) > 0 and len(i18n) > 0,
                        "failed to scan packages files (%s) (%s)" % (p, s))
        #print(p,s,i18n)

    def testDropArch(self):
        p = CURDIR + "/test-data-cdrom"
        cdrom = AptCdrom(None, p)
        (p, s, i18n) = cdrom._scanCD()
        self.assertTrue(len(cdrom._dropArch(p)) < len(p),
                        "drop arch did not drop (%s) < (%s)" % (
                            len(cdrom._dropArch(p)), len(p)))

    def testDiskName(self):
        " read and escape the disskname"
        cdrom = AptCdrom(None, CURDIR + "/test-data-cdrom")
        s = cdrom._readDiskName()
        self.assertEqual(
            "Ubuntu 8.10 _Intrepid Ibex_ - Beta amd64 (20080930.4)", s,
            "_readDiskName failed (got %s)" % s)

    def testGenerateSourcesListLine(self):
        cdrom = AptCdrom(None, CURDIR + "/test-data-cdrom")
        (p, s, i18n) = cdrom._scanCD()
        p = cdrom._dropArch(p)
        line = cdrom._generateSourcesListLine(cdrom._readDiskName(), p)
        #print(line)
        self.assertEqual("deb cdrom:[Ubuntu 8.10 _Intrepid Ibex_ - Beta amd64 "
                         "(20080930.4)]/ intrepid restricted", line,
                         "deb line wrong (got %s)" % line)

    def testCopyi18n(self):
        cdrom = AptCdrom(None, CURDIR + "/test-data-cdrom")
        (p, s, i18n) = cdrom._scanCD()
        p = cdrom._dropArch(p)
        d = tempfile.mkdtemp()
        cdrom._copyTranslations(i18n, d)
        self.assertTrue(
            os.path.exists(os.path.join(d,
                                        "Ubuntu%208.10%20%5fIntrepid%20Ibex"
                                        "%5f%20-%20Beta%20amd64%20(20080930.4)"
                                        "_dists_intrepid_main_i18n_"
                                        "Translation-be")),
            "no outfile in '%s'" % os.listdir(d))

    def testCopyPackages(self):
        cdrom = AptCdrom(None, CURDIR + "/test-data-cdrom")
        (p, s, i18n) = cdrom._scanCD()
        p = cdrom._dropArch(p)
        d = tempfile.mkdtemp()
        cdrom._copyPackages(p, d)
        self.assertTrue(
            os.path.exists(os.path.join(d,
                                        "Ubuntu%208.10%20%5fIntrepid%20Ibex"
                                        "%5f%20-%20Beta%20amd64%20(20080930.4)"
                                        "_dists_intrepid_restricted_binary-"
                                        "amd64_Packages")),
            "no outfile in '%s'" % os.listdir(d))

    def testVerifyRelease(self):
        cdrom = AptCdrom(None, CURDIR + "/test-data-cdrom")
        (p, s, i18n) = cdrom._scanCD()
        res = cdrom._verifyRelease(s)
        self.assertTrue(res)

    def testCopyRelease(self):
        cdrom = AptCdrom(None, CURDIR + "/test-data-cdrom")
        (p, s, i18n) = cdrom._scanCD()
        d = tempfile.mkdtemp()
        cdrom._copyRelease(s, d)
        self.assertTrue(
            os.path.exists(os.path.join(d,
                                        "Ubuntu%208.10%20%5fIntrepid%20Ibex"
                                        "%5f%20-%20Beta%20amd64%20(20080930.4)"
                                        "_dists_intrepid_Release")),
            "no outfile in '%s' (%s)" % (d, os.listdir(d)))

    def testSourcesList(self):
        cdrom = AptCdrom(None, CURDIR + "/test-data-cdrom")
        (p, s, i18n) = cdrom._scanCD()
        p = cdrom._dropArch(p)
        line = cdrom._generateSourcesListLine(cdrom._readDiskName(), p)
        self.assertEqual("deb cdrom:[Ubuntu 8.10 _Intrepid Ibex_ - Beta amd64 "
                         "(20080930.4)]/ intrepid restricted",
                         line,
                         "sources.list line incorrect, got %s" % line)

    def test_comment_out(self):
        tmpdir = tempfile.mkdtemp()
        sourceslist = os.path.join(tmpdir, "sources.list")
        apt_pkg.config.set("dir::etc::sourcelist", sourceslist)
        apt_pkg.config.set("dir::state::lists", tmpdir)
        view = Mock()
        cdrom = AptCdrom(view, CURDIR + "/test-data-cdrom")
        cdrom.add()
        cdrom.comment_out_cdrom_entry()
        with open(sourceslist) as f:
            sourceslines = f.readlines()
        for line in sourceslines:
            self.assertTrue(line.startswith("#"))
        self.assertEqual(len(sourceslines), 2)


if __name__ == "__main__":
    apt_pkg.init()
    apt_pkg.config.set("APT::Architecture", "amd64")
    unittest.main()