3 from db_test import DBDakTestCase
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
12 from sqlalchemy.orm.exc import MultipleResultsFound
16 'fake package class used for testing'
24 'fake Upload class used for testing'
26 def __init__(self, pkg):
29 class PackageTestCase(DBDakTestCase):
31 PackageTestCase checks the handling of source and binary packages in dak's
35 def setup_suites(self):
36 "setup a hash of Suite objects in self.suite"
38 if 'suite' in self.__dict__:
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())
45 def setup_architectures(self):
46 "setup Architecture objects in self.arch and connect to suites"
48 if 'arch' in self.__dict__:
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()
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())
64 super(PackageTestCase, self).setUp()
65 self.setup_architectures()
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)
99 def setup_locations(self):
100 'create some Location objects, TODO: add component'
102 if 'loc' in self.__dict__:
105 self.loc['main'] = Location(path = \
106 '/srv/ftp-master.debian.org/ftp/pool/')
107 self.session.add(self.loc['main'])
109 def setup_poolfiles(self):
110 'create some PoolFile objects'
112 if 'file' in self.__dict__:
114 self.setup_locations()
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())
127 def test_poolfiles(self):
129 Test the relation of the classes PoolFile and Location.
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:
137 somelocation.files.append(somefile)
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)
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())
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))
185 def setup_maintainers(self):
186 'create some Maintainer objects'
188 if 'maintainer' in self.__dict__:
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())
196 def setup_sources(self):
197 'create a DBSource object; but it cannot be stored in the DB yet'
199 if 'source' in self.__dict__:
201 self.setup_maintainers()
202 self.setup_poolfiles()
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())
223 def test_maintainers(self):
225 tests relation between Maintainer and DBSource
227 TODO: add relations to changes_pending_source
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, [])
250 def get_source_in_suite_fail(self):
252 This function throws the MultipleResultsFound exception because
253 get_source_in_suite is broken.
255 TODO: fix get_source_in_suite
258 return get_source_in_suite('hello', 'sid', self.session)
260 def test_sources(self):
261 'test relation between DBSource and PoolFile or Suite'
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)
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))
293 def test_upload(self):
294 'tests function add_dsc_to_db()'
296 self.setup_maintainers()
297 self.setup_locations()
298 self.setup_poolfiles()
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': '' }
307 self.session.refresh(self.file['hello'])
308 pkg.files['hello_2.2-2.dsc'] = { \
310 'location id': self.loc['main'].component_id,
311 'files id': self.file['hello'].file_id }
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)
325 def test_source_exists(self):
326 'test function source_exists()'
330 hello = self.source['hello']
331 self.assertTrue(source_exists(hello.source, hello.version, \
332 suites = ['sid'], session = self.session))
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))
345 self.assertTrue(source_exists(hello.source, hello.version, \
346 session = self.session))
348 def test_package_to_suite(self):
349 'test function package_to_suite()'
353 pkg.changes = { 'distribution': {} }
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))
371 def test_get_newest_source(self):
372 'test function get_newest_source()'
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))
380 def test_get_suite_version(self):
381 'test function get_suite_version()'
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)
393 if __name__ == '__main__':