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, Component, \
10 get_component_by_package_suite
11 from daklib.queue_install import package_to_suite
12 from daklib.queue import get_newest_source, get_suite_version_by_source, \
13 get_source_by_package_and_suite, get_suite_version_by_package
15 from sqlalchemy.orm.exc import MultipleResultsFound
19 'fake package class used for testing'
27 'fake Upload class used for testing'
29 def __init__(self, pkg):
32 class PackageTestCase(DBDakTestCase):
34 PackageTestCase checks the handling of source and binary packages in dak's
38 def setup_suites(self):
39 "setup a hash of Suite objects in self.suite"
41 if 'suite' in self.__dict__:
44 for suite_name in ('lenny', 'squeeze', 'sid'):
45 self.suite[suite_name] = Suite(suite_name = suite_name, version = '-')
46 self.session.add_all(self.suite.values())
48 def setup_architectures(self):
49 "setup Architecture objects in self.arch and connect to suites"
51 if 'arch' in self.__dict__:
55 for arch_string in ('source', 'all', 'i386', 'amd64', 'kfreebsd-i386'):
56 self.arch[arch_string] = Architecture(arch_string)
57 if arch_string != 'kfreebsd-i386':
58 self.arch[arch_string].suites = self.suite.values()
60 self.arch[arch_string].suites = [self.suite['squeeze'], self.suite['sid']]
61 # hard code ids for source and all
62 self.arch['source'].arch_id = 1
63 self.arch['all'].arch_id = 2
64 self.session.add_all(self.arch.values())
66 def setup_components(self):
67 'create some Component objects'
69 if 'comp' in self.__dict__:
72 self.comp['main'] = Component(component_name = 'main')
73 self.comp['contrib'] = Component(component_name = 'contrib')
74 self.session.add_all(self.comp.values())
76 def setup_locations(self):
77 'create some Location objects'
79 if 'loc' in self.__dict__:
81 self.setup_components()
83 self.loc['main'] = Location( \
84 path = '/srv/ftp-master.debian.org/ftp/pool/', \
85 component = self.comp['main'])
86 self.loc['contrib'] = Location( \
87 path = '/srv/ftp-master.debian.org/ftp/pool/', \
88 component = self.comp['contrib'])
89 self.session.add_all(self.loc.values())
91 def setup_poolfiles(self):
92 'create some PoolFile objects'
94 if 'file' in self.__dict__:
96 self.setup_locations()
98 self.file['hello_2.2-3.dsc'] = PoolFile(filename = 'main/h/hello/hello_2.2-3.dsc', \
99 location = self.loc['main'], filesize = 0, md5sum = '')
100 self.file['hello_2.2-2.dsc'] = PoolFile(filename = 'main/h/hello/hello_2.2-2.dsc', \
101 location = self.loc['main'], filesize = 0, md5sum = '')
102 self.file['hello_2.2-1.dsc'] = PoolFile(filename = 'main/h/hello/hello_2.2-1.dsc', \
103 location = self.loc['main'], filesize = 0, md5sum = '')
104 self.file['hello_2.2-1_i386.deb'] = PoolFile( \
105 filename = 'main/h/hello/hello_2.2-1_i386.deb', \
106 location = self.loc['main'], filesize = 0, md5sum = '')
107 self.file['gnome-hello_2.2-1_i386.deb'] = PoolFile( \
108 filename = 'main/h/hello/gnome-hello_2.2-1_i386.deb', \
109 location = self.loc['main'], filesize = 0, md5sum = '')
110 self.file['python-hello_2.2-1_all.deb'] = PoolFile( \
111 filename = 'main/h/hello/python-hello_2.2-1_all.deb', \
112 location = self.loc['main'], filesize = 0, md5sum = '')
113 self.file['sl_3.03-16.dsc'] = PoolFile(filename = 'main/s/sl/sl_3.03-16.dsc', \
114 location = self.loc['main'], filesize = 0, md5sum = '')
115 self.file['python2.6_2.6.6-8.dsc'] = PoolFile( \
116 filename = 'main/p/python2.6/python2.6_2.6.6-8.dsc', \
117 location = self.loc['main'], filesize = 0, md5sum = '')
118 self.session.add_all(self.file.values())
120 def setup_maintainers(self):
121 'create some Maintainer objects'
123 if 'maintainer' in self.__dict__:
126 self.maintainer['maintainer'] = Maintainer(name = 'Mr. Maintainer')
127 self.maintainer['uploader'] = Maintainer(name = 'Mrs. Uploader')
128 self.maintainer['lazyguy'] = Maintainer(name = 'Lazy Guy')
129 self.session.add_all(self.maintainer.values())
131 def setup_sources(self):
132 'create DBSource objects'
134 if 'source' in self.__dict__:
136 self.setup_maintainers()
138 self.setup_poolfiles()
140 self.source['hello_2.2-2'] = DBSource(source = 'hello', version = '2.2-2', \
141 maintainer = self.maintainer['maintainer'], \
142 changedby = self.maintainer['uploader'], \
143 poolfile = self.file['hello_2.2-2.dsc'], install_date = self.now())
144 self.source['hello_2.2-2'].suites.append(self.suite['sid'])
145 self.source['hello_2.2-1'] = DBSource(source = 'hello', version = '2.2-1', \
146 maintainer = self.maintainer['maintainer'], \
147 changedby = self.maintainer['uploader'], \
148 poolfile = self.file['hello_2.2-1.dsc'], install_date = self.now())
149 self.source['hello_2.2-1'].suites.append(self.suite['sid'])
150 self.source['sl_3.03-16'] = DBSource(source = 'sl', version = '3.03-16', \
151 maintainer = self.maintainer['maintainer'], \
152 changedby = self.maintainer['uploader'], \
153 poolfile = self.file['sl_3.03-16.dsc'], install_date = self.now())
154 self.source['sl_3.03-16'].suites.append(self.suite['squeeze'])
155 self.source['sl_3.03-16'].suites.append(self.suite['sid'])
156 self.session.add_all(self.source.values())
158 def setup_binaries(self):
159 'create DBBinary objects'
161 if 'binary' in self.__dict__:
164 self.setup_architectures()
166 self.binary['hello_2.2-1_i386'] = DBBinary(package = 'hello', \
167 source = self.source['hello_2.2-1'], version = '2.2-1', \
168 maintainer = self.maintainer['maintainer'], \
169 architecture = self.arch['i386'], \
170 poolfile = self.file['hello_2.2-1_i386.deb'])
171 self.binary['hello_2.2-1_i386'].suites.append(self.suite['squeeze'])
172 self.binary['hello_2.2-1_i386'].suites.append(self.suite['sid'])
173 self.binary['gnome-hello_2.2-1_i386'] = DBBinary(package = 'gnome-hello', \
174 source = self.source['hello_2.2-1'], version = '2.2-1', \
175 maintainer = self.maintainer['maintainer'], \
176 architecture = self.arch['i386'], \
177 poolfile = self.file['gnome-hello_2.2-1_i386.deb'])
178 self.binary['gnome-hello_2.2-1_i386'].suites.append(self.suite['squeeze'])
179 self.binary['gnome-hello_2.2-1_i386'].suites.append(self.suite['sid'])
180 self.binary['python-hello_2.2-1_i386'] = DBBinary(package = 'python-hello', \
181 source = self.source['hello_2.2-1'], version = '2.2-1', \
182 maintainer = self.maintainer['maintainer'], \
183 architecture = self.arch['all'], \
184 poolfile = self.file['python-hello_2.2-1_all.deb'])
185 self.binary['python-hello_2.2-1_i386'].suites.append(self.suite['squeeze'])
186 self.session.add_all(self.binary.values())
189 super(PackageTestCase, self).setUp()
190 self.setup_binaries()
191 # flush to make sure that the setup is correct
194 def test_suite_architecture(self):
195 # check the id for architectures source and all
196 self.assertEqual(1, self.arch['source'].arch_id)
197 self.assertEqual(2, self.arch['all'].arch_id)
198 # check the many to many relation between Suite and Architecture
199 self.assertEqual('source', self.suite['lenny'].architectures[0])
200 self.assertEqual(4, len(self.suite['lenny'].architectures))
201 self.assertEqual(3, len(self.arch['i386'].suites))
202 # check the function get_suite_architectures()
203 architectures = get_suite_architectures('lenny', session = self.session)
204 self.assertEqual(4, len(architectures))
205 self.assertTrue(self.arch['source'] in architectures)
206 self.assertTrue(self.arch['all'] in architectures)
207 self.assertTrue(self.arch['kfreebsd-i386'] not in architectures)
208 architectures = get_suite_architectures('sid', session = self.session)
209 self.assertEqual(5, len(architectures))
210 self.assertTrue(self.arch['kfreebsd-i386'] in architectures)
211 architectures = get_suite_architectures('lenny', skipsrc = True, session = self.session)
212 self.assertEqual(3, len(architectures))
213 self.assertTrue(self.arch['source'] not in architectures)
214 architectures = get_suite_architectures('lenny', skipall = True, session = self.session)
215 self.assertEqual(3, len(architectures))
216 self.assertTrue(self.arch['all'] not in architectures)
217 # check the function get_architecture_suites()
218 suites = get_architecture_suites('i386', self.session)
219 self.assertEqual(3, len(suites))
220 self.assertTrue(self.suite['lenny'] in suites)
221 suites = get_architecture_suites('kfreebsd-i386', self.session)
222 self.assertEqual(2, len(suites))
223 self.assertTrue(self.suite['lenny'] not in suites)
225 def test_poolfiles(self):
227 Test the relation of the classes PoolFile and Location.
229 The code needs some explaination. The property Location.files is not a
230 list as in other relations because such a list would become rather
231 huge. It is a query object that can be queried, filtered, and iterated
232 as usual. But list like methods like append() and remove() are
233 supported as well which allows code like:
235 somelocation.files.append(somefile)
238 main = self.loc['main']
239 contrib = self.loc['contrib']
240 self.assertEqual('/srv/ftp-master.debian.org/ftp/pool/', main.path)
241 count = len(self.file.keys())
242 self.assertEqual(count, main.files.count())
243 self.assertEqual(0, contrib.files.count())
244 poolfile = main.files. \
245 filter(PoolFile.filename.like('%/hello/hello%')). \
246 order_by(PoolFile.filename)[0]
247 self.assertEqual('main/h/hello/hello_2.2-1.dsc', poolfile.filename)
248 self.assertEqual(main, poolfile.location)
250 self.assertEqual(poolfile, \
251 self.session.query(PoolFile).get(poolfile.file_id))
252 self.assertEqual(None, self.session.query(PoolFile).get(-1))
253 # test remove() and append()
254 main.files.remove(self.file['sl_3.03-16.dsc'])
255 contrib.files.append(self.file['sl_3.03-16.dsc'])
256 self.assertEqual(count - 1, main.files.count())
257 self.assertEqual(1, contrib.files.count())
259 self.assertEqual('/srv/ftp-master.debian.org/ftp/pool/main/s/sl/sl_3.03-16.dsc', \
260 self.file['sl_3.03-16.dsc'].fullpath)
261 # test check_poolfile()
262 self.assertEqual((True, self.file['sl_3.03-16.dsc']), \
263 check_poolfile('main/s/sl/sl_3.03-16.dsc', 0, '', \
264 contrib.location_id, self.session))
265 self.assertEqual((False, None), \
266 check_poolfile('foobar', 0, '', contrib.location_id, self.session))
267 self.assertEqual((False, self.file['sl_3.03-16.dsc']), \
268 check_poolfile('main/s/sl/sl_3.03-16.dsc', 42, '', \
269 contrib.location_id, self.session))
270 self.assertEqual((False, self.file['sl_3.03-16.dsc']), \
271 check_poolfile('main/s/sl/sl_3.03-16.dsc', 0, 'deadbeef', \
272 contrib.location_id, self.session))
273 # test get_poolfile_like_name()
274 self.assertEqual([self.file['sl_3.03-16.dsc']], \
275 get_poolfile_like_name('sl_3.03-16.dsc', self.session))
276 self.assertEqual([], get_poolfile_like_name('foobar', self.session))
278 def test_maintainers(self):
280 tests relation between Maintainer and DBSource
282 TODO: add relations to changes_pending_source
285 maintainer = self.maintainer['maintainer']
286 self.assertEqual(maintainer,
287 self.session.query(Maintainer).get(maintainer.maintainer_id))
288 uploader = self.maintainer['uploader']
289 self.assertEqual(uploader,
290 self.session.query(Maintainer).get(uploader.maintainer_id))
291 lazyguy = self.maintainer['lazyguy']
292 self.assertEqual(lazyguy,
293 self.session.query(Maintainer).get(lazyguy.maintainer_id))
294 self.assertEqual(3, len(maintainer.maintains_sources))
295 self.assertTrue(self.source['hello_2.2-2'] in maintainer.maintains_sources)
296 self.assertEqual(maintainer.changed_sources, [])
297 self.assertEqual(uploader.maintains_sources, [])
298 self.assertEqual(3, len(uploader.changed_sources))
299 self.assertTrue(self.source['sl_3.03-16'] in uploader.changed_sources)
300 self.assertEqual(lazyguy.maintains_sources, [])
301 self.assertEqual(lazyguy.changed_sources, [])
303 def get_source_in_suite_fail(self):
305 This function throws the MultipleResultsFound exception because
306 get_source_in_suite is broken.
308 TODO: fix get_source_in_suite
311 return get_source_in_suite('hello', 'sid', self.session)
313 def test_sources(self):
314 'test relation between DBSource and PoolFile or Suite'
317 self.assertEqual(self.file['hello_2.2-2.dsc'], self.source['hello_2.2-2'].poolfile)
318 self.assertEqual(self.source['hello_2.2-2'], self.file['hello_2.2-2.dsc'].source)
319 self.assertEqual(None, self.file['python2.6_2.6.6-8.dsc'].source)
321 squeeze = self.session.query(Suite). \
322 filter(Suite.sources.contains(self.source['sl_3.03-16'])). \
323 order_by(Suite.suite_name)[1]
324 self.assertEqual(self.suite['squeeze'], squeeze)
325 self.assertEqual(1, squeeze.sources.count())
326 self.assertEqual(self.source['sl_3.03-16'], squeeze.sources[0])
327 sl = self.session.query(DBSource). \
328 filter(DBSource.suites.contains(self.suite['squeeze'])).one()
329 self.assertEqual(self.source['sl_3.03-16'], sl)
330 self.assertEqual(2, len(sl.suites))
331 self.assertTrue(self.suite['sid'] in sl.suites)
332 # test get_source_in_suite()
333 self.assertRaises(MultipleResultsFound, self.get_source_in_suite_fail)
334 self.assertEqual(None, \
335 get_source_in_suite('hello', 'squeeze', self.session))
336 self.assertEqual(self.source['sl_3.03-16'], \
337 get_source_in_suite('sl', 'sid', self.session))
338 # test get_suites_source_in()
339 self.assertEqual([self.suite['sid']], \
340 get_suites_source_in('hello', self.session))
341 self.assertEqual(2, len(get_suites_source_in('sl', self.session)))
342 self.assertTrue(self.suite['squeeze'] in \
343 get_suites_source_in('sl', self.session))
345 def test_add_dsc_to_db(self):
346 'tests function add_dsc_to_db()'
349 pkg.dsc['source'] = 'hello'
350 pkg.dsc['version'] = '2.2-3'
351 pkg.dsc['maintainer'] = self.maintainer['maintainer'].name
352 pkg.changes['changed-by'] = self.maintainer['uploader'].name
353 pkg.changes['fingerprint'] = 'deadbeef'
354 pkg.changes['distribution'] = { 'sid': '' }
355 pkg.files['hello_2.2-3.dsc'] = { \
357 'location id': self.loc['main'].location_id,
358 'files id': self.file['hello_2.2-3.dsc'].file_id }
361 (source, dsc_component, dsc_location_id, pfs) = \
362 add_dsc_to_db(upload, 'hello_2.2-3.dsc', self.session)
363 self.assertEqual('hello', source.source)
364 self.assertEqual('2.2-3', source.version)
365 self.assertEqual('sid', source.suites[0].suite_name)
366 self.assertEqual('main', dsc_component)
367 self.assertEqual(self.loc['main'].location_id, dsc_location_id)
368 self.assertEqual([], pfs)
370 def test_source_exists(self):
371 'test function source_exists()'
373 hello = self.source['hello_2.2-2']
374 self.assertTrue(source_exists(hello.source, hello.version, \
375 suites = ['sid'], session = self.session))
377 self.assertTrue(source_exists(hello.source, hello.version + '+b7', \
378 suites = ['sid'], session = self.session))
379 self.assertTrue(not source_exists(hello.source, hello.version, \
380 suites = ['lenny', 'squeeze'], session = self.session))
381 self.assertTrue(not source_exists(hello.source, hello.version, \
382 suites = ['lenny', 'sid'], session = self.session))
383 self.assertTrue(not source_exists(hello.source, hello.version, \
384 suites = ['sid', 'lenny'], session = self.session))
385 self.assertTrue(not source_exists(hello.source, '0815', \
386 suites = ['sid'], session = self.session))
388 self.assertTrue(source_exists(hello.source, hello.version, \
389 session = self.session))
391 def test_package_to_suite(self):
392 'test function package_to_suite()'
395 pkg.changes = { 'distribution': {} }
397 self.assertTrue(not package_to_suite(upload, 'sid', self.session))
398 pkg.changes['distribution'] = { 'sid': '' }
399 pkg.changes['architecture'] = { 'source': '' }
400 self.assertTrue(package_to_suite(upload, 'sid', self.session))
401 pkg.changes['architecture'] = {}
402 pkg.changes['source'] = self.source['hello_2.2-2'].source
403 pkg.changes['version'] = self.source['hello_2.2-2'].version
404 self.assertTrue(not package_to_suite(upload, 'sid', self.session))
405 pkg.changes['version'] = '42'
406 self.assertTrue(package_to_suite(upload, 'sid', self.session))
407 pkg.changes['source'] = 'foobar'
408 pkg.changes['version'] = self.source['hello_2.2-2'].version
409 self.assertTrue(package_to_suite(upload, 'sid', self.session))
410 pkg.changes['distribution'] = { 'lenny': '' }
411 self.assertTrue(package_to_suite(upload, 'lenny', self.session))
413 def test_get_newest_source(self):
414 'test function get_newest_source()'
417 daklib.queue.dm_suites = ['sid']
418 self.assertEqual(self.source['hello_2.2-2'], get_newest_source('hello', self.session))
419 self.assertEqual(None, get_newest_source('foobar', self.session))
421 def test_get_suite_version_by_source(self):
422 'test function get_suite_version_by_source()'
424 result = get_suite_version_by_source('hello', self.session)
425 self.assertEqual(2, len(result))
426 self.assertTrue(('sid', '2.2-1') in result)
427 self.assertTrue(('sid', '2.2-2') in result)
428 result = get_suite_version_by_source('sl', self.session)
429 self.assertEqual(2, len(result))
430 self.assertTrue(('squeeze', '3.03-16') in result)
431 self.assertTrue(('sid', '3.03-16') in result)
433 def test_binaries(self):
435 tests class DBBinary; TODO: test relation with Architecture, Maintainer,
436 PoolFile, and Fingerprint
439 # test Suite relation
440 self.assertEqual(2, self.suite['sid'].binaries.count())
441 self.assertTrue(self.binary['hello_2.2-1_i386'] in \
442 self.suite['sid'].binaries.all())
443 self.assertEqual(0, self.suite['lenny'].binaries.count())
444 # test DBSource relation
445 self.assertEqual(3, len(self.source['hello_2.2-1'].binaries))
446 self.assertTrue(self.binary['hello_2.2-1_i386'] in \
447 self.source['hello_2.2-1'].binaries)
448 self.assertEqual(0, len(self.source['hello_2.2-2'].binaries))
449 # test get_suites_binary_in()
450 self.assertEqual(2, len(get_suites_binary_in('hello', self.session)))
451 self.assertTrue(self.suite['sid'] in \
452 get_suites_binary_in('hello', self.session))
453 self.assertEqual(2, len(get_suites_binary_in('gnome-hello', self.session)))
454 self.assertTrue(self.suite['squeeze'] in \
455 get_suites_binary_in('gnome-hello', self.session))
456 self.assertEqual(0, len(get_suites_binary_in('sl', self.session)))
458 def test_add_deb_to_db(self):
459 'tests function add_deb_to_db()'
462 pkg.changes['fingerprint'] = 'deadbeef'
463 pkg.changes['distribution'] = { 'sid': '' }
464 pkg.files['hello_2.2-2_i386.deb'] = { \
467 'maintainer': self.maintainer['maintainer'].name,
468 'architecture': 'i386',
470 'pool name': 'main/h/hello/',
471 'location id': self.loc['main'].location_id,
472 'source package': 'hello',
473 'source version': '2.2-2',
475 'md5sum': 'deadbeef',
476 'sha1sum': 'deadbeef',
477 'sha256sum': 'deadbeef'}
479 poolfile = add_deb_to_db(upload, 'hello_2.2-2_i386.deb', self.session)
480 self.session.refresh(poolfile)
481 self.session.refresh(poolfile.binary)
482 self.assertEqual('main/h/hello/hello_2.2-2_i386.deb', poolfile.filename)
483 self.assertEqual('hello', poolfile.binary.package)
484 self.assertEqual('2.2-2', poolfile.binary.version)
485 self.assertEqual(['sid'], poolfile.binary.suites)
486 self.assertEqual('Mr. Maintainer', poolfile.binary.maintainer.name)
487 self.assertEqual('i386', poolfile.binary.architecture.arch_string)
488 self.assertEqual('deb', poolfile.binary.binarytype)
489 self.assertEqual(self.loc['main'], poolfile.location)
490 self.assertEqual(self.source['hello_2.2-2'], poolfile.binary.source)
491 self.assertEqual(0, poolfile.filesize)
492 self.assertEqual('deadbeef', poolfile.md5sum)
493 self.assertEqual('deadbeef', poolfile.sha1sum)
494 self.assertEqual('deadbeef', poolfile.sha256sum)
496 def test_get_source_by_package_and_suite(self):
497 'test get_source_by_package_and_suite()'
499 query = get_source_by_package_and_suite('hello', 'sid', self.session)
500 self.assertEqual(self.source['hello_2.2-1'], query.one())
501 query = get_source_by_package_and_suite('gnome-hello', 'squeeze', self.session)
502 self.assertEqual(self.source['hello_2.2-1'], query.one())
503 query = get_source_by_package_and_suite('hello', 'hamm', self.session)
504 self.assertEqual(0, query.count())
505 query = get_source_by_package_and_suite('foobar', 'squeeze', self.session)
506 self.assertEqual(0, query.count())
508 def test_get_suite_version_by_package(self):
509 'test function get_suite_version_by_package()'
511 result = get_suite_version_by_package('hello', 'i386', self.session)
512 self.assertEqual(2, len(result))
513 self.assertTrue(('sid', '2.2-1') in result)
514 result = get_suite_version_by_package('hello', 'amd64', self.session)
515 self.assertEqual(0, len(result))
516 result = get_suite_version_by_package('python-hello', 'i386', self.session)
517 self.assertEqual([('squeeze', '2.2-1')], result)
518 result = get_suite_version_by_package('python-hello', 'amd64', self.session)
519 self.assertEqual([('squeeze', '2.2-1')], result)
521 def test_components(self):
522 'test class Component'
524 self.assertEqual(self.loc['main'], self.comp['main'].location)
525 self.assertEqual(self.loc['contrib'], self.comp['contrib'].location)
527 def test_get_component_by_package_suite(self):
528 'test get_component_by_package_suite()'
530 result = get_component_by_package_suite('hello', ['sid'], self.session)
531 self.assertEqual('main', result)
532 result = get_component_by_package_suite('hello', ['hamm'], self.session)
533 self.assertEqual(None, result)
534 result = get_component_by_package_suite('foobar', ['sid'], self.session)
535 self.assertEqual(None, result)
537 if __name__ == '__main__':