11# pylint: disable=wrong-import-position
22# pylint: disable=protected-access
33
4+ import errno
45import io
56import os
67import subprocess
1314sys .path .insert (0 , os .path .abspath (os .path .join (os .path .dirname (__file__ ), '..' )))
1415sys .path .insert (0 , os .path .abspath (os .path .join (os .path .dirname (__file__ ), '../examples' )))
1516
17+ from loopback import cli as cli_loopback # noqa: E402
1618from memory import cli as cli_memory # noqa: E402
1719from memory_nullpath import cli as cli_memory_nullpath # noqa: E402
1820
1921
2022class RunCLI :
21- def __init__ (self , cli , mount_point ):
23+ def __init__ (self , cli , mount_point , arguments ):
2224 self .timeout = 4
2325 self .mount_point = str (mount_point )
24- self .args = [self .mount_point ]
26+ self .args = [* arguments , self .mount_point ]
2527 self .thread = threading .Thread (target = cli , args = (self .args ,))
2628
2729 self ._stdout = None
@@ -49,8 +51,8 @@ def __exit__(self, exception_type, exception_value, exception_traceback):
4951 output = stdout .read ()
5052 errors = stderr .read ()
5153
52- problematicWords = ['[Warning]' , '[Error]' ]
53- if any (word in output or word in errors for word in problematicWords ):
54+ problematic_words = ['[Warning]' , '[Error]' ]
55+ if any (word in output or word in errors for word in problematic_words ):
5456 print ("===== stdout =====\n " , output )
5557 print ("===== stderr =====\n " , errors )
5658 raise AssertionError ("There were warnings or errors!" )
@@ -60,20 +62,20 @@ def __exit__(self, exception_type, exception_value, exception_traceback):
6062 self .thread .join (self .timeout )
6163
6264 def get_stdout (self ):
63- oldPosition = sys .stdout .tell ()
65+ old_position = sys .stdout .tell ()
6466 try :
6567 sys .stdout .seek (0 )
6668 return sys .stdout .read ()
6769 finally :
68- sys .stdout .seek (oldPosition )
70+ sys .stdout .seek (old_position )
6971
7072 def get_stderr (self ):
71- oldPosition = sys .stderr .tell ()
73+ old_position = sys .stderr .tell ()
7274 try :
7375 sys .stderr .seek (0 )
7476 return sys .stderr .read ()
7577 finally :
76- sys .stderr .seek (oldPosition )
78+ sys .stderr .seek (old_position )
7779
7880 def wait_for_mount_point (self ):
7981 t0 = time .time ()
@@ -111,43 +113,60 @@ def unmount(self):
111113 time .sleep (0.1 )
112114
113115
114- @pytest .mark .parametrize ('cli' , [cli_memory , cli_memory_nullpath ])
115- def test_memory_file_system (cli , tmpdir ):
116- mount_point = tmpdir
117- with RunCLI (cli , mount_point ):
116+ @pytest .mark .parametrize ('cli' , [cli_loopback , cli_memory , cli_memory_nullpath ])
117+ def test_read_write_file_system (cli , tmp_path ):
118+ if cli == cli_loopback :
119+ mount_source = tmp_path / "folder"
120+ mount_point = tmp_path / "mounted"
121+ mount_source .mkdir ()
122+ mount_point .mkdir ()
123+ arguments = [str (mount_source )]
124+ else :
125+ mount_point = tmp_path
126+ arguments = []
127+ with RunCLI (cli , mount_point , arguments ):
118128 assert os .path .isdir (mount_point )
119129 assert not os .path .isdir (mount_point / "foo" )
120130
121131 path = mount_point / "foo"
122- with open (path , 'wb' ) as file :
123- assert file .write (b"bar" ) == 3
132+ assert path .write_bytes (b"bar" ) == 3
124133
125134 assert os .path .exists (path )
126135 assert os .path .isfile (path )
127136 assert not os .path .isdir (path )
128137
129- with open (path , 'rb' ) as file :
130- assert file .read () == b"bar"
138+ assert path .read_bytes () == b"bar"
131139
132140 os .truncate (path , 2 )
133- with open (path , 'rb' ) as file :
134- assert file .read () == b"ba"
141+ assert path .read_bytes () == b"ba"
135142
136143 os .chmod (path , 0 )
137144 assert os .stat (path ).st_mode & 0o777 == 0
138145 os .chmod (path , 0o777 )
139146 assert os .stat (path ).st_mode & 0o777 == 0o777
140147
141- os .chown (path , 12345 , 23456 )
142- assert os .stat (path ).st_uid == 12345
143- assert os .stat (path ).st_gid == 23456
148+ try :
149+ # Only works for memory file systems.
150+ os .chown (path , 12345 , 23456 )
151+ assert os .stat (path ).st_uid == 12345
152+ assert os .stat (path ).st_gid == 23456
153+ except PermissionError :
154+ assert cli == cli_loopback
155+
156+ os .chown (path , os .getuid (), os .getgid ())
157+ assert os .stat (path ).st_uid == os .getuid ()
158+ assert os .stat (path ).st_gid == os .getgid ()
144159
145- assert not os .listxattr (path )
146- os .setxattr (path , b"user.tag-test" , b"FOO-RESULT" )
147- assert os .listxattr (path )
148- assert os .getxattr (path , b"user.tag-test" ) == b"FOO-RESULT"
149- os .removexattr (path , b"user.tag-test" )
150- assert not os .listxattr (path )
160+ try :
161+ assert not os .listxattr (path )
162+ os .setxattr (path , b"user.tag-test" , b"FOO-RESULT" )
163+ assert os .listxattr (path )
164+ assert os .getxattr (path , b"user.tag-test" ) == b"FOO-RESULT"
165+ os .removexattr (path , b"user.tag-test" )
166+ assert not os .listxattr (path )
167+ except OSError as exception :
168+ assert cli == cli_loopback
169+ assert exception .errno == errno .ENOTSUP
151170
152171 os .utime (path , (1.5 , 12.5 ))
153172 assert os .stat (path ).st_atime == 1.5
@@ -178,10 +197,11 @@ def test_memory_file_system(cli, tmpdir):
178197 # assert os.path.isfile(path) # Does not have a follow_symlink argument but it seems to be True, see below.
179198 assert os .path .isdir (path )
180199 assert os .path .islink (path )
181- assert os .readlink (path ) == mount_point / "bar"
200+ assert os .readlink (path ) == str ( mount_point / "bar" )
182201
183202 os .rmdir (mount_point / "bar" )
184203 assert not os .path .exists (mount_point / "bar" )
185204
186- assert os .statvfs (mount_point ).f_bsize == 512
187- assert os .statvfs (mount_point ).f_bavail == 2048
205+ if cli != cli_loopback :
206+ assert os .statvfs (mount_point ).f_bsize == 512
207+ assert os .statvfs (mount_point ).f_bavail == 2048
0 commit comments