1 #!/usr/bin/env python2.4
2 from basetest
import BaseTest
3 import sys
, tempfile
, os
, shutil
, logging
4 from StringIO
import StringIO
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
15 foo_iface_uri
= 'http://foo'
17 logger
= logging
.getLogger()
19 class TestAutoPolicy(BaseTest
):
22 stream
= tempfile
.TemporaryFile()
23 stream
.write(data
.thomas_key
)
25 gpg
.import_key(stream
)
27 def cache_iface(self
, name
, data
):
28 cached_ifaces
= basedir
.save_cache_path('0install.net',
31 f
= file(os
.path
.join(cached_ifaces
, model
.escape(name
)), 'w')
35 def testNoNeedDl(self
):
36 policy
= autopolicy
.AutoPolicy(foo_iface_uri
,
37 download_only
= False)
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" ?>
50 xmlns="http://zero-install.sourceforge.net/2004/injector/interface">
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'/>
57 </interface>""" % foo_iface_uri
)
58 policy
= autopolicy
.AutoPolicy(foo_iface_uri
,
59 download_only
= False)
62 assert policy
.need_download()
64 except model
.SafeException
, ex
:
65 assert 'Unknown digest algorithm' in str(ex
)
67 def testDownload(self
):
68 tmp
= tempfile
.NamedTemporaryFile()
70 """<?xml version="1.0" ?>
72 main='ThisBetterNotExist'
73 xmlns="http://zero-install.sourceforge.net/2004/injector/interface">
75 <summary>Foo</summary>
76 <description>Foo</description>
77 <implementation version='1.0' id='/bin'/>
80 policy
= autopolicy
.AutoPolicy(tmp
.name
, False, False)
82 policy
.download_and_execute(['Hello'])
84 except model
.SafeException
, ex
:
85 assert "ThisBetterNotExist" in str(ex
)
89 tmp
= tempfile
.NamedTemporaryFile()
91 """<?xml version="1.0" ?>
93 xmlns="http://zero-install.sourceforge.net/2004/injector/interface">
95 <summary>Foo</summary>
96 <description>Foo</description>
97 <implementation version='1.0' id='/bin'/>
100 policy
= autopolicy
.AutoPolicy(tmp
.name
, False, False)
102 policy
.download_and_execute(['Hello'])
104 except model
.SafeException
, ex
:
105 assert "library" in str(ex
)
108 def testNeedDL(self
):
109 self
.cache_iface(foo_iface_uri
,
110 """<?xml version="1.0" ?>
111 <interface last-modified="0"
113 main='ThisBetterNotExist'
114 xmlns="http://zero-install.sourceforge.net/2004/injector/interface">
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'/>
121 </interface>""" % foo_iface_uri
)
122 policy
= autopolicy
.AutoPolicy(foo_iface_uri
, False, True)
124 policy
.network_use
= model
.network_full
126 assert policy
.need_download()
129 policy
.execute([], main
= 'NOTHING')
131 except NeedDownload
, ex
:
134 def testBinding(self
):
135 local_impl
= os
.path
.dirname(os
.path
.abspath(__file__
))
136 tmp
= tempfile
.NamedTemporaryFile()
138 """<?xml version="1.0" ?>
140 main='testautopolicy.py'
141 xmlns="http://zero-install.sourceforge.net/2004/injector/interface">
143 <summary>Bar</summary>
144 <description>Bar</description>
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='.'/>
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'/>
156 </interface>""" % (foo_iface_uri
, local_impl
))
158 self
.cache_iface(foo_iface_uri
,
159 """<?xml version="1.0" ?>
160 <interface last-modified="0"
162 xmlns="http://zero-install.sourceforge.net/2004/injector/interface">
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',
171 policy
= autopolicy
.AutoPolicy(tmp
.name
, False,
173 policy
.network_use
= model
.network_offline
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')