Catch cyclic dependency graphs and give a nice error instead of running out of stack
[zeroinstall/zeroinstall-mseaborn.git] / tests / testautopolicy.py
blob9ece847949b2ca3079cd33ce6e5fd45f7550bb6b
1 #!/usr/bin/env python2.4
2 from basetest import BaseTest
3 import sys, tempfile, os, logging
4 from StringIO import StringIO
5 import unittest
7 sys.path.insert(0, '..')
9 from zeroinstall import NeedDownload
10 from zeroinstall.injector import model, autopolicy, gpg, iface_cache, namespaces, reader
11 from zeroinstall.support import basedir
12 import data
14 foo_iface_uri = 'http://foo'
16 logger = logging.getLogger()
18 class TestAutoPolicy(BaseTest):
19 def setUp(self):
20 BaseTest.setUp(self)
21 stream = tempfile.TemporaryFile()
22 stream.write(data.thomas_key)
23 stream.seek(0)
24 gpg.import_key(stream)
26 def cache_iface(self, name, data):
27 cached_ifaces = basedir.save_cache_path('0install.net',
28 'interfaces')
30 f = file(os.path.join(cached_ifaces, model.escape(name)), 'w')
31 f.write(data)
32 f.close()
34 def testNoNeedDl(self):
35 policy = autopolicy.AutoPolicy(foo_iface_uri,
36 download_only = False)
37 policy.freshness = 0
38 assert policy.need_download()
40 policy = autopolicy.AutoPolicy(os.path.abspath('Foo.xml'),
41 download_only = False)
42 assert not policy.need_download()
44 def testUnknownAlg(self):
45 self.cache_iface(foo_iface_uri,
46 """<?xml version="1.0" ?>
47 <interface
48 uri="%s"
49 xmlns="http://zero-install.sourceforge.net/2004/injector/interface">
50 <name>Foo</name>
51 <summary>Foo</summary>
52 <description>Foo</description>
53 <implementation id='unknown=123' version='1.0'>
54 <archive href='http://foo/foo.tgz' size='100'/>
55 </implementation>
56 </interface>""" % foo_iface_uri)
57 policy = autopolicy.AutoPolicy(foo_iface_uri,
58 download_only = False)
59 policy.freshness = 0
60 try:
61 assert policy.need_download()
62 policy.execute([])
63 except model.SafeException, ex:
64 assert 'Unknown digest algorithm' in str(ex)
66 def testDownload(self):
67 tmp = tempfile.NamedTemporaryFile()
68 tmp.write(
69 """<?xml version="1.0" ?>
70 <interface
71 main='ThisBetterNotExist'
72 xmlns="http://zero-install.sourceforge.net/2004/injector/interface">
73 <name>Foo</name>
74 <summary>Foo</summary>
75 <description>Foo</description>
76 <implementation version='1.0' id='/bin'/>
77 </interface>""")
78 tmp.flush()
79 policy = autopolicy.AutoPolicy(tmp.name, False, False)
80 try:
81 policy.download_and_execute(['Hello'])
82 assert 0
83 except model.SafeException, ex:
84 assert "ThisBetterNotExist" in str(ex)
85 tmp.close()
87 def testNoMain(self):
88 tmp = tempfile.NamedTemporaryFile()
89 tmp.write(
90 """<?xml version="1.0" ?>
91 <interface
92 xmlns="http://zero-install.sourceforge.net/2004/injector/interface">
93 <name>Foo</name>
94 <summary>Foo</summary>
95 <description>Foo</description>
96 <implementation version='1.0' id='/bin'/>
97 </interface>""")
98 tmp.flush()
99 policy = autopolicy.AutoPolicy(tmp.name, False, False)
100 try:
101 policy.download_and_execute(['Hello'])
102 assert 0
103 except model.SafeException, ex:
104 assert "library" in str(ex)
105 tmp.close()
107 def testNeedDL(self):
108 self.cache_iface(foo_iface_uri,
109 """<?xml version="1.0" ?>
110 <interface last-modified="0"
111 uri="%s"
112 main='ThisBetterNotExist'
113 xmlns="http://zero-install.sourceforge.net/2004/injector/interface">
114 <name>Foo</name>
115 <summary>Foo</summary>
116 <description>Foo</description>
117 <implementation version='1.0' id='sha1=123'>
118 <archive href='http://foo/foo.tgz' size='100'/>
119 </implementation>
120 </interface>""" % foo_iface_uri)
121 policy = autopolicy.AutoPolicy(foo_iface_uri, False, True)
122 policy.freshness = 0
123 policy.network_use = model.network_full
124 policy.recalculate()
125 assert policy.need_download()
126 assert policy.ready
127 try:
128 policy.execute([], main = 'NOTHING')
129 assert False
130 except NeedDownload, ex:
131 pass
133 def testBinding(self):
134 local_impl = os.path.dirname(os.path.abspath(__file__))
135 tmp = tempfile.NamedTemporaryFile()
136 tmp.write(
137 """<?xml version="1.0" ?>
138 <interface
139 main='testautopolicy.py'
140 xmlns="http://zero-install.sourceforge.net/2004/injector/interface">
141 <name>Bar</name>
142 <summary>Bar</summary>
143 <description>Bar</description>
144 <group>
145 <requires interface='%s'>
146 <environment name='FOO_PATH' insert='.'/>
147 <environment name='BAR_PATH' insert='.' default='/a:/b'/>
148 <environment name='XDG_DATA_DIRS' insert='.'/>
149 </requires>
150 <environment name='SELF_GROUP' insert='group' mode='replace'/>
151 <implementation version='1.0' id='%s'>
152 <environment name='SELF_IMPL' insert='impl' mode='replace'/>
153 </implementation>
154 </group>
155 </interface>""" % (foo_iface_uri, local_impl))
156 tmp.flush()
157 self.cache_iface(foo_iface_uri,
158 """<?xml version="1.0" ?>
159 <interface last-modified="0"
160 uri="%s"
161 xmlns="http://zero-install.sourceforge.net/2004/injector/interface">
162 <name>Foo</name>
163 <summary>Foo</summary>
164 <description>Foo</description>
165 <implementation version='1.0' id='sha1=123'/>
166 </interface>""" % foo_iface_uri)
167 cached_impl = basedir.save_cache_path('0install.net',
168 'implementations',
169 'sha1=123')
170 policy = autopolicy.AutoPolicy(tmp.name, False,
171 dry_run = True)
172 policy.network_use = model.network_offline
173 self.changes_environ()
174 os.environ['FOO_PATH'] = "old"
175 old, sys.stdout = sys.stdout, StringIO()
176 try:
177 policy.download_and_execute(['Hello'])
178 finally:
179 sys.stdout = old
180 self.assertEquals(cached_impl + '/.:old',
181 os.environ['FOO_PATH'])
182 self.assertEquals(cached_impl + '/.:/a:/b',
183 os.environ['BAR_PATH'])
185 self.assertEquals(os.path.join(local_impl, 'group'), os.environ['SELF_GROUP'])
186 self.assertEquals(os.path.join(local_impl, 'impl'), os.environ['SELF_IMPL'])
188 del os.environ['FOO_PATH']
189 if 'XDG_DATA_DIRS' in os.environ:
190 del os.environ['XDG_DATA_DIRS']
191 os.environ['BAR_PATH'] = '/old'
192 old, sys.stdout = sys.stdout, StringIO()
193 try:
194 policy.download_and_execute(['Hello'])
195 finally:
196 sys.stdout = old
197 self.assertEquals(cached_impl + '/.',
198 os.environ['FOO_PATH'])
199 self.assertEquals(cached_impl + '/.:/old',
200 os.environ['BAR_PATH'])
201 self.assertEquals(cached_impl + '/.:/usr/local/share:/usr/share',
202 os.environ['XDG_DATA_DIRS'])
204 policy.download_only = True
205 policy.download_and_execute(['Hello'])
207 def testFeeds(self):
208 self.cache_iface(foo_iface_uri,
209 """<?xml version="1.0" ?>
210 <interface last-modified="0"
211 uri="%s"
212 xmlns="http://zero-install.sourceforge.net/2004/injector/interface">
213 <name>Foo</name>
214 <summary>Foo</summary>
215 <description>Foo</description>
216 <feed src='http://bar'/>
217 </interface>""" % foo_iface_uri)
218 self.cache_iface('http://bar',
219 """<?xml version="1.0" ?>
220 <interface last-modified="0"
221 uri="http://bar"
222 xmlns="http://zero-install.sourceforge.net/2004/injector/interface">
223 <feed-for interface='%s'/>
224 <name>Bar</name>
225 <summary>Bar</summary>
226 <description>Bar</description>
227 <implementation version='1.0' id='sha1=123'/>
228 </interface>""" % foo_iface_uri)
229 policy = autopolicy.AutoPolicy(foo_iface_uri, False,
230 dry_run = True)
231 policy.freshness = 0
232 policy.network_use = model.network_full
233 policy.recalculate()
234 assert policy.ready
235 foo_iface = iface_cache.iface_cache.get_interface(foo_iface_uri)
236 self.assertEquals('sha1=123', policy.implementation[foo_iface].id)
238 def testBadConfig(self):
239 path = basedir.save_config_path(namespaces.config_site,
240 namespaces.config_prog)
241 glob = os.path.join(path, 'global')
242 assert not os.path.exists(glob)
243 stream = file(glob, 'w')
244 stream.write('hello!')
245 stream.close()
247 logger.setLevel(logging.ERROR)
248 policy = autopolicy.AutoPolicy(foo_iface_uri,
249 download_only = False)
250 logger.setLevel(logging.WARN)
252 def testNoLocal(self):
253 self.cache_iface(foo_iface_uri,
254 """<?xml version="1.0" ?>
255 <interface last-modified="1110752708"
256 uri="%s"
257 xmlns="http://zero-install.sourceforge.net/2004/injector/interface">
258 <name>Foo</name>
259 <summary>Foo</summary>
260 <description>Foo</description>
261 <feed src='/etc/passwd'/>
262 </interface>""" % foo_iface_uri)
263 policy = autopolicy.AutoPolicy(foo_iface_uri,
264 download_only = False)
265 policy.network_use = model.network_offline
266 try:
267 iface_cache.iface_cache.get_interface(foo_iface_uri)
268 assert False
269 except reader.InvalidInterface, ex:
270 assert 'Invalid feed URL' in str(ex)
272 def testDLfeed(self):
273 self.cache_iface(foo_iface_uri,
274 """<?xml version="1.0" ?>
275 <interface last-modified="1110752708"
276 uri="%s"
277 xmlns="http://zero-install.sourceforge.net/2004/injector/interface">
278 <name>Foo</name>
279 <summary>Foo</summary>
280 <description>Foo</description>
281 <feed src='http://example.com'/>
282 </interface>""" % foo_iface_uri)
283 policy = autopolicy.AutoPolicy(foo_iface_uri, dry_run = True)
284 policy.network_use = model.network_full
285 policy.freshness = 0
287 try:
288 policy.recalculate()
289 assert False
290 except NeedDownload, ex:
291 pass
293 iface = iface_cache.iface_cache.get_interface(foo_iface_uri)
294 iface._main_feed.feeds = [model.Feed('/BadFeed', None, False)]
296 logger.setLevel(logging.ERROR)
297 policy.recalculate() # Triggers warning
298 logger.setLevel(logging.WARN)
300 def testBestUnusable(self):
301 self.cache_iface(foo_iface_uri,
302 """<?xml version="1.0" ?>
303 <interface last-modified="1110752708"
304 uri="%s"
305 xmlns="http://zero-install.sourceforge.net/2004/injector/interface">
306 <name>Foo</name>
307 <summary>Foo</summary>
308 <description>Foo</description>
309 <implementation id='sha1=123' version='1.0' arch='odd-weird'/>
310 </interface>""" % foo_iface_uri)
311 policy = autopolicy.AutoPolicy(foo_iface_uri,
312 download_only = False)
313 policy.network_use = model.network_offline
314 policy.recalculate()
315 assert not policy.ready, policy.implementation
316 try:
317 policy.download_and_execute([])
318 assert False
319 except model.SafeException, ex:
320 assert "Can't find all required implementations" in str(ex)
322 def testNoArchives(self):
323 self.cache_iface(foo_iface_uri,
324 """<?xml version="1.0" ?>
325 <interface last-modified="1110752708"
326 uri="%s"
327 xmlns="http://zero-install.sourceforge.net/2004/injector/interface">
328 <name>Foo</name>
329 <summary>Foo</summary>
330 <description>Foo</description>
331 <implementation id='sha1=123' version='1.0'/>
332 </interface>""" % foo_iface_uri)
333 policy = autopolicy.AutoPolicy(foo_iface_uri,
334 download_only = False)
335 policy.freshness = 0
336 policy.recalculate()
337 assert policy.ready
338 try:
339 policy.download_and_execute([])
340 assert False
341 except model.SafeException, ex:
342 assert 'no download locations' in str(ex), ex
344 def testCycle(self):
345 self.cache_iface(foo_iface_uri,
346 """<?xml version="1.0" ?>
347 <interface last-modified="1110752708"
348 uri="%s"
349 xmlns="http://zero-install.sourceforge.net/2004/injector/interface">
350 <name>Foo</name>
351 <summary>Foo</summary>
352 <description>Foo</description>
353 <group>
354 <requires interface='%s'/>
355 <implementation id='sha1=123' version='1.0'/>
356 </group>
357 </interface>""" % (foo_iface_uri, foo_iface_uri))
358 policy = autopolicy.AutoPolicy(foo_iface_uri,
359 download_only = False)
360 policy.freshness = 0
361 policy.recalculate()
363 def testConstraints(self):
364 self.cache_iface('http://bar',
365 """<?xml version="1.0" ?>
366 <interface last-modified="1110752708"
367 uri="http://bar"
368 xmlns="http://zero-install.sourceforge.net/2004/injector/interface">
369 <name>Bar</name>
370 <summary>Bar</summary>
371 <description>Bar</description>
372 <implementation id='sha1=100' version='1.0'/>
373 <implementation id='sha1=150' stability='developer' version='1.5'/>
374 <implementation id='sha1=200' version='2.0'/>
375 </interface>""")
376 self.cache_iface(foo_iface_uri,
377 """<?xml version="1.0" ?>
378 <interface last-modified="1110752708"
379 uri="%s"
380 xmlns="http://zero-install.sourceforge.net/2004/injector/interface">
381 <name>Foo</name>
382 <summary>Foo</summary>
383 <description>Foo</description>
384 <group>
385 <requires interface='http://bar'>
386 <version/>
387 </requires>
388 <implementation id='sha1=123' version='1.0'/>
389 </group>
390 </interface>""" % foo_iface_uri)
391 policy = autopolicy.AutoPolicy(foo_iface_uri,
392 download_only = False)
393 policy.network_use = model.network_full
394 policy.freshness = 0
395 #logger.setLevel(logging.DEBUG)
396 policy.recalculate()
397 #logger.setLevel(logging.WARN)
398 foo_iface = iface_cache.iface_cache.get_interface(foo_iface_uri)
399 bar_iface = iface_cache.iface_cache.get_interface('http://bar')
400 assert policy.implementation[bar_iface].id == 'sha1=200'
402 dep = policy.implementation[foo_iface].dependencies['http://bar']
403 assert len(dep.restrictions) == 1
404 restriction = dep.restrictions[0]
406 restriction.before = model.parse_version('2.0')
407 policy.recalculate()
408 assert policy.implementation[bar_iface].id == 'sha1=100'
410 restriction.not_before = model.parse_version('1.5')
411 policy.recalculate()
412 assert policy.implementation[bar_iface].id == 'sha1=150'
414 suite = unittest.makeSuite(TestAutoPolicy)
415 if __name__ == '__main__':
416 sys.argv.append('-v')
417 unittest.main()