Fixed some problems with the download unit tests.
[zeroinstall.git] / tests / testdownload.py
bloba6eb2b2bb48e52564f43fc890d912c6792feacaa
1 #!/usr/bin/env python2.3
2 import sys, tempfile, os, shutil
3 from StringIO import StringIO
4 import unittest, signal
5 from logging import getLogger, DEBUG, INFO
6 #getLogger().setLevel(DEBUG)
8 sys.path.insert(0, '..')
10 from zeroinstall.injector import model, basedir, autopolicy, gpg, iface_cache, download
11 import data
13 import server
15 class Reply:
16 def __init__(self, reply):
17 self.reply = reply
19 def readline(self):
20 return self.reply
22 class TestDownload(unittest.TestCase):
23 def setUp(self):
24 self.config_home = tempfile.mktemp()
25 self.cache_home = tempfile.mktemp()
26 os.environ['XDG_CONFIG_HOME'] = self.config_home
27 os.environ['XDG_CACHE_HOME'] = self.cache_home
28 os.environ['XDG_CACHE_DIRS'] = ''
29 reload(basedir)
31 os.mkdir(self.config_home, 0700)
32 os.mkdir(self.cache_home, 0700)
33 if os.environ.has_key('DISPLAY'):
34 del os.environ['DISPLAY']
35 self.gnupg_home = tempfile.mktemp()
36 os.environ['GNUPGHOME'] = self.gnupg_home
37 os.mkdir(self.gnupg_home, 0700)
38 stream = tempfile.TemporaryFile()
39 stream.write(data.thomas_key)
40 stream.seek(0)
41 gpg.import_key(stream)
42 iface_cache.iface_cache.__init__()
43 download._downloads = {}
44 self.child = None
46 def tearDown(self):
47 if self.child is not None:
48 os.kill(self.child, signal.SIGTERM)
49 os.waitpid(self.child, 0)
50 self.child = None
51 shutil.rmtree(self.config_home)
52 shutil.rmtree(self.cache_home)
53 shutil.rmtree(self.gnupg_home)
55 def testRejectKey(self):
56 old_out = sys.stdout
57 try:
58 sys.stdout = StringIO()
59 self.child = server.handle_requests('Hello', '6FCF121BE2390E0B.gpg')
60 policy = autopolicy.AutoPolicy('http://localhost:8000/Hello', download_only = False)
61 assert policy.need_download()
62 sys.stdin = Reply("N\n")
63 try:
64 policy.download_and_execute(['Hello'])
65 assert 0
66 except model.SafeException, ex:
67 if "Not signed with a trusted key" not in str(ex):
68 raise ex
69 finally:
70 sys.stdout = old_out
72 def testAcceptKey(self):
73 old_out = sys.stdout
74 try:
75 sys.stdout = StringIO()
76 self.child = server.handle_requests('Hello', '6FCF121BE2390E0B.gpg', 'HelloWorld.tgz')
77 policy = autopolicy.AutoPolicy('http://localhost:8000/Hello', download_only = False)
78 assert policy.need_download()
79 sys.stdin = Reply("Y\n")
80 try:
81 policy.download_and_execute(['Hello'], main = 'Missing')
82 assert 0
83 except model.SafeException, ex:
84 if "HelloWorld/Missing" not in str(ex):
85 raise ex
86 finally:
87 sys.stdout = old_out
89 def testRecipe(self):
90 old_out = sys.stdout
91 try:
92 sys.stdout = StringIO()
93 self.child = server.handle_requests(('HelloWorld.tar.bz2', 'dummy_1-1_all.deb'))
94 policy = autopolicy.AutoPolicy(os.path.abspath('Recipe.xml'), download_only = False)
95 try:
96 policy.download_and_execute([])
97 assert False
98 except model.SafeException, ex:
99 if "HelloWorld/Missing" not in str(ex):
100 raise ex
101 finally:
102 sys.stdout = old_out
104 def testAutopackage(self):
105 old_out = sys.stdout
106 try:
107 sys.stdout = StringIO()
108 self.child = server.handle_requests('HelloWorld.autopackage')
109 policy = autopolicy.AutoPolicy(os.path.abspath('Autopackage.xml'), download_only = False)
110 try:
111 policy.download_and_execute([])
112 assert False
113 except model.SafeException, ex:
114 if "HelloWorld/Missing" not in str(ex):
115 raise ex
116 finally:
117 sys.stdout = old_out
119 def testRecipeFailure(self):
120 old_out = sys.stdout
121 try:
122 sys.stdout = StringIO()
123 self.child = server.handle_requests('HelloWorld.tar.bz2')
124 policy = autopolicy.AutoPolicy(os.path.abspath('Recipe.xml'), download_only = False)
125 try:
126 policy.download_and_execute([])
127 assert False
128 except download.DownloadError, ex:
129 if "Connection" not in str(ex):
130 raise ex
131 finally:
132 sys.stdout = old_out
134 suite = unittest.makeSuite(TestDownload)
135 if __name__ == '__main__':
136 sys.argv.append('-v')
137 unittest.main()