__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, tls_verify=False)
res = client.login(
hostname=registry,
tls_verify=False,
username=credentials.user,
password=credentials.password,
)
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_basic_push_pul_via_sha_ref(tmp_path, registry, credentials, target):
"""
Basic tests for oras pushing and then pulling with SHA reference
"""
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
using_ref = f"{registry}/dinosaur/artifact@{res.headers['Docker-Content-Digest']}"
files = client.pull(target=using_ref, 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])
[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])
[docs]
@pytest.mark.with_auth(True)
def test_directory_push_pull_selfsigned_auth(
tmp_path, registry, credentials, target_dir
):
"""
Test push and pull for directory using a self-signed cert registry (`tls_verify=False`) and basic auth (`auth_backend="basic"`)
"""
client = oras.client.OrasClient(
hostname=registry, tls_verify=False, auth_backend="basic"
)
res = client.login(
hostname=registry,
tls_verify=False,
username=credentials.user,
password=credentials.password,
)
assert res["Status"] == "Login Succeeded"
# 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])
[docs]
@pytest.mark.with_auth(True)
def test_custom_docker_config_path(tmp_path, registry, credentials, target_dir):
"""
Custom docker config_path for login, push, pull
"""
my_dockercfg_path = tmp_path / "myconfig.json"
client = oras.client.OrasClient(
hostname=registry, tls_verify=False, auth_backend="basic"
)
res = client.login(
hostname=registry,
tls_verify=False,
username=credentials.user,
password=credentials.password,
config_path=my_dockercfg_path, # <-- for login
)
assert res["Status"] == "Login Succeeded"
# Test push/pull with custom docker config_path
upload_dir = os.path.join(here, "upload_data")
res = client.push(
files=[upload_dir], target=target_dir, config_path=my_dockercfg_path
)
assert res.status_code == 201
files = client.pull(
target=target_dir, outdir=tmp_path, config_path=my_dockercfg_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])
client.logout(registry)