3 from db_test import DBDakTestCase
5 from daklib.dbconn import Architecture, Suite, get_suite_architectures, \
6 get_architecture_suites, Maintainer, DBSource, Location, PoolFile, \
7 check_poolfile, get_poolfile_like_name, get_source_in_suite, \
8 get_suites_source_in, add_dsc_to_db, source_exists, DBBinary, \
9 get_suites_binary_in, add_deb_to_db
10 from daklib.queue_install import package_to_suite
11 from daklib.queue import get_newest_source, get_suite_version
13 from sqlalchemy.orm.exc import MultipleResultsFound
17 'fake package class used for testing'
25 'fake Upload class used for testing'
27 def __init__(self, pkg):
30 class PackageTestCase(DBDakTestCase):
32 PackageTestCase checks the handling of source and binary packages in dak's
36 def setup_suites(self):
37 "setup a hash of Suite objects in self.suite"
39 if 'suite' in self.__dict__:
42 for suite_name in ('lenny', 'squeeze', 'sid'):
43 self.suite[suite_name] = Suite(suite_name = suite_name, version = '-')
44 self.session.add_all(self.suite.values())
46 def setup_architectures(self):
47 "setup Architecture objects in self.arch and connect to suites"
49 if 'arch' in self.__dict__:
53 for arch_string in ('source', 'all', 'i386', 'amd64', 'kfreebsd-i386'):
54 self.arch[arch_string] = Architecture(arch_string)
55 if arch_string != 'kfreebsd-i386':
56 self.arch[arch_string].suites = self.suite.values()
58 self.arch[arch_string].suites = [self.suite['squeeze'], self.suite['sid']]
59 # hard code ids for source and all
60 self.arch['source'].arch_id = 1
61 self.arch['all'].arch_id = 2
62 self.session.add_all(self.arch.values())
64 def setup_locations(self):
65 'create some Location objects, TODO: add component'
67 if 'loc' in self.__dict__:
70 self.loc['main'] = Location(path = \
71 '/srv/ftp-master.debian.org/ftp/pool/')
72 self.loc['contrib'] = Location(path = \
73 '/srv/ftp-master.debian.org/ftp/pool/')
74 self.session.add_all(self.loc.values())
76 def setup_poolfiles(self):
77 'create some PoolFile objects'
79 if 'file' in self.__dict__:
81 self.setup_locations()
83 self.file['hello_2.2-3.dsc'] = PoolFile(filename = 'main/h/hello/hello_2.2-3.dsc', \
84 location = self.loc['main'], filesize = 0, md5sum = '')
85 self.file['hello_2.2-2.dsc'] = PoolFile(filename = 'main/h/hello/hello_2.2-2.dsc', \
86 location = self.loc['main'], filesize = 0, md5sum = '')
87 self.file['hello_2.2-1.dsc'] = PoolFile(filename = 'main/h/hello/hello_2.2-1.dsc', \
88 location = self.loc['main'], filesize = 0, md5sum = '')
89 self.file['hello_2.2-1_i386.deb'] = PoolFile( \
90 filename = 'main/h/hello/hello_2.2-1_i386.deb', \
91 location = self.loc['main'], filesize = 0, md5sum = '')
92 self.file['gnome-hello_2.2-1_i386.deb'] = PoolFile( \
93 filename = 'main/h/hello/gnome-hello_2.2-1_i386.deb', \
94 location = self.loc['main'], filesize = 0, md5sum = '')
95 self.file['sl_3.03-16.dsc'] = PoolFile(filename = 'main/s/sl/sl_3.03-16.dsc', \
96 location = self.loc['main'], filesize = 0, md5sum = '')
97 self.file['python2.6_2.6.6-8.dsc'] = PoolFile( \
98 filename = 'main/p/python2.6/python2.6_2.6.6-8.dsc', \
99 location = self.loc['main'], filesize = 0, md5sum = '')
100 self.session.add_all(self.file.values())
102 def setup_maintainers(self):
103 'create some Maintainer objects'
105 if 'maintainer' in self.__dict__:
108 self.maintainer['maintainer'] = Maintainer(name = 'Mr. Maintainer')
109 self.maintainer['uploader'] = Maintainer(name = 'Mrs. Uploader')
110 self.maintainer['lazyguy'] = Maintainer(name = 'Lazy Guy')
111 self.session.add_all(self.maintainer.values())
113 def setup_sources(self):
114 'create DBSource objects'
116 if 'source' in self.__dict__:
118 self.setup_maintainers()
120 self.setup_poolfiles()
122 self.source['hello_2.2-2'] = DBSource(source = 'hello', version = '2.2-2', \
123 maintainer = self.maintainer['maintainer'], \
124 changedby = self.maintainer['uploader'], \
125 poolfile = self.file['hello_2.2-2.dsc'], install_date = self.now())
126 self.source['hello_2.2-2'].suites.append(self.suite['sid'])
127 self.source['hello_2.2-1'] = DBSource(source = 'hello', version = '2.2-1', \
128 maintainer = self.maintainer['maintainer'], \
129 changedby = self.maintainer['uploader'], \
130 poolfile = self.file['hello_2.2-1.dsc'], install_date = self.now())
131 self.source['hello_2.2-1'].suites.append(self.suite['sid'])
132 self.source['sl_3.03-16'] = DBSource(source = 'sl', version = '3.03-16', \
133 maintainer = self.maintainer['maintainer'], \
134 changedby = self.maintainer['uploader'], \
135 poolfile = self.file['sl_3.03-16.dsc'], install_date = self.now())
136 self.source['sl_3.03-16'].suites.append(self.suite['squeeze'])
137 self.source['sl_3.03-16'].suites.append(self.suite['sid'])
138 self.session.add_all(self.source.values())
140 def setup_binaries(self):
141 'create DBBinary objects'
143 if 'binary' in self.__dict__:
146 self.setup_architectures()
148 self.binary['hello_2.2-1_i386'] = DBBinary(package = 'hello', \
149 source = self.source['hello_2.2-1'], version = '2.2-1', \
150 maintainer = self.maintainer['maintainer'], \
151 architecture = self.arch['i386'], \
152 poolfile = self.file['hello_2.2-1_i386.deb'])
153 self.binary['hello_2.2-1_i386'].suites.append(self.suite['squeeze'])
154 self.binary['hello_2.2-1_i386'].suites.append(self.suite['sid'])
155 self.binary['gnome-hello_2.2-1_i386'] = DBBinary(package = 'gnome-hello', \
156 source = self.source['hello_2.2-1'], version = '2.2-1', \
157 maintainer = self.maintainer['maintainer'], \
158 architecture = self.arch['i386'], \
159 poolfile = self.file['gnome-hello_2.2-1_i386.deb'])
160 self.binary['gnome-hello_2.2-1_i386'].suites.append(self.suite['squeeze'])
161 self.binary['gnome-hello_2.2-1_i386'].suites.append(self.suite['sid'])
162 self.session.add_all(self.binary.values())
165 super(PackageTestCase, self).setUp()
166 self.setup_binaries()
167 # flush to make sure that the setup is correct
170 def test_suite_architecture(self):
171 # check the id for architectures source and all
172 self.assertEqual(1, self.arch['source'].arch_id)
173 self.assertEqual(2, self.arch['all'].arch_id)
174 # check the many to many relation between Suite and Architecture
175 self.assertEqual('source', self.suite['lenny'].architectures[0])
176 self.assertEqual(4, len(self.suite['lenny'].architectures))
177 self.assertEqual(3, len(self.arch['i386'].suites))
178 # check the function get_suite_architectures()
179 architectures = get_suite_architectures('lenny', session = self.session)
180 self.assertEqual(4, len(architectures))
181 self.assertTrue(self.arch['source'] in architectures)
182 self.assertTrue(self.arch['all'] in architectures)
183 self.assertTrue(self.arch['kfreebsd-i386'] not in architectures)
184 architectures = get_suite_architectures('sid', session = self.session)
185 self.assertEqual(5, len(architectures))
186 self.assertTrue(self.arch['kfreebsd-i386'] in architectures)
187 architectures = get_suite_architectures('lenny', skipsrc = True, session = self.session)
188 self.assertEqual(3, len(architectures))
189 self.assertTrue(self.arch['source'] not in architectures)
190 architectures = get_suite_architectures('lenny', skipall = True, session = self.session)
191 self.assertEqual(3, len(architectures))
192 self.assertTrue(self.arch['all'] not in architectures)
193 # check the function get_architecture_suites()
194 suites = get_architecture_suites('i386', self.session)
195 self.assertEqual(3, len(suites))
196 self.assertTrue(self.suite['lenny'] in suites)
197 suites = get_architecture_suites('kfreebsd-i386', self.session)
198 self.assertEqual(2, len(suites))
199 self.assertTrue(self.suite['lenny'] not in suites)
201 def test_poolfiles(self):
203 Test the relation of the classes PoolFile and Location.
205 The code needs some explaination. The property Location.files is not a
206 list as in other relations because such a list would become rather
207 huge. It is a query object that can be queried, filtered, and iterated
208 as usual. But list like methods like append() and remove() are
209 supported as well which allows code like:
211 somelocation.files.append(somefile)
214 main = self.loc['main']
215 contrib = self.loc['contrib']
216 self.assertEqual('/srv/ftp-master.debian.org/ftp/pool/', main.path)
217 count = len(self.file.keys())
218 self.assertEqual(count, main.files.count())
219 self.assertEqual(0, contrib.files.count())
220 poolfile = main.files. \
221 filter(PoolFile.filename.like('%/hello/hello%')). \
222 order_by(PoolFile.filename)[0]
223 self.assertEqual('main/h/hello/hello_2.2-1.dsc', poolfile.filename)
224 self.assertEqual(main, poolfile.location)
226 self.assertEqual(poolfile, \
227 self.session.query(PoolFile).get(poolfile.file_id))
228 self.assertEqual(None, self.session.query(PoolFile).get(-1))
229 # test remove() and append()
230 main.files.remove(self.file['sl_3.03-16.dsc'])
231 contrib.files.append(self.file['sl_3.03-16.dsc'])
232 self.assertEqual(count - 1, main.files.count())
233 self.assertEqual(1, contrib.files.count())
235 self.assertEqual('/srv/ftp-master.debian.org/ftp/pool/main/s/sl/sl_3.03-16.dsc', \
236 self.file['sl_3.03-16.dsc'].fullpath)
237 # test check_poolfile()
238 self.assertEqual((True, self.file['sl_3.03-16.dsc']), \
239 check_poolfile('main/s/sl/sl_3.03-16.dsc', 0, '', \
240 contrib.location_id, self.session))
241 self.assertEqual((False, None), \
242 check_poolfile('foobar', 0, '', contrib.location_id, self.session))
243 self.assertEqual((False, self.file['sl_3.03-16.dsc']), \
244 check_poolfile('main/s/sl/sl_3.03-16.dsc', 42, '', \
245 contrib.location_id, self.session))
246 self.assertEqual((False, self.file['sl_3.03-16.dsc']), \
247 check_poolfile('main/s/sl/sl_3.03-16.dsc', 0, 'deadbeef', \
248 contrib.location_id, self.session))
249 # test get_poolfile_like_name()
250 self.assertEqual([self.file['sl_3.03-16.dsc']], \
251 get_poolfile_like_name('sl_3.03-16.dsc', self.session))
252 self.assertEqual([], get_poolfile_like_name('foobar', self.session))
254 def test_maintainers(self):
256 tests relation between Maintainer and DBSource
258 TODO: add relations to changes_pending_source
261 maintainer = self.maintainer['maintainer']
262 self.assertEqual(maintainer,
263 self.session.query(Maintainer).get(maintainer.maintainer_id))
264 uploader = self.maintainer['uploader']
265 self.assertEqual(uploader,
266 self.session.query(Maintainer).get(uploader.maintainer_id))
267 lazyguy = self.maintainer['lazyguy']
268 self.assertEqual(lazyguy,
269 self.session.query(Maintainer).get(lazyguy.maintainer_id))
270 self.assertEqual(3, len(maintainer.maintains_sources))
271 self.assertTrue(self.source['hello_2.2-2'] in maintainer.maintains_sources)
272 self.assertEqual(maintainer.changed_sources, [])
273 self.assertEqual(uploader.maintains_sources, [])
274 self.assertEqual(3, len(uploader.changed_sources))
275 self.assertTrue(self.source['sl_3.03-16'] in uploader.changed_sources)
276 self.assertEqual(lazyguy.maintains_sources, [])
277 self.assertEqual(lazyguy.changed_sources, [])
279 def get_source_in_suite_fail(self):
281 This function throws the MultipleResultsFound exception because
282 get_source_in_suite is broken.
284 TODO: fix get_source_in_suite
287 return get_source_in_suite('hello', 'sid', self.session)
289 def test_sources(self):
290 'test relation between DBSource and PoolFile or Suite'
293 self.assertEqual(self.file['hello_2.2-2.dsc'], self.source['hello_2.2-2'].poolfile)
294 self.assertEqual(self.source['hello_2.2-2'], self.file['hello_2.2-2.dsc'].source)
295 self.assertEqual(None, self.file['python2.6_2.6.6-8.dsc'].source)
297 squeeze = self.session.query(Suite). \
298 filter(Suite.sources.contains(self.source['sl_3.03-16'])). \
299 order_by(Suite.suite_name)[1]
300 self.assertEqual(self.suite['squeeze'], squeeze)
301 self.assertEqual(1, squeeze.sources.count())
302 self.assertEqual(self.source['sl_3.03-16'], squeeze.sources[0])
303 sl = self.session.query(DBSource). \
304 filter(DBSource.suites.contains(self.suite['squeeze'])).one()
305 self.assertEqual(self.source['sl_3.03-16'], sl)
306 self.assertEqual(2, len(sl.suites))
307 self.assertTrue(self.suite['sid'] in sl.suites)
308 # test get_source_in_suite()
309 self.assertRaises(MultipleResultsFound, self.get_source_in_suite_fail)
310 self.assertEqual(None, \
311 get_source_in_suite('hello', 'squeeze', self.session))
312 self.assertEqual(self.source['sl_3.03-16'], \
313 get_source_in_suite('sl', 'sid', self.session))
314 # test get_suites_source_in()
315 self.assertEqual([self.suite['sid']], \
316 get_suites_source_in('hello', self.session))
317 self.assertEqual(2, len(get_suites_source_in('sl', self.session)))
318 self.assertTrue(self.suite['squeeze'] in \
319 get_suites_source_in('sl', self.session))
321 def test_add_dsc_to_db(self):
322 'tests function add_dsc_to_db()'
325 pkg.dsc['source'] = 'hello'
326 pkg.dsc['version'] = '2.2-3'
327 pkg.dsc['maintainer'] = self.maintainer['maintainer'].name
328 pkg.changes['changed-by'] = self.maintainer['uploader'].name
329 pkg.changes['fingerprint'] = 'deadbeef'
330 pkg.changes['distribution'] = { 'sid': '' }
331 pkg.files['hello_2.2-3.dsc'] = { \
333 'location id': self.loc['main'].location_id,
334 'files id': self.file['hello_2.2-3.dsc'].file_id }
337 (source, dsc_component, dsc_location_id, pfs) = \
338 add_dsc_to_db(upload, 'hello_2.2-3.dsc', self.session)
339 self.assertEqual('hello', source.source)
340 self.assertEqual('2.2-3', source.version)
341 self.assertEqual('sid', source.suites[0].suite_name)
342 self.assertEqual('main', dsc_component)
343 self.assertEqual(self.loc['main'].location_id, dsc_location_id)
344 self.assertEqual([], pfs)
346 def test_source_exists(self):
347 'test function source_exists()'
349 hello = self.source['hello_2.2-2']
350 self.assertTrue(source_exists(hello.source, hello.version, \
351 suites = ['sid'], session = self.session))
353 self.assertTrue(source_exists(hello.source, hello.version + '+b7', \
354 suites = ['sid'], session = self.session))
355 self.assertTrue(not source_exists(hello.source, hello.version, \
356 suites = ['lenny', 'squeeze'], session = self.session))
357 self.assertTrue(not source_exists(hello.source, hello.version, \
358 suites = ['lenny', 'sid'], session = self.session))
359 self.assertTrue(not source_exists(hello.source, hello.version, \
360 suites = ['sid', 'lenny'], session = self.session))
361 self.assertTrue(not source_exists(hello.source, '0815', \
362 suites = ['sid'], session = self.session))
364 self.assertTrue(source_exists(hello.source, hello.version, \
365 session = self.session))
367 def test_package_to_suite(self):
368 'test function package_to_suite()'
371 pkg.changes = { 'distribution': {} }
373 self.assertTrue(not package_to_suite(upload, 'sid', self.session))
374 pkg.changes['distribution'] = { 'sid': '' }
375 pkg.changes['architecture'] = { 'source': '' }
376 self.assertTrue(package_to_suite(upload, 'sid', self.session))
377 pkg.changes['architecture'] = {}
378 pkg.changes['source'] = self.source['hello_2.2-2'].source
379 pkg.changes['version'] = self.source['hello_2.2-2'].version
380 self.assertTrue(not package_to_suite(upload, 'sid', self.session))
381 pkg.changes['version'] = '42'
382 self.assertTrue(package_to_suite(upload, 'sid', self.session))
383 pkg.changes['source'] = 'foobar'
384 pkg.changes['version'] = self.source['hello_2.2-2'].version
385 self.assertTrue(package_to_suite(upload, 'sid', self.session))
386 pkg.changes['distribution'] = { 'lenny': '' }
387 self.assertTrue(package_to_suite(upload, 'lenny', self.session))
389 def test_get_newest_source(self):
390 'test function get_newest_source()'
393 daklib.queue.dm_suites = ['sid']
394 self.assertEqual(self.source['hello_2.2-2'], get_newest_source('hello', self.session))
395 self.assertEqual(None, get_newest_source('foobar', self.session))
397 def test_get_suite_version(self):
398 'test function get_suite_version()'
400 result = get_suite_version('hello', self.session)
401 self.assertEqual(2, len(result))
402 self.assertTrue(('sid', '2.2-1') in result)
403 self.assertTrue(('sid', '2.2-2') in result)
404 result = get_suite_version('sl', self.session)
405 self.assertEqual(2, len(result))
406 self.assertTrue(('squeeze', '3.03-16') in result)
407 self.assertTrue(('sid', '3.03-16') in result)
409 def test_binaries(self):
411 tests class DBBinary; TODO: test relation with Architecture, Maintainer,
412 PoolFile, and Fingerprint
415 # test Suite relation
416 self.assertEqual(2, self.suite['sid'].binaries.count())
417 self.assertTrue(self.binary['hello_2.2-1_i386'] in \
418 self.suite['sid'].binaries.all())
419 self.assertEqual(0, self.suite['lenny'].binaries.count())
420 # test DBSource relation
421 self.assertEqual(2, len(self.source['hello_2.2-1'].binaries))
422 self.assertTrue(self.binary['hello_2.2-1_i386'] in \
423 self.source['hello_2.2-1'].binaries)
424 self.assertEqual(0, len(self.source['hello_2.2-2'].binaries))
425 # test get_suites_binary_in()
426 self.assertEqual(2, len(get_suites_binary_in('hello', self.session)))
427 self.assertTrue(self.suite['sid'] in \
428 get_suites_binary_in('hello', self.session))
429 self.assertEqual(2, len(get_suites_binary_in('gnome-hello', self.session)))
430 self.assertTrue(self.suite['squeeze'] in \
431 get_suites_binary_in('gnome-hello', self.session))
432 self.assertEqual(0, len(get_suites_binary_in('sl', self.session)))
434 def test_add_deb_to_db(self):
435 'tests function add_deb_to_db()'
438 pkg.changes['fingerprint'] = 'deadbeef'
439 pkg.changes['distribution'] = { 'sid': '' }
440 pkg.files['hello_2.2-2_i386.deb'] = { \
443 'maintainer': self.maintainer['maintainer'].name,
444 'architecture': 'i386',
446 'pool name': 'main/h/hello/',
447 'location id': self.loc['main'].location_id,
448 'source package': 'hello',
449 'source version': '2.2-2',
451 'md5sum': 'deadbeef',
452 'sha1sum': 'deadbeef',
453 'sha256sum': 'deadbeef'}
455 poolfile = add_deb_to_db(upload, 'hello_2.2-2_i386.deb', self.session)
456 self.session.refresh(poolfile)
457 self.session.refresh(poolfile.binary)
458 self.assertEqual('main/h/hello/hello_2.2-2_i386.deb', poolfile.filename)
459 self.assertEqual('hello', poolfile.binary.package)
460 self.assertEqual('2.2-2', poolfile.binary.version)
461 self.assertEqual(['sid'], poolfile.binary.suites)
462 self.assertEqual('Mr. Maintainer', poolfile.binary.maintainer.name)
463 self.assertEqual('i386', poolfile.binary.architecture.arch_string)
464 self.assertEqual('deb', poolfile.binary.binarytype)
465 self.assertEqual(self.loc['main'], poolfile.location)
466 self.assertEqual(self.source['hello_2.2-2'], poolfile.binary.source)
467 self.assertEqual(0, poolfile.filesize)
468 self.assertEqual('deadbeef', poolfile.md5sum)
469 self.assertEqual('deadbeef', poolfile.sha1sum)
470 self.assertEqual('deadbeef', poolfile.sha256sum)
472 if __name__ == '__main__':