1 #!/usr/bin/env python2.3
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 config_home
= tempfile
.mktemp()
11 cache_home
= tempfile
.mktemp()
12 os
.environ
['XDG_CONFIG_HOME'] = config_home
13 os
.environ
['XDG_CACHE_HOME'] = cache_home
14 os
.environ
['XDG_CACHE_DIRS'] = ''
16 from zeroinstall
import NeedDownload
17 from zeroinstall
.injector
import model
, basedir
, autopolicy
, gpg
, iface_cache
, namespaces
, reader
21 foo_iface_uri
= 'http://foo'
23 logger
= logging
.getLogger()
25 class TestAutoPolicy(BaseTest
):
28 stream
= tempfile
.TemporaryFile()
29 stream
.write(data
.thomas_key
)
31 gpg
.import_key(stream
)
33 def cache_iface(self
, name
, data
):
34 cached_ifaces
= basedir
.save_cache_path('0install.net',
37 f
= file(os
.path
.join(cached_ifaces
, model
.escape(name
)), 'w')
41 def testNoNeedDl(self
):
42 policy
= autopolicy
.AutoPolicy(foo_iface_uri
,
43 download_only
= False)
45 assert policy
.need_download()
46 self
.cache_iface(foo_iface_uri
,
47 """<?xml version="1.0" ?>
48 <interface last-modified="1110752708"
50 xmlns="http://zero-install.sourceforge.net/2004/injector/interface">
52 <summary>Foo</summary>
53 <description>Foo</description>
54 </interface>""" % foo_iface_uri
)
55 iface_cache
.iface_cache
._interfaces
= {}
56 assert not policy
.need_download()
58 def testUnknownAlg(self
):
59 self
.cache_iface(foo_iface_uri
,
60 """<?xml version="1.0" ?>
63 xmlns="http://zero-install.sourceforge.net/2004/injector/interface">
65 <summary>Foo</summary>
66 <description>Foo</description>
67 <implementation id='unknown=123' version='1.0'>
68 <archive href='http://foo/foo.tgz' size='100'/>
70 </interface>""" % foo_iface_uri
)
71 policy
= autopolicy
.AutoPolicy(foo_iface_uri
,
72 download_only
= False)
75 assert policy
.need_download()
77 except model
.SafeException
, ex
:
78 assert 'Unknown digest algorithm' in str(ex
)
80 def testDownload(self
):
81 tmp
= tempfile
.NamedTemporaryFile()
83 """<?xml version="1.0" ?>
85 main='ThisBetterNotExist'
86 xmlns="http://zero-install.sourceforge.net/2004/injector/interface">
88 <summary>Foo</summary>
89 <description>Foo</description>
90 <implementation version='1.0' id='/bin'/>
93 policy
= autopolicy
.AutoPolicy(tmp
.name
, False, False)
95 policy
.download_and_execute(['Hello'])
97 except model
.SafeException
, ex
:
98 assert "ThisBetterNotExist" in str(ex
)
101 def testNoMain(self
):
102 tmp
= tempfile
.NamedTemporaryFile()
104 """<?xml version="1.0" ?>
106 xmlns="http://zero-install.sourceforge.net/2004/injector/interface">
108 <summary>Foo</summary>
109 <description>Foo</description>
110 <implementation version='1.0' id='/bin'/>
113 policy
= autopolicy
.AutoPolicy(tmp
.name
, False, False)
115 policy
.download_and_execute(['Hello'])
117 except model
.SafeException
, ex
:
118 assert "library" in str(ex
)
121 def testNeedDL(self
):
122 self
.cache_iface(foo_iface_uri
,
123 """<?xml version="1.0" ?>
124 <interface last-modified="0"
126 main='ThisBetterNotExist'
127 xmlns="http://zero-install.sourceforge.net/2004/injector/interface">
129 <summary>Foo</summary>
130 <description>Foo</description>
131 <implementation version='1.0' id='sha1=123'>
132 <archive href='http://foo/foo.tgz' size='100'/>
134 </interface>""" % foo_iface_uri
)
135 policy
= autopolicy
.AutoPolicy(foo_iface_uri
, False, True)
137 policy
.network_use
= model
.network_full
139 assert policy
.need_download()
141 policy
.start_downloading_impls()
143 except NeedDownload
, ex
:
146 def testBinding(self
):
147 tmp
= tempfile
.NamedTemporaryFile()
149 """<?xml version="1.0" ?>
151 main='testautopolicy.py'
152 xmlns="http://zero-install.sourceforge.net/2004/injector/interface">
154 <summary>Bar</summary>
155 <description>Bar</description>
157 <requires interface='%s'>
158 <environment name='FOO_PATH' insert='.'/>
159 <environment name='BAR_PATH' insert='.' default='/a:/b'/>
160 <environment name='XDG_DATA_DIRS' insert='.'/>
162 <implementation version='1.0' id='%s'/>
164 </interface>""" % (foo_iface_uri
, os
.path
.dirname(os
.path
.abspath(__file__
))))
166 self
.cache_iface(foo_iface_uri
,
167 """<?xml version="1.0" ?>
168 <interface last-modified="0"
170 xmlns="http://zero-install.sourceforge.net/2004/injector/interface">
172 <summary>Foo</summary>
173 <description>Foo</description>
174 <implementation version='1.0' id='sha1=123'/>
175 </interface>""" % foo_iface_uri
)
176 cached_impl
= basedir
.save_cache_path('0install.net',
179 policy
= autopolicy
.AutoPolicy(tmp
.name
, False,
181 policy
.network_use
= model
.network_offline
182 os
.environ
['FOO_PATH'] = "old"
183 old
, sys
.stdout
= sys
.stdout
, StringIO()
185 policy
.download_and_execute(['Hello'])
188 self
.assertEquals(cached_impl
+ '/.:old',
189 os
.environ
['FOO_PATH'])
190 self
.assertEquals(cached_impl
+ '/.:/a:/b',
191 os
.environ
['BAR_PATH'])
193 del os
.environ
['FOO_PATH']
194 if 'XDG_DATA_DIRS' in os
.environ
:
195 del os
.environ
['XDG_DATA_DIRS']
196 os
.environ
['BAR_PATH'] = '/old'
197 old
, sys
.stdout
= sys
.stdout
, StringIO()
199 policy
.download_and_execute(['Hello'])
202 self
.assertEquals(cached_impl
+ '/.',
203 os
.environ
['FOO_PATH'])
204 self
.assertEquals(cached_impl
+ '/.:/old',
205 os
.environ
['BAR_PATH'])
206 self
.assertEquals(cached_impl
+ '/.:/usr/local/share:/usr/share',
207 os
.environ
['XDG_DATA_DIRS'])
210 self
.cache_iface(foo_iface_uri
,
211 """<?xml version="1.0" ?>
212 <interface last-modified="0"
214 xmlns="http://zero-install.sourceforge.net/2004/injector/interface">
216 <summary>Foo</summary>
217 <description>Foo</description>
218 <feed src='http://bar'/>
219 </interface>""" % foo_iface_uri
)
220 self
.cache_iface('http://bar',
221 """<?xml version="1.0" ?>
222 <interface last-modified="0"
224 xmlns="http://zero-install.sourceforge.net/2004/injector/interface">
225 <feed-for interface='%s'/>
227 <summary>Bar</summary>
228 <description>Bar</description>
229 <implementation version='1.0' id='sha1=123'/>
230 </interface>""" % foo_iface_uri
)
231 policy
= autopolicy
.AutoPolicy(foo_iface_uri
, False,
234 policy
.network_use
= model
.network_full
237 foo_iface
= policy
.get_interface(foo_iface_uri
)
238 self
.assertEquals('sha1=123', policy
.implementation
[foo_iface
].id)
240 def testBadConfig(self
):
241 path
= basedir
.save_config_path(namespaces
.config_site
,
242 namespaces
.config_prog
)
243 glob
= os
.path
.join(path
, 'global')
244 assert not os
.path
.exists(glob
)
245 stream
= file(glob
, 'w')
246 stream
.write('hello!')
249 logger
.setLevel(logging
.ERROR
)
250 policy
= autopolicy
.AutoPolicy(foo_iface_uri
,
251 download_only
= False)
252 logger
.setLevel(logging
.WARN
)
254 def testRanking(self
):
255 self
.cache_iface('http://bar',
256 """<?xml version="1.0" ?>
257 <interface last-modified="1110752708"
259 xmlns="http://zero-install.sourceforge.net/2004/injector/interface">
261 <summary>Bar</summary>
262 <description>Bar</description>
263 <implementation id='sha1=125' version='1.0' arch='odd-weird' stability='buggy'/>
264 <implementation id='sha1=126' version='1.0'/>
266 self
.cache_iface(foo_iface_uri
,
267 """<?xml version="1.0" ?>
268 <interface last-modified="1110752708"
270 xmlns="http://zero-install.sourceforge.net/2004/injector/interface">
272 <summary>Foo</summary>
273 <description>Foo</description>
274 <feed src='http://example.com' arch='odd-unknown'/>
275 <feed src='http://bar'/>
276 <implementation id='sha1=123' version='1.0' arch='odd-strange'/>
277 <implementation id='sha1=124' version='1.0' arch='odd-weird'/>
278 </interface>""" % foo_iface_uri
)
279 policy
= autopolicy
.AutoPolicy(foo_iface_uri
,
280 download_only
= False)
281 policy
.network_use
= model
.network_full
283 impls
= policy
.get_ranked_implementations(
284 policy
.get_interface(policy
.root
))
285 assert len(impls
) == 4
287 logger
.setLevel(logging
.ERROR
)
288 policy
.network_use
= model
.network_offline
# Changes sort order tests
289 policy
.recalculate() # Triggers feed-for warning
290 logger
.setLevel(logging
.WARN
)
292 def testNoLocal(self
):
293 self
.cache_iface(foo_iface_uri
,
294 """<?xml version="1.0" ?>
295 <interface last-modified="1110752708"
297 xmlns="http://zero-install.sourceforge.net/2004/injector/interface">
299 <summary>Foo</summary>
300 <description>Foo</description>
301 <feed src='/etc/passwd'/>
302 </interface>""" % foo_iface_uri
)
303 policy
= autopolicy
.AutoPolicy(foo_iface_uri
,
304 download_only
= False)
305 policy
.network_use
= model
.network_offline
307 policy
.get_interface(foo_iface_uri
)
309 except reader
.InvalidInterface
, ex
:
310 assert 'Invalid feed URL' in str(ex
)
312 def testDLfeed(self
):
313 self
.cache_iface(foo_iface_uri
,
314 """<?xml version="1.0" ?>
315 <interface last-modified="1110752708"
317 xmlns="http://zero-install.sourceforge.net/2004/injector/interface">
319 <summary>Foo</summary>
320 <description>Foo</description>
321 <feed src='http://example.com'/>
322 </interface>""" % foo_iface_uri
)
323 policy
= autopolicy
.AutoPolicy(foo_iface_uri
, dry_run
= True)
324 policy
.network_use
= model
.network_full
330 except NeedDownload
, ex
:
333 iface
= policy
.get_interface(foo_iface_uri
)
334 iface
.feeds
= [model
.Feed('/BadFeed', None, False)]
336 logger
.setLevel(logging
.ERROR
)
337 policy
.recalculate() # Triggers warning
338 logger
.setLevel(logging
.WARN
)
340 def testBestUnusable(self
):
341 self
.cache_iface(foo_iface_uri
,
342 """<?xml version="1.0" ?>
343 <interface last-modified="1110752708"
345 xmlns="http://zero-install.sourceforge.net/2004/injector/interface">
347 <summary>Foo</summary>
348 <description>Foo</description>
349 <implementation id='sha1=123' version='1.0' arch='odd-weird'/>
350 </interface>""" % foo_iface_uri
)
351 policy
= autopolicy
.AutoPolicy(foo_iface_uri
,
352 download_only
= False)
353 policy
.network_use
= model
.network_offline
355 assert not policy
.ready
357 policy
.download_and_execute([])
359 except model
.SafeException
, ex
:
360 assert "Can't find all required implementations" in str(ex
)
362 def testNoArchives(self
):
363 self
.cache_iface(foo_iface_uri
,
364 """<?xml version="1.0" ?>
365 <interface last-modified="1110752708"
367 xmlns="http://zero-install.sourceforge.net/2004/injector/interface">
369 <summary>Foo</summary>
370 <description>Foo</description>
371 <implementation id='sha1=123' version='1.0'/>
372 </interface>""" % foo_iface_uri
)
373 policy
= autopolicy
.AutoPolicy(foo_iface_uri
,
374 download_only
= False)
379 policy
.download_and_execute([])
381 except model
.SafeException
, ex
:
382 assert 'no download locations' in str(ex
)
385 self
.cache_iface(foo_iface_uri
,
386 """<?xml version="1.0" ?>
387 <interface last-modified="1110752708"
389 xmlns="http://zero-install.sourceforge.net/2004/injector/interface">
391 <summary>Foo</summary>
392 <description>Foo</description>
394 <requires interface='%s'/>
395 <implementation id='sha1=123' version='1.0'/>
397 </interface>""" % (foo_iface_uri
, foo_iface_uri
))
398 policy
= autopolicy
.AutoPolicy(foo_iface_uri
,
399 download_only
= False)
402 def testConstraints(self
):
403 self
.cache_iface('http://bar',
404 """<?xml version="1.0" ?>
405 <interface last-modified="1110752708"
407 xmlns="http://zero-install.sourceforge.net/2004/injector/interface">
409 <summary>Bar</summary>
410 <description>Bar</description>
411 <implementation id='sha1=100' version='1.0'/>
412 <implementation id='sha1=150' stability='developer' version='1.5'/>
413 <implementation id='sha1=200' version='2.0'/>
415 self
.cache_iface(foo_iface_uri
,
416 """<?xml version="1.0" ?>
417 <interface last-modified="1110752708"
419 xmlns="http://zero-install.sourceforge.net/2004/injector/interface">
421 <summary>Foo</summary>
422 <description>Foo</description>
424 <requires interface='http://bar'>
427 <implementation id='sha1=123' version='1.0'/>
429 </interface>""" % foo_iface_uri
)
430 policy
= autopolicy
.AutoPolicy(foo_iface_uri
,
431 download_only
= False)
432 policy
.network_use
= model
.network_full
434 #logger.setLevel(logging.DEBUG)
436 #logger.setLevel(logging.WARN)
437 foo_iface
= policy
.get_interface(foo_iface_uri
)
438 bar_iface
= policy
.get_interface('http://bar')
439 assert policy
.implementation
[bar_iface
].id == 'sha1=200'
441 dep
= policy
.implementation
[foo_iface
].dependencies
['http://bar']
442 assert len(dep
.restrictions
) == 1
443 restriction
= dep
.restrictions
[0]
445 restriction
.before
= model
.parse_version('2.0')
447 assert policy
.implementation
[bar_iface
].id == 'sha1=100'
449 restriction
.not_before
= model
.parse_version('1.5')
451 assert policy
.implementation
[bar_iface
].id == 'sha1=150'
453 suite
= unittest
.makeSuite(TestAutoPolicy
)
454 if __name__
== '__main__':
455 sys
.argv
.append('-v')