github.com/treeverse/lakefs@v1.24.1-0.20240520134607-95648127bfb0/clients/python-wrapper/tests/utests/common.py (about) 1 import importlib 2 import os 3 from contextlib import contextmanager 4 from copy import deepcopy 5 6 import lakefs_sdk 7 import lakefs.repository 8 from lakefs import config as client_config 9 10 TEST_SERVER = "https://test_server" 11 TEST_ACCESS_KEY_ID = "test_access_key_id" 12 TEST_SECRET_ACCESS_KEY = "test_secret_access_key" 13 TEST_CONFIG = f''' 14 server: 15 endpoint_url: {TEST_SERVER} 16 17 credentials: 18 access_key_id: {TEST_ACCESS_KEY_ID} 19 secret_access_key: {TEST_SECRET_ACCESS_KEY} 20 ''' 21 22 TEST_ENDPOINT_PATH = "/api/v1" 23 24 TEST_REPO_ARGS = lakefs_sdk.RepositoryCreation(name="test-repo", 25 storage_namespace="s3://test_namespace", 26 default_branch="default-branch", 27 samples_data=True) 28 29 30 def get_test_client(): 31 from lakefs.client import Client 32 clt = Client(username=TEST_ACCESS_KEY_ID, password=TEST_SECRET_ACCESS_KEY, host=TEST_SERVER) 33 return clt 34 35 36 @contextmanager 37 def lakectl_test_config_context(monkey, tmp_path): 38 cfg_file = tmp_path / "test.yaml" 39 cfg_file.write_bytes(TEST_CONFIG.encode()) 40 with monkey.context(): 41 monkey.setattr(client_config, "_LAKECTL_YAML_PATH", cfg_file) 42 from lakefs import client # Must be imported after the monkey patching 43 client = importlib.reload(client) 44 try: 45 yield client 46 finally: 47 client._DEFAULT_CLIENT = None 48 49 50 @contextmanager 51 def lakectl_no_config_context(monkey): 52 with monkey.context(): 53 monkey.setattr(client_config, "_LAKECTL_YAML_PATH", "file_not_found") 54 from lakefs import client # Must be imported after the monkey patching 55 yield client 56 57 58 @contextmanager 59 def env_var_context(): 60 old_env = deepcopy(os.environ) 61 try: 62 yield 63 finally: 64 os.environ = old_env 65 66 67 def get_test_repo() -> lakefs.Repository: 68 from lakefs.client import Client 69 client = Client(username="test_user", password="test_password", host="http://127.0.0.1:8000") 70 return lakefs.Repository(repository_id=TEST_REPO_ARGS.name, client=client) 71 72 73 @contextmanager 74 def expect_exception_context(ex, message=None): 75 try: 76 yield 77 assert False, f"No exception raised! Expected exception of type {ex.__name__}" 78 except ex as e: 79 if message is not None: 80 assert message in str(e)