1 #!/usr/bin/env python2.5
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 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'])
203 policy
.download_only
= True
204 policy
.download_and_execute(['Hello'])
207 self
.cache_iface(foo_iface_uri
,
208 """<?xml version="1.0" ?>
209 <interface last-modified="0"
211 xmlns="http://zero-install.sourceforge.net/2004/injector/interface">
213 <summary>Foo</summary>
214 <description>Foo</description>
215 <feed src='http://bar'/>
216 </interface>""" % foo_iface_uri
)
217 self
.cache_iface('http://bar',
218 """<?xml version="1.0" ?>
219 <interface last-modified="0"
221 xmlns="http://zero-install.sourceforge.net/2004/injector/interface">
222 <feed-for interface='%s'/>
224 <summary>Bar</summary>
225 <description>Bar</description>
226 <implementation version='1.0' id='sha1=123'/>
227 </interface>""" % foo_iface_uri
)
228 policy
= autopolicy
.AutoPolicy(foo_iface_uri
, False,
231 policy
.network_use
= model
.network_full
234 foo_iface
= iface_cache
.iface_cache
.get_interface(foo_iface_uri
)
235 self
.assertEquals('sha1=123', policy
.implementation
[foo_iface
].id)
237 def testBadConfig(self
):
238 path
= basedir
.save_config_path(namespaces
.config_site
,
239 namespaces
.config_prog
)
240 glob
= os
.path
.join(path
, 'global')
241 assert not os
.path
.exists(glob
)
242 stream
= file(glob
, 'w')
243 stream
.write('hello!')
246 logger
.setLevel(logging
.ERROR
)
247 policy
= autopolicy
.AutoPolicy(foo_iface_uri
,
248 download_only
= False)
249 logger
.setLevel(logging
.WARN
)
251 def testNoLocal(self
):
252 self
.cache_iface(foo_iface_uri
,
253 """<?xml version="1.0" ?>
254 <interface last-modified="1110752708"
256 xmlns="http://zero-install.sourceforge.net/2004/injector/interface">
258 <summary>Foo</summary>
259 <description>Foo</description>
260 <feed src='/etc/passwd'/>
261 </interface>""" % foo_iface_uri
)
262 policy
= autopolicy
.AutoPolicy(foo_iface_uri
,
263 download_only
= False)
264 policy
.network_use
= model
.network_offline
266 iface_cache
.iface_cache
.get_interface(foo_iface_uri
)
268 except reader
.InvalidInterface
, ex
:
269 assert 'Invalid feed URL' in str(ex
)
271 def testDLfeed(self
):
272 self
.cache_iface(foo_iface_uri
,
273 """<?xml version="1.0" ?>
274 <interface last-modified="1110752708"
276 xmlns="http://zero-install.sourceforge.net/2004/injector/interface">
278 <summary>Foo</summary>
279 <description>Foo</description>
280 <feed src='http://example.com'/>
281 </interface>""" % foo_iface_uri
)
282 policy
= autopolicy
.AutoPolicy(foo_iface_uri
, dry_run
= True)
283 policy
.network_use
= model
.network_full
289 except NeedDownload
, ex
:
292 iface
= iface_cache
.iface_cache
.get_interface(foo_iface_uri
)
293 iface
._main
_feed
.feeds
= [model
.Feed('/BadFeed', None, False)]
295 logger
.setLevel(logging
.ERROR
)
296 policy
.recalculate() # Triggers warning
297 logger
.setLevel(logging
.WARN
)
299 def testBestUnusable(self
):
300 self
.cache_iface(foo_iface_uri
,
301 """<?xml version="1.0" ?>
302 <interface last-modified="1110752708"
304 xmlns="http://zero-install.sourceforge.net/2004/injector/interface">
306 <summary>Foo</summary>
307 <description>Foo</description>
308 <implementation id='sha1=123' version='1.0' arch='odd-weird'/>
309 </interface>""" % foo_iface_uri
)
310 policy
= autopolicy
.AutoPolicy(foo_iface_uri
,
311 download_only
= False)
312 policy
.network_use
= model
.network_offline
314 assert not policy
.ready
, policy
.implementation
316 policy
.download_and_execute([])
318 except model
.SafeException
, ex
:
319 assert "Can't find all required implementations" in str(ex
)
321 def testNoArchives(self
):
322 self
.cache_iface(foo_iface_uri
,
323 """<?xml version="1.0" ?>
324 <interface last-modified="1110752708"
326 xmlns="http://zero-install.sourceforge.net/2004/injector/interface">
328 <summary>Foo</summary>
329 <description>Foo</description>
330 <implementation id='sha1=123' version='1.0'/>
331 </interface>""" % foo_iface_uri
)
332 policy
= autopolicy
.AutoPolicy(foo_iface_uri
,
333 download_only
= False)
338 policy
.download_and_execute([])
340 except model
.SafeException
, ex
:
341 assert 'no download locations' in str(ex
), ex
344 self
.cache_iface(foo_iface_uri
,
345 """<?xml version="1.0" ?>
346 <interface last-modified="1110752708"
348 xmlns="http://zero-install.sourceforge.net/2004/injector/interface">
350 <summary>Foo</summary>
351 <description>Foo</description>
353 <requires interface='%s'/>
354 <implementation id='sha1=123' version='1.0'/>
356 </interface>""" % (foo_iface_uri
, foo_iface_uri
))
357 policy
= autopolicy
.AutoPolicy(foo_iface_uri
,
358 download_only
= False)
362 def testConstraints(self
):
363 self
.cache_iface('http://bar',
364 """<?xml version="1.0" ?>
365 <interface last-modified="1110752708"
367 xmlns="http://zero-install.sourceforge.net/2004/injector/interface">
369 <summary>Bar</summary>
370 <description>Bar</description>
371 <implementation id='sha1=100' version='1.0'/>
372 <implementation id='sha1=150' stability='developer' version='1.5'/>
373 <implementation id='sha1=200' version='2.0'/>
375 self
.cache_iface(foo_iface_uri
,
376 """<?xml version="1.0" ?>
377 <interface last-modified="1110752708"
379 xmlns="http://zero-install.sourceforge.net/2004/injector/interface">
381 <summary>Foo</summary>
382 <description>Foo</description>
384 <requires interface='http://bar'>
387 <implementation id='sha1=123' version='1.0'/>
389 </interface>""" % foo_iface_uri
)
390 policy
= autopolicy
.AutoPolicy(foo_iface_uri
,
391 download_only
= False)
392 policy
.network_use
= model
.network_full
394 #logger.setLevel(logging.DEBUG)
396 #logger.setLevel(logging.WARN)
397 foo_iface
= iface_cache
.iface_cache
.get_interface(foo_iface_uri
)
398 bar_iface
= iface_cache
.iface_cache
.get_interface('http://bar')
399 assert policy
.implementation
[bar_iface
].id == 'sha1=200'
401 dep
= policy
.implementation
[foo_iface
].dependencies
['http://bar']
402 assert len(dep
.restrictions
) == 1
403 restriction
= dep
.restrictions
[0]
405 restriction
.before
= model
.parse_version('2.0')
407 assert policy
.implementation
[bar_iface
].id == 'sha1=100'
409 restriction
.not_before
= model
.parse_version('1.5')
411 assert policy
.implementation
[bar_iface
].id == 'sha1=150'
413 suite
= unittest
.makeSuite(TestAutoPolicy
)
414 if __name__
== '__main__':
415 sys
.argv
.append('-v')