Added unit-tests for background updates
[zeroinstall/solver.git] / tests / testautopolicy.py
blob32a3a44e4eed012b5a1d6095864a4cc273fda359
1 #!/usr/bin/env python2.4
2 from basetest import BaseTest
3 import sys, tempfile, os, shutil, logging
4 from StringIO import StringIO
5 import unittest
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
13 import data
15 foo_iface_uri = 'http://foo'
17 logger = logging.getLogger()
19 class TestAutoPolicy(BaseTest):
20 def setUp(self):
21 BaseTest.setUp(self)
22 stream = tempfile.TemporaryFile()
23 stream.write(data.thomas_key)
24 stream.seek(0)
25 gpg.import_key(stream)
27 def cache_iface(self, name, data):
28 cached_ifaces = basedir.save_cache_path('0install.net',
29 'interfaces')
31 f = file(os.path.join(cached_ifaces, model.escape(name)), 'w')
32 f.write(data)
33 f.close()
35 def testNoNeedDl(self):
36 policy = autopolicy.AutoPolicy(foo_iface_uri,
37 download_only = False)
38 policy.freshness = 0
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" ?>
48 <interface
49 uri="%s"
50 xmlns="http://zero-install.sourceforge.net/2004/injector/interface">
51 <name>Foo</name>
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'/>
56 </implementation>
57 </interface>""" % foo_iface_uri)
58 policy = autopolicy.AutoPolicy(foo_iface_uri,
59 download_only = False)
60 policy.freshness = 0
61 try:
62 assert policy.need_download()
63 policy.execute([])
64 except model.SafeException, ex:
65 assert 'Unknown digest algorithm' in str(ex)
67 def testDownload(self):
68 tmp = tempfile.NamedTemporaryFile()
69 tmp.write(
70 """<?xml version="1.0" ?>
71 <interface
72 main='ThisBetterNotExist'
73 xmlns="http://zero-install.sourceforge.net/2004/injector/interface">
74 <name>Foo</name>
75 <summary>Foo</summary>
76 <description>Foo</description>
77 <implementation version='1.0' id='/bin'/>
78 </interface>""")
79 tmp.flush()
80 policy = autopolicy.AutoPolicy(tmp.name, False, False)
81 try:
82 policy.download_and_execute(['Hello'])
83 assert 0
84 except model.SafeException, ex:
85 assert "ThisBetterNotExist" in str(ex)
86 tmp.close()
88 def testNoMain(self):
89 tmp = tempfile.NamedTemporaryFile()
90 tmp.write(
91 """<?xml version="1.0" ?>
92 <interface
93 xmlns="http://zero-install.sourceforge.net/2004/injector/interface">
94 <name>Foo</name>
95 <summary>Foo</summary>
96 <description>Foo</description>
97 <implementation version='1.0' id='/bin'/>
98 </interface>""")
99 tmp.flush()
100 policy = autopolicy.AutoPolicy(tmp.name, False, False)
101 try:
102 policy.download_and_execute(['Hello'])
103 assert 0
104 except model.SafeException, ex:
105 assert "library" in str(ex)
106 tmp.close()
108 def testNeedDL(self):
109 self.cache_iface(foo_iface_uri,
110 """<?xml version="1.0" ?>
111 <interface last-modified="0"
112 uri="%s"
113 main='ThisBetterNotExist'
114 xmlns="http://zero-install.sourceforge.net/2004/injector/interface">
115 <name>Foo</name>
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'/>
120 </implementation>
121 </interface>""" % foo_iface_uri)
122 policy = autopolicy.AutoPolicy(foo_iface_uri, False, True)
123 policy.freshness = 0
124 policy.network_use = model.network_full
125 policy.recalculate()
126 assert policy.need_download()
127 assert policy.ready
128 try:
129 policy.execute([], main = 'NOTHING')
130 assert False
131 except NeedDownload, ex:
132 pass
134 def testBinding(self):
135 local_impl = os.path.dirname(os.path.abspath(__file__))
136 tmp = tempfile.NamedTemporaryFile()
137 tmp.write(
138 """<?xml version="1.0" ?>
139 <interface
140 main='testautopolicy.py'
141 xmlns="http://zero-install.sourceforge.net/2004/injector/interface">
142 <name>Bar</name>
143 <summary>Bar</summary>
144 <description>Bar</description>
145 <group>
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='.'/>
150 </requires>
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'/>
154 </implementation>
155 </group>
156 </interface>""" % (foo_iface_uri, local_impl))
157 tmp.flush()
158 self.cache_iface(foo_iface_uri,
159 """<?xml version="1.0" ?>
160 <interface last-modified="0"
161 uri="%s"
162 xmlns="http://zero-install.sourceforge.net/2004/injector/interface">
163 <name>Foo</name>
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',
169 'implementations',
170 'sha1=123')
171 policy = autopolicy.AutoPolicy(tmp.name, False,
172 dry_run = True)
173 policy.network_use = model.network_offline
174 os.environ['FOO_PATH'] = "old"
175 old, sys.stdout = sys.stdout, StringIO()
176 try:
177 policy.download_and_execute(['Hello'])
178 finally:
179 sys.stdout = old
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()
193 try:
194 policy.download_and_execute(['Hello'])
195 finally:
196 sys.stdout = old
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'])
207 def testFeeds(self):
208 self.cache_iface(foo_iface_uri,
209 """<?xml version="1.0" ?>
210 <interface last-modified="0"
211 uri="%s"
212 xmlns="http://zero-install.sourceforge.net/2004/injector/interface">
213 <name>Foo</name>
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"
221 uri="http://bar"
222 xmlns="http://zero-install.sourceforge.net/2004/injector/interface">
223 <feed-for interface='%s'/>
224 <name>Bar</name>
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,
230 dry_run = True)
231 policy.freshness = 0
232 policy.network_use = model.network_full
233 policy.recalculate()
234 assert policy.ready
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!')
245 stream.close()
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"
256 uri="%s"
257 xmlns="http://zero-install.sourceforge.net/2004/injector/interface">
258 <name>Foo</name>
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
266 try:
267 iface_cache.iface_cache.get_interface(foo_iface_uri)
268 assert False
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"
276 uri="%s"
277 xmlns="http://zero-install.sourceforge.net/2004/injector/interface">
278 <name>Foo</name>
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
285 policy.freshness = 0
287 try:
288 policy.recalculate()
289 assert False
290 except NeedDownload, ex:
291 pass
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"
304 uri="%s"
305 xmlns="http://zero-install.sourceforge.net/2004/injector/interface">
306 <name>Foo</name>
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
314 policy.recalculate()
315 assert not policy.ready, policy.implementation
316 try:
317 policy.download_and_execute([])
318 assert False
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"
326 uri="%s"
327 xmlns="http://zero-install.sourceforge.net/2004/injector/interface">
328 <name>Foo</name>
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)
335 policy.freshness = 0
336 policy.recalculate()
337 assert policy.ready
338 try:
339 policy.download_and_execute([])
340 assert False
341 except model.SafeException, ex:
342 assert 'no download locations' in str(ex), ex
344 def testCycle(self):
345 self.cache_iface(foo_iface_uri,
346 """<?xml version="1.0" ?>
347 <interface last-modified="1110752708"
348 uri="%s"
349 xmlns="http://zero-install.sourceforge.net/2004/injector/interface">
350 <name>Foo</name>
351 <summary>Foo</summary>
352 <description>Foo</description>
353 <group>
354 <requires interface='%s'/>
355 <implementation id='sha1=123' version='1.0'/>
356 </group>
357 </interface>""" % (foo_iface_uri, foo_iface_uri))
358 policy = autopolicy.AutoPolicy(foo_iface_uri,
359 download_only = False)
360 policy.freshness = 0
361 policy.recalculate()
363 def testConstraints(self):
364 self.cache_iface('http://bar',
365 """<?xml version="1.0" ?>
366 <interface last-modified="1110752708"
367 uri="http://bar"
368 xmlns="http://zero-install.sourceforge.net/2004/injector/interface">
369 <name>Bar</name>
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'/>
375 </interface>""")
376 self.cache_iface(foo_iface_uri,
377 """<?xml version="1.0" ?>
378 <interface last-modified="1110752708"
379 uri="%s"
380 xmlns="http://zero-install.sourceforge.net/2004/injector/interface">
381 <name>Foo</name>
382 <summary>Foo</summary>
383 <description>Foo</description>
384 <group>
385 <requires interface='http://bar'>
386 <version/>
387 </requires>
388 <implementation id='sha1=123' version='1.0'/>
389 </group>
390 </interface>""" % foo_iface_uri)
391 policy = autopolicy.AutoPolicy(foo_iface_uri,
392 download_only = False)
393 policy.network_use = model.network_full
394 policy.freshness = 0
395 #logger.setLevel(logging.DEBUG)
396 policy.recalculate()
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')
407 policy.recalculate()
408 assert policy.implementation[bar_iface].id == 'sha1=100'
410 restriction.not_before = model.parse_version('1.5')
411 policy.recalculate()
412 assert policy.implementation[bar_iface].id == 'sha1=150'
414 suite = unittest.makeSuite(TestAutoPolicy)
415 if __name__ == '__main__':
416 sys.argv.append('-v')
417 unittest.main()