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