]> git.decadent.org.uk Git - dak.git/blob - tests/dbtest_packages.py
Create, test, and refactor get_source_by_package_and_suite().
[dak.git] / tests / dbtest_packages.py
1 #!/usr/bin/env python
2
3 from db_test import DBDakTestCase
4
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, \
12     get_source_by_package_and_suite
13
14 from sqlalchemy.orm.exc import MultipleResultsFound
15 import unittest
16
17 class Pkg():
18     'fake package class used for testing'
19
20     def __init__(self):
21         self.dsc = {}
22         self.files = {}
23         self.changes = {}
24
25 class Upload():
26     'fake Upload class used for testing'
27
28     def __init__(self, pkg):
29         self.pkg = pkg
30
31 class PackageTestCase(DBDakTestCase):
32     """
33     PackageTestCase checks the handling of source and binary packages in dak's
34     database.
35     """
36
37     def setup_suites(self):
38         "setup a hash of Suite objects in self.suite"
39
40         if 'suite' in self.__dict__:
41             return
42         self.suite = {}
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())
46
47     def setup_architectures(self):
48         "setup Architecture objects in self.arch and connect to suites"
49
50         if 'arch' in self.__dict__:
51             return
52         self.setup_suites()
53         self.arch = {}
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()
58             else:
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())
64
65     def setup_locations(self):
66         'create some Location objects, TODO: add component'
67
68         if 'loc' in self.__dict__:
69             return
70         self.loc = {}
71         self.loc['main'] = Location(path = \
72             '/srv/ftp-master.debian.org/ftp/pool/')
73         self.loc['contrib'] = Location(path = \
74             '/srv/ftp-master.debian.org/ftp/pool/')
75         self.session.add_all(self.loc.values())
76
77     def setup_poolfiles(self):
78         'create some PoolFile objects'
79
80         if 'file' in self.__dict__:
81             return
82         self.setup_locations()
83         self.file = {}
84         self.file['hello_2.2-3.dsc'] = PoolFile(filename = 'main/h/hello/hello_2.2-3.dsc', \
85             location = self.loc['main'], filesize = 0, md5sum = '')
86         self.file['hello_2.2-2.dsc'] = PoolFile(filename = 'main/h/hello/hello_2.2-2.dsc', \
87             location = self.loc['main'], filesize = 0, md5sum = '')
88         self.file['hello_2.2-1.dsc'] = PoolFile(filename = 'main/h/hello/hello_2.2-1.dsc', \
89             location = self.loc['main'], filesize = 0, md5sum = '')
90         self.file['hello_2.2-1_i386.deb'] = PoolFile( \
91             filename = 'main/h/hello/hello_2.2-1_i386.deb', \
92             location = self.loc['main'], filesize = 0, md5sum = '')
93         self.file['gnome-hello_2.2-1_i386.deb'] = PoolFile( \
94             filename = 'main/h/hello/gnome-hello_2.2-1_i386.deb', \
95             location = self.loc['main'], filesize = 0, md5sum = '')
96         self.file['sl_3.03-16.dsc'] = PoolFile(filename = 'main/s/sl/sl_3.03-16.dsc', \
97             location = self.loc['main'], filesize = 0, md5sum = '')
98         self.file['python2.6_2.6.6-8.dsc'] = PoolFile( \
99             filename = 'main/p/python2.6/python2.6_2.6.6-8.dsc', \
100             location = self.loc['main'], filesize = 0, md5sum = '')
101         self.session.add_all(self.file.values())
102
103     def setup_maintainers(self):
104         'create some Maintainer objects'
105
106         if 'maintainer' in self.__dict__:
107             return
108         self.maintainer = {}
109         self.maintainer['maintainer'] = Maintainer(name = 'Mr. Maintainer')
110         self.maintainer['uploader'] = Maintainer(name = 'Mrs. Uploader')
111         self.maintainer['lazyguy'] = Maintainer(name = 'Lazy Guy')
112         self.session.add_all(self.maintainer.values())
113
114     def setup_sources(self):
115         'create DBSource objects'
116
117         if 'source' in self.__dict__:
118             return
119         self.setup_maintainers()
120         self.setup_suites()
121         self.setup_poolfiles()
122         self.source = {}
123         self.source['hello_2.2-2'] = DBSource(source = 'hello', version = '2.2-2', \
124             maintainer = self.maintainer['maintainer'], \
125             changedby = self.maintainer['uploader'], \
126             poolfile = self.file['hello_2.2-2.dsc'], install_date = self.now())
127         self.source['hello_2.2-2'].suites.append(self.suite['sid'])
128         self.source['hello_2.2-1'] = DBSource(source = 'hello', version = '2.2-1', \
129             maintainer = self.maintainer['maintainer'], \
130             changedby = self.maintainer['uploader'], \
131             poolfile = self.file['hello_2.2-1.dsc'], install_date = self.now())
132         self.source['hello_2.2-1'].suites.append(self.suite['sid'])
133         self.source['sl_3.03-16'] = DBSource(source = 'sl', version = '3.03-16', \
134             maintainer = self.maintainer['maintainer'], \
135             changedby = self.maintainer['uploader'], \
136             poolfile = self.file['sl_3.03-16.dsc'], install_date = self.now())
137         self.source['sl_3.03-16'].suites.append(self.suite['squeeze'])
138         self.source['sl_3.03-16'].suites.append(self.suite['sid'])
139         self.session.add_all(self.source.values())
140
141     def setup_binaries(self):
142         'create DBBinary objects'
143
144         if 'binary' in self.__dict__:
145             return
146         self.setup_sources()
147         self.setup_architectures()
148         self.binary = {}
149         self.binary['hello_2.2-1_i386'] = DBBinary(package = 'hello', \
150             source = self.source['hello_2.2-1'], version = '2.2-1', \
151             maintainer = self.maintainer['maintainer'], \
152             architecture = self.arch['i386'], \
153             poolfile = self.file['hello_2.2-1_i386.deb'])
154         self.binary['hello_2.2-1_i386'].suites.append(self.suite['squeeze'])
155         self.binary['hello_2.2-1_i386'].suites.append(self.suite['sid'])
156         self.binary['gnome-hello_2.2-1_i386'] = DBBinary(package = 'gnome-hello', \
157             source = self.source['hello_2.2-1'], version = '2.2-1', \
158             maintainer = self.maintainer['maintainer'], \
159             architecture = self.arch['i386'], \
160             poolfile = self.file['gnome-hello_2.2-1_i386.deb'])
161         self.binary['gnome-hello_2.2-1_i386'].suites.append(self.suite['squeeze'])
162         self.binary['gnome-hello_2.2-1_i386'].suites.append(self.suite['sid'])
163         self.session.add_all(self.binary.values())
164
165     def setUp(self):
166         super(PackageTestCase, self).setUp()
167         self.setup_binaries()
168         # flush to make sure that the setup is correct
169         self.session.flush()
170
171     def test_suite_architecture(self):
172         # check the id for architectures source and all
173         self.assertEqual(1, self.arch['source'].arch_id)
174         self.assertEqual(2, self.arch['all'].arch_id)
175         # check the many to many relation between Suite and Architecture
176         self.assertEqual('source', self.suite['lenny'].architectures[0])
177         self.assertEqual(4, len(self.suite['lenny'].architectures))
178         self.assertEqual(3, len(self.arch['i386'].suites))
179         # check the function get_suite_architectures()
180         architectures = get_suite_architectures('lenny', session = self.session)
181         self.assertEqual(4, len(architectures))
182         self.assertTrue(self.arch['source'] in architectures)
183         self.assertTrue(self.arch['all'] in architectures)
184         self.assertTrue(self.arch['kfreebsd-i386'] not in architectures)
185         architectures = get_suite_architectures('sid', session = self.session)
186         self.assertEqual(5, len(architectures))
187         self.assertTrue(self.arch['kfreebsd-i386'] in architectures)
188         architectures = get_suite_architectures('lenny', skipsrc = True, session = self.session)
189         self.assertEqual(3, len(architectures))
190         self.assertTrue(self.arch['source'] not in architectures)
191         architectures = get_suite_architectures('lenny', skipall = True, session = self.session)
192         self.assertEqual(3, len(architectures))
193         self.assertTrue(self.arch['all'] not in architectures)
194         # check the function get_architecture_suites()
195         suites = get_architecture_suites('i386', self.session)
196         self.assertEqual(3, len(suites))
197         self.assertTrue(self.suite['lenny'] in suites)
198         suites = get_architecture_suites('kfreebsd-i386', self.session)
199         self.assertEqual(2, len(suites))
200         self.assertTrue(self.suite['lenny'] not in suites)
201
202     def test_poolfiles(self):
203         '''
204         Test the relation of the classes PoolFile and Location.
205
206         The code needs some explaination. The property Location.files is not a
207         list as in other relations because such a list would become rather
208         huge. It is a query object that can be queried, filtered, and iterated
209         as usual.  But list like methods like append() and remove() are
210         supported as well which allows code like:
211
212         somelocation.files.append(somefile)
213         '''
214
215         main = self.loc['main']
216         contrib = self.loc['contrib']
217         self.assertEqual('/srv/ftp-master.debian.org/ftp/pool/', main.path)
218         count = len(self.file.keys())
219         self.assertEqual(count, main.files.count())
220         self.assertEqual(0, contrib.files.count())
221         poolfile = main.files. \
222                 filter(PoolFile.filename.like('%/hello/hello%')). \
223                 order_by(PoolFile.filename)[0]
224         self.assertEqual('main/h/hello/hello_2.2-1.dsc', poolfile.filename)
225         self.assertEqual(main, poolfile.location)
226         # test get()
227         self.assertEqual(poolfile, \
228                 self.session.query(PoolFile).get(poolfile.file_id))
229         self.assertEqual(None, self.session.query(PoolFile).get(-1))
230         # test remove() and append()
231         main.files.remove(self.file['sl_3.03-16.dsc'])
232         contrib.files.append(self.file['sl_3.03-16.dsc'])
233         self.assertEqual(count - 1, main.files.count())
234         self.assertEqual(1, contrib.files.count())
235         # test fullpath
236         self.assertEqual('/srv/ftp-master.debian.org/ftp/pool/main/s/sl/sl_3.03-16.dsc', \
237             self.file['sl_3.03-16.dsc'].fullpath)
238         # test check_poolfile()
239         self.assertEqual((True, self.file['sl_3.03-16.dsc']), \
240             check_poolfile('main/s/sl/sl_3.03-16.dsc', 0, '', \
241                 contrib.location_id, self.session))
242         self.assertEqual((False, None), \
243             check_poolfile('foobar', 0, '', contrib.location_id, self.session))
244         self.assertEqual((False, self.file['sl_3.03-16.dsc']), \
245             check_poolfile('main/s/sl/sl_3.03-16.dsc', 42, '', \
246                 contrib.location_id, self.session))
247         self.assertEqual((False, self.file['sl_3.03-16.dsc']), \
248             check_poolfile('main/s/sl/sl_3.03-16.dsc', 0, 'deadbeef', \
249                 contrib.location_id, self.session))
250         # test get_poolfile_like_name()
251         self.assertEqual([self.file['sl_3.03-16.dsc']], \
252             get_poolfile_like_name('sl_3.03-16.dsc', self.session))
253         self.assertEqual([], get_poolfile_like_name('foobar', self.session))
254
255     def test_maintainers(self):
256         '''
257         tests relation between Maintainer and DBSource
258
259         TODO: add relations to changes_pending_source
260         '''
261
262         maintainer = self.maintainer['maintainer']
263         self.assertEqual(maintainer,
264             self.session.query(Maintainer).get(maintainer.maintainer_id))
265         uploader = self.maintainer['uploader']
266         self.assertEqual(uploader,
267             self.session.query(Maintainer).get(uploader.maintainer_id))
268         lazyguy = self.maintainer['lazyguy']
269         self.assertEqual(lazyguy,
270             self.session.query(Maintainer).get(lazyguy.maintainer_id))
271         self.assertEqual(3, len(maintainer.maintains_sources))
272         self.assertTrue(self.source['hello_2.2-2'] in maintainer.maintains_sources)
273         self.assertEqual(maintainer.changed_sources, [])
274         self.assertEqual(uploader.maintains_sources, [])
275         self.assertEqual(3, len(uploader.changed_sources))
276         self.assertTrue(self.source['sl_3.03-16'] in uploader.changed_sources)
277         self.assertEqual(lazyguy.maintains_sources, [])
278         self.assertEqual(lazyguy.changed_sources, [])
279
280     def get_source_in_suite_fail(self):
281         '''
282         This function throws the MultipleResultsFound exception because
283         get_source_in_suite is broken.
284
285         TODO: fix get_source_in_suite
286         '''
287
288         return get_source_in_suite('hello', 'sid', self.session)
289
290     def test_sources(self):
291         'test relation between DBSource and PoolFile or Suite'
292
293         # test PoolFile
294         self.assertEqual(self.file['hello_2.2-2.dsc'], self.source['hello_2.2-2'].poolfile)
295         self.assertEqual(self.source['hello_2.2-2'], self.file['hello_2.2-2.dsc'].source)
296         self.assertEqual(None, self.file['python2.6_2.6.6-8.dsc'].source)
297         # test Suite
298         squeeze = self.session.query(Suite). \
299             filter(Suite.sources.contains(self.source['sl_3.03-16'])). \
300             order_by(Suite.suite_name)[1]
301         self.assertEqual(self.suite['squeeze'], squeeze)
302         self.assertEqual(1, squeeze.sources.count())
303         self.assertEqual(self.source['sl_3.03-16'], squeeze.sources[0])
304         sl = self.session.query(DBSource). \
305             filter(DBSource.suites.contains(self.suite['squeeze'])).one()
306         self.assertEqual(self.source['sl_3.03-16'], sl)
307         self.assertEqual(2, len(sl.suites))
308         self.assertTrue(self.suite['sid'] in sl.suites)
309         # test get_source_in_suite()
310         self.assertRaises(MultipleResultsFound, self.get_source_in_suite_fail)
311         self.assertEqual(None, \
312             get_source_in_suite('hello', 'squeeze', self.session))
313         self.assertEqual(self.source['sl_3.03-16'], \
314             get_source_in_suite('sl', 'sid', self.session))
315         # test get_suites_source_in()
316         self.assertEqual([self.suite['sid']], \
317             get_suites_source_in('hello', self.session))
318         self.assertEqual(2, len(get_suites_source_in('sl', self.session)))
319         self.assertTrue(self.suite['squeeze'] in \
320             get_suites_source_in('sl', self.session))
321
322     def test_add_dsc_to_db(self):
323         'tests function add_dsc_to_db()'
324
325         pkg = Pkg()
326         pkg.dsc['source'] = 'hello'
327         pkg.dsc['version'] = '2.2-3'
328         pkg.dsc['maintainer'] = self.maintainer['maintainer'].name
329         pkg.changes['changed-by'] = self.maintainer['uploader'].name
330         pkg.changes['fingerprint'] = 'deadbeef'
331         pkg.changes['distribution'] = { 'sid': '' }
332         pkg.files['hello_2.2-3.dsc'] = { \
333             'component': 'main',
334             'location id': self.loc['main'].location_id,
335             'files id': self.file['hello_2.2-3.dsc'].file_id }
336         pkg.dsc_files = {}
337         upload = Upload(pkg)
338         (source, dsc_component, dsc_location_id, pfs) = \
339             add_dsc_to_db(upload, 'hello_2.2-3.dsc', self.session)
340         self.assertEqual('hello', source.source)
341         self.assertEqual('2.2-3', source.version)
342         self.assertEqual('sid', source.suites[0].suite_name)
343         self.assertEqual('main', dsc_component)
344         self.assertEqual(self.loc['main'].location_id, dsc_location_id)
345         self.assertEqual([], pfs)
346
347     def test_source_exists(self):
348         'test function source_exists()'
349
350         hello = self.source['hello_2.2-2']
351         self.assertTrue(source_exists(hello.source, hello.version, \
352             suites = ['sid'], session = self.session))
353         # binNMU
354         self.assertTrue(source_exists(hello.source, hello.version + '+b7', \
355             suites = ['sid'], session = self.session))
356         self.assertTrue(not source_exists(hello.source, hello.version, \
357             suites = ['lenny', 'squeeze'], session = self.session))
358         self.assertTrue(not source_exists(hello.source, hello.version, \
359             suites = ['lenny', 'sid'], session = self.session))
360         self.assertTrue(not source_exists(hello.source, hello.version, \
361             suites = ['sid', 'lenny'], session = self.session))
362         self.assertTrue(not source_exists(hello.source, '0815', \
363             suites = ['sid'], session = self.session))
364         # 'any' suite
365         self.assertTrue(source_exists(hello.source, hello.version, \
366             session = self.session))
367
368     def test_package_to_suite(self):
369         'test function package_to_suite()'
370
371         pkg = Pkg()
372         pkg.changes = { 'distribution': {} }
373         upload = Upload(pkg)
374         self.assertTrue(not package_to_suite(upload, 'sid', self.session))
375         pkg.changes['distribution'] = { 'sid': '' }
376         pkg.changes['architecture'] = { 'source': '' }
377         self.assertTrue(package_to_suite(upload, 'sid', self.session))
378         pkg.changes['architecture'] = {}
379         pkg.changes['source'] = self.source['hello_2.2-2'].source
380         pkg.changes['version'] = self.source['hello_2.2-2'].version
381         self.assertTrue(not package_to_suite(upload, 'sid', self.session))
382         pkg.changes['version'] = '42'
383         self.assertTrue(package_to_suite(upload, 'sid', self.session))
384         pkg.changes['source'] = 'foobar'
385         pkg.changes['version'] = self.source['hello_2.2-2'].version
386         self.assertTrue(package_to_suite(upload, 'sid', self.session))
387         pkg.changes['distribution'] = { 'lenny': '' }
388         self.assertTrue(package_to_suite(upload, 'lenny', self.session))
389
390     def test_get_newest_source(self):
391         'test function get_newest_source()'
392
393         import daklib.queue
394         daklib.queue.dm_suites = ['sid']
395         self.assertEqual(self.source['hello_2.2-2'], get_newest_source('hello', self.session))
396         self.assertEqual(None, get_newest_source('foobar', self.session))
397
398     def test_get_suite_version(self):
399         'test function get_suite_version()'
400
401         result = get_suite_version('hello', self.session)
402         self.assertEqual(2, len(result))
403         self.assertTrue(('sid', '2.2-1') in result)
404         self.assertTrue(('sid', '2.2-2') in result)
405         result = get_suite_version('sl', self.session)
406         self.assertEqual(2, len(result))
407         self.assertTrue(('squeeze', '3.03-16') in result)
408         self.assertTrue(('sid', '3.03-16') in result)
409
410     def test_binaries(self):
411         '''
412         tests class DBBinary; TODO: test relation with Architecture, Maintainer,
413         PoolFile, and Fingerprint
414         '''
415
416         # test Suite relation
417         self.assertEqual(2, self.suite['sid'].binaries.count())
418         self.assertTrue(self.binary['hello_2.2-1_i386'] in \
419             self.suite['sid'].binaries.all())
420         self.assertEqual(0, self.suite['lenny'].binaries.count())
421         # test DBSource relation
422         self.assertEqual(2, len(self.source['hello_2.2-1'].binaries))
423         self.assertTrue(self.binary['hello_2.2-1_i386'] in \
424             self.source['hello_2.2-1'].binaries)
425         self.assertEqual(0, len(self.source['hello_2.2-2'].binaries))
426         # test get_suites_binary_in()
427         self.assertEqual(2, len(get_suites_binary_in('hello', self.session)))
428         self.assertTrue(self.suite['sid'] in \
429             get_suites_binary_in('hello', self.session))
430         self.assertEqual(2, len(get_suites_binary_in('gnome-hello', self.session)))
431         self.assertTrue(self.suite['squeeze'] in \
432             get_suites_binary_in('gnome-hello', self.session))
433         self.assertEqual(0, len(get_suites_binary_in('sl', self.session)))
434
435     def test_add_deb_to_db(self):
436         'tests function add_deb_to_db()'
437
438         pkg = Pkg()
439         pkg.changes['fingerprint'] = 'deadbeef'
440         pkg.changes['distribution'] = { 'sid': '' }
441         pkg.files['hello_2.2-2_i386.deb'] = { \
442             'package': 'hello',
443             'version': '2.2-2',
444             'maintainer': self.maintainer['maintainer'].name,
445             'architecture': 'i386',
446             'dbtype': 'deb',
447             'pool name': 'main/h/hello/',
448             'location id': self.loc['main'].location_id,
449             'source package': 'hello',
450             'source version': '2.2-2',
451             'size': 0,
452             'md5sum': 'deadbeef',
453             'sha1sum': 'deadbeef',
454             'sha256sum': 'deadbeef'}
455         upload = Upload(pkg)
456         poolfile = add_deb_to_db(upload, 'hello_2.2-2_i386.deb', self.session)
457         self.session.refresh(poolfile)
458         self.session.refresh(poolfile.binary)
459         self.assertEqual('main/h/hello/hello_2.2-2_i386.deb', poolfile.filename)
460         self.assertEqual('hello', poolfile.binary.package)
461         self.assertEqual('2.2-2', poolfile.binary.version)
462         self.assertEqual(['sid'], poolfile.binary.suites)
463         self.assertEqual('Mr. Maintainer', poolfile.binary.maintainer.name)
464         self.assertEqual('i386', poolfile.binary.architecture.arch_string)
465         self.assertEqual('deb', poolfile.binary.binarytype)
466         self.assertEqual(self.loc['main'], poolfile.location)
467         self.assertEqual(self.source['hello_2.2-2'], poolfile.binary.source)
468         self.assertEqual(0, poolfile.filesize)
469         self.assertEqual('deadbeef', poolfile.md5sum)
470         self.assertEqual('deadbeef', poolfile.sha1sum)
471         self.assertEqual('deadbeef', poolfile.sha256sum)
472
473     def test_get_source_by_package_and_suite(self):
474         'test get_source_by_package_and_suite()'
475
476         query = get_source_by_package_and_suite('hello', 'sid', self.session)
477         self.assertEqual(self.source['hello_2.2-1'], query.one())
478         query = get_source_by_package_and_suite('gnome-hello', 'squeeze', self.session)
479         self.assertEqual(self.source['hello_2.2-1'], query.one())
480         query = get_source_by_package_and_suite('hello', 'hamm', self.session)
481         self.assertEqual(0, query.count())
482         query = get_source_by_package_and_suite('foobar', 'squeeze', self.session)
483         self.assertEqual(0, query.count())
484
485 if __name__ == '__main__':
486     unittest.main()