]> git.decadent.org.uk Git - dak.git/blob - tests/dbtest_packages.py
Add workaround for SQLAlchemy 0.5.
[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
13 from sqlalchemy.orm.exc import MultipleResultsFound
14 import unittest
15
16 class Pkg():
17     'fake package class used for testing'
18
19     def __init__(self):
20         self.dsc = {}
21         self.files = {}
22         self.changes = {}
23
24 class Upload():
25     'fake Upload class used for testing'
26
27     def __init__(self, pkg):
28         self.pkg = pkg
29
30 class PackageTestCase(DBDakTestCase):
31     """
32     PackageTestCase checks the handling of source and binary packages in dak's
33     database.
34     """
35
36     def setup_suites(self):
37         "setup a hash of Suite objects in self.suite"
38
39         if 'suite' in self.__dict__:
40             return
41         self.suite = {}
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())
45
46     def setup_architectures(self):
47         "setup Architecture objects in self.arch and connect to suites"
48
49         if 'arch' in self.__dict__:
50             return
51         self.setup_suites()
52         self.arch = {}
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()
57             else:
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())
63
64     def setup_locations(self):
65         'create some Location objects, TODO: add component'
66
67         if 'loc' in self.__dict__:
68             return
69         self.loc = {}
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())
75
76     def setup_poolfiles(self):
77         'create some PoolFile objects'
78
79         if 'file' in self.__dict__:
80             return
81         self.setup_locations()
82         self.file = {}
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())
101
102     def setup_maintainers(self):
103         'create some Maintainer objects'
104
105         if 'maintainer' in self.__dict__:
106             return
107         self.maintainer = {}
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())
112
113     def setup_sources(self):
114         'create DBSource objects'
115
116         if 'source' in self.__dict__:
117             return
118         self.setup_maintainers()
119         self.setup_suites()
120         self.setup_poolfiles()
121         self.source = {}
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())
139
140     def setup_binaries(self):
141         'create DBBinary objects'
142
143         if 'binary' in self.__dict__:
144             return
145         self.setup_sources()
146         self.setup_architectures()
147         self.binary = {}
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())
163
164     def setUp(self):
165         super(PackageTestCase, self).setUp()
166         self.setup_binaries()
167         # flush to make sure that the setup is correct
168         self.session.flush()
169
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)
200
201     def test_poolfiles(self):
202         '''
203         Test the relation of the classes PoolFile and Location.
204
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:
210
211         somelocation.files.append(somefile)
212         '''
213
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)
225         # test get()
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())
234         # test fullpath
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))
253
254     def test_maintainers(self):
255         '''
256         tests relation between Maintainer and DBSource
257
258         TODO: add relations to changes_pending_source
259         '''
260
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, [])
278
279     def get_source_in_suite_fail(self):
280         '''
281         This function throws the MultipleResultsFound exception because
282         get_source_in_suite is broken.
283
284         TODO: fix get_source_in_suite
285         '''
286
287         return get_source_in_suite('hello', 'sid', self.session)
288
289     def test_sources(self):
290         'test relation between DBSource and PoolFile or Suite'
291
292         # test PoolFile
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)
296         # test Suite
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))
320
321     def test_add_dsc_to_db(self):
322         'tests function add_dsc_to_db()'
323
324         pkg = Pkg()
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'] = { \
332             'component': 'main',
333             'location id': self.loc['main'].location_id,
334             'files id': self.file['hello_2.2-3.dsc'].file_id }
335         pkg.dsc_files = {}
336         upload = Upload(pkg)
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)
345
346     def test_source_exists(self):
347         'test function source_exists()'
348
349         hello = self.source['hello_2.2-2']
350         self.assertTrue(source_exists(hello.source, hello.version, \
351             suites = ['sid'], session = self.session))
352         # binNMU
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))
363         # 'any' suite
364         self.assertTrue(source_exists(hello.source, hello.version, \
365             session = self.session))
366
367     def test_package_to_suite(self):
368         'test function package_to_suite()'
369
370         pkg = Pkg()
371         pkg.changes = { 'distribution': {} }
372         upload = Upload(pkg)
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))
388
389     def test_get_newest_source(self):
390         'test function get_newest_source()'
391
392         import daklib.queue
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))
396
397     def test_get_suite_version(self):
398         'test function get_suite_version()'
399
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)
408
409     def test_binaries(self):
410         '''
411         tests class DBBinary; TODO: test relation with Architecture, Maintainer,
412         PoolFile, and Fingerprint
413         '''
414
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)))
433
434     def test_add_deb_to_db(self):
435         'tests function add_deb_to_db()'
436
437         pkg = Pkg()
438         pkg.changes['fingerprint'] = 'deadbeef'
439         pkg.changes['distribution'] = { 'sid': '' }
440         pkg.files['hello_2.2-2_i386.deb'] = { \
441             'package': 'hello',
442             'version': '2.2-2',
443             'maintainer': self.maintainer['maintainer'].name,
444             'architecture': 'i386',
445             'dbtype': 'deb',
446             'pool name': 'main/h/hello/',
447             'location id': self.loc['main'].location_id,
448             'source package': 'hello',
449             'source version': '2.2-2',
450             'size': 0,
451             'md5sum': 'deadbeef',
452             'sha1sum': 'deadbeef',
453             'sha256sum': 'deadbeef'}
454         upload = Upload(pkg)
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)
471
472 if __name__ == '__main__':
473     unittest.main()