blob: 633621192173092cae569ed2043d0c5af5c577a2 [file] [log] [blame]
Eduardo Sousaa0117812019-02-05 15:57:09 +00001# Copyright 2018 Whitestack, LLC
2# Copyright 2018 Telefonica S.A.
3#
4# Licensed under the Apache License, Version 2.0 (the "License"); you may
5# not use this file except in compliance with the License. You may obtain
6# a copy of the License at
7#
8# http://www.apache.org/licenses/LICENSE-2.0
9#
10# Unless required by applicable law or agreed to in writing, software
11# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
12# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
13# License for the specific language governing permissions and limitations
14# under the License.
15#
16# For those usages not covered by the Apache License, Version 2.0 please
17# contact: esousa@whitestack.com or alfonso.tiernosepulveda@telefonica.com
18##
19
aticig3dd0db62022-03-04 19:35:45 +030020import http
Eduardo Sousaf5119a82018-04-25 17:55:17 +010021import io
22import logging
Eduardo Sousaf5119a82018-04-25 17:55:17 +010023import os
aticig3dd0db62022-03-04 19:35:45 +030024import shutil
Eduardo Sousaf5119a82018-04-25 17:55:17 +010025import tarfile
26import tempfile
27import uuid
Eduardo Sousaf5119a82018-04-25 17:55:17 +010028
29from osm_common.fsbase import FsException
30from osm_common.fslocal import FsLocal
aticig3dd0db62022-03-04 19:35:45 +030031import pytest
Eduardo Sousaf5119a82018-04-25 17:55:17 +010032
33__author__ = "Eduardo Sousa <eduardosousa@av.it.pt>"
34
tiernob20a9022018-05-22 12:07:05 +020035
Eduardo Sousaf5119a82018-04-25 17:55:17 +010036def valid_path():
garciadeblas2644b762021-03-24 09:21:01 +010037 return tempfile.gettempdir() + "/"
Eduardo Sousaf5119a82018-04-25 17:55:17 +010038
tiernob20a9022018-05-22 12:07:05 +020039
Eduardo Sousaf5119a82018-04-25 17:55:17 +010040def invalid_path():
garciadeblas2644b762021-03-24 09:21:01 +010041 return "/#tweeter/"
Eduardo Sousaf5119a82018-04-25 17:55:17 +010042
tiernob20a9022018-05-22 12:07:05 +020043
tierno1e9a3292018-11-05 18:18:45 +010044@pytest.fixture(scope="function", params=[True, False])
45def fs_local(request):
46 fs = FsLocal(lock=request.param)
garciadeblas2644b762021-03-24 09:21:01 +010047 fs.fs_connect({"path": valid_path()})
Eduardo Sousaf5119a82018-04-25 17:55:17 +010048 return fs
49
tiernob20a9022018-05-22 12:07:05 +020050
Eduardo Sousaf5119a82018-04-25 17:55:17 +010051def fs_connect_exception_message(path):
garciadeblas2644b762021-03-24 09:21:01 +010052 return "storage exception Invalid configuration param at '[storage]': path '{}' does not exist".format(
53 path
54 )
Eduardo Sousaf5119a82018-04-25 17:55:17 +010055
tiernob20a9022018-05-22 12:07:05 +020056
Eduardo Sousaf5119a82018-04-25 17:55:17 +010057def file_open_file_not_found_exception(storage):
garciadeblas2644b762021-03-24 09:21:01 +010058 f = storage if isinstance(storage, str) else "/".join(storage)
Eduardo Sousaf5119a82018-04-25 17:55:17 +010059 return "storage exception File {} does not exist".format(f)
60
tiernob20a9022018-05-22 12:07:05 +020061
Eduardo Sousaf5119a82018-04-25 17:55:17 +010062def file_open_io_exception(storage):
garciadeblas2644b762021-03-24 09:21:01 +010063 f = storage if isinstance(storage, str) else "/".join(storage)
Eduardo Sousaf5119a82018-04-25 17:55:17 +010064 return "storage exception File {} cannot be opened".format(f)
65
tiernob20a9022018-05-22 12:07:05 +020066
Eduardo Sousaf5119a82018-04-25 17:55:17 +010067def dir_ls_not_a_directory_exception(storage):
garciadeblas2644b762021-03-24 09:21:01 +010068 f = storage if isinstance(storage, str) else "/".join(storage)
Eduardo Sousaf5119a82018-04-25 17:55:17 +010069 return "storage exception File {} does not exist".format(f)
70
tiernob20a9022018-05-22 12:07:05 +020071
Eduardo Sousaf5119a82018-04-25 17:55:17 +010072def dir_ls_io_exception(storage):
garciadeblas2644b762021-03-24 09:21:01 +010073 f = storage if isinstance(storage, str) else "/".join(storage)
Eduardo Sousaf5119a82018-04-25 17:55:17 +010074 return "storage exception File {} cannot be opened".format(f)
75
tiernob20a9022018-05-22 12:07:05 +020076
Eduardo Sousaf5119a82018-04-25 17:55:17 +010077def file_delete_exception_message(storage):
78 return "storage exception File {} does not exist".format(storage)
79
tiernob20a9022018-05-22 12:07:05 +020080
Eduardo Sousaf5119a82018-04-25 17:55:17 +010081def test_constructor_without_logger():
82 fs = FsLocal()
garciadeblas2644b762021-03-24 09:21:01 +010083 assert fs.logger == logging.getLogger("fs")
Eduardo Sousaf5119a82018-04-25 17:55:17 +010084 assert fs.path is None
85
tiernob20a9022018-05-22 12:07:05 +020086
Eduardo Sousaf5119a82018-04-25 17:55:17 +010087def test_constructor_with_logger():
garciadeblas2644b762021-03-24 09:21:01 +010088 logger_name = "fs_local"
Eduardo Sousaf5119a82018-04-25 17:55:17 +010089 fs = FsLocal(logger_name=logger_name)
Eduardo Sousaf5119a82018-04-25 17:55:17 +010090 assert fs.logger == logging.getLogger(logger_name)
91 assert fs.path is None
92
tiernob20a9022018-05-22 12:07:05 +020093
Eduardo Sousa5ffda642018-05-09 19:42:00 +010094def test_get_params(fs_local):
95 params = fs_local.get_params()
Eduardo Sousa5ffda642018-05-09 19:42:00 +010096 assert len(params) == 2
97 assert "fs" in params
98 assert "path" in params
99 assert params["fs"] == "local"
100 assert params["path"] == valid_path()
101
tiernob20a9022018-05-22 12:07:05 +0200102
garciadeblas2644b762021-03-24 09:21:01 +0100103@pytest.mark.parametrize(
104 "config, exp_logger, exp_path",
105 [
106 ({"logger_name": "fs_local", "path": valid_path()}, "fs_local", valid_path()),
107 (
108 {"logger_name": "fs_local", "path": valid_path()[:-1]},
109 "fs_local",
110 valid_path(),
111 ),
112 ({"path": valid_path()}, "fs", valid_path()),
113 ({"path": valid_path()[:-1]}, "fs", valid_path()),
114 ],
115)
Eduardo Sousaf5119a82018-04-25 17:55:17 +0100116def test_fs_connect_with_valid_config(config, exp_logger, exp_path):
117 fs = FsLocal()
118 fs.fs_connect(config)
Eduardo Sousaf5119a82018-04-25 17:55:17 +0100119 assert fs.logger == logging.getLogger(exp_logger)
120 assert fs.path == exp_path
121
tiernob20a9022018-05-22 12:07:05 +0200122
garciadeblas2644b762021-03-24 09:21:01 +0100123@pytest.mark.parametrize(
124 "config, exp_exception_message",
125 [
126 (
127 {"logger_name": "fs_local", "path": invalid_path()},
128 fs_connect_exception_message(invalid_path()),
129 ),
130 (
131 {"logger_name": "fs_local", "path": invalid_path()[:-1]},
132 fs_connect_exception_message(invalid_path()[:-1]),
133 ),
134 ({"path": invalid_path()}, fs_connect_exception_message(invalid_path())),
135 (
136 {"path": invalid_path()[:-1]},
137 fs_connect_exception_message(invalid_path()[:-1]),
138 ),
139 ],
140)
Eduardo Sousaf5119a82018-04-25 17:55:17 +0100141def test_fs_connect_with_invalid_path(config, exp_exception_message):
142 fs = FsLocal()
Eduardo Sousaf5119a82018-04-25 17:55:17 +0100143 with pytest.raises(FsException) as excinfo:
144 fs.fs_connect(config)
145 assert str(excinfo.value) == exp_exception_message
146
tiernob20a9022018-05-22 12:07:05 +0200147
Eduardo Sousa5ffda642018-05-09 19:42:00 +0100148def test_fs_disconnect(fs_local):
149 fs_local.fs_disconnect()
150
tiernob20a9022018-05-22 12:07:05 +0200151
Eduardo Sousaf5119a82018-04-25 17:55:17 +0100152def test_mkdir_with_valid_path(fs_local):
153 folder_name = str(uuid.uuid4())
154 folder_path = valid_path() + folder_name
Eduardo Sousaf5119a82018-04-25 17:55:17 +0100155 fs_local.mkdir(folder_name)
Eduardo Sousaf5119a82018-04-25 17:55:17 +0100156 assert os.path.exists(folder_path)
tiernoc7ac30d2019-01-25 08:56:17 +0000157 # test idempotency
158 fs_local.mkdir(folder_name)
159 assert os.path.exists(folder_path)
Eduardo Sousaf5119a82018-04-25 17:55:17 +0100160 os.rmdir(folder_path)
161
tiernob20a9022018-05-22 12:07:05 +0200162
Eduardo Sousaf5119a82018-04-25 17:55:17 +0100163def test_mkdir_with_exception(fs_local):
164 folder_name = str(uuid.uuid4())
Eduardo Sousaf5119a82018-04-25 17:55:17 +0100165 with pytest.raises(FsException) as excinfo:
tiernoc7ac30d2019-01-25 08:56:17 +0000166 fs_local.mkdir(folder_name + "/" + folder_name)
Eduardo Sousaf5119a82018-04-25 17:55:17 +0100167 assert excinfo.value.http_code == http.HTTPStatus.INTERNAL_SERVER_ERROR
Eduardo Sousaf5119a82018-04-25 17:55:17 +0100168
tiernob20a9022018-05-22 12:07:05 +0200169
garciadeblas2644b762021-03-24 09:21:01 +0100170@pytest.mark.parametrize(
171 "storage, mode, expected",
172 [
173 (str(uuid.uuid4()), "file", False),
174 ([str(uuid.uuid4())], "file", False),
175 (str(uuid.uuid4()), "dir", False),
176 ([str(uuid.uuid4())], "dir", False),
177 ],
178)
Eduardo Sousaf5119a82018-04-25 17:55:17 +0100179def test_file_exists_returns_false(fs_local, storage, mode, expected):
180 assert fs_local.file_exists(storage, mode) == expected
181
tiernob20a9022018-05-22 12:07:05 +0200182
garciadeblas2644b762021-03-24 09:21:01 +0100183@pytest.mark.parametrize(
184 "storage, mode, expected",
185 [
186 (str(uuid.uuid4()), "file", True),
187 ([str(uuid.uuid4())], "file", True),
188 (str(uuid.uuid4()), "dir", True),
189 ([str(uuid.uuid4())], "dir", True),
190 ],
191)
Eduardo Sousaf5119a82018-04-25 17:55:17 +0100192def test_file_exists_returns_true(fs_local, storage, mode, expected):
garciadeblas2644b762021-03-24 09:21:01 +0100193 path = (
194 valid_path() + storage
195 if isinstance(storage, str)
196 else valid_path() + storage[0]
197 )
198 if mode == "file":
Eduardo Sousaf5119a82018-04-25 17:55:17 +0100199 os.mknod(path)
garciadeblas2644b762021-03-24 09:21:01 +0100200 elif mode == "dir":
Eduardo Sousaf5119a82018-04-25 17:55:17 +0100201 os.mkdir(path)
Eduardo Sousaf5119a82018-04-25 17:55:17 +0100202 assert fs_local.file_exists(storage, mode) == expected
garciadeblas2644b762021-03-24 09:21:01 +0100203 if mode == "file":
Eduardo Sousaf5119a82018-04-25 17:55:17 +0100204 os.remove(path)
garciadeblas2644b762021-03-24 09:21:01 +0100205 elif mode == "dir":
Eduardo Sousaf5119a82018-04-25 17:55:17 +0100206 os.rmdir(path)
207
tiernob20a9022018-05-22 12:07:05 +0200208
garciadeblas2644b762021-03-24 09:21:01 +0100209@pytest.mark.parametrize(
210 "storage, mode",
211 [
212 (str(uuid.uuid4()), "file"),
213 ([str(uuid.uuid4())], "file"),
214 (str(uuid.uuid4()), "dir"),
215 ([str(uuid.uuid4())], "dir"),
216 ],
217)
Eduardo Sousaf5119a82018-04-25 17:55:17 +0100218def test_file_size(fs_local, storage, mode):
garciadeblas2644b762021-03-24 09:21:01 +0100219 path = (
220 valid_path() + storage
221 if isinstance(storage, str)
222 else valid_path() + storage[0]
223 )
224 if mode == "file":
Eduardo Sousaf5119a82018-04-25 17:55:17 +0100225 os.mknod(path)
garciadeblas2644b762021-03-24 09:21:01 +0100226 elif mode == "dir":
Eduardo Sousaf5119a82018-04-25 17:55:17 +0100227 os.mkdir(path)
Eduardo Sousaf5119a82018-04-25 17:55:17 +0100228 size = os.path.getsize(path)
Eduardo Sousaf5119a82018-04-25 17:55:17 +0100229 assert fs_local.file_size(storage) == size
garciadeblas2644b762021-03-24 09:21:01 +0100230 if mode == "file":
Eduardo Sousaf5119a82018-04-25 17:55:17 +0100231 os.remove(path)
garciadeblas2644b762021-03-24 09:21:01 +0100232 elif mode == "dir":
Eduardo Sousaf5119a82018-04-25 17:55:17 +0100233 os.rmdir(path)
234
tiernob20a9022018-05-22 12:07:05 +0200235
garciadeblas2644b762021-03-24 09:21:01 +0100236@pytest.mark.parametrize(
237 "files, path",
238 [
239 (["foo", "bar", "foobar"], str(uuid.uuid4())),
240 (["foo", "bar", "foobar"], [str(uuid.uuid4())]),
241 ],
242)
Eduardo Sousaf5119a82018-04-25 17:55:17 +0100243def test_file_extract(fs_local, files, path):
244 for f in files:
245 os.mknod(valid_path() + f)
garciadeblas2644b762021-03-24 09:21:01 +0100246 tar_path = valid_path() + str(uuid.uuid4()) + ".tar"
247 with tarfile.open(tar_path, "w") as tar:
Eduardo Sousaf5119a82018-04-25 17:55:17 +0100248 for f in files:
249 tar.add(valid_path() + f, arcname=f)
garciadeblas2644b762021-03-24 09:21:01 +0100250 with tarfile.open(tar_path, "r") as tar:
Eduardo Sousaf5119a82018-04-25 17:55:17 +0100251 fs_local.file_extract(tar, path)
garciadeblas2644b762021-03-24 09:21:01 +0100252 extracted_path = valid_path() + (path if isinstance(path, str) else "/".join(path))
Eduardo Sousaf5119a82018-04-25 17:55:17 +0100253 ls_dir = os.listdir(extracted_path)
Eduardo Sousaf5119a82018-04-25 17:55:17 +0100254 assert len(ls_dir) == len(files)
255 for f in files:
256 assert f in ls_dir
Eduardo Sousaf5119a82018-04-25 17:55:17 +0100257 os.remove(tar_path)
Eduardo Sousaf5119a82018-04-25 17:55:17 +0100258 for f in files:
259 os.remove(valid_path() + f)
Eduardo Sousaf5119a82018-04-25 17:55:17 +0100260 shutil.rmtree(extracted_path)
261
tiernob20a9022018-05-22 12:07:05 +0200262
garciadeblas2644b762021-03-24 09:21:01 +0100263@pytest.mark.parametrize(
264 "storage, mode",
265 [
266 (str(uuid.uuid4()), "r"),
267 (str(uuid.uuid4()), "w"),
268 (str(uuid.uuid4()), "a"),
269 (str(uuid.uuid4()), "rb"),
270 (str(uuid.uuid4()), "wb"),
271 (str(uuid.uuid4()), "ab"),
272 ([str(uuid.uuid4())], "r"),
273 ([str(uuid.uuid4())], "w"),
274 ([str(uuid.uuid4())], "a"),
275 ([str(uuid.uuid4())], "rb"),
276 ([str(uuid.uuid4())], "wb"),
277 ([str(uuid.uuid4())], "ab"),
278 ],
279)
Eduardo Sousaf5119a82018-04-25 17:55:17 +0100280def test_file_open(fs_local, storage, mode):
garciadeblas2644b762021-03-24 09:21:01 +0100281 path = (
282 valid_path() + storage
283 if isinstance(storage, str)
284 else valid_path() + storage[0]
285 )
Eduardo Sousaf5119a82018-04-25 17:55:17 +0100286 os.mknod(path)
Eduardo Sousaf5119a82018-04-25 17:55:17 +0100287 file_obj = fs_local.file_open(storage, mode)
Eduardo Sousaf5119a82018-04-25 17:55:17 +0100288 assert isinstance(file_obj, io.IOBase)
tiernob20a9022018-05-22 12:07:05 +0200289 assert file_obj.closed is False
Eduardo Sousaf5119a82018-04-25 17:55:17 +0100290 os.remove(path)
291
tiernob20a9022018-05-22 12:07:05 +0200292
garciadeblas2644b762021-03-24 09:21:01 +0100293@pytest.mark.parametrize(
294 "storage, mode",
295 [
296 (str(uuid.uuid4()), "r"),
297 (str(uuid.uuid4()), "rb"),
298 ([str(uuid.uuid4())], "r"),
299 ([str(uuid.uuid4())], "rb"),
300 ],
301)
Eduardo Sousaf5119a82018-04-25 17:55:17 +0100302def test_file_open_file_not_found_exception(fs_local, storage, mode):
303 with pytest.raises(FsException) as excinfo:
304 fs_local.file_open(storage, mode)
305 assert str(excinfo.value) == file_open_file_not_found_exception(storage)
306 assert excinfo.value.http_code == http.HTTPStatus.NOT_FOUND
307
tiernob20a9022018-05-22 12:07:05 +0200308
garciadeblas2644b762021-03-24 09:21:01 +0100309@pytest.mark.parametrize(
310 "storage, mode",
311 [
312 (str(uuid.uuid4()), "r"),
313 (str(uuid.uuid4()), "w"),
314 (str(uuid.uuid4()), "a"),
315 (str(uuid.uuid4()), "rb"),
316 (str(uuid.uuid4()), "wb"),
317 (str(uuid.uuid4()), "ab"),
318 ([str(uuid.uuid4())], "r"),
319 ([str(uuid.uuid4())], "w"),
320 ([str(uuid.uuid4())], "a"),
321 ([str(uuid.uuid4())], "rb"),
322 ([str(uuid.uuid4())], "wb"),
323 ([str(uuid.uuid4())], "ab"),
324 ],
325)
Eduardo Sousaf5119a82018-04-25 17:55:17 +0100326def test_file_open_io_error(fs_local, storage, mode):
garciadeblas2644b762021-03-24 09:21:01 +0100327 path = (
328 valid_path() + storage
329 if isinstance(storage, str)
330 else valid_path() + storage[0]
331 )
Eduardo Sousaf5119a82018-04-25 17:55:17 +0100332 os.mknod(path)
333 os.chmod(path, 0)
Eduardo Sousaf5119a82018-04-25 17:55:17 +0100334 with pytest.raises(FsException) as excinfo:
335 fs_local.file_open(storage, mode)
336 assert str(excinfo.value) == file_open_io_exception(storage)
337 assert excinfo.value.http_code == http.HTTPStatus.BAD_REQUEST
Eduardo Sousaf5119a82018-04-25 17:55:17 +0100338 os.remove(path)
339
tiernob20a9022018-05-22 12:07:05 +0200340
garciadeblas2644b762021-03-24 09:21:01 +0100341@pytest.mark.parametrize(
342 "storage, with_files",
343 [
344 (str(uuid.uuid4()), True),
345 (str(uuid.uuid4()), False),
346 ([str(uuid.uuid4())], True),
347 ([str(uuid.uuid4())], False),
348 ],
349)
Eduardo Sousaf5119a82018-04-25 17:55:17 +0100350def test_dir_ls(fs_local, storage, with_files):
garciadeblas2644b762021-03-24 09:21:01 +0100351 path = (
352 valid_path() + storage
353 if isinstance(storage, str)
354 else valid_path() + storage[0]
355 )
Eduardo Sousaf5119a82018-04-25 17:55:17 +0100356 os.mkdir(path)
tiernob20a9022018-05-22 12:07:05 +0200357 if with_files is True:
Eduardo Sousaf5119a82018-04-25 17:55:17 +0100358 file_name = str(uuid.uuid4())
garciadeblas2644b762021-03-24 09:21:01 +0100359 file_path = path + "/" + file_name
Eduardo Sousaf5119a82018-04-25 17:55:17 +0100360 os.mknod(file_path)
Eduardo Sousaf5119a82018-04-25 17:55:17 +0100361 result = fs_local.dir_ls(storage)
362
tiernob20a9022018-05-22 12:07:05 +0200363 if with_files is True:
Eduardo Sousaf5119a82018-04-25 17:55:17 +0100364 assert len(result) == 1
365 assert result[0] == file_name
366 else:
367 assert len(result) == 0
Eduardo Sousaf5119a82018-04-25 17:55:17 +0100368 shutil.rmtree(path)
369
tiernob20a9022018-05-22 12:07:05 +0200370
garciadeblas2644b762021-03-24 09:21:01 +0100371@pytest.mark.parametrize("storage", [(str(uuid.uuid4())), ([str(uuid.uuid4())])])
Eduardo Sousaf5119a82018-04-25 17:55:17 +0100372def test_dir_ls_with_not_a_directory_error(fs_local, storage):
garciadeblas2644b762021-03-24 09:21:01 +0100373 path = (
374 valid_path() + storage
375 if isinstance(storage, str)
376 else valid_path() + storage[0]
377 )
Eduardo Sousaf5119a82018-04-25 17:55:17 +0100378 os.mknod(path)
Eduardo Sousaf5119a82018-04-25 17:55:17 +0100379 with pytest.raises(FsException) as excinfo:
380 fs_local.dir_ls(storage)
381 assert str(excinfo.value) == dir_ls_not_a_directory_exception(storage)
382 assert excinfo.value.http_code == http.HTTPStatus.NOT_FOUND
Eduardo Sousaf5119a82018-04-25 17:55:17 +0100383 os.remove(path)
384
tiernob20a9022018-05-22 12:07:05 +0200385
garciadeblas2644b762021-03-24 09:21:01 +0100386@pytest.mark.parametrize("storage", [(str(uuid.uuid4())), ([str(uuid.uuid4())])])
Eduardo Sousaf5119a82018-04-25 17:55:17 +0100387def test_dir_ls_with_io_error(fs_local, storage):
garciadeblas2644b762021-03-24 09:21:01 +0100388 path = (
389 valid_path() + storage
390 if isinstance(storage, str)
391 else valid_path() + storage[0]
392 )
Eduardo Sousaf5119a82018-04-25 17:55:17 +0100393 os.mkdir(path)
394 os.chmod(path, 0)
Eduardo Sousaf5119a82018-04-25 17:55:17 +0100395 with pytest.raises(FsException) as excinfo:
396 fs_local.dir_ls(storage)
397 assert str(excinfo.value) == dir_ls_io_exception(storage)
398 assert excinfo.value.http_code == http.HTTPStatus.BAD_REQUEST
Eduardo Sousaf5119a82018-04-25 17:55:17 +0100399 os.rmdir(path)
400
tiernob20a9022018-05-22 12:07:05 +0200401
garciadeblas2644b762021-03-24 09:21:01 +0100402@pytest.mark.parametrize(
403 "storage, with_files, ignore_non_exist",
404 [
405 (str(uuid.uuid4()), True, True),
406 (str(uuid.uuid4()), False, True),
407 (str(uuid.uuid4()), True, False),
408 (str(uuid.uuid4()), False, False),
409 ([str(uuid.uuid4())], True, True),
410 ([str(uuid.uuid4())], False, True),
411 ([str(uuid.uuid4())], True, False),
412 ([str(uuid.uuid4())], False, False),
413 ],
414)
Eduardo Sousaf5119a82018-04-25 17:55:17 +0100415def test_file_delete_with_dir(fs_local, storage, with_files, ignore_non_exist):
garciadeblas2644b762021-03-24 09:21:01 +0100416 path = (
417 valid_path() + storage
418 if isinstance(storage, str)
419 else valid_path() + storage[0]
420 )
Eduardo Sousaf5119a82018-04-25 17:55:17 +0100421 os.mkdir(path)
tiernob20a9022018-05-22 12:07:05 +0200422 if with_files is True:
garciadeblas2644b762021-03-24 09:21:01 +0100423 file_path = path + "/" + str(uuid.uuid4())
Eduardo Sousaf5119a82018-04-25 17:55:17 +0100424 os.mknod(file_path)
Eduardo Sousaf5119a82018-04-25 17:55:17 +0100425 fs_local.file_delete(storage, ignore_non_exist)
tiernob20a9022018-05-22 12:07:05 +0200426 assert os.path.exists(path) is False
Eduardo Sousaf5119a82018-04-25 17:55:17 +0100427
Eduardo Sousaf5119a82018-04-25 17:55:17 +0100428
garciadeblas2644b762021-03-24 09:21:01 +0100429@pytest.mark.parametrize("storage", [(str(uuid.uuid4())), ([str(uuid.uuid4())])])
Eduardo Sousaf5119a82018-04-25 17:55:17 +0100430def test_file_delete_expect_exception(fs_local, storage):
431 with pytest.raises(FsException) as excinfo:
432 fs_local.file_delete(storage)
433 assert str(excinfo.value) == file_delete_exception_message(storage)
434 assert excinfo.value.http_code == http.HTTPStatus.NOT_FOUND
435
tiernob20a9022018-05-22 12:07:05 +0200436
garciadeblas2644b762021-03-24 09:21:01 +0100437@pytest.mark.parametrize("storage", [(str(uuid.uuid4())), ([str(uuid.uuid4())])])
Eduardo Sousaf5119a82018-04-25 17:55:17 +0100438def test_file_delete_no_exception(fs_local, storage):
garciadeblas2644b762021-03-24 09:21:01 +0100439 path = (
440 valid_path() + storage
441 if isinstance(storage, str)
442 else valid_path() + storage[0]
443 )
Eduardo Sousaf5119a82018-04-25 17:55:17 +0100444 fs_local.file_delete(storage, ignore_non_exist=True)
tiernob20a9022018-05-22 12:07:05 +0200445 assert os.path.exists(path) is False