1
1
import os
2
2
import pwd
3
+ import random
4
+ import tempfile
3
5
4
6
from osgtest .library import core
5
7
from osgtest .library import files
@@ -21,35 +23,66 @@ def _setcfg(key, val):
21
23
22
24
23
25
class TestStashCache (OSGTestCase ):
24
- _text = "this is a test"
26
+ # testfiles with random contents
27
+ testfiles = [
28
+ ("testfile%d" % x , str (random .random ()))
29
+ for x in range (4 )
30
+ ]
31
+
32
+ def assertCached (self , name , contents ):
33
+ fpath = os .path .join (_getcfg ("cache_dir" ), name )
34
+ self .assertTrue (os .path .exists (fpath ),
35
+ name + " not cached" )
36
+ self .assertEqual (contents , files .read (fpath , as_single_string = True ),
37
+ "cached file %s contents do not match expected" % name )
25
38
26
39
@core .elrelease (7 ,8 )
27
40
def setUp (self ):
28
41
core .skip_ok_unless_installed ("stashcache-origin-server" , "stashcache-cache-server" , "stashcache-client" )
29
42
self .skip_bad_unless (service .is_running ("xrootd@stashcache-origin-server" ))
30
43
self .skip_bad_unless (service .is_running ("xrootd@stashcache-cache-server" ))
31
44
32
- def test_01_create_file (self ):
45
+ def test_01_create_files (self ):
33
46
xrootd_user = pwd .getpwnam ("xrootd" )
34
- files .write (os .path .join (_getcfg ("origin_dir" ), "testfile" ),
35
- self ._text , backup = False , chmod = 0o644 ,
36
- chown = (xrootd_user .pw_uid , xrootd_user .pw_gid ))
47
+ for name , contents in self .testfiles :
48
+ files .write (os .path .join (_getcfg ("origin_dir" ), name ),
49
+ contents , backup = False , chmod = 0o644 ,
50
+ chown = (xrootd_user .pw_uid , xrootd_user .pw_gid ))
37
51
38
- def test_02_fetch_from_origin (self ):
52
+ def test_02_xroot_fetch_from_origin (self ):
53
+ name , contents = self .testfiles [0 ]
39
54
result , _ , _ = \
40
55
core .check_system (["xrdcp" , "-d1" , "-N" , "-f" ,
41
- "root://localhost:%d//testfile " % _getcfg ("origin_port" ),
56
+ "root://localhost:%d//%s " % ( _getcfg ("origin_xroot_port" ), name ),
42
57
"-" ], "Checking xroot copy from origin" )
43
- self .assertEqual (result , self . _text , "downloaded file does not match expected" )
58
+ self .assertEqual (result , contents , "downloaded file does not match expected" )
44
59
45
60
def test_03_http_fetch_from_cache (self ):
61
+ name , contents = self .testfiles [1 ]
46
62
try :
47
63
f = urlopen (
48
- "http://localhost:%d/testfile " % _getcfg ("cache_http_port" )
64
+ "http://localhost:%d/%s " % ( _getcfg ("cache_http_port" ), name )
49
65
)
50
66
result = f .read ()
51
67
except IOError as e :
52
68
self .fail ("Unable to download from cache via http: %s" % e )
53
- self .assertEqual (result , self ._text , "downloaded file does not match expected" )
54
- self .assertTrue (os .path .exists (os .path .join (_getcfg ("cache_dir" ), "testfile" )),
55
- "testfile not cached" )
69
+ self .assertEqual (result , contents , "downloaded file does not match expected" )
70
+ self .assertCached (name , contents )
71
+
72
+ def test_04_xroot_fetch_from_cache (self ):
73
+ name , contents = self .testfiles [2 ]
74
+ result , _ , _ = \
75
+ core .check_system (["xrdcp" , "-d1" , "-N" , "-f" ,
76
+ "root://localhost:%d//%s" % (_getcfg ("cache_xroot_port" ), name ),
77
+ "-" ], "Checking xroot copy from cache" )
78
+ self .assertEqual (result , contents , "downloaded file does not match expected" )
79
+ self .assertCached (name , contents )
80
+
81
+ def test_05_stashcp (self ):
82
+ name , contents = self .testfiles [3 ]
83
+ with tempfile .NamedTemporaryFile (mode = "r+b" ) as tf :
84
+ core .check_system (["stashcp" , "-d" , "/" + name , tf .name ],
85
+ "Checking stashcp" )
86
+ result = tf .read ()
87
+ self .assertEqual (result , contents , "stashcp'ed file does match expected" )
88
+ self .assertCached (name , contents )
0 commit comments