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'])
205 self
.cache_iface(foo_iface_uri
,
206 """<?xml version="1.0" ?>
207 <interface last-modified="0"
209 xmlns="http://zero-install.sourceforge.net/2004/injector/interface">
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"
219 xmlns="http://zero-install.sourceforge.net/2004/injector/interface">
220 <feed-for interface='%s'/>
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,
229 policy
.network_use
= model
.network_full
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!')
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"
254 xmlns="http://zero-install.sourceforge.net/2004/injector/interface">
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
264 iface_cache
.iface_cache
.get_interface(foo_iface_uri
)
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"
274 xmlns="http://zero-install.sourceforge.net/2004/injector/interface">
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
287 except NeedDownload
, ex
:
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"
302 xmlns="http://zero-install.sourceforge.net/2004/injector/interface">
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
312 assert not policy
.ready
, policy
.implementation
314 policy
.download_and_execute([])
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"
324 xmlns="http://zero-install.sourceforge.net/2004/injector/interface">
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)
336 policy
.download_and_execute([])
338 except model
.SafeException
, ex
:
339 assert 'no download locations' in str(ex
), ex
342 self
.cache_iface(foo_iface_uri
,
343 """<?xml version="1.0" ?>
344 <interface last-modified="1110752708"
346 xmlns="http://zero-install.sourceforge.net/2004/injector/interface">
348 <summary>Foo</summary>
349 <description>Foo</description>
351 <requires interface='%s'/>
352 <implementation id='sha1=123' version='1.0'/>
354 </interface>""" % (foo_iface_uri
, foo_iface_uri
))
355 policy
= autopolicy
.AutoPolicy(foo_iface_uri
,
356 download_only
= False)
360 def testConstraints(self
):
361 self
.cache_iface('http://bar',
362 """<?xml version="1.0" ?>
363 <interface last-modified="1110752708"
365 xmlns="http://zero-install.sourceforge.net/2004/injector/interface">
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'/>
373 self
.cache_iface(foo_iface_uri
,
374 """<?xml version="1.0" ?>
375 <interface last-modified="1110752708"
377 xmlns="http://zero-install.sourceforge.net/2004/injector/interface">
379 <summary>Foo</summary>
380 <description>Foo</description>
382 <requires interface='http://bar'>
385 <implementation id='sha1=123' version='1.0'/>
387 </interface>""" % foo_iface_uri
)
388 policy
= autopolicy
.AutoPolicy(foo_iface_uri
,
389 download_only
= False)
390 policy
.network_use
= model
.network_full
392 #logger.setLevel(logging.DEBUG)
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')
405 assert policy
.implementation
[bar_iface
].id == 'sha1=100'
407 restriction
.not_before
= model
.parse_version('1.5')
409 assert policy
.implementation
[bar_iface
].id == 'sha1=150'
411 suite
= unittest
.makeSuite(TestAutoPolicy
)
412 if __name__
== '__main__':
413 sys
.argv
.append('-v')