]> git.saurik.com Git - apt.git/blob - test/pre-upload-check.py
de43122ceba625e818f311d4e82bf4f2bc9ddea2
[apt.git] / test / pre-upload-check.py
1 #!/usr/bin/python
2
3 import sys
4 import os
5 import glob
6 import os.path
7 import shutil
8 import time
9 from subprocess import call, PIPE
10
11 import unittest
12
13 stdout = os.open("/dev/null",0) #sys.stdout
14 stderr = os.open("/dev/null",0) # sys.stderr
15
16 apt_args = []
17 #apt_args = ["-o","Debug::pkgAcquire::Auth=true"]
18
19 class testAptAuthenticationReliability(unittest.TestCase):
20 """
21 test if the spec https://wiki.ubuntu.com/AptAuthenticationReliability
22 is properly implemented
23 """
24 #apt = "../bin/apt-get"
25 apt = "apt-get"
26
27 def setUp(self):
28 if os.path.exists("/tmp/autFailure"):
29 os.unlink("/tmp/authFailure");
30 def testRepositorySigFailure(self):
31 """
32 test if a repository that used to be authenticated and fails on
33 apt-get update refuses to update and uses the old state
34 """
35 # copy valid signatures into lists (those are ok, even
36 # if the name is "-broken-" ...
37 for f in glob.glob("./authReliability/lists/*"):
38 shutil.copy(f,"/var/lib/apt/lists")
39 # ensure we do *not* get a I-M-S hit
40 os.utime("/var/lib/apt/lists/%s" % os.path.basename(f), (0,0))
41 res = call([self.apt,
42 "update",
43 "-o","Dir::Etc::sourcelist=./authReliability/sources.list.failure",
44 "-o",'APT::Update::Auth-Failure::=touch /tmp/authFailure',
45 ] + apt_args,
46 stdout=stdout, stderr=stderr)
47 self.assert_(os.path.exists("/var/lib/apt/lists/people.ubuntu.com_%7emvo_apt_auth-test-suit_gpg-package-broken_Release.gpg"),
48 "The gpg file disappeared, this should not happen")
49 self.assert_(os.path.exists("/var/lib/apt/lists/people.ubuntu.com_%7emvo_apt_auth-test-suit_gpg-package-broken_Packages"),
50 "The Packages file disappeared, this should not happen")
51 self.assert_(os.path.exists("/tmp/authFailure"),
52 "The APT::Update::Auth-Failure script did not run")
53 os.unlink("/tmp/authFailure");
54 # the same with i-m-s hit this time
55 for f in glob.glob("./authReliability/lists/*"):
56 shutil.copy(f,"/var/lib/apt/lists")
57 os.utime("/var/lib/apt/lists/%s" % os.path.basename(f), (time.time(),time.time()))
58 res = call([self.apt,
59 "update",
60 "-o","Dir::Etc::sourcelist=./authReliability/sources.list.failure",
61 "-o",'APT::Update::Auth-Failure::=touch /tmp/authFailure',
62 ] + apt_args,
63 stdout=stdout, stderr=stderr)
64 self.assert_(os.path.exists("/var/lib/apt/lists/people.ubuntu.com_%7emvo_apt_auth-test-suit_gpg-package-broken_Release.gpg"),
65 "The gpg file disappeared, this should not happen")
66 self.assert_(os.path.exists("/var/lib/apt/lists/people.ubuntu.com_%7emvo_apt_auth-test-suit_gpg-package-broken_Packages"),
67 "The Packages file disappeared, this should not happen")
68 self.assert_(os.path.exists("/tmp/authFailure"),
69 "The APT::Update::Auth-Failure script did not run")
70 def testRepositorySigGood(self):
71 """
72 test that a regular repository with good data stays good
73 """
74 res = call([self.apt,
75 "update",
76 "-o","Dir::Etc::sourcelist=./authReliability/sources.list.good"
77 ] + apt_args,
78 stdout=stdout, stderr=stderr)
79 self.assert_(os.path.exists("/var/lib/apt/lists/people.ubuntu.com_%7emvo_apt_auth-test-suit_gpg-package-ok_Release.gpg"),
80 "The gpg file disappeared after a regular download, this should not happen")
81 self.assert_(os.path.exists("/var/lib/apt/lists/people.ubuntu.com_%7emvo_apt_auth-test-suit_gpg-package-ok_Packages"),
82 "The Packages file disappeared, this should not happen")
83 # test good is still good after non I-M-S hit and a previous files in lists/
84 for f in glob.glob("./authReliability/lists/*"):
85 shutil.copy(f,"/var/lib/apt/lists")
86 # ensure we do *not* get a I-M-S hit
87 os.utime("/var/lib/apt/lists/%s" % os.path.basename(f), (0,0))
88 res = call([self.apt,
89 "update",
90 "-o","Dir::Etc::sourcelist=./authReliability/sources.list.good"
91 ] + apt_args,
92 stdout=stdout, stderr=stderr)
93 self.assert_(os.path.exists("/var/lib/apt/lists/people.ubuntu.com_%7emvo_apt_auth-test-suit_gpg-package-ok_Release.gpg"),
94 "The gpg file disappeared after a I-M-S hit, this should not happen")
95 self.assert_(os.path.exists("/var/lib/apt/lists/people.ubuntu.com_%7emvo_apt_auth-test-suit_gpg-package-ok_Packages"),
96 "The Packages file disappeared, this should not happen")
97 # test good is still good after I-M-S hit
98 for f in glob.glob("./authReliability/lists/*"):
99 shutil.copy(f,"/var/lib/apt/lists")
100 # ensure we do get a I-M-S hit
101 os.utime("/var/lib/apt/lists/%s" % os.path.basename(f), (time.time(),time.time()))
102 res = call([self.apt,
103 "update",
104 "-o","Dir::Etc::sourcelist=./authReliability/sources.list.good"
105 ] + apt_args,
106 stdout=stdout, stderr=stderr)
107 self.assert_(os.path.exists("/var/lib/apt/lists/people.ubuntu.com_%7emvo_apt_auth-test-suit_gpg-package-ok_Release.gpg"),
108 "The gpg file disappeared, this should not happen")
109 self.assert_(os.path.exists("/var/lib/apt/lists/people.ubuntu.com_%7emvo_apt_auth-test-suit_gpg-package-ok_Packages"),
110 "The Packages file disappeared, this should not happen")
111
112
113 class testAuthentication(unittest.TestCase):
114 """
115 test if the authentication is working, the repository
116 of the test-data can be found here:
117 bzr get http://people.ubuntu.com/~mvo/bzr/apt/apt-auth-test-suit/
118 """
119
120 # some class wide data
121 apt = "apt-get"
122 pkg = "libglib2.0-data"
123 pkgver = "2.13.6-1ubuntu1"
124 pkgpath = "/var/cache/apt/archives/libglib2.0-data_2.13.6-1ubuntu1_all.deb"
125
126 def setUp(self):
127 for f in glob.glob("testkeys/*,key"):
128 call(["apt-key", "add", f], stdout=stdout, stderr=stderr)
129
130 def _cleanup(self):
131 " make sure we get new lists and no i-m-s "
132 call(["rm","-f", "/var/lib/apt/lists/*"])
133 if os.path.exists(self.pkgpath):
134 os.unlink(self.pkgpath)
135
136 def _expectedRes(self, resultstr):
137 if resultstr == 'ok':
138 return 0
139 elif resultstr == 'broken':
140 return 100
141
142
143 def testPackages(self):
144 for f in glob.glob("testsources.list/sources.list*package*"):
145 self._cleanup()
146 (prefix, testtype, result) = f.split("-")
147 expected_res = self._expectedRes(result)
148 # update first
149 call([self.apt,"update",
150 "-o","Dir::Etc::sourcelist=./%s" % f]+apt_args,
151 stdout=stdout, stderr=stderr)
152 # then get the pkg
153 cmd = ["install", "-y", "-d", "--reinstall",
154 "%s=%s" % (self.pkg, self.pkgver),
155 "-o","Dir::state::Status=./fake-status"]
156 res = call([self.apt, "-o","Dir::Etc::sourcelist=./%s" % f]+cmd+apt_args,
157 stdout=stdout, stderr=stderr)
158 self.assert_(res == expected_res,
159 "test '%s' failed (got %s expected %s" % (f,res,expected_res))
160
161
162 def testGPG(self):
163 for f in glob.glob("testsources.list/sources.list*gpg*"):
164 self._cleanup()
165 (prefix, testtype, result) = f.split("-")
166 expected_res = self._expectedRes(result)
167 # update first
168 call([self.apt,"update",
169 "-o","Dir::Etc::sourcelist=./%s" % f]+apt_args,
170 stdout=stdout, stderr=stderr)
171 cmd = ["install", "-y", "-d", "--reinstall",
172 "%s=%s" % (self.pkg, self.pkgver),
173 "-o","Dir::state::Status=./fake-status"]
174 res = call([self.apt, "-o","Dir::Etc::sourcelist=./%s" % f]+
175 cmd+apt_args,
176 stdout=stdout, stderr=stderr)
177 self.assert_(res == expected_res,
178 "test '%s' failed (got %s expected %s" % (f,res,expected_res))
179
180 def testRelease(self):
181 for f in glob.glob("testsources.list/sources.list*release*"):
182 self._cleanup()
183 (prefix, testtype, result) = f.split("-")
184 expected_res = self._expectedRes(result)
185 cmd = ["update"]
186 res = call([self.apt,"-o","Dir::Etc::sourcelist=./%s" % f]+cmd+apt_args,
187 stdout=stdout, stderr=stderr)
188 self.assert_(res == expected_res,
189 "test '%s' failed (got %s expected %s" % (f,res,expected_res))
190 if expected_res == 0:
191 self.assert_(len(glob.glob("/var/lib/apt/lists/partial/*")) == 0,
192 "partial/ dir has leftover files: %s" % glob.glob("/var/lib/apt/lists/partial/*"))
193
194
195 class testLocalRepositories(unittest.TestCase):
196 " test local repository regressions "
197
198 repo_dir = "local-repo"
199 apt = "apt-get"
200 pkg = "gdebi-test4"
201
202 def setUp(self):
203 self.repo = os.path.abspath(os.path.join(os.getcwd(), self.repo_dir))
204 self.sources = os.path.join(self.repo, "sources.list")
205 s = open(self.sources,"w")
206 s.write("deb file://%s/ /\n" % self.repo)
207 s.close()
208
209 def testLocalRepoAuth(self):
210 # two times to get at least one i-m-s hit
211 for i in range(2):
212 self.assert_(os.path.exists(self.sources))
213 cmd = [self.apt,"update","-o", "Dir::Etc::sourcelist=%s" % self.sources]+apt_args
214 res = call(cmd, stdout=stdout, stderr=stderr)
215 self.assertEqual(res, 0, "local repo test failed")
216 self.assert_(os.path.exists(os.path.join(self.repo,"Packages.gz")),
217 "Packages.gz vanished from local repo")
218
219 def testInstallFromLocalRepo(self):
220 apt = [self.apt,"-o", "Dir::Etc::sourcelist=%s"% self.sources]+apt_args
221 cmd = apt+["update"]
222 res = call(cmd, stdout=stdout, stderr=stderr)
223 self.assertEqual(res, 0)
224 res = call(apt+["-y","install","--reinstall",self.pkg],
225 stdout=stdout, stderr=stderr)
226 self.assert_(res == 0,
227 "installing %s failed (got %s)" % (self.pkg, res))
228 res = call(apt+["-y","remove",self.pkg],
229 stdout=stdout, stderr=stderr)
230 self.assert_(res == 0,
231 "removing %s failed (got %s)" % (self.pkg, res))
232
233 def testPythonAptInLocalRepo(self):
234 import apt, apt_pkg
235 apt_pkg.Config.Set("Dir::Etc::sourcelist",self.sources)
236 cache = apt.Cache()
237 cache.update()
238 pkg = cache["apt"]
239 self.assert_(pkg.name == 'apt')
240
241
242
243 if __name__ == "__main__":
244 print "Runing simple testsuit on current apt-get and libapt"
245 if len(sys.argv) > 1 and sys.argv[1] == "-v":
246 stdout = sys.stdout
247 stderr = sys.stderr
248
249 # run only one for now
250 unittest.main(defaultTest="testAptAuthenticationReliability")
251 #unittest.main()