55from moto .moto_server .threaded_moto_server import ThreadedMotoServer
66from jupyter_fsspec .file_manager import FileSystemManager
77
8+ PORT = 5555
9+ ENDPOINT_URI = "http://127.0.0.1:%s/" % PORT
10+
11+
812pytest_plugins = [
913 "pytest_jupyter.jupyter_server" ,
1014 "jupyter_server.pytest_plugin" ,
1620def setup_tmp_local (tmp_path : Path ):
1721 local_root = tmp_path / "test"
1822 local_root .mkdir ()
23+ nested_dir = local_root / "nested"
24+ nested_dir .mkdir ()
25+ nested_file1 = nested_dir / ".empty"
26+ nested_file1 .touch ()
27+ nested_file2 = nested_dir / ".keep"
28+ nested_file2 .touch ()
1929 local_file = local_root / "file_loc.txt"
2030 local_file .touch ()
31+
2132 local_empty_root = tmp_path / "empty"
2233 local_empty_root .mkdir ()
2334
24- return [local_root , local_empty_root ]
35+ yield [local_root , local_empty_root ]
2536
2637
2738@pytest .fixture (scope = "function" , autouse = True )
2839def setup_config_file_fs (tmp_path : Path , setup_tmp_local ):
2940 tmp_local = setup_tmp_local [0 ]
41+ items_tmp_local = list (tmp_local .iterdir ())
42+ print (f"items_tmp_local: { items_tmp_local } " )
3043 empty_tmp_local = setup_tmp_local [1 ]
3144 config_dir = tmp_path / "config"
3245 config_dir .mkdir (exist_ok = True )
3346
3447 yaml_content = f"""sources:
3548 - name: "TestSourceAWS"
3649 path: "s3://my-test-bucket/"
37- additional_options :
50+ kwargs :
3851 anon: false
3952 key: "my-access-key"
4053 secret: "my-secret-key"
54+ client_kwargs:
55+ endpoint_url: "{ ENDPOINT_URI } "
4156 - name: "TestDir"
4257 path: "{ tmp_local } "
4358 protocol: "local"
@@ -57,11 +72,11 @@ def setup_config_file_fs(tmp_path: Path, setup_tmp_local):
5772 print (f"Patching jupyter_config_dir to: { config_dir } " )
5873 fs_manager = FileSystemManager (config_file = "jupyter-fsspec.yaml" )
5974
60- return fs_manager
75+ yield fs_manager
6176
6277
6378@pytest .fixture (scope = "function" )
64- def fs_manager_instance (setup_config_file_fs ):
79+ def fs_manager_instance (setup_config_file_fs , s3_client ):
6580 fs_manager = setup_config_file_fs
6681 fs_info = fs_manager .get_filesystem_by_protocol ("memory" )
6782 mem_fs = fs_info ["info" ]["instance" ]
@@ -88,8 +103,7 @@ def fs_manager_instance(setup_config_file_fs):
88103 print ("In memory filesystem NOT FOUND" )
89104
90105 if mem_fs .exists (f"{ mem_root_path } /test_dir/file1.txt" ):
91- file_info = mem_fs .info (f"{ mem_root_path } /test_dir/file1.txt" )
92- print (f"File exists. size: { file_info } " )
106+ mem_fs .info (f"{ mem_root_path } /test_dir/file1.txt" )
93107 else :
94108 print ("File does not exist!" )
95109 return fs_manager
@@ -98,120 +112,45 @@ def fs_manager_instance(setup_config_file_fs):
98112def get_boto3_client ():
99113 from botocore .session import Session
100114
101- # NB: we use the sync botocore client for setup
102115 session = Session ()
116+ return session .create_client (
117+ "s3" ,
118+ endpoint_url = ENDPOINT_URI ,
119+ aws_access_key_id = "my-access-key" ,
120+ aws_secret_access_key = "my-secret-key" ,
121+ )
103122
104- endpoint_uri = "http://127.0.0.1:%s/" % "5555"
105- return session .create_client ("s3" , endpoint_url = endpoint_uri )
106-
107-
108- @pytest .fixture (scope = "function" )
109- def s3_client (mock_s3_fs ):
110- s3_client = get_boto3_client ()
111- s3_client .create_bucket (Bucket = "my-test-bucket" )
112- return s3_client
113-
114-
115- @pytest .fixture (scope = "function" )
116- def s3_fs_manager_instance (setup_config_file_fs ):
117- fs_manager = setup_config_file_fs
118- # fs_info = fs_manager.get_filesystem_by_protocol("s3")
119- # key = fs_info["key"]
120- # fs = fs_info["info"]["instance"]
121- # root_path = fs_info["info"]["path"]
122-
123- # endpoint_uri = "http://127.0.0.1:%s/" % "5555"
124- # fs = fsspec.filesystem('s3', asynchronous=True, anon=False, client_kwargs={'endpoint_url': endpoint_uri})
125- return fs_manager
126-
127-
128- @pytest .fixture (params = ["memory" , "local" , "s3" ])
129- def filesystem_protocol (request ):
130- return request .param
131123
132-
133- @pytest .fixture (scope = "function" )
134- def populated_file_system (filesystem_protocol ):
135- fs_manager = FileSystemManager (config_file = "jupyter-fsspec.yaml" )
136- fs_protocol = filesystem_protocol
137- fs_info = fs_manager .get_filesystem_by_protocol (fs_protocol )
138- fs = fs_info ["info" ]["instance" ]
139-
140- if fs :
141- # Delete any existting directories
142- # Populate the filesystem
143- # mkdir => root_path + 'rootA'
144- # mkdir => root_path + 'rootB'
145- # touch => root_path + 'file1.txt'
146- # touch => root_path + 'rootA' + 'file_in_rootA.txt'
147- print (f"valid filesystem: { fs } " )
148- else :
149- print (f"invalid filesystem: { fs } " )
150- return {"fs_protocol" : fs_protocol , "fs_manager" : fs_manager }
151-
152-
153- # TODO: Update this fixture from s3fs
154- @pytest .fixture (scope = "function" )
155- def mock_s3_fs ():
156- # This fixture is module-scoped, meaning that we can re-use the MotoServer across all tests
157- server = ThreadedMotoServer (ip_address = "127.0.0.1" , port = 5555 )
124+ @pytest .fixture (scope = "module" )
125+ def s3_base ():
126+ server = ThreadedMotoServer (ip_address = "127.0.0.1" , port = PORT )
158127 server .start ()
159128 if "AWS_SECRET_ACCESS_KEY" not in os .environ :
160- os .environ ["AWS_SECRET_ACCESS_KEY" ] = "foo "
129+ os .environ ["AWS_SECRET_ACCESS_KEY" ] = "my-accesss-key "
161130 if "AWS_ACCESS_KEY_ID" not in os .environ :
162- os .environ ["AWS_ACCESS_KEY_ID" ] = "foo"
163- # aws_session_token=os.environ["AWS_SESSION_TOKEN"]
164- if "AWS_SESSION_TOKEN" not in os .environ :
165- os .environ ["AWS_SESSION_TOKEN" ] = "foo"
131+ os .environ ["AWS_ACCESS_KEY_ID" ] = "my-secret-key"
132+
166133 print ("server up" )
167134 yield
168135 print ("moto done" )
169136 server .stop ()
170137
171138
172- @pytest .fixture (scope = "function" )
173- def fs_manager_instance_parameterized (populated_file_system ):
174- fs_ret = populated_file_system
175- fs_protocol = fs_ret ["fs_protocol" ]
176- fs_manager = fs_ret ["fs_manager" ]
177- fs_info = fs_manager .get_filesystem_by_protocol (fs_protocol )
178- fs = fs_info ["info" ]["instance" ]
179- root_path = fs_info ["info" ]["path" ]
180-
181- # fs_info = fs_manager.get_filesystem_by_protocol('local')
182- # key = fs_info['key']
183- # fs = fs_info['info']['instance']
184- # local_root_path = fs_info['info']['path']
185-
186- if fs :
187- # TODO: Update file creation FOR PATHS!!!
188- if fs .exists (f"{ root_path } /test_dir" ):
189- print (f"{ root_path } /test_dir EXISTS!!!!" )
190- # fs.rm(f'{root_path}/test_dir', recursive=True)
191- if fs .exists (f"{ root_path } /second_dir" ):
192- print (f"{ root_path } /second_dir EXISTS!!!!" )
193- # fs.rm('/my_dir/second_dir', recursive=True)
194-
195- fs .touch (f"{ root_path } /file_in_root.txt" )
196- with fs .open (f"{ root_path } /file_in_root.txt" , "wb" ) as f :
197- f .write ("Root file content" .encode ())
198-
199- # fs.mkdir('/my_dir/test_dir', exist_ok=True)
200- # fs.mkdir('/my_dir/second_dir', exist_ok=True)
201- # # fs.mkdir('/my_dir/second_dir/subdir', exist_ok=True)
202- # fs.touch('/my_dir/test_dir/file1.txt')
203- # with fs.open('/my_dir/test_dir/file1.txt', "wb") as f:
204- # f.write("Test content".encode())
205- # f.close()
206- else :
207- print (f"Filesystem of protocol { fs_protocol } NOT FOUND" )
208-
209- if fs .exists (f"{ root_path } test_dir/file1.txt" ):
210- file_info = fs .info (f"/{ root_path } /test_dir/file1.txt" )
211- print (f"File exists. size: { file_info } " )
212- else :
213- print ("File does not exist!" )
214- return fs_manager
139+ @pytest .fixture (scope = "module" )
140+ def s3_client (s3_base ):
141+ client = get_boto3_client ()
142+ client .create_bucket (Bucket = "my-test-bucket" , ACL = "public-read" )
143+ client .put_object (
144+ Body = b"Hello, World1!" , Bucket = "my-test-bucket" , Key = "bucket-filename1.txt"
145+ )
146+ client .put_object (
147+ Body = b"Hello, World2!" , Bucket = "my-test-bucket" , Key = "some/bucket-filename2.txt"
148+ )
149+ client .put_object (
150+ Body = b"Hello, World3!" , Bucket = "my-test-bucket" , Key = "some/bucket-filename3.txt"
151+ )
152+
153+ yield client
215154
216155
217156@pytest .fixture
0 commit comments