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