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()
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 os
.environ
['FOO_PATH'] = "old"
174 old
, sys
.stdout
= sys
.stdout
, StringIO()
176 policy
.download_and_execute(['Hello'])
179 self
.assertEquals(cached_impl
+ '/.:old',
180 os
.environ
['FOO_PATH'])
181 self
.assertEquals(cached_impl
+ '/.:/a:/b',
182 os
.environ
['BAR_PATH'])
184 self
.assertEquals(os
.path
.join(local_impl
, 'group'), os
.environ
['SELF_GROUP'])
185 self
.assertEquals(os
.path
.join(local_impl
, 'impl'), os
.environ
['SELF_IMPL'])
187 del os
.environ
['FOO_PATH']
188 if 'XDG_DATA_DIRS' in os
.environ
:
189 del os
.environ
['XDG_DATA_DIRS']
190 os
.environ
['BAR_PATH'] = '/old'
191 old
, sys
.stdout
= sys
.stdout
, StringIO()
193 policy
.download_and_execute(['Hello'])
196 self
.assertEquals(cached_impl
+ '/.',
197 os
.environ
['FOO_PATH'])
198 self
.assertEquals(cached_impl
+ '/.:/old',
199 os
.environ
['BAR_PATH'])
200 self
.assertEquals(cached_impl
+ '/.:/usr/local/share:/usr/share',
201 os
.environ
['XDG_DATA_DIRS'])
204 self
.cache_iface(foo_iface_uri
,
205 """<?xml version="1.0" ?>
206 <interface last-modified="0"
208 xmlns="http://zero-install.sourceforge.net/2004/injector/interface">
210 <summary>Foo</summary>
211 <description>Foo</description>
212 <feed src='http://bar'/>
213 </interface>""" % foo_iface_uri
)
214 self
.cache_iface('http://bar',
215 """<?xml version="1.0" ?>
216 <interface last-modified="0"
218 xmlns="http://zero-install.sourceforge.net/2004/injector/interface">
219 <feed-for interface='%s'/>
221 <summary>Bar</summary>
222 <description>Bar</description>
223 <implementation version='1.0' id='sha1=123'/>
224 </interface>""" % foo_iface_uri
)
225 policy
= autopolicy
.AutoPolicy(foo_iface_uri
, False,
228 policy
.network_use
= model
.network_full
231 foo_iface
= policy
.get_interface(foo_iface_uri
)
232 self
.assertEquals('sha1=123', policy
.implementation
[foo_iface
].id)
234 def testBadConfig(self
):
235 path
= basedir
.save_config_path(namespaces
.config_site
,
236 namespaces
.config_prog
)
237 glob
= os
.path
.join(path
, 'global')
238 assert not os
.path
.exists(glob
)
239 stream
= file(glob
, 'w')
240 stream
.write('hello!')
243 logger
.setLevel(logging
.ERROR
)
244 policy
= autopolicy
.AutoPolicy(foo_iface_uri
,
245 download_only
= False)
246 logger
.setLevel(logging
.WARN
)
248 def testNoLocal(self
):
249 self
.cache_iface(foo_iface_uri
,
250 """<?xml version="1.0" ?>
251 <interface last-modified="1110752708"
253 xmlns="http://zero-install.sourceforge.net/2004/injector/interface">
255 <summary>Foo</summary>
256 <description>Foo</description>
257 <feed src='/etc/passwd'/>
258 </interface>""" % foo_iface_uri
)
259 policy
= autopolicy
.AutoPolicy(foo_iface_uri
,
260 download_only
= False)
261 policy
.network_use
= model
.network_offline
263 policy
.get_interface(foo_iface_uri
)
265 except reader
.InvalidInterface
, ex
:
266 assert 'Invalid feed URL' in str(ex
)
268 def testDLfeed(self
):
269 self
.cache_iface(foo_iface_uri
,
270 """<?xml version="1.0" ?>
271 <interface last-modified="1110752708"
273 xmlns="http://zero-install.sourceforge.net/2004/injector/interface">
275 <summary>Foo</summary>
276 <description>Foo</description>
277 <feed src='http://example.com'/>
278 </interface>""" % foo_iface_uri
)
279 policy
= autopolicy
.AutoPolicy(foo_iface_uri
, dry_run
= True)
280 policy
.network_use
= model
.network_full
286 except NeedDownload
, ex
:
289 iface
= policy
.get_interface(foo_iface_uri
)
290 iface
._main
_feed
.feeds
= [model
.Feed('/BadFeed', None, False)]
292 logger
.setLevel(logging
.ERROR
)
293 policy
.recalculate() # Triggers warning
294 logger
.setLevel(logging
.WARN
)
296 def testBestUnusable(self
):
297 self
.cache_iface(foo_iface_uri
,
298 """<?xml version="1.0" ?>
299 <interface last-modified="1110752708"
301 xmlns="http://zero-install.sourceforge.net/2004/injector/interface">
303 <summary>Foo</summary>
304 <description>Foo</description>
305 <implementation id='sha1=123' version='1.0' arch='odd-weird'/>
306 </interface>""" % foo_iface_uri
)
307 policy
= autopolicy
.AutoPolicy(foo_iface_uri
,
308 download_only
= False)
309 policy
.network_use
= model
.network_offline
311 assert not policy
.ready
, policy
.implementation
313 policy
.download_and_execute([])
315 except model
.SafeException
, ex
:
316 assert "Can't find all required implementations" in str(ex
)
318 def testNoArchives(self
):
319 self
.cache_iface(foo_iface_uri
,
320 """<?xml version="1.0" ?>
321 <interface last-modified="1110752708"
323 xmlns="http://zero-install.sourceforge.net/2004/injector/interface">
325 <summary>Foo</summary>
326 <description>Foo</description>
327 <implementation id='sha1=123' version='1.0'/>
328 </interface>""" % foo_iface_uri
)
329 policy
= autopolicy
.AutoPolicy(foo_iface_uri
,
330 download_only
= False)
335 policy
.download_and_execute([])
337 except model
.SafeException
, ex
:
338 assert 'no download locations' in str(ex
), ex
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>
350 <requires interface='%s'/>
351 <implementation id='sha1=123' version='1.0'/>
353 </interface>""" % (foo_iface_uri
, foo_iface_uri
))
354 policy
= autopolicy
.AutoPolicy(foo_iface_uri
,
355 download_only
= False)
359 def testConstraints(self
):
360 self
.cache_iface('http://bar',
361 """<?xml version="1.0" ?>
362 <interface last-modified="1110752708"
364 xmlns="http://zero-install.sourceforge.net/2004/injector/interface">
366 <summary>Bar</summary>
367 <description>Bar</description>
368 <implementation id='sha1=100' version='1.0'/>
369 <implementation id='sha1=150' stability='developer' version='1.5'/>
370 <implementation id='sha1=200' version='2.0'/>
372 self
.cache_iface(foo_iface_uri
,
373 """<?xml version="1.0" ?>
374 <interface last-modified="1110752708"
376 xmlns="http://zero-install.sourceforge.net/2004/injector/interface">
378 <summary>Foo</summary>
379 <description>Foo</description>
381 <requires interface='http://bar'>
384 <implementation id='sha1=123' version='1.0'/>
386 </interface>""" % foo_iface_uri
)
387 policy
= autopolicy
.AutoPolicy(foo_iface_uri
,
388 download_only
= False)
389 policy
.network_use
= model
.network_full
391 #logger.setLevel(logging.DEBUG)
393 #logger.setLevel(logging.WARN)
394 foo_iface
= policy
.get_interface(foo_iface_uri
)
395 bar_iface
= policy
.get_interface('http://bar')
396 assert policy
.implementation
[bar_iface
].id == 'sha1=200'
398 dep
= policy
.implementation
[foo_iface
].dependencies
['http://bar']
399 assert len(dep
.restrictions
) == 1
400 restriction
= dep
.restrictions
[0]
402 restriction
.before
= model
.parse_version('2.0')
404 assert policy
.implementation
[bar_iface
].id == 'sha1=100'
406 restriction
.not_before
= model
.parse_version('1.5')
408 assert policy
.implementation
[bar_iface
].id == 'sha1=150'
410 suite
= unittest
.makeSuite(TestAutoPolicy
)
411 if __name__
== '__main__':
412 sys
.argv
.append('-v')