github.com/treeverse/lakefs@v1.24.1-0.20240520134607-95648127bfb0/clients/python-wrapper/tests/integration/test_sanity.py (about)

     1  import ssl
     2  import time
     3  import pytest
     4  
     5  from tests.utests.common import expect_exception_context
     6  from lakefs.exceptions import NotFoundException, ConflictException, ObjectNotFoundException
     7  import lakefs
     8  
     9  
    10  def test_repository_sanity(storage_namespace, setup_repo):
    11      _, repo = setup_repo
    12      repo = lakefs.repository(repo.properties.id)  # test the lakefs.repository function works properly
    13      default_branch = "main"
    14      expected_properties = lakefs.RepositoryProperties(id=repo.properties.id,
    15                                                        default_branch=default_branch,
    16                                                        storage_namespace=storage_namespace,
    17                                                        creation_date=repo.properties.creation_date)
    18      assert repo.properties == expected_properties
    19  
    20      # Create with allow exists
    21      new_data = repo.create(storage_namespace, default_branch, True, exist_ok=True)
    22      assert new_data.properties == repo.properties
    23  
    24      # Try to create twice and expect conflict
    25      with expect_exception_context(ConflictException):
    26          repo.create(storage_namespace, default_branch, True)
    27  
    28      # Get metadata
    29      _ = repo.metadata
    30  
    31      # List branches
    32      branches = list(repo.branches())
    33      assert len(branches) == 1
    34  
    35      # Delete repository
    36      repo.delete()
    37  
    38      # Delete non existent
    39      with expect_exception_context(NotFoundException):
    40          repo.delete()
    41  
    42  
    43  def test_branch_sanity(setup_repo):
    44      _, repo = setup_repo
    45      branch_name = "test_branch"
    46  
    47      main_branch = repo.branch("main")
    48      new_branch = repo.branch(branch_name).create("main")
    49      assert new_branch.repo_id == repo.properties.id
    50      assert new_branch.id == branch_name
    51      assert new_branch.head.id == main_branch.head.id
    52  
    53      new_branch.delete()
    54      with expect_exception_context(NotFoundException):
    55          new_branch.head  # pylint: disable=pointless-statement
    56  
    57  
    58  def test_ref_sanity(setup_repo):
    59      _, repo = setup_repo
    60      ref_id = "main"
    61      ref = repo.ref(ref_id)
    62      assert ref.repo_id == repo.properties.id
    63      assert ref.id == ref_id
    64      assert ref.get_commit().metadata == {}
    65      assert ref.get_commit().message == "Repository created"
    66  
    67  
    68  def test_tag_sanity(setup_repo):
    69      _, repo = setup_repo
    70      tag_name = "test_tag"
    71      tag = repo.tag(tag_name)
    72  
    73      # expect not found
    74      with expect_exception_context(NotFoundException):
    75          tag.get_commit()
    76  
    77      branch = repo.branch("main")
    78      commit = branch.get_commit()
    79      res = tag.create(commit)
    80      assert res == tag
    81      assert tag.id == tag_name
    82      assert tag.get_commit().metadata == commit.metadata
    83      assert tag.get_commit().message == commit.message
    84  
    85      # Create again
    86      with expect_exception_context(ConflictException):
    87          tag.create(commit.id)
    88  
    89      # Create again with exist_ok
    90      tag2 = tag.create(tag_name, True)
    91      assert tag2 == tag
    92  
    93      # Delete tag
    94      tag.delete()
    95  
    96      # expect not found
    97      with expect_exception_context(NotFoundException):
    98          tag.get_commit()
    99  
   100      # Delete twice
   101      with expect_exception_context(NotFoundException):
   102          tag.delete()
   103  
   104      # Create again
   105      tag.create(commit.id)
   106  
   107  
   108  def test_object_sanity(setup_repo):
   109      clt, repo = setup_repo
   110      data = b"test_data"
   111      path = "test_obj"
   112      metadata = {"foo": "bar"}
   113      obj = lakefs.WriteableObject(repository_id=repo.properties.id, reference_id="main", path=path, client=clt).upload(
   114          data=data, metadata=metadata)
   115      with obj.reader() as fd:
   116          assert fd.read() == data
   117  
   118      stats = obj.stat()
   119      assert stats.path == path == obj.path
   120      assert stats.mtime <= time.time()
   121      assert stats.size_bytes == len(data)
   122      assert stats.metadata == metadata
   123      assert stats.content_type == "application/octet-stream"
   124  
   125      obj.delete()
   126      with expect_exception_context(ObjectNotFoundException):
   127          obj.stat()
   128  
   129  
   130  @pytest.mark.parametrize("ssl_enabled", (True, False, None))
   131  def test_ssl_configuration(ssl_enabled):
   132      expected_ssl = ssl_enabled is not False
   133      expected_cert_req = ssl.CERT_REQUIRED if expected_ssl else ssl.CERT_NONE
   134      proxy = "http://someproxy:1234"
   135      cert = "somecert"
   136      clt = lakefs.client.Client(verify_ssl=ssl_enabled, proxy=proxy, ssl_ca_cert=cert)
   137      assert clt.config.verify_ssl is expected_ssl
   138      assert clt.config.proxy == proxy
   139      assert clt.config.ssl_ca_cert == cert
   140      assert clt.sdk_client._api.rest_client.pool_manager.connection_pool_kw.get("cert_reqs") is expected_cert_req
   141      assert str(clt.sdk_client._api.rest_client.pool_manager.proxy) == proxy
   142      assert clt.sdk_client._api.rest_client.pool_manager.connection_pool_kw.get("ca_certs") is cert