1 #!/usr/bin/env python2.4
2 from basetest
import BaseTest
3 import sys
, tempfile
, os
, logging
4 from StringIO
import StringIO
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
14 foo_iface_uri
= 'http://foo'
16 logger
= logging
.getLogger()
18 class TestAutoPolicy(BaseTest
):
21 stream
= tempfile
.TemporaryFile()
22 stream
.write(data
.thomas_key
)
24 gpg
.import_key(stream
)
26 def cache_iface(self
, name
, data
):
27 cached_ifaces
= basedir
.save_cache_path('0install.net',
30 f
= file(os
.path
.join(cached_ifaces
, model
.escape(name
)), 'w')
34 def testNoNeedDl(self
):
35 policy
= autopolicy
.AutoPolicy(foo_iface_uri
,
36 download_only
= False)
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" ?>
49 xmlns="http://zero-install.sourceforge.net/2004/injector/interface">
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'/>
56 </interface>""" % foo_iface_uri
)
57 policy
= autopolicy
.AutoPolicy(foo_iface_uri
,
58 download_only
= False)
61 assert policy
.need_download()
63 except model
.SafeException
, ex
:
64 assert 'Unknown digest algorithm' in str(ex
)
66 def testDownload(self
):
67 tmp
= tempfile
.NamedTemporaryFile()
69 """<?xml version="1.0" ?>
71 main='ThisBetterNotExist'
72 xmlns="http://zero-install.sourceforge.net/2004/injector/interface">
74 <summary>Foo</summary>
75 <description>Foo</description>
76 <implementation version='1.0' id='/bin'/>
79 policy
= autopolicy
.AutoPolicy(tmp
.name
, False, False)
81 policy
.download_and_execute(['Hello'])
83 except model
.SafeException
, ex
:
84 assert "ThisBetterNotExist" in str(ex
)
88 tmp
= tempfile
.NamedTemporaryFile()
90 """<?xml version="1.0" ?>
92 xmlns="http://zero-install.sourceforge.net/2004/injector/interface">
94 <summary>Foo</summary>
95 <description>Foo</description>
96 <implementation version='1.0' id='/bin'/>
99 policy
= autopolicy
.AutoPolicy(tmp
.name
, False, False)
101 policy
.download_and_execute(['Hello'])
103 except model
.SafeException
, ex
:
104 assert "library" in str(ex
)
107 def testNeedDL(self
):
108 self
.cache_iface(foo_iface_uri
,
109 """<?xml version="1.0" ?>
110 <interface last-modified="0"
112 main='ThisBetterNotExist'
113 xmlns="http://zero-install.sourceforge.net/2004/injector/interface">
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'/>
120 </interface>""" % foo_iface_uri
)
121 policy
= autopolicy
.AutoPolicy(foo_iface_uri
, False, True)
123 policy
.network_use
= model
.network_full
125 assert policy
.need_download()
128 policy
.execute([], main
= 'NOTHING')
130 except NeedDownload
, ex
:
133 def testBinding(self
):
134 local_impl
= os
.path
.dirname(os
.path
.abspath(__file__
))
135 tmp
= tempfile
.NamedTemporaryFile()
137 """<?xml version="1.0" ?>
139 main='testautopolicy.py'
140 xmlns="http://zero-install.sourceforge.net/2004/injector/interface">
142 <summary>Bar</summary>
143 <description>Bar</description>
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='.'/>
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'/>
155 </interface>""" % (foo_iface_uri
, local_impl
))
157 self
.cache_iface(foo_iface_uri
,
158 """<?xml version="1.0" ?>
159 <interface last-modified="0"
161 xmlns="http://zero-install.sourceforge.net/2004/injector/interface">
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',
170 policy
= autopolicy
.AutoPolicy(tmp
.name
, False,
172 policy
.network_use
= model
.network_offline
173 self
.changes_environ()
174 os
.environ
['FOO_PATH'] = "old"
175 old
, sys
.stdout
= sys
.stdout
, StringIO()
177 policy
.download_and_execute(['Hello'])
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()
194 policy
.download_and_execute(['Hello'])
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'])
208 self
.cache_iface(foo_iface_uri
,
209 """<?xml version="1.0" ?>
210 <interface last-modified="0"
212 xmlns="http://zero-install.sourceforge.net/2004/injector/interface">
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"
222 xmlns="http://zero-install.sourceforge.net/2004/injector/interface">
223 <feed-for interface='%s'/>
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,
232 policy
.network_use
= model
.network_full
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!')
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"
257 xmlns="http://zero-install.sourceforge.net/2004/injector/interface">
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
267 iface_cache
.iface_cache
.get_interface(foo_iface_uri
)
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"
277 xmlns="http://zero-install.sourceforge.net/2004/injector/interface">
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
290 except NeedDownload
, ex
:
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"
305 xmlns="http://zero-install.sourceforge.net/2004/injector/interface">
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
315 assert not policy
.ready
, policy
.implementation
317 policy
.download_and_execute([])
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"
327 xmlns="http://zero-install.sourceforge.net/2004/injector/interface">
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)
339 policy
.download_and_execute([])
341 except model
.SafeException
, ex
:
342 assert 'no download locations' in str(ex
), ex
345 self
.cache_iface(foo_iface_uri
,
346 """<?xml version="1.0" ?>
347 <interface last-modified="1110752708"
349 xmlns="http://zero-install.sourceforge.net/2004/injector/interface">
351 <summary>Foo</summary>
352 <description>Foo</description>
354 <requires interface='%s'/>
355 <implementation id='sha1=123' version='1.0'/>
357 </interface>""" % (foo_iface_uri
, foo_iface_uri
))
358 policy
= autopolicy
.AutoPolicy(foo_iface_uri
,
359 download_only
= False)
363 def testConstraints(self
):
364 self
.cache_iface('http://bar',
365 """<?xml version="1.0" ?>
366 <interface last-modified="1110752708"
368 xmlns="http://zero-install.sourceforge.net/2004/injector/interface">
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'/>
376 self
.cache_iface(foo_iface_uri
,
377 """<?xml version="1.0" ?>
378 <interface last-modified="1110752708"
380 xmlns="http://zero-install.sourceforge.net/2004/injector/interface">
382 <summary>Foo</summary>
383 <description>Foo</description>
385 <requires interface='http://bar'>
388 <implementation id='sha1=123' version='1.0'/>
390 </interface>""" % foo_iface_uri
)
391 policy
= autopolicy
.AutoPolicy(foo_iface_uri
,
392 download_only
= False)
393 policy
.network_use
= model
.network_full
395 #logger.setLevel(logging.DEBUG)
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')
408 assert policy
.implementation
[bar_iface
].id == 'sha1=100'
410 restriction
.not_before
= model
.parse_version('1.5')
412 assert policy
.implementation
[bar_iface
].id == 'sha1=150'
414 suite
= unittest
.makeSuite(TestAutoPolicy
)
415 if __name__
== '__main__':
416 sys
.argv
.append('-v')