]> git.decadent.org.uk Git - dak.git/blob - tests/dbtest_packages.py
Merge branch 'master' into merge
[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
9 from daklib.queue_install import package_to_suite
10 from daklib.queue import get_newest_source, get_suite_version
11
12 from sqlalchemy.orm.exc import MultipleResultsFound
13 import unittest
14
15 class Pkg():
16     'fake package class used for testing'
17
18     def __init__(self):
19         self.dsc = {}
20         self.files = {}
21         self.changes = {}
22
23 class Upload():
24     'fake Upload class used for testing'
25
26     def __init__(self, pkg):
27         self.pkg = pkg
28
29 class PackageTestCase(DBDakTestCase):
30     """
31     PackageTestCase checks the handling of source and binary packages in dak's
32     database.
33     """
34
35     def setup_suites(self):
36         "setup a hash of Suite objects in self.suite"
37
38         if 'suite' in self.__dict__:
39             return
40         self.suite = {}
41         for suite_name in ('lenny', 'squeeze', 'sid'):
42             self.suite[suite_name] = Suite(suite_name = suite_name, version = '-')
43         self.session.add_all(self.suite.values())
44
45     def setup_architectures(self):
46         "setup Architecture objects in self.arch and connect to suites"
47
48         if 'arch' in self.__dict__:
49             return
50         self.setup_suites()
51         self.arch = {}
52         for arch_string in ('source', 'all', 'i386', 'amd64', 'kfreebsd-i386'):
53             self.arch[arch_string] = Architecture(arch_string)
54             if arch_string != 'kfreebsd-i386':
55                 self.arch[arch_string].suites = self.suite.values()
56             else:
57                 self.arch[arch_string].suites = [self.suite['squeeze'], self.suite['sid']]
58         # hard code ids for source and all
59         self.arch['source'].arch_id = 1
60         self.arch['all'].arch_id = 2
61         self.session.add_all(self.arch.values())
62
63     def setup_locations(self):
64         'create some Location objects, TODO: add component'
65
66         if 'loc' in self.__dict__:
67             return
68         self.loc = {}
69         self.loc['main'] = Location(path = \
70             '/srv/ftp-master.debian.org/ftp/pool/')
71         self.loc['contrib'] = Location(path = \
72             '/srv/ftp-master.debian.org/ftp/pool/')
73         self.session.add_all(self.loc.values())
74
75     def setup_poolfiles(self):
76         'create some PoolFile objects'
77
78         if 'file' in self.__dict__:
79             return
80         self.setup_locations()
81         self.file = {}
82         self.file['hello_new'] = PoolFile(filename = 'main/h/hello/hello_2.2-3.dsc', \
83             location = self.loc['main'], filesize = 0, md5sum = '')
84         self.file['hello'] = PoolFile(filename = 'main/h/hello/hello_2.2-2.dsc', \
85             location = self.loc['main'], filesize = 0, md5sum = '')
86         self.file['hello_old'] = PoolFile(filename = 'main/h/hello/hello_2.2-1.dsc', \
87             location = self.loc['main'], filesize = 0, md5sum = '')
88         self.file['sl'] = PoolFile(filename = 'main/s/sl/sl_3.03-16.dsc', \
89             location = self.loc['main'], filesize = 0, md5sum = '')
90         self.file['python'] = PoolFile( \
91             filename = 'main/p/python2.6/python2.6_2.6.6-8.dsc', \
92             location = self.loc['main'], filesize = 0, md5sum = '')
93         self.session.add_all(self.file.values())
94
95     def setup_maintainers(self):
96         'create some Maintainer objects'
97
98         if 'maintainer' in self.__dict__:
99             return
100         self.maintainer = {}
101         self.maintainer['maintainer'] = Maintainer(name = 'Mr. Maintainer')
102         self.maintainer['uploader'] = Maintainer(name = 'Mrs. Uploader')
103         self.maintainer['lazyguy'] = Maintainer(name = 'Lazy Guy')
104         self.session.add_all(self.maintainer.values())
105
106     def setup_sources(self):
107         'create a DBSource object; but it cannot be stored in the DB yet'
108
109         if 'source' in self.__dict__:
110             return
111         self.setup_maintainers()
112         self.source = {}
113         self.source['hello'] = DBSource(source = 'hello', version = '2.2-2', \
114             maintainer = self.maintainer['maintainer'], \
115             changedby = self.maintainer['uploader'], \
116             poolfile = self.file['hello'], install_date = self.now())
117         self.source['hello'].suites.append(self.suite['sid'])
118         self.source['hello_old'] = DBSource(source = 'hello', version = '2.2-1', \
119             maintainer = self.maintainer['maintainer'], \
120             changedby = self.maintainer['uploader'], \
121             poolfile = self.file['hello_old'], install_date = self.now())
122         self.source['hello_old'].suites.append(self.suite['sid'])
123         self.source['sl'] = DBSource(source = 'sl', version = '3.03-16', \
124             maintainer = self.maintainer['maintainer'], \
125             changedby = self.maintainer['uploader'], \
126             poolfile = self.file['sl'], install_date = self.now())
127         self.source['sl'].suites.append(self.suite['squeeze'])
128         self.source['sl'].suites.append(self.suite['sid'])
129         self.session.add_all(self.source.values())
130
131     def setUp(self):
132         super(PackageTestCase, self).setUp()
133         self.setup_architectures()
134         self.setup_poolfiles()
135         self.setup_sources()
136         # flush to make sure that the setup is correct
137         self.session.flush()
138
139     def test_suite_architecture(self):
140         # check the id for architectures source and all
141         self.assertEqual(1, self.arch['source'].arch_id)
142         self.assertEqual(2, self.arch['all'].arch_id)
143         # check the many to many relation between Suite and Architecture
144         self.assertEqual('source', self.suite['lenny'].architectures[0])
145         self.assertEqual(4, len(self.suite['lenny'].architectures))
146         self.assertEqual(3, len(self.arch['i386'].suites))
147         # check the function get_suite_architectures()
148         architectures = get_suite_architectures('lenny', session = self.session)
149         self.assertEqual(4, len(architectures))
150         self.assertTrue(self.arch['source'] in architectures)
151         self.assertTrue(self.arch['all'] in architectures)
152         self.assertTrue(self.arch['kfreebsd-i386'] not in architectures)
153         architectures = get_suite_architectures('sid', session = self.session)
154         self.assertEqual(5, len(architectures))
155         self.assertTrue(self.arch['kfreebsd-i386'] in architectures)
156         architectures = get_suite_architectures('lenny', skipsrc = True, session = self.session)
157         self.assertEqual(3, len(architectures))
158         self.assertTrue(self.arch['source'] not in architectures)
159         architectures = get_suite_architectures('lenny', skipall = True, session = self.session)
160         self.assertEqual(3, len(architectures))
161         self.assertTrue(self.arch['all'] not in architectures)
162         # check the function get_architecture_suites()
163         suites = get_architecture_suites('i386', self.session)
164         self.assertEqual(3, len(suites))
165         self.assertTrue(self.suite['lenny'] in suites)
166         suites = get_architecture_suites('kfreebsd-i386', self.session)
167         self.assertEqual(2, len(suites))
168         self.assertTrue(self.suite['lenny'] not in suites)
169
170     def test_poolfiles(self):
171         '''
172         Test the relation of the classes PoolFile and Location.
173
174         The code needs some explaination. The property Location.files is not a
175         list as in other relations because such a list would become rather
176         huge. It is a query object that can be queried, filtered, and iterated
177         as usual.  But list like methods like append() and remove() are
178         supported as well which allows code like:
179
180         somelocation.files.append(somefile)
181         '''
182
183         main = self.loc['main']
184         contrib = self.loc['contrib']
185         self.assertEqual('/srv/ftp-master.debian.org/ftp/pool/', main.path)
186         self.assertEqual(5, main.files.count())
187         self.assertEqual(0, contrib.files.count())
188         poolfile = main.files. \
189                 filter(PoolFile.filename.like('%/hello/hello%')). \
190                 order_by(PoolFile.filename)[1]
191         self.assertEqual('main/h/hello/hello_2.2-2.dsc', poolfile.filename)
192         self.assertEqual(main, poolfile.location)
193         # test get()
194         self.assertEqual(poolfile, \
195                 self.session.query(PoolFile).get(poolfile.file_id))
196         self.assertEqual(None, self.session.query(PoolFile).get(-1))
197         # test remove() and append()
198         main.files.remove(self.file['sl'])
199         contrib.files.append(self.file['sl'])
200         self.assertEqual(4, main.files.count())
201         self.assertEqual(1, contrib.files.count())
202         # test fullpath
203         self.assertEqual('/srv/ftp-master.debian.org/ftp/pool/main/s/sl/sl_3.03-16.dsc', \
204             self.file['sl'].fullpath)
205         # test check_poolfile()
206         self.assertEqual((True, self.file['sl']), \
207             check_poolfile('main/s/sl/sl_3.03-16.dsc', 0, '', \
208                 contrib.location_id, self.session))
209         self.assertEqual((False, None), \
210             check_poolfile('foobar', 0, '', contrib.location_id, self.session))
211         self.assertEqual((False, self.file['sl']), \
212             check_poolfile('main/s/sl/sl_3.03-16.dsc', 42, '', \
213                 contrib.location_id, self.session))
214         self.assertEqual((False, self.file['sl']), \
215             check_poolfile('main/s/sl/sl_3.03-16.dsc', 0, 'deadbeef', \
216                 contrib.location_id, self.session))
217         # test get_poolfile_like_name()
218         self.assertEqual([self.file['sl']], \
219             get_poolfile_like_name('sl_3.03-16.dsc', self.session))
220         self.assertEqual([], get_poolfile_like_name('foobar', self.session))
221
222     def test_maintainers(self):
223         '''
224         tests relation between Maintainer and DBSource
225
226         TODO: add relations to changes_pending_source
227         '''
228
229         maintainer = self.maintainer['maintainer']
230         self.assertEqual(maintainer,
231             self.session.query(Maintainer).get(maintainer.maintainer_id))
232         uploader = self.maintainer['uploader']
233         self.assertEqual(uploader,
234             self.session.query(Maintainer).get(uploader.maintainer_id))
235         lazyguy = self.maintainer['lazyguy']
236         self.assertEqual(lazyguy,
237             self.session.query(Maintainer).get(lazyguy.maintainer_id))
238         self.assertEqual(3, len(maintainer.maintains_sources))
239         self.assertTrue(self.source['hello'] in maintainer.maintains_sources)
240         self.assertEqual(maintainer.changed_sources, [])
241         self.assertEqual(uploader.maintains_sources, [])
242         self.assertEqual(3, len(uploader.changed_sources))
243         self.assertTrue(self.source['sl'] in uploader.changed_sources)
244         self.assertEqual(lazyguy.maintains_sources, [])
245         self.assertEqual(lazyguy.changed_sources, [])
246
247     def get_source_in_suite_fail(self):
248         '''
249         This function throws the MultipleResultsFound exception because
250         get_source_in_suite is broken.
251
252         TODO: fix get_source_in_suite
253         '''
254
255         return get_source_in_suite('hello', 'sid', self.session)
256
257     def test_sources(self):
258         'test relation between DBSource and PoolFile or Suite'
259
260         # test PoolFile
261         self.assertEqual(self.file['hello'], self.source['hello'].poolfile)
262         self.assertEqual(self.source['hello'], self.file['hello'].source)
263         self.assertEqual(None, self.file['python'].source)
264         # test Suite
265         squeeze = self.session.query(Suite). \
266             filter(Suite.sources.contains(self.source['sl'])). \
267             order_by(Suite.suite_name)[1]
268         self.assertEqual(self.suite['squeeze'], squeeze)
269         self.assertEqual(1, len(squeeze.sources))
270         self.assertEqual(self.source['sl'], squeeze.sources[0])
271         sl = self.session.query(DBSource). \
272             filter(DBSource.suites.contains(self.suite['squeeze'])).one()
273         self.assertEqual(self.source['sl'], sl)
274         self.assertEqual(2, len(sl.suites))
275         self.assertTrue(self.suite['sid'] in sl.suites)
276         # test get_source_in_suite()
277         self.assertRaises(MultipleResultsFound, self.get_source_in_suite_fail)
278         self.assertEqual(None, \
279             get_source_in_suite('hello', 'squeeze', self.session))
280         self.assertEqual(self.source['sl'], \
281             get_source_in_suite('sl', 'sid', self.session))
282         # test get_suites_source_in()
283         self.assertEqual([self.suite['sid']], \
284             get_suites_source_in('hello', self.session))
285         self.assertEqual(2, len(get_suites_source_in('sl', self.session)))
286         self.assertTrue(self.suite['squeeze'] in \
287             get_suites_source_in('sl', self.session))
288
289     def test_upload(self):
290         'tests function add_dsc_to_db()'
291
292         pkg = Pkg()
293         pkg.dsc['source'] = 'hello'
294         pkg.dsc['version'] = '2.2-3'
295         pkg.dsc['maintainer'] = self.maintainer['maintainer'].name
296         pkg.changes['changed-by'] = self.maintainer['uploader'].name
297         pkg.changes['fingerprint'] = 'deadbeef'
298         pkg.changes['distribution'] = { 'sid': '' }
299         pkg.files['hello_2.2-3.dsc'] = { \
300             'component': 'main',
301             'location id': self.loc['main'].component_id,
302             'files id': self.file['hello_new'].file_id }
303         pkg.dsc_files = {}
304         upload = Upload(pkg)
305         (source, dsc_component, dsc_location_id, pfs) = \
306             add_dsc_to_db(upload, 'hello_2.2-3.dsc', self.session)
307         self.assertEqual('hello', source.source)
308         self.assertEqual('2.2-3', source.version)
309         self.assertEqual('sid', source.suites[0].suite_name)
310         self.assertEqual('main', dsc_component)
311         # no dsc files defined above
312         self.assertEqual(None, dsc_location_id)
313         self.assertEqual([], pfs)
314
315     def test_source_exists(self):
316         'test function source_exists()'
317
318         hello = self.source['hello']
319         self.assertTrue(source_exists(hello.source, hello.version, \
320             suites = ['sid'], session = self.session))
321         # binNMU
322         self.assertTrue(source_exists(hello.source, hello.version + '+b7', \
323             suites = ['sid'], session = self.session))
324         self.assertTrue(not source_exists(hello.source, hello.version, \
325             suites = ['lenny', 'squeeze'], session = self.session))
326         self.assertTrue(not source_exists(hello.source, hello.version, \
327             suites = ['lenny', 'sid'], session = self.session))
328         self.assertTrue(not source_exists(hello.source, hello.version, \
329             suites = ['sid', 'lenny'], session = self.session))
330         self.assertTrue(not source_exists(hello.source, '0815', \
331             suites = ['sid'], session = self.session))
332         # 'any' suite
333         self.assertTrue(source_exists(hello.source, hello.version, \
334             session = self.session))
335
336     def test_package_to_suite(self):
337         'test function package_to_suite()'
338
339         pkg = Pkg()
340         pkg.changes = { 'distribution': {} }
341         upload = Upload(pkg)
342         self.assertTrue(not package_to_suite(upload, 'sid', self.session))
343         pkg.changes['distribution'] = { 'sid': '' }
344         pkg.changes['architecture'] = { 'source': '' }
345         self.assertTrue(package_to_suite(upload, 'sid', self.session))
346         pkg.changes['architecture'] = {}
347         pkg.changes['source'] = self.source['hello'].source
348         pkg.changes['version'] = self.source['hello'].version
349         self.assertTrue(not package_to_suite(upload, 'sid', self.session))
350         pkg.changes['version'] = '42'
351         self.assertTrue(package_to_suite(upload, 'sid', self.session))
352         pkg.changes['source'] = 'foobar'
353         pkg.changes['version'] = self.source['hello'].version
354         self.assertTrue(package_to_suite(upload, 'sid', self.session))
355         pkg.changes['distribution'] = { 'lenny': '' }
356         self.assertTrue(package_to_suite(upload, 'lenny', self.session))
357
358     def test_get_newest_source(self):
359         'test function get_newest_source()'
360
361         import daklib.queue
362         daklib.queue.dm_suites = ['sid']
363         self.assertEqual(self.source['hello'], get_newest_source('hello', self.session))
364         self.assertEqual(None, get_newest_source('foobar', self.session))
365
366     def test_get_suite_version(self):
367         'test function get_suite_version()'
368
369         result = get_suite_version('hello', self.session)
370         self.assertEqual(2, len(result))
371         self.assertTrue(('sid', '2.2-1') in result)
372         self.assertTrue(('sid', '2.2-2') in result)
373         result = get_suite_version('sl', self.session)
374         self.assertEqual(2, len(result))
375         self.assertTrue(('squeeze', '3.03-16') in result)
376         self.assertTrue(('sid', '3.03-16') in result)
377
378 if __name__ == '__main__':
379     unittest.main()