import repository from arizona
[raven.git] / 2.0 / python / test / arizonatransfer_bittorrentTest.py
1 #! /usr/bin/env python
2 """
3 Stork Project (http://www.cs.arizona.edu/stork/)
4 Module: arizonatransfer_bittorrentTest
5 Description:   test module for arizona_bittorrent
6
7 Notes:
8    See arizonatransfer_bittorrent for more details.
9    #Make sure that /tmp/http_test directory doesn't exist or is empty
10    #before run this test.
11    Modified to be more generic (for similar transfer methods, only 2 lines
12    would need to be modified).
13    Now ensures that the test directory is empty prior to use.
14 """
15
16 import arizonatransfer_bittorrent as transfermethod
17 import arizonaunittest as unittestlib
18 import arizonaconfig
19 import os
20
21 test_dir='/tmp/transfer_testdir'
22 transfermethod_name="arizona_bittorrent"
23 test_server="stork-repository.cs.arizona.edu"
24
25 class test(unittestlib.TestCase):
26
27
28    #------------------------------------------------------------------------------
29    # close_transfer_program():
30    #------------------------------------------------------------------------------
31    def test_close_transfer_program(self):
32       pass
33
34
35    #------------------------------------------------------------------------------
36    # init_transfer_program(uname=None,passwd=None,prt=None,ignore4=None):
37    #------------------------------------------------------------------------------
38    def test_init_transfer_program(self):
39       pass
40
41
42    #------------------------------------------------------------------------------
43    # retrieve_files(host, filelist,hashlist, destdir, progress_indicator)
44    #------------------------------------------------------------------------------
45    def test_retrieve_files(self):
46       global test_dir
47       global test_server
48       # basic set-up for this test      
49       host = test_server+'/packages/PlanetLab/V3/Distribution/7173f45b26761e23c7bb96ff83f004e7e1bef2e7'
50       # one of the files in the directory
51       files = ['gnuchess-5.02-11.i386.rpm']
52       # a temp directory where retrieved files will be placed
53       #direc = '/tmp/http_testdir'
54       # make sure temp dir is empty (errors caused during this part of the test may leave things behind)
55       if os.path.exists(test_dir):
56          self.__remove_files([test_dir+"/"+i for i in os.listdir(test_dir)])
57       else:os.makedirs(test_dir)
58       
59       self.restore_stdout()      
60
61       # retrieve one file
62       result = [test_dir+"/"+files[0]]
63       self.assertEqual(transfermethod.retrieve_files(host, files,[""],test_dir),(True, result))
64       self.__remove_files(result)
65
66       # retrieve several files
67       files = ['gnuchess-5.02-11.i386.rpm', 'sync_file2']
68       result = [test_dir+'/'+files[0]]
69       self.assertEqual(transfermethod.retrieve_files(host, files,[""],test_dir),(True, result))
70       self.__remove_files(result)            
71
72       # if indicator passed in is incorret
73       self.assertEqual(transfermethod.retrieve_files(host, files,[""],test_dir, 3),(False, []))
74       self.assertEqual(transfermethod.retrieve_files(host, files,[""],test_dir, 'a'),(False, []))
75       self.assertEqual(transfermethod.retrieve_files(host, files,[""],test_dir, unittestlib),(False, []))
76
77       # if host is not a string
78       self.assertEqual(transfermethod.retrieve_files(3, files,[""],test_dir),(False, []))      
79
80       # if hostname is wrong
81       wronghost = test_server+'/test'
82       self.assertEqual(transfermethod.retrieve_files(wronghost, files, [""],test_dir),(False, []))
83       wronghost = test_server+'/test/byu'
84       self.assertEqual(transfermethod.retrieve_files(wronghost, files, [""],test_dir),(False, []))
85
86       # if filelist is not a list
87       self.assertEqual(transfermethod.retrieve_files(host, 'a', [""],test_dir),(False, []))      
88       self.assertEqual(transfermethod.retrieve_files(host, 3, [""],test_dir),(False, []))      
89
90       # if filelist is not a list of strings
91       self.assertEqual(transfermethod.retrieve_files(host, ['a', 3, 'b'], [""],test_dir),(False, []))      
92       
93       # if test_dir is not a string
94       self.assertEqual(transfermethod.retrieve_files(host, files, [""],3),(False, []))      
95
96       # if test_dir does not exist
97       self.assertEqual(transfermethod.retrieve_files(host, files, [""],'/no_such_dir'),(False, []))      
98             
99       # if try to retrieve files which don't exist
100       wrongfiles = ['a']
101       self.assertEqual(transfermethod.retrieve_files(host, wrongfiles, [""],test_dir),(False, []))
102       wrongfiles = ['a', 'test', 'never']
103       self.assertEqual(transfermethod.retrieve_files(host, wrongfiles, [""],test_dir),(False, []))
104
105
106       # if try to retrieve files some of which don't exist
107       wrongfiles = ['never', 'gnuchess-5.02-11.i386.rpm']
108       result = [test_dir+'/gnuchess-5.02-11.i386.rpm']
109       self.assertEqual(transfermethod.retrieve_files(host, wrongfiles, [""],test_dir),(True, result))
110       self.__remove_files(result)
111       wrongfiles = ['a', 'metafile', 'never', 'gnuchess-5.02-11.i386.rpm']
112       result = [test_dir+'/gnuchess-5.02-11.i386.rpm']
113       self.assertEqual(transfermethod.retrieve_files(host, wrongfiles, [""],test_dir),(True, result))
114       self.__remove_files(result)
115
116       # if try to retrieve files that a user doesn't have permission to read
117       host = test_server+'/test'
118       files = ['auth.py']
119       self.assertEqual(transfermethod.retrieve_files(host, files, [""],test_dir),(False, []))      
120       self.__remove_files(result)
121
122       os.rmdir(test_dir)
123
124    #------------------------------------------------------------------------------
125    # transfer_name():
126    #------------------------------------------------------------------------------
127    def test_transfer_name(self):
128       global transfermethod_name
129       self.assertEqual(transfermethod.transfer_name(),transfermethod_name)
130       
131
132          
133
134    # helper func to remove files in list
135    def __remove_files(self, filelist):
136       for fname in filelist:
137          try:
138             os.unlink(fname)
139          except:
140             pass
141
142 #=====================================================================
143 # Run tests
144 #=====================================================================
145 if __name__ == '__main__':
146    unittestlib.main()