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