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 from daklib.queue_install import package_to_suite
11 from daklib.queue import get_newest_source, get_suite_version_by_source, \
12 get_source_by_package_and_suite, get_suite_version_by_package
14 from sqlalchemy.orm.exc import MultipleResultsFound
18 'fake package class used for testing'
26 'fake Upload class used for testing'
28 def __init__(self, pkg):
31 class PackageTestCase(DBDakTestCase):
33 PackageTestCase checks the handling of source and binary packages in dak's
37 def setup_suites(self):
38 "setup a hash of Suite objects in self.suite"
40 if 'suite' in self.__dict__:
43 for suite_name in ('lenny', 'squeeze', 'sid'):
44 self.suite[suite_name] = Suite(suite_name = suite_name, version = '-')
45 self.session.add_all(self.suite.values())
47 def setup_architectures(self):
48 "setup Architecture objects in self.arch and connect to suites"
50 if 'arch' in self.__dict__:
54 for arch_string in ('source', 'all', 'i386', 'amd64', 'kfreebsd-i386'):
55 self.arch[arch_string] = Architecture(arch_string)
56 if arch_string != 'kfreebsd-i386':
57 self.arch[arch_string].suites = self.suite.values()
59 self.arch[arch_string].suites = [self.suite['squeeze'], self.suite['sid']]
60 # hard code ids for source and all
61 self.arch['source'].arch_id = 1
62 self.arch['all'].arch_id = 2
63 self.session.add_all(self.arch.values())
65 def setup_components(self):
66 'create some Component objects'
68 if 'comp' in self.__dict__:
71 self.comp['main'] = Component(component_name = 'main')
72 self.comp['contrib'] = Component(component_name = 'contrib')
73 self.session.add_all(self.comp.values())
75 def setup_locations(self):
76 'create some Location objects'
78 if 'loc' in self.__dict__:
80 self.setup_components()
82 self.loc['main'] = Location( \
83 path = '/srv/ftp-master.debian.org/ftp/pool/', \
84 component = self.comp['main'])
85 self.loc['contrib'] = Location( \
86 path = '/srv/ftp-master.debian.org/ftp/pool/', \
87 component = self.comp['contrib'])
88 self.session.add_all(self.loc.values())
90 def setup_poolfiles(self):
91 'create some PoolFile objects'
93 if 'file' in self.__dict__:
95 self.setup_locations()
97 self.file['hello_2.2-3.dsc'] = PoolFile(filename = 'main/h/hello/hello_2.2-3.dsc', \
98 location = self.loc['main'], filesize = 0, md5sum = '')
99 self.file['hello_2.2-2.dsc'] = PoolFile(filename = 'main/h/hello/hello_2.2-2.dsc', \
100 location = self.loc['main'], filesize = 0, md5sum = '')
101 self.file['hello_2.2-1.dsc'] = PoolFile(filename = 'main/h/hello/hello_2.2-1.dsc', \
102 location = self.loc['main'], filesize = 0, md5sum = '')
103 self.file['hello_2.2-1_i386.deb'] = PoolFile( \
104 filename = 'main/h/hello/hello_2.2-1_i386.deb', \
105 location = self.loc['main'], filesize = 0, md5sum = '')
106 self.file['gnome-hello_2.2-1_i386.deb'] = PoolFile( \
107 filename = 'main/h/hello/gnome-hello_2.2-1_i386.deb', \
108 location = self.loc['main'], filesize = 0, md5sum = '')
109 self.file['python-hello_2.2-1_all.deb'] = PoolFile( \
110 filename = 'main/h/hello/python-hello_2.2-1_all.deb', \
111 location = self.loc['main'], filesize = 0, md5sum = '')
112 self.file['sl_3.03-16.dsc'] = PoolFile(filename = 'main/s/sl/sl_3.03-16.dsc', \
113 location = self.loc['main'], filesize = 0, md5sum = '')
114 self.file['python2.6_2.6.6-8.dsc'] = PoolFile( \
115 filename = 'main/p/python2.6/python2.6_2.6.6-8.dsc', \
116 location = self.loc['main'], filesize = 0, md5sum = '')
117 self.session.add_all(self.file.values())
119 def setup_maintainers(self):
120 'create some Maintainer objects'
122 if 'maintainer' in self.__dict__:
125 self.maintainer['maintainer'] = Maintainer(name = 'Mr. Maintainer')
126 self.maintainer['uploader'] = Maintainer(name = 'Mrs. Uploader')
127 self.maintainer['lazyguy'] = Maintainer(name = 'Lazy Guy')
128 self.session.add_all(self.maintainer.values())
130 def setup_sources(self):
131 'create DBSource objects'
133 if 'source' in self.__dict__:
135 self.setup_maintainers()
137 self.setup_poolfiles()
139 self.source['hello_2.2-2'] = DBSource(source = 'hello', version = '2.2-2', \
140 maintainer = self.maintainer['maintainer'], \
141 changedby = self.maintainer['uploader'], \
142 poolfile = self.file['hello_2.2-2.dsc'], install_date = self.now())
143 self.source['hello_2.2-2'].suites.append(self.suite['sid'])
144 self.source['hello_2.2-1'] = DBSource(source = 'hello', version = '2.2-1', \
145 maintainer = self.maintainer['maintainer'], \
146 changedby = self.maintainer['uploader'], \
147 poolfile = self.file['hello_2.2-1.dsc'], install_date = self.now())
148 self.source['hello_2.2-1'].suites.append(self.suite['sid'])
149 self.source['sl_3.03-16'] = DBSource(source = 'sl', version = '3.03-16', \
150 maintainer = self.maintainer['maintainer'], \
151 changedby = self.maintainer['uploader'], \
152 poolfile = self.file['sl_3.03-16.dsc'], install_date = self.now())
153 self.source['sl_3.03-16'].suites.append(self.suite['squeeze'])
154 self.source['sl_3.03-16'].suites.append(self.suite['sid'])
155 self.session.add_all(self.source.values())
157 def setup_binaries(self):
158 'create DBBinary objects'
160 if 'binary' in self.__dict__:
163 self.setup_architectures()
165 self.binary['hello_2.2-1_i386'] = DBBinary(package = 'hello', \
166 source = self.source['hello_2.2-1'], version = '2.2-1', \
167 maintainer = self.maintainer['maintainer'], \
168 architecture = self.arch['i386'], \
169 poolfile = self.file['hello_2.2-1_i386.deb'])
170 self.binary['hello_2.2-1_i386'].suites.append(self.suite['squeeze'])
171 self.binary['hello_2.2-1_i386'].suites.append(self.suite['sid'])
172 self.binary['gnome-hello_2.2-1_i386'] = DBBinary(package = 'gnome-hello', \
173 source = self.source['hello_2.2-1'], version = '2.2-1', \
174 maintainer = self.maintainer['maintainer'], \
175 architecture = self.arch['i386'], \
176 poolfile = self.file['gnome-hello_2.2-1_i386.deb'])
177 self.binary['gnome-hello_2.2-1_i386'].suites.append(self.suite['squeeze'])
178 self.binary['gnome-hello_2.2-1_i386'].suites.append(self.suite['sid'])
179 self.binary['python-hello_2.2-1_i386'] = DBBinary(package = 'python-hello', \
180 source = self.source['hello_2.2-1'], version = '2.2-1', \
181 maintainer = self.maintainer['maintainer'], \
182 architecture = self.arch['all'], \
183 poolfile = self.file['python-hello_2.2-1_all.deb'])
184 self.binary['python-hello_2.2-1_i386'].suites.append(self.suite['squeeze'])
185 self.session.add_all(self.binary.values())
188 super(PackageTestCase, self).setUp()
189 self.setup_binaries()
190 # flush to make sure that the setup is correct
193 def test_suite_architecture(self):
194 # check the id for architectures source and all
195 self.assertEqual(1, self.arch['source'].arch_id)
196 self.assertEqual(2, self.arch['all'].arch_id)
197 # check the many to many relation between Suite and Architecture
198 self.assertEqual('source', self.suite['lenny'].architectures[0])
199 self.assertEqual(4, len(self.suite['lenny'].architectures))
200 self.assertEqual(3, len(self.arch['i386'].suites))
201 # check the function get_suite_architectures()
202 architectures = get_suite_architectures('lenny', session = self.session)
203 self.assertEqual(4, len(architectures))
204 self.assertTrue(self.arch['source'] in architectures)
205 self.assertTrue(self.arch['all'] in architectures)
206 self.assertTrue(self.arch['kfreebsd-i386'] not in architectures)
207 architectures = get_suite_architectures('sid', session = self.session)
208 self.assertEqual(5, len(architectures))
209 self.assertTrue(self.arch['kfreebsd-i386'] in architectures)
210 architectures = get_suite_architectures('lenny', skipsrc = True, session = self.session)
211 self.assertEqual(3, len(architectures))
212 self.assertTrue(self.arch['source'] not in architectures)
213 architectures = get_suite_architectures('lenny', skipall = True, session = self.session)
214 self.assertEqual(3, len(architectures))
215 self.assertTrue(self.arch['all'] not in architectures)
216 # check the function get_architecture_suites()
217 suites = get_architecture_suites('i386', self.session)
218 self.assertEqual(3, len(suites))
219 self.assertTrue(self.suite['lenny'] in suites)
220 suites = get_architecture_suites('kfreebsd-i386', self.session)
221 self.assertEqual(2, len(suites))
222 self.assertTrue(self.suite['lenny'] not in suites)
224 def test_poolfiles(self):
226 Test the relation of the classes PoolFile and Location.
228 The code needs some explaination. The property Location.files is not a
229 list as in other relations because such a list would become rather
230 huge. It is a query object that can be queried, filtered, and iterated
231 as usual. But list like methods like append() and remove() are
232 supported as well which allows code like:
234 somelocation.files.append(somefile)
237 main = self.loc['main']
238 contrib = self.loc['contrib']
239 self.assertEqual('/srv/ftp-master.debian.org/ftp/pool/', main.path)
240 count = len(self.file.keys())
241 self.assertEqual(count, main.files.count())
242 self.assertEqual(0, contrib.files.count())
243 poolfile = main.files. \
244 filter(PoolFile.filename.like('%/hello/hello%')). \
245 order_by(PoolFile.filename)[0]
246 self.assertEqual('main/h/hello/hello_2.2-1.dsc', poolfile.filename)
247 self.assertEqual(main, poolfile.location)
249 self.assertEqual(poolfile, \
250 self.session.query(PoolFile).get(poolfile.file_id))
251 self.assertEqual(None, self.session.query(PoolFile).get(-1))
252 # test remove() and append()
253 main.files.remove(self.file['sl_3.03-16.dsc'])
254 contrib.files.append(self.file['sl_3.03-16.dsc'])
255 self.assertEqual(count - 1, main.files.count())
256 self.assertEqual(1, contrib.files.count())
258 self.assertEqual('/srv/ftp-master.debian.org/ftp/pool/main/s/sl/sl_3.03-16.dsc', \
259 self.file['sl_3.03-16.dsc'].fullpath)
260 # test check_poolfile()
261 self.assertEqual((True, self.file['sl_3.03-16.dsc']), \
262 check_poolfile('main/s/sl/sl_3.03-16.dsc', 0, '', \
263 contrib.location_id, self.session))
264 self.assertEqual((False, None), \
265 check_poolfile('foobar', 0, '', contrib.location_id, self.session))
266 self.assertEqual((False, self.file['sl_3.03-16.dsc']), \
267 check_poolfile('main/s/sl/sl_3.03-16.dsc', 42, '', \
268 contrib.location_id, self.session))
269 self.assertEqual((False, self.file['sl_3.03-16.dsc']), \
270 check_poolfile('main/s/sl/sl_3.03-16.dsc', 0, 'deadbeef', \
271 contrib.location_id, self.session))
272 # test get_poolfile_like_name()
273 self.assertEqual([self.file['sl_3.03-16.dsc']], \
274 get_poolfile_like_name('sl_3.03-16.dsc', self.session))
275 self.assertEqual([], get_poolfile_like_name('foobar', self.session))
277 def test_maintainers(self):
279 tests relation between Maintainer and DBSource
281 TODO: add relations to changes_pending_source
284 maintainer = self.maintainer['maintainer']
285 self.assertEqual(maintainer,
286 self.session.query(Maintainer).get(maintainer.maintainer_id))
287 uploader = self.maintainer['uploader']
288 self.assertEqual(uploader,
289 self.session.query(Maintainer).get(uploader.maintainer_id))
290 lazyguy = self.maintainer['lazyguy']
291 self.assertEqual(lazyguy,
292 self.session.query(Maintainer).get(lazyguy.maintainer_id))
293 self.assertEqual(3, len(maintainer.maintains_sources))
294 self.assertTrue(self.source['hello_2.2-2'] in maintainer.maintains_sources)
295 self.assertEqual(maintainer.changed_sources, [])
296 self.assertEqual(uploader.maintains_sources, [])
297 self.assertEqual(3, len(uploader.changed_sources))
298 self.assertTrue(self.source['sl_3.03-16'] in uploader.changed_sources)
299 self.assertEqual(lazyguy.maintains_sources, [])
300 self.assertEqual(lazyguy.changed_sources, [])
302 def get_source_in_suite_fail(self):
304 This function throws the MultipleResultsFound exception because
305 get_source_in_suite is broken.
307 TODO: fix get_source_in_suite
310 return get_source_in_suite('hello', 'sid', self.session)
312 def test_sources(self):
313 'test relation between DBSource and PoolFile or Suite'
316 self.assertEqual(self.file['hello_2.2-2.dsc'], self.source['hello_2.2-2'].poolfile)
317 self.assertEqual(self.source['hello_2.2-2'], self.file['hello_2.2-2.dsc'].source)
318 self.assertEqual(None, self.file['python2.6_2.6.6-8.dsc'].source)
320 squeeze = self.session.query(Suite). \
321 filter(Suite.sources.contains(self.source['sl_3.03-16'])). \
322 order_by(Suite.suite_name)[1]
323 self.assertEqual(self.suite['squeeze'], squeeze)
324 self.assertEqual(1, squeeze.sources.count())
325 self.assertEqual(self.source['sl_3.03-16'], squeeze.sources[0])
326 sl = self.session.query(DBSource). \
327 filter(DBSource.suites.contains(self.suite['squeeze'])).one()
328 self.assertEqual(self.source['sl_3.03-16'], sl)
329 self.assertEqual(2, len(sl.suites))
330 self.assertTrue(self.suite['sid'] in sl.suites)
331 # test get_source_in_suite()
332 self.assertRaises(MultipleResultsFound, self.get_source_in_suite_fail)
333 self.assertEqual(None, \
334 get_source_in_suite('hello', 'squeeze', self.session))
335 self.assertEqual(self.source['sl_3.03-16'], \
336 get_source_in_suite('sl', 'sid', self.session))
337 # test get_suites_source_in()
338 self.assertEqual([self.suite['sid']], \
339 get_suites_source_in('hello', self.session))
340 self.assertEqual(2, len(get_suites_source_in('sl', self.session)))
341 self.assertTrue(self.suite['squeeze'] in \
342 get_suites_source_in('sl', self.session))
344 def test_add_dsc_to_db(self):
345 'tests function add_dsc_to_db()'
348 pkg.dsc['source'] = 'hello'
349 pkg.dsc['version'] = '2.2-3'
350 pkg.dsc['maintainer'] = self.maintainer['maintainer'].name
351 pkg.changes['changed-by'] = self.maintainer['uploader'].name
352 pkg.changes['fingerprint'] = 'deadbeef'
353 pkg.changes['distribution'] = { 'sid': '' }
354 pkg.files['hello_2.2-3.dsc'] = { \
356 'location id': self.loc['main'].location_id,
357 'files id': self.file['hello_2.2-3.dsc'].file_id }
360 (source, dsc_component, dsc_location_id, pfs) = \
361 add_dsc_to_db(upload, 'hello_2.2-3.dsc', self.session)
362 self.assertEqual('hello', source.source)
363 self.assertEqual('2.2-3', source.version)
364 self.assertEqual('sid', source.suites[0].suite_name)
365 self.assertEqual('main', dsc_component)
366 self.assertEqual(self.loc['main'].location_id, dsc_location_id)
367 self.assertEqual([], pfs)
369 def test_source_exists(self):
370 'test function source_exists()'
372 hello = self.source['hello_2.2-2']
373 self.assertTrue(source_exists(hello.source, hello.version, \
374 suites = ['sid'], session = self.session))
376 self.assertTrue(source_exists(hello.source, hello.version + '+b7', \
377 suites = ['sid'], session = self.session))
378 self.assertTrue(not source_exists(hello.source, hello.version, \
379 suites = ['lenny', 'squeeze'], session = self.session))
380 self.assertTrue(not source_exists(hello.source, hello.version, \
381 suites = ['lenny', 'sid'], session = self.session))
382 self.assertTrue(not source_exists(hello.source, hello.version, \
383 suites = ['sid', 'lenny'], session = self.session))
384 self.assertTrue(not source_exists(hello.source, '0815', \
385 suites = ['sid'], session = self.session))
387 self.assertTrue(source_exists(hello.source, hello.version, \
388 session = self.session))
390 def test_package_to_suite(self):
391 'test function package_to_suite()'
394 pkg.changes = { 'distribution': {} }
396 self.assertTrue(not package_to_suite(upload, 'sid', self.session))
397 pkg.changes['distribution'] = { 'sid': '' }
398 pkg.changes['architecture'] = { 'source': '' }
399 self.assertTrue(package_to_suite(upload, 'sid', self.session))
400 pkg.changes['architecture'] = {}
401 pkg.changes['source'] = self.source['hello_2.2-2'].source
402 pkg.changes['version'] = self.source['hello_2.2-2'].version
403 self.assertTrue(not package_to_suite(upload, 'sid', self.session))
404 pkg.changes['version'] = '42'
405 self.assertTrue(package_to_suite(upload, 'sid', self.session))
406 pkg.changes['source'] = 'foobar'
407 pkg.changes['version'] = self.source['hello_2.2-2'].version
408 self.assertTrue(package_to_suite(upload, 'sid', self.session))
409 pkg.changes['distribution'] = { 'lenny': '' }
410 self.assertTrue(package_to_suite(upload, 'lenny', self.session))
412 def test_get_newest_source(self):
413 'test function get_newest_source()'
416 daklib.queue.dm_suites = ['sid']
417 self.assertEqual(self.source['hello_2.2-2'], get_newest_source('hello', self.session))
418 self.assertEqual(None, get_newest_source('foobar', self.session))
420 def test_get_suite_version_by_source(self):
421 'test function get_suite_version_by_source()'
423 result = get_suite_version_by_source('hello', self.session)
424 self.assertEqual(2, len(result))
425 self.assertTrue(('sid', '2.2-1') in result)
426 self.assertTrue(('sid', '2.2-2') in result)
427 result = get_suite_version_by_source('sl', self.session)
428 self.assertEqual(2, len(result))
429 self.assertTrue(('squeeze', '3.03-16') in result)
430 self.assertTrue(('sid', '3.03-16') in result)
432 def test_binaries(self):
434 tests class DBBinary; TODO: test relation with Architecture, Maintainer,
435 PoolFile, and Fingerprint
438 # test Suite relation
439 self.assertEqual(2, self.suite['sid'].binaries.count())
440 self.assertTrue(self.binary['hello_2.2-1_i386'] in \
441 self.suite['sid'].binaries.all())
442 self.assertEqual(0, self.suite['lenny'].binaries.count())
443 # test DBSource relation
444 self.assertEqual(3, len(self.source['hello_2.2-1'].binaries))
445 self.assertTrue(self.binary['hello_2.2-1_i386'] in \
446 self.source['hello_2.2-1'].binaries)
447 self.assertEqual(0, len(self.source['hello_2.2-2'].binaries))
448 # test get_suites_binary_in()
449 self.assertEqual(2, len(get_suites_binary_in('hello', self.session)))
450 self.assertTrue(self.suite['sid'] in \
451 get_suites_binary_in('hello', self.session))
452 self.assertEqual(2, len(get_suites_binary_in('gnome-hello', self.session)))
453 self.assertTrue(self.suite['squeeze'] in \
454 get_suites_binary_in('gnome-hello', self.session))
455 self.assertEqual(0, len(get_suites_binary_in('sl', self.session)))
457 def test_add_deb_to_db(self):
458 'tests function add_deb_to_db()'
461 pkg.changes['fingerprint'] = 'deadbeef'
462 pkg.changes['distribution'] = { 'sid': '' }
463 pkg.files['hello_2.2-2_i386.deb'] = { \
466 'maintainer': self.maintainer['maintainer'].name,
467 'architecture': 'i386',
469 'pool name': 'main/h/hello/',
470 'location id': self.loc['main'].location_id,
471 'source package': 'hello',
472 'source version': '2.2-2',
474 'md5sum': 'deadbeef',
475 'sha1sum': 'deadbeef',
476 'sha256sum': 'deadbeef'}
478 poolfile = add_deb_to_db(upload, 'hello_2.2-2_i386.deb', self.session)
479 self.session.refresh(poolfile)
480 self.session.refresh(poolfile.binary)
481 self.assertEqual('main/h/hello/hello_2.2-2_i386.deb', poolfile.filename)
482 self.assertEqual('hello', poolfile.binary.package)
483 self.assertEqual('2.2-2', poolfile.binary.version)
484 self.assertEqual(['sid'], poolfile.binary.suites)
485 self.assertEqual('Mr. Maintainer', poolfile.binary.maintainer.name)
486 self.assertEqual('i386', poolfile.binary.architecture.arch_string)
487 self.assertEqual('deb', poolfile.binary.binarytype)
488 self.assertEqual(self.loc['main'], poolfile.location)
489 self.assertEqual(self.source['hello_2.2-2'], poolfile.binary.source)
490 self.assertEqual(0, poolfile.filesize)
491 self.assertEqual('deadbeef', poolfile.md5sum)
492 self.assertEqual('deadbeef', poolfile.sha1sum)
493 self.assertEqual('deadbeef', poolfile.sha256sum)
495 def test_get_source_by_package_and_suite(self):
496 'test get_source_by_package_and_suite()'
498 query = get_source_by_package_and_suite('hello', 'sid', self.session)
499 self.assertEqual(self.source['hello_2.2-1'], query.one())
500 query = get_source_by_package_and_suite('gnome-hello', 'squeeze', self.session)
501 self.assertEqual(self.source['hello_2.2-1'], query.one())
502 query = get_source_by_package_and_suite('hello', 'hamm', self.session)
503 self.assertEqual(0, query.count())
504 query = get_source_by_package_and_suite('foobar', 'squeeze', self.session)
505 self.assertEqual(0, query.count())
507 def test_get_suite_version_by_package(self):
508 'test function get_suite_version_by_package()'
510 result = get_suite_version_by_package('hello', 'i386', self.session)
511 self.assertEqual(2, len(result))
512 self.assertTrue(('sid', '2.2-1') in result)
513 result = get_suite_version_by_package('hello', 'amd64', self.session)
514 self.assertEqual(0, len(result))
515 result = get_suite_version_by_package('python-hello', 'i386', self.session)
516 self.assertEqual([('squeeze', '2.2-1')], result)
517 result = get_suite_version_by_package('python-hello', 'amd64', self.session)
518 self.assertEqual([('squeeze', '2.2-1')], result)
520 def test_components(self):
521 'test class Component'
523 self.assertEqual(self.loc['main'], self.comp['main'].location)
524 self.assertEqual(self.loc['contrib'], self.comp['contrib'].location)
526 if __name__ == '__main__':