import repository from arizona
[raven.git] / 2.0 / python / test / storktarTest.py
1 #! /usr/bin/env python
2 """
3 Stork Project (http://www.cs.arizona.edu/stork/)
4 Module: storktar
5
6 Author: Updated by Collin Reynolds
7
8 Description:   test module for storktar
9
10 Note : 
11    See storktar for more details
12 """
13
14
15 import storktar
16 import arizonaunittest
17 import arizonaconfig
18 import os
19 import shutil
20 import arizonageneral
21 import arizonareport
22
23 class test(arizonaunittest.TestCase):
24
25    #------------------------------------------------------------------------------
26    # initialize():
27    #------------------------------------------------------------------------------
28    def test_initialize(self):
29       dependencies = storktar.initialize()
30       self.assertEquals(dependencies,[])
31       
32       
33       
34
35
36
37    #------------------------------------------------------------------------------
38    # def is_package_understood(filename):
39    #------------------------------------------------------------------------------
40    def test_is_package_understood(self):
41       self.restore_stdout()
42       # A non-string
43       self.assertException(TypeError, storktar.is_package_understood, 3)
44
45       # The empty string
46       self.assertFalse(storktar.is_package_understood(""))
47
48       # A file that should exist and be readable
49       self.assertFalse(storktar.is_package_understood("/bin/ls"))
50
51       # A path
52       self.assertFalse(storktar.is_package_understood("/tmp"))
53
54       # An invalid path
55       self.assertFalse(storktar.is_package_understood("/jasdfljk/ajsdfjkadf/asjdf"))
56
57       # An invalid file
58       self.assertFalse(storktar.is_package_understood("/usr/bin/asjdf"))
59       try:
60          shutil.copyfile("/bin/ls", "/tmp/test1.tgz")
61       except:
62          pass
63       self.assertFalse(storktar.is_package_understood("/tmp/test1.tgz"))
64       os.unlink("/tmp/test1.tgz")
65       
66       # valid file
67       shutil.copyfile(os.getenv("HOME") + "/stork/webpage/example.tar.gz", "/tmp/example.tgz")
68       self.assertTrue(storktar.is_package_understood("/tmp/example.tgz"))
69       os.unlink("/tmp/example.tgz")
70       
71       
72       
73       
74       
75
76
77    #------------------------------------------------------------------------------
78    # get_packages_provide(filename_list)
79    #------------------------------------------------------------------------------
80    def test_get_packages_provide(self):
81       deps = storktar.get_packages_provide(["../test/storkendusertar-2.0b-30.tar.gz"])
82       self.assertEqual(deps,["storkendusertar = 2.0b-30"])
83       deps = storktar.get_packages_provide([])
84       self.assertEqual(deps,[])
85       deps = storktar.get_packages_provide(["../test/storkendusertar-2.0b-30.tar.gz","../test/example.tar.gz"])
86       self.assertEqual(deps,["storkendusertar = 2.0b-30","example = 0.0.0-0"])
87       
88       
89       
90       
91       
92       
93    #------------------------------------------------------------------------------   
94    # get_packages_require(filename_list)
95    #------------------------------------------------------------------------------
96    def test_get_packages_require(self):
97       # Doesn't return anything yet except an empty list
98       self.assertEqual(storktar.get_packages_require([""]),[])
99       self.assertException(TypeError,storktar.get_packages_require,123)
100       self.assertException(TypeError,storktar.get_packages_require,{})
101       self.assertException(TypeError,storktar.get_packages_require,[123])      
102       
103       
104       
105       
106       
107       
108    #------------------------------------------------------------------------------   
109    # get_packages_files(filename_list)
110    #------------------------------------------------------------------------------
111    def test_get_packages_files(self):
112       # Following needs to be modified so that it goes to the test directory every time
113       files = ["../test/example.tar.gz","../test/storkendusertar-2.0b-30.tar.gz"]
114       fileList = storktar.get_packages_files(files)
115       self.assertEquals(fileList,['example/example.py', 'stork-enduser-tar-2.0b/', 'stork-enduser-tar-2.0b/storkutil.py', 'stork-enduser-tar-2.0b/arizonaconfig.py', 'stork-enduser-tar-2.0b/arizonareport.py', 'stork-enduser-tar-2.0b/storkerror.py', 'stork-enduser-tar-2.0b/arizonacrypt.py', 'stork-enduser-tar-2.0b/arizonaxml.py', 'stork-enduser-tar-2.0b/storkpackage.py', 'stork-enduser-tar-2.0b/arizonageneral.py', 'stork-enduser-tar-2.0b/storkpackagelist.py', 'stork-enduser-tar-2.0b/arizonatransfer.py', 'stork-enduser-tar-2.0b/securerandom.py', 'stork-enduser-tar-2.0b/storktransaction.py', 'stork-enduser-tar-2.0b/comonscript.py', 'stork-enduser-tar-2.0b/package/', 'stork-enduser-tar-2.0b/package/__init__.py', 'stork-enduser-tar-2.0b/package/storktar.py', 'stork-enduser-tar-2.0b/package/storkrpm.py', 'stork-enduser-tar-2.0b/package/storknestrpm.py', 'stork-enduser-tar-2.0b/default.publickey', 'stork-enduser-tar-2.0b/sample-stork.conf'])
116       
117       
118       
119       
120       
121       
122       
123    #------------------------------------------------------------------------------   
124    # get_package_info(filename)
125    #------------------------------------------------------------------------------   
126    def test_get_package_info(self):
127       filename = "../test/storkendusertar-2.0b-30.tar.gz"
128       infoList = storktar.get_package_info(filename)
129       self.assertEquals(infoList,['storkendusertar', '2.0b', '30', '419163'])
130       
131       filename = "../test/example.tar.gz"
132       infoList = storktar.get_package_info(filename)
133       self.assertEquals(infoList,['example', '0.0.0', '0', '1423'])      
134       
135       
136       
137       
138       
139
140    #------------------------------------------------------------------------------
141    # get_package_names(filename_list):
142    # Not implemented yet
143    #------------------------------------------------------------------------------
144    def test_get_package_names(self):
145       pass
146       
147       
148       
149       
150       
151       
152    #------------------------------------------------------------------------------
153    # get_installedpackages_fulfilling(dep_list):
154    #------------------------------------------------------------------------------
155    def test_get_installedpackages_fulfilling(self): 
156       # create a info file 
157       packinfo_dir = arizonaconfig.get_option("tarpackinfopath")
158       if not os.path.exists(packinfo_dir):
159          os.mkdir(packinfo_dir)
160       testpack1 = "testpack1.packinfo"
161       tempfile = file(packinfo_dir + "/"+ testpack1, "w")
162       tempfile.write("testing1\nversion\n3.5\nTest pack")
163       tempfile.close()
164       
165       # check it with package name 'testpack1'
166       self.assertEqual(storktar.get_installedpackages_fulfilling(["testpack1"]), ["testing1-version-3.5"])      
167
168       # create another info file
169       testpack2 = "testpack2.tgz.packinfo"
170       tempfile = file(packinfo_dir + "/"+ testpack2, "w")
171       tempfile.write("testing2\nversion\n3.14\nTest pack")
172       tempfile.close()
173       
174       # check it with package name 'testpack1'
175       self.assertEqual(storktar.get_installedpackages_fulfilling(["testpack2"]), ["testing2-version-3.14"])      
176       
177       # create 3rd info file
178       testpack3 = "testpack3.tar.gz.packinfo"
179       tempfile = file(packinfo_dir + "/"+ testpack3, "w")
180       tempfile.write("testing3\nversion\n2.93\nTest pack3")
181       tempfile.close()
182       
183       # check it with package name 'testpack1'
184       self.assertEqual(storktar.get_installedpackages_fulfilling(["testpack3"]), ["testing3-version-2.93"])      
185
186       # put these files all together
187       self.assertEqual(storktar.get_installedpackages_fulfilling(["testpack1", "testpack2", "testpack3"]), ['testing1-version-3.5', 'testing2-version-3.14', 'testing3-version-2.93'])      
188
189       # some packages of the package list don't exist
190       self.assertEqual(storktar.get_installedpackages_fulfilling(["testpack1", "rpm", "testpack2", "ls",  "nano", "testpack3"]), 
191                                                          ['testing1-version-3.5', 'testing2-version-3.14', 'testing3-version-2.93'])            
192       
193
194       # remove testing files and directory
195       os.remove(packinfo_dir + "/" + testpack1)
196       os.remove(packinfo_dir + "/" + testpack2)
197       os.remove(packinfo_dir + "/" + testpack3)
198       os.rmdir(packinfo_dir)
199       
200       
201       
202       
203       
204
205    #------------------------------------------------------------------------------
206    # get_installedpackages_provide(package_list):
207    #------------------------------------------------------------------------------
208    def test_get_installedpackages_provide(self):
209       # create a info file 
210       packinfo_dir = arizonaconfig.get_option("tarpackinfopath")
211       if not os.path.exists(packinfo_dir):
212          os.mkdir(packinfo_dir)
213       testpack1 = "testpack1.packinfo"
214       tempfile = file(packinfo_dir + "/"+ testpack1, "w")
215       tempfile.write("testing1\nversion\n3.5\nTest pack")
216       tempfile.close()
217       
218       # check it with package name 'testpack1'
219       self.assertEqual(storktar.get_installedpackages_provide(["testpack1"]), ["testing1 = version-3.5"])      
220
221       # create another info file
222       testpack2 = "testpack2.tgz.packinfo"
223       tempfile = file(packinfo_dir + "/"+ testpack2, "w")
224       tempfile.write("testing2\nversion\n3.14\nTest pack")
225       tempfile.close()
226       
227       # check it with package name 'testpack1'
228       self.assertEqual(storktar.get_installedpackages_provide(["testpack2"]), ["testing2 = version-3.14"])      
229       
230       # create 3rd info file
231       testpack3 = "testpack3.tar.gz.packinfo"
232       tempfile = file(packinfo_dir + "/"+ testpack3, "w")
233       tempfile.write("testing3\nversion\n2.93\nTest pack3")
234       tempfile.close()
235       
236       # check it with package name 'testpack1'
237       self.assertEqual(storktar.get_installedpackages_provide(["testpack3"]), ["testing3 = version-2.93"])      
238
239       # put these files all together
240       self.assertEqual(storktar.get_installedpackages_provide(["testpack1", "testpack2", "testpack3"]), ['testing1 = version-3.5', 'testing2 = version-3.14', 'testing3 = version-2.93'])      
241
242       # some packages of the package list don't exist
243       self.assertEqual(storktar.get_installedpackages_provide(["testpack1", "rpm", "testpack2", "ls",  "nano", "testpack3"]), 
244                                                          ['testing1 = version-3.5', 'testing2 = version-3.14', 'testing3 = version-2.93'])            
245       
246
247       # remove testing files and directory
248       os.remove(packinfo_dir + "/" + testpack1)
249       os.remove(packinfo_dir + "/" + testpack2)
250       os.remove(packinfo_dir + "/" + testpack3)
251       os.rmdir(packinfo_dir)
252       
253       
254       
255       
256       
257
258    #------------------------------------------------------------------------------
259    # get_installed_versions(package_list):
260    #------------------------------------------------------------------------------
261    def test_get_installed_versions(self):
262       self.restore_stdout()
263       # A non-string
264       self.assertException(TypeError, storktar.get_installed_versions, 3)
265       # a list of ints
266       self.assertException(TypeError, storktar.get_installed_versions, [3, 5, 7])
267       
268       # obviously not-existing package
269       self.assertEqual(storktar.get_installed_versions([""]), [])      
270
271       # another not-existing package
272       self.assertEqual(storktar.get_installed_versions(["rpm"]), [])      
273
274       # create a info file 
275       packinfo_dir = arizonaconfig.get_option("tarpackinfopath")
276       if not os.path.exists(packinfo_dir):
277          os.mkdir(packinfo_dir)
278       testpack1 = "testpack1.packinfo"
279       tempfile = file(packinfo_dir + "/"+ testpack1, "w")
280       tempfile.write("testing1\nversion\n3.5\nTest pack")
281       tempfile.close()
282       
283       # check it with package name 'testpack1'
284       self.assertEqual(storktar.get_installed_versions(["testpack1"]), ["testing1 = version-3.5"])      
285
286       # create another info file
287       testpack2 = "testpack2.tgz.packinfo"
288       tempfile = file(packinfo_dir + "/"+ testpack2, "w")
289       tempfile.write("testing2\nversion\n3.14\nTest pack")
290       tempfile.close()
291       
292       # check it with package name 'testpack1'
293       self.assertEqual(storktar.get_installed_versions(["testpack2"]), ["testing2 = version-3.14"])      
294       
295       # create 3rd info file
296       testpack3 = "testpack3.tar.gz.packinfo"
297       tempfile = file(packinfo_dir + "/"+ testpack3, "w")
298       tempfile.write("testing3\nversion\n2.93\nTest pack3")
299       tempfile.close()
300       
301       # check it with package name 'testpack1'
302       self.assertEqual(storktar.get_installed_versions(["testpack3"]), ["testing3 = version-2.93"])      
303
304       # put these files all together
305       self.assertEqual(storktar.get_installed_versions(["testpack1", "testpack2", "testpack3"]), ['testing1 = version-3.5', 'testing2 = version-3.14', 'testing3 = version-2.93'])      
306
307       # some packages of the package list don't exist
308       self.assertEqual(storktar.get_installed_versions(["testpack1", "rpm", "testpack2", "ls",  "nano", "testpack3"]), 
309                                                          ['testing1 = version-3.5', 'testing2 = version-3.14', 'testing3 = version-2.93'])            
310       
311
312       # remove testing files and directory
313       os.remove(packinfo_dir + "/" + testpack1)
314       os.remove(packinfo_dir + "/" + testpack2)
315       os.remove(packinfo_dir + "/" + testpack3)
316       os.rmdir(packinfo_dir)
317       
318       
319       
320       
321
322    #------------------------------------------------------------------------------      
323    # get_installedpackages_requiring(deplist)
324    # not implemented yet
325    #------------------------------------------------------------------------------
326    def test_get_installedpackages_requiring(self):
327       pass
328       
329       
330       
331       
332       
333
334    #------------------------------------------------------------------------------      
335    # get_installedpackages()
336    # not implemented
337    #------------------------------------------------------------------------------
338    def test_get_installedpackages(self):
339       pass
340       
341       
342       
343       
344       
345
346
347    #------------------------------------------------------------------------------   
348    # get_installedpackages_requires(package_list)
349    # not implemented
350    #------------------------------------------------------------------------------
351    def test_get_installedpackages_requires(self):
352       pass   
353       
354       
355       
356       
357       
358
359    #------------------------------------------------------------------------------      
360    # execute(trans_list):
361    #------------------------------------------------------------------------------
362    def test_execute(self):
363       import storktransaction
364       tl = storktransaction.tl_create()
365       #Just test a blank transaction list to execute
366       storktar.execute(tl)      
367       
368       
369       
370       
371       
372       
373       
374       
375       
376    #------------------------------------------------------------------------------      
377    # determine_package_type_no_re(filename)
378    #------------------------------------------------------------------------------
379    def test_determine_package_type_no_re(self):
380       self.assertEqual(storktar.determine_package_type_no_re("../test/example.tar.gz"),"z")
381       
382       
383       
384       
385       
386
387
388    #------------------------------------------------------------------------------
389    # remove(package_list):
390    #------------------------------------------------------------------------------
391    def test_remove(self):
392       self.restore_stdout()
393       # A non-string
394       self.assertException(TypeError, storktar.remove, 3)
395       # a list of ints
396       self.assertException(TypeError, storktar.remove, [3, 5, 7])
397
398       # The empty string
399       self.assertException(arizonageneral.Exception_Data ,storktar.remove, [""])
400
401       # invalid package name
402       self.assertException(arizonageneral.Exception_Data ,storktar.remove, ["dhsfkj"])
403
404
405
406    def __check_files(self, pack):
407       (out, err, status) = arizonageneral.popen5("tar -Pztf " + pack)
408
409       if out[0][0] != "/":
410          path_head = os.getenv("HOME") + "/"
411       else:
412          path_head = ""
413
414       for afile in out:
415          afile = afile.strip('\n')
416
417          if not os.path.exists(path_head + afile):
418             print "\nsome of files on package " + pack + " has not installed"
419             os._exit(1)
420
421 #=====================================================================
422 # Run tests
423 #=====================================================================
424 if __name__=='__main__':
425    arizonaconfig.init_options("tarpackinfopath") 
426    arizonaconfig.set_option("tarpackinfopath", "/tmp/tar_packinfo")
427    arizonaunittest.main()