* apt-pkg/indexrecords.cc:
[ntk/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 from subprocess import call, PIPE
8
9 import unittest
10
11 stdout = os.open("/dev/null",0) #sys.stdout
12 stderr = os.open("/dev/null",0) # sys.stderr
13
14 apt_args = [] # ["-o","Debug::pkgAcquire::Auth=true"]
15
16
17 class testAuthentication(unittest.TestCase):
18 """
19 test if the authentication is working, the repository
20 of the test-data can be found here:
21 bzr get http://people.ubuntu.com/~mvo/bzr/apt/apt-auth-test-suit/
22 """
23
24 # some class wide data
25 apt = "apt-get"
26 pkg = "libglib2.0-data"
27 pkgver = "2.13.6-1ubuntu1"
28 pkgpath = "/var/cache/apt/archives/libglib2.0-data_2.13.6-1ubuntu1_all.deb"
29
30 def setUp(self):
31 for f in glob.glob("testkeys/*,key"):
32 call(["apt-key", "add", f], stdout=stdout, stderr=stderr)
33
34 def _cleanup(self):
35 " make sure we get new lists and no i-m-s "
36 call(["rm","-f", "/var/lib/apt/lists/*"])
37 if os.path.exists(self.pkgpath):
38 os.unlink(self.pkgpath)
39
40 def _expectedRes(self, resultstr):
41 if resultstr == 'ok':
42 return 0
43 elif resultstr == 'broken':
44 return 100
45
46
47 def testPackages(self):
48 for f in glob.glob("testsources.list/sources.list*package*"):
49 self._cleanup()
50 (prefix, testtype, result) = f.split("-")
51 expected_res = self._expectedRes(result)
52 # update first
53 call([self.apt,"update",
54 "-o","Dir::Etc::sourcelist=./%s" % f]+apt_args,
55 stdout=stdout, stderr=stderr)
56 # then get the pkg
57 cmd = ["install", "-y", "-d", "--reinstall",
58 "%s=%s" % (self.pkg, self.pkgver),
59 "-o","Dir::state::Status=./fake-status"]
60 res = call([self.apt, "-o","Dir::Etc::sourcelist=./%s" % f]+cmd+apt_args,
61 stdout=stdout, stderr=stderr)
62 self.assert_(res == expected_res,
63 "test '%s' failed (got %s expected %s" % (f,res,expected_res))
64
65
66 def testGPG(self):
67 for f in glob.glob("testsources.list/sources.list*gpg*"):
68 self._cleanup()
69 (prefix, testtype, result) = f.split("-")
70 expected_res = self._expectedRes(result)
71 # update first
72 call([self.apt,"update",
73 "-o","Dir::Etc::sourcelist=./%s" % f]+apt_args,
74 stdout=stdout, stderr=stderr)
75 cmd = ["install", "-y", "-d", "--reinstall",
76 "%s=%s" % (self.pkg, self.pkgver),
77 "-o","Dir::state::Status=./fake-status"]
78 res = call([self.apt, "-o","Dir::Etc::sourcelist=./%s" % f]+
79 cmd+apt_args,
80 stdout=stdout, stderr=stderr)
81 self.assert_(res == expected_res,
82 "test '%s' failed (got %s expected %s" % (f,res,expected_res))
83
84 def testRelease(self):
85 for f in glob.glob("testsources.list/sources.list*release*"):
86 self._cleanup()
87 (prefix, testtype, result) = f.split("-")
88 expected_res = self._expectedRes(result)
89 cmd = ["update"]
90 res = call([self.apt,"-o","Dir::Etc::sourcelist=./%s" % f]+cmd+apt_args,
91 stdout=stdout, stderr=stderr)
92 self.assert_(res == expected_res,
93 "test '%s' failed (got %s expected %s" % (f,res,expected_res))
94 if expected_res == 0:
95 self.assert_(len(glob.glob("/var/lib/apt/lists/partial/*")) == 0,
96 "partial/ dir has leftover files: %s" % glob.glob("/var/lib/apt/lists/partial/*"))
97
98 def testValid(self):
99 for f in glob.glob("testsources.list/sources.list*validuntil*"):
100 self._cleanup()
101 (prefix, testtype, result) = f.split("-")
102 expected_res = self._expectedRes(result)
103 cmd = ["update"]
104 res = call([self.apt,"-o","Dir::Etc::sourcelist=./%s" % f]+cmd+apt_args,
105 stdout=stdout, stderr=stderr)
106 self.assert_(res == expected_res,
107 "test '%s' failed (got %s expected %s" % (f,res,expected_res))
108 if expected_res == 0:
109 self.assert_(len(glob.glob("/var/lib/apt/lists/partial/*")) == 0,
110 "partial/ dir has leftover files: %s" % glob.glob("/var/lib/apt/lists/partial/*"))
111
112
113 class testLocalRepositories(unittest.TestCase):
114 " test local repository regressions "
115
116 repo_dir = "local-repo"
117 apt = "apt-get"
118 pkg = "gdebi-test4"
119
120 def setUp(self):
121 self.repo = os.path.abspath(os.path.join(os.getcwd(), self.repo_dir))
122 self.sources = os.path.join(self.repo, "sources.list")
123 s = open(self.sources,"w")
124 s.write("deb file://%s/ /\n" % self.repo)
125 s.close()
126
127 def testLocalRepoAuth(self):
128 # two times to get at least one i-m-s hit
129 for i in range(2):
130 self.assert_(os.path.exists(self.sources))
131 cmd = [self.apt,"update","-o", "Dir::Etc::sourcelist=%s" % self.sources]+apt_args
132 res = call(cmd, stdout=stdout, stderr=stderr)
133 self.assertEqual(res, 0, "local repo test failed")
134 self.assert_(os.path.exists(os.path.join(self.repo,"Packages.gz")),
135 "Packages.gz vanished from local repo")
136
137 def testInstallFromLocalRepo(self):
138 apt = [self.apt,"-o", "Dir::Etc::sourcelist=%s"% self.sources]+apt_args
139 cmd = apt+["update"]
140 res = call(cmd, stdout=stdout, stderr=stderr)
141 self.assertEqual(res, 0)
142 res = call(apt+["-y","install","--reinstall",self.pkg],
143 stdout=stdout, stderr=stderr)
144 self.assert_(res == 0,
145 "installing %s failed (got %s)" % (self.pkg, res))
146 res = call(apt+["-y","remove",self.pkg],
147 stdout=stdout, stderr=stderr)
148 self.assert_(res == 0,
149 "removing %s failed (got %s)" % (self.pkg, res))
150
151 def testPythonAptInLocalRepo(self):
152 import apt, apt_pkg
153 apt_pkg.Config.Set("Dir::Etc::sourcelist",self.sources)
154 cache = apt.Cache()
155 cache.update()
156 pkg = cache["apt"]
157 self.assert_(pkg.name == 'apt')
158
159
160
161 if __name__ == "__main__":
162 print "Runing simple testsuit on current apt-get and libapt"
163 if len(sys.argv) > 1 and sys.argv[1] == "-v":
164 stdout = sys.stdout
165 stderr = sys.stderr
166 unittest.main()
167
168