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