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