1+ """
2+ Run the test suite like:
3+ python test.py
4+ You'll get a statement printed
5+ if all of the tests pass.
6+ """
7+
18import os
29import sys
310import subprocess
@@ -19,6 +26,14 @@ def run_cmd(cmd):
1926 cmd = cmd .split ()
2027 p = subprocess .Popen (cmd , stdout = subprocess .PIPE , stderr = subprocess .PIPE )
2128 output , err = p .communicate ()
29+
30+ # When running in Python 3, the output of subprocess.Popen.communicate()
31+ # is a bytes object. We need to convert it to a string.
32+ if isinstance (output , bytes ):
33+ output = output .decode ("utf-8" )
34+ if isinstance (err , bytes ):
35+ err = err .decode ("utf-8" )
36+
2237 print (output )
2338 print (err )
2439 return output , err
@@ -52,6 +67,8 @@ def cleanup():
5267 shutil .rmtree ('zstash_test' )
5368 if os .path .exists ('zstash_test_backup' ):
5469 shutil .rmtree ('zstash_test_backup' )
70+ if os .path .exists ('zstash' ):
71+ shutil .rmtree ('zstash' )
5572 cmd = 'hsi rm -R {}' .format (HPSS_PATH )
5673 run_cmd (cmd )
5774
@@ -62,9 +79,9 @@ def stop():
6279 cleanup ()
6380 sys .exit ()
6481
65-
66- # TODO: Change the hpss directory to a dir that's accessable to everyone.
67- HPSS_PATH = '/home/z/zshaheen/ zstash_test'
82+ # Makes this in the home dir of the user on HPSS.
83+ # Ex: /home/z/zshaheen/zstash_test
84+ HPSS_PATH = 'zstash_test'
6885
6986# Create files and directories
7087print ('Creating files.' )
@@ -92,29 +109,16 @@ def stop():
92109if not os .path .lexists ('zstash_test/file0_hard.txt' ):
93110 os .link ('zstash_test/file0.txt' , 'zstash_test/file0_hard.txt' )
94111
112+
95113print ('Adding files to HPSS' )
96114cmd = 'zstash create --hpss={} zstash_test' .format (HPSS_PATH )
97115output , err = run_cmd (cmd )
98116str_in (output + err , 'Transferring file to HPSS' )
99117str_not_in (output + err , 'ERROR' )
100118
101- print ('Testing ls' )
102- cmd = 'zstash ls --hpss={}' .format (HPSS_PATH )
103- output , err = run_cmd (cmd )
104- str_in (output + err , 'file0.txt' )
105- str_not_in (output + err , 'ERROR' )
106- cmd = 'zstash ls -l --hpss={}' .format (HPSS_PATH )
107- output , err = run_cmd (cmd )
108- str_in (output + err , 'tar' )
109- str_not_in (output + err , 'ERROR' )
110119
111120print ('Testing chgrp' )
112121GROUP = 'acme'
113- print ('First, make sure that the files are not already in the {} group' .format (GROUP ))
114- cmd = 'hsi ls -l {}' .format (HPSS_PATH )
115- output , err = run_cmd (cmd )
116- str_not_in (output + err , GROUP )
117- str_not_in (output + err , 'ERROR' )
118122print ('Running zstash chgrp' )
119123cmd = 'zstash chgrp -R {} {}' .format (GROUP , HPSS_PATH )
120124output , err = run_cmd (cmd )
@@ -125,6 +129,7 @@ def stop():
125129str_in (output + err , 'acme' )
126130str_not_in (output + err , 'ERROR' )
127131
132+
128133print ('Running update on the newly created directory, nothing should happen' )
129134os .chdir ('zstash_test' )
130135cmd = 'zstash update --hpss={}' .format (HPSS_PATH )
@@ -133,6 +138,7 @@ def stop():
133138str_in (output + err , 'Nothing to update' )
134139str_not_in (output + err , 'ERROR' )
135140
141+
136142print ('Testing update with an actual change' )
137143if not os .path .exists ('zstash_test/dir2' ):
138144 os .mkdir ('zstash_test/dir2' )
@@ -145,24 +151,108 @@ def stop():
145151os .chdir ('../' )
146152str_in (output + err , 'Transferring file to HPSS' )
147153str_not_in (output + err , 'ERROR' )
148- # Make sure none of the old files are moved
154+ # Make sure none of the old files are moved.
149155str_not_in (output + err , 'file0' )
150156str_not_in (output + err , 'file_empty' )
151157str_not_in (output + err , 'empty_dir' )
152158str_not_in (output + err , 'ERROR' )
153159
160+
161+ print ('Adding many more files to the HPSS archive.' )
162+ msg = 'This is because we need many separate tar archives'
163+ msg += ' for testing zstash extract/check with parallel.'
164+ print (msg )
165+ write_file ('zstash_test/file3.txt' , 'file3 stuff' )
166+ os .chdir ('zstash_test' )
167+ cmd = 'zstash update --hpss={}' .format (HPSS_PATH )
168+ output , err = run_cmd (cmd )
169+ os .chdir ('../' )
170+ write_file ('zstash_test/file4.txt' , 'file4 stuff' )
171+ os .chdir ('zstash_test' )
172+ cmd = 'zstash update --hpss={}' .format (HPSS_PATH )
173+ output , err = run_cmd (cmd )
174+ os .chdir ('../' )
175+ write_file ('zstash_test/file5.txt' , 'file5 stuff' )
176+ os .chdir ('zstash_test' )
177+ cmd = 'zstash update --hpss={}' .format (HPSS_PATH )
178+ output , err = run_cmd (cmd )
179+ os .chdir ('../' )
180+
181+
182+ print ('Testing ls' )
183+ cmd = 'zstash ls --hpss={}' .format (HPSS_PATH )
184+ output , err = run_cmd (cmd )
185+ str_in (output + err , 'file0.txt' )
186+ str_not_in (output + err , 'ERROR' )
187+ cmd = 'zstash ls -l --hpss={}' .format (HPSS_PATH )
188+ output , err = run_cmd (cmd )
189+ str_in (output + err , 'tar' )
190+ str_not_in (output + err , 'ERROR' )
191+
192+
154193print ('Testing the checking functionality' )
155194cmd = 'zstash check --hpss={}' .format (HPSS_PATH )
156195output , err = run_cmd (cmd )
196+ str_in (output + err , 'Checking file0.txt' )
197+ str_in (output + err , 'Checking file0_hard.txt' )
198+ str_in (output + err , 'Checking file0_soft.txt' )
199+ str_in (output + err , 'Checking file_empty.txt' )
200+ str_in (output + err , 'Checking dir/file1.txt' )
201+ str_in (output + err , 'Checking empty_dir' )
202+ str_in (output + err , 'Checking dir2/file2.txt' )
203+ str_in (output + err , 'Checking file3.txt' )
204+ str_in (output + err , 'Checking file4.txt' )
205+ str_in (output + err , 'Checking file5.txt' )
157206str_not_in (output + err , 'ERROR' )
158207
208+
159209print ('Testing the extract functionality' )
160210os .rename ('zstash_test' , 'zstash_test_backup' )
161211os .mkdir ('zstash_test' )
162212os .chdir ('zstash_test' )
163213cmd = 'zstash extract --hpss={}' .format (HPSS_PATH )
164214output , err = run_cmd (cmd )
165215os .chdir ('../' )
216+ str_in (output + err , 'Extracting file0.txt' )
217+ str_in (output + err , 'Extracting file0_hard.txt' )
218+ str_in (output + err , 'Extracting file0_soft.txt' )
219+ str_in (output + err , 'Extracting file_empty.txt' )
220+ str_in (output + err , 'Extracting dir/file1.txt' )
221+ str_in (output + err , 'Extracting empty_dir' )
222+ str_in (output + err , 'Extracting dir2/file2.txt' )
223+ str_in (output + err , 'Extracting file3.txt' )
224+ str_in (output + err , 'Extracting file4.txt' )
225+ str_in (output + err , 'Extracting file5.txt' )
226+ str_not_in (output + err , 'ERROR' )
227+ str_not_in (output + err , 'Not extracting' )
228+
229+ print ('Testing the extract functionality again, nothing should happen' )
230+ os .chdir ('zstash_test' )
231+ cmd = 'zstash extract --hpss={}' .format (HPSS_PATH )
232+ output , err = run_cmd (cmd )
233+ os .chdir ('../' )
234+ str_in (output + err , 'Not extracting file0.txt' )
235+ str_in (output + err , 'Not extracting file0_hard.txt' )
236+ # It's okay to extract the symlinks.
237+ str_not_in (output + err , 'Not extracting file0_soft.txt' )
238+ str_in (output + err , 'Not extracting file_empty.txt' )
239+ str_in (output + err , 'Not extracting dir/file1.txt' )
240+ # It's okay to extract empty dirs.
241+ str_not_in (output + err , 'Not extracting empty_dir' )
242+ str_in (output + err , 'Not extracting dir2/file2.txt' )
243+ str_in (output + err , 'Not extracting file3.txt' )
244+ str_in (output + err , 'Not extracting file4.txt' )
245+ str_in (output + err , 'Not extracting file5.txt' )
246+ str_not_in (output + err , 'ERROR' )
247+
248+
249+ print ('Deleting the extracted files and doing it again in parallel.' )
250+ shutil .rmtree ('zstash_test' )
251+ os .mkdir ('zstash_test' )
252+ os .chdir ('zstash_test' )
253+ cmd = 'zstash extract --hpss={} --workers=3' .format (HPSS_PATH )
254+ output , err = run_cmd (cmd )
255+ os .chdir ('../' )
166256str_in (output + err , 'Transferring from HPSS' )
167257str_in (output + err , 'Extracting file0.txt' )
168258str_in (output + err , 'Extracting file0_hard.txt' )
@@ -171,19 +261,93 @@ def stop():
171261str_in (output + err , 'Extracting dir/file1.txt' )
172262str_in (output + err , 'Extracting empty_dir' )
173263str_in (output + err , 'Extracting dir2/file2.txt' )
264+ str_in (output + err , 'Extracting file3.txt' )
265+ str_in (output + err , 'Extracting file4.txt' )
266+ str_in (output + err , 'Extracting file5.txt' )
174267str_not_in (output + err , 'ERROR' )
268+ str_not_in (output + err , 'Not extracting' )
269+ # Checking that the printing was done in order.
270+ tar_order = []
271+ console_output = output + err
272+ for word in console_output .replace ('\n ' , ' ' ).split (' ' ):
273+ if '.tar' in word :
274+ word = word .replace ('zstash/' , '' )
275+ tar_order .append (word )
276+ if tar_order != sorted (tar_order ):
277+ print ('*' * 40 )
278+ print ('The tars were printed in this order: {}' .format (tar_order ))
279+ print ('When it should have been in this order: {}' .format (sorted (tar_order )))
280+ print ('*' * 40 )
281+ stop ()
282+
175283
176- print ('Running update on the newly extracted directory, nothing should happen ' )
284+ print ('Checking the files again in parallel. ' )
177285os .chdir ('zstash_test' )
178- cmd = 'zstash update --hpss={}' .format (HPSS_PATH )
286+ cmd = 'zstash check --hpss={} --workers=3 ' .format (HPSS_PATH )
179287output , err = run_cmd (cmd )
180288os .chdir ('../' )
181- str_in (output + err , 'Nothing to update' )
289+ str_in (output + err , 'Checking file0.txt' )
290+ str_in (output + err , 'Checking file0_hard.txt' )
291+ str_in (output + err , 'Checking file0_soft.txt' )
292+ str_in (output + err , 'Checking file_empty.txt' )
293+ str_in (output + err , 'Checking dir/file1.txt' )
294+ str_in (output + err , 'Checking empty_dir' )
295+ str_in (output + err , 'Checking dir2/file2.txt' )
296+ str_in (output + err , 'Checking file3.txt' )
297+ str_in (output + err , 'Checking file4.txt' )
298+ str_in (output + err , 'Checking file5.txt' )
182299str_not_in (output + err , 'ERROR' )
183300
301+
302+ print ('Causing MD5 mismatch errors and checking the files.' )
303+ os .chdir ('zstash_test' )
304+ shutil .copy ('zstash/index.db' , 'zstash/index_old.db' )
305+ print ('Messing up the MD5 of all of the files with an even id.' )
306+ cmd = ['sqlite3' , 'zstash/index.db' , 'UPDATE files SET md5 = 0 WHERE id % 2 = 0;' ]
307+ run_cmd (cmd )
308+ cmd = 'zstash check --hpss={}' .format (HPSS_PATH )
309+ output , err = run_cmd (cmd )
310+ str_in (output + err , 'md5 mismatch for: dir/file1.txt' )
311+ str_in (output + err , 'md5 mismatch for: file3.txt' )
312+ str_in (output + err , 'md5 mismatch for: file3.txt' )
313+ str_in (output + err , 'ERROR: 000001.tar' )
314+ str_in (output + err , 'ERROR: 000004.tar' )
315+ str_in (output + err , 'ERROR: 000002.tar' )
316+ str_not_in (output + err , 'ERROR: 000000.tar' )
317+ str_not_in (output + err , 'ERROR: 000003.tar' )
318+ str_not_in (output + err , 'ERROR: 000005.tar' )
319+ # Put the original index.db back.
320+ os .remove ('zstash/index.db' )
321+ shutil .copy ('zstash/index_old.db' , 'zstash/index.db' )
322+ os .chdir ('../' )
323+
324+
325+ print ('Causing MD5 mismatch errors and checking the files in parallel.' )
326+ os .chdir ('zstash_test' )
327+ shutil .copy ('zstash/index.db' , 'zstash/index_old.db' )
328+ print ('Messing up the MD5 of all of the files with an even id.' )
329+ cmd = ['sqlite3' , 'zstash/index.db' , 'UPDATE files SET md5 = 0 WHERE id % 2 = 0;' ]
330+ run_cmd (cmd )
331+ cmd = 'zstash check --hpss={} --workers=3' .format (HPSS_PATH )
332+ output , err = run_cmd (cmd )
333+ str_in (output + err , 'md5 mismatch for: dir/file1.txt' )
334+ str_in (output + err , 'md5 mismatch for: file3.txt' )
335+ str_in (output + err , 'md5 mismatch for: file3.txt' )
336+ str_in (output + err , 'ERROR: 000001.tar' )
337+ str_in (output + err , 'ERROR: 000004.tar' )
338+ str_in (output + err , 'ERROR: 000002.tar' )
339+ str_not_in (output + err , 'ERROR: 000000.tar' )
340+ str_not_in (output + err , 'ERROR: 000003.tar' )
341+ str_not_in (output + err , 'ERROR: 000005.tar' )
342+ # Put the original index.db back.
343+ os .remove ('zstash/index.db' )
344+ shutil .copy ('zstash/index_old.db' , 'zstash/index.db' )
345+ os .chdir ('../' )
346+
347+
184348print ('Verifying the data from database with the actual files' )
185349# Checksums from HPSS
186- cmd = ['sqlite3' , 'zstash_test/zstash/index.db' , 'select md5, name from files;' ]
350+ cmd = ['sqlite3' , 'zstash_test/zstash/index.db' , 'SELECT md5, name FROM files;' ]
187351output_hpss , err_hpss = run_cmd (cmd )
188352hpss_dict = {}
189353
@@ -210,6 +374,7 @@ def stop():
210374for k in local_dict :
211375 print ('{}|{}|{}' .format (k , hpss_dict [k ], local_dict [k ]))
212376
377+
213378cleanup ()
214379print ('*' * 40 )
215380print ('All of the tests passed! :)' )
0 commit comments