Source code for oras.tests.test_oras
__author__ = "Vanessa Sochat"
__copyright__ = "Copyright The ORAS Authors."
__license__ = "Apache-2.0"
import os
import shutil
import pytest
import oras.client
here = os.path.abspath(os.path.dirname(__file__))
[docs]
def test_basic_oras(registry):
"""
Basic tests for oras (without authentication)
"""
client = oras.client.OrasClient(hostname=registry, insecure=True)
assert "Python version" in client.version()
[docs]
@pytest.mark.with_auth(True)
def test_login_logout(registry, credentials):
"""
Login and logout are all we can test with basic auth!
"""
client = oras.client.OrasClient(hostname=registry, insecure=True)
res = client.login(
hostname=registry,
username=credentials.user,
password=credentials.password,
insecure=True,
)
assert res["Status"] == "Login Succeeded"
client.logout(registry)
[docs]
@pytest.mark.with_auth(False)
def test_basic_push_pull(tmp_path, registry, credentials, target):
"""
Basic tests for oras (without authentication)
"""
client = oras.client.OrasClient(hostname=registry, insecure=True)
artifact = os.path.join(here, "artifact.txt")
assert os.path.exists(artifact)
res = client.push(files=[artifact], target=target)
assert res.status_code in [200, 201]
# Test pulling elsewhere
files = client.pull(target=target, outdir=tmp_path)
assert len(files) == 1
assert os.path.basename(files[0]) == "artifact.txt"
assert str(tmp_path) in files[0]
assert os.path.exists(files[0])
# Move artifact outside of context (should not work)
moved_artifact = tmp_path / os.path.basename(artifact)
shutil.copyfile(artifact, moved_artifact)
with pytest.raises(ValueError):
client.push(files=[moved_artifact], target=target)
# This should work because we aren't checking paths
res = client.push(files=[artifact], target=target, disable_path_validation=True)
assert res.status_code == 201
[docs]
@pytest.mark.with_auth(False)
def test_directory_push_pull(tmp_path, registry, credentials, target_dir):
"""
Test push and pull for directory
"""
client = oras.client.OrasClient(hostname=registry, insecure=True)
# Test upload of a directory
upload_dir = os.path.join(here, "upload_data")
res = client.push(files=[upload_dir], target=target_dir)
assert res.status_code == 201
files = client.pull(target=target_dir, outdir=tmp_path)
assert len(files) == 1
assert os.path.basename(files[0]) == "upload_data"
assert str(tmp_path) in files[0]
assert os.path.exists(files[0])
assert "artifact.txt" in os.listdir(files[0])