github.com/treeverse/lakefs@v1.24.1-0.20240520134607-95648127bfb0/clients/python-wrapper/tests/utests/test_repository.py (about) 1 import http 2 import os 3 import time 4 5 import lakefs_sdk 6 7 from tests.utests.common import ( 8 get_test_repo, 9 expect_exception_context, 10 lakectl_no_config_context, 11 env_var_context, 12 TEST_REPO_ARGS 13 ) 14 15 from lakefs.exceptions import ( 16 ServerException, 17 NotAuthorizedException, 18 NotFoundException, 19 ConflictException, 20 NoAuthenticationFound 21 ) 22 from lakefs import RepositoryProperties 23 24 25 def monkey_create_repository(_self, repository_creation, *_): 26 assert repository_creation.name == TEST_REPO_ARGS.name 27 assert repository_creation.storage_namespace == TEST_REPO_ARGS.storage_namespace 28 assert repository_creation.default_branch == TEST_REPO_ARGS.default_branch 29 assert repository_creation.sample_data == TEST_REPO_ARGS.sample_data 30 return lakefs_sdk.Repository(id=TEST_REPO_ARGS.name, 31 creation_date=int(time.time()), 32 storage_namespace=TEST_REPO_ARGS.storage_namespace, 33 default_branch=TEST_REPO_ARGS.default_branch) 34 35 36 def test_repository_creation(monkeypatch): 37 repo = get_test_repo() 38 39 with monkeypatch.context(): 40 monkeypatch.setattr(lakefs_sdk.RepositoriesApi, "create_repository", monkey_create_repository) 41 repo.create(storage_namespace=TEST_REPO_ARGS.storage_namespace, 42 default_branch=TEST_REPO_ARGS.default_branch, 43 include_samples=TEST_REPO_ARGS.sample_data) 44 45 46 def test_repository_creation_already_exists(monkeypatch): 47 repo = get_test_repo() 48 ex = lakefs_sdk.exceptions.ApiException(status=http.HTTPStatus.CONFLICT.value) 49 50 with monkeypatch.context(): 51 def monkey_create_repository_ex(*_): 52 raise ex 53 54 monkeypatch.setattr(lakefs_sdk.RepositoriesApi, "create_repository", monkey_create_repository_ex) 55 56 # Expect success when exist_ok = True 57 existing = lakefs_sdk.Repository(id=TEST_REPO_ARGS.name, 58 default_branch="main", 59 storage_namespace="s3://existing-namespace", 60 creation_date=12345) 61 62 def monkey_get_repository(*_): 63 return existing 64 65 monkeypatch.setattr(lakefs_sdk.RepositoriesApi, "get_repository", monkey_get_repository) 66 res = repo.create(storage_namespace=TEST_REPO_ARGS.storage_namespace, 67 default_branch=TEST_REPO_ARGS.default_branch, 68 include_samples=TEST_REPO_ARGS.sample_data, 69 exist_ok=True) 70 71 assert res.properties == RepositoryProperties(**existing.dict()) 72 73 # Expect fail on exists 74 with expect_exception_context(ConflictException): 75 repo.create(storage_namespace=TEST_REPO_ARGS.storage_namespace, 76 default_branch=TEST_REPO_ARGS.default_branch, 77 include_samples=TEST_REPO_ARGS.sample_data) 78 79 # Expect fail on exists 80 ex = lakefs_sdk.exceptions.UnauthorizedException(http.HTTPStatus.UNAUTHORIZED) 81 with expect_exception_context(NotAuthorizedException): 82 repo.create(storage_namespace=TEST_REPO_ARGS.storage_namespace, 83 default_branch=TEST_REPO_ARGS.default_branch, 84 include_samples=TEST_REPO_ARGS.sample_data) 85 86 87 def test_delete_repository(monkeypatch): 88 repo = get_test_repo() 89 with monkeypatch.context(): 90 monkeypatch.setattr(lakefs_sdk.RepositoriesApi, "delete_repository", lambda *args: None) 91 repo.delete() 92 93 ex = None 94 95 def monkey_delete_repository(*_): 96 raise ex 97 98 monkeypatch.setattr(lakefs_sdk.RepositoriesApi, "delete_repository", monkey_delete_repository) 99 # Not found 100 ex = lakefs_sdk.exceptions.NotFoundException(status=http.HTTPStatus.NOT_FOUND) 101 with expect_exception_context(NotFoundException): 102 repo.delete() 103 104 # Unauthorized 105 ex = lakefs_sdk.exceptions.UnauthorizedException(status=http.HTTPStatus.UNAUTHORIZED) 106 with expect_exception_context(NotAuthorizedException): 107 repo.delete() 108 109 # Other error 110 ex = lakefs_sdk.exceptions.ApiException() 111 with expect_exception_context(ServerException): 112 repo.delete() 113 114 115 def test_create_repository_no_authentication(monkeypatch): 116 with lakectl_no_config_context(monkeypatch): 117 from lakefs.repository import Repository 118 repo = Repository("test-repo", None) 119 with expect_exception_context(NoAuthenticationFound): 120 repo.create(storage_namespace=TEST_REPO_ARGS.storage_namespace) 121 122 # update credentials and retry create 123 monkeypatch.setattr(lakefs_sdk.RepositoriesApi, "create_repository", monkey_create_repository) 124 with env_var_context(): 125 from lakefs import config as client_config 126 # Create a new client with env vars 127 os.environ[client_config._LAKECTL_ENDPOINT_ENV] = "endpoint" 128 os.environ[client_config._LAKECTL_SECRET_ACCESS_KEY_ENV] = "secret" 129 os.environ[client_config._LAKECTL_ACCESS_KEY_ID_ENV] = "key" 130 repo.create(storage_namespace=TEST_REPO_ARGS.storage_namespace, 131 default_branch=TEST_REPO_ARGS.default_branch)