Skip to content

Commit

Permalink
tests: move registry infos to fixtures
Browse files Browse the repository at this point in the history
Signed-off-by: Tiziano Müller <tiziano.mueller@hpe.com>
  • Loading branch information
dev-zero committed Sep 27, 2023
1 parent 38d634f commit 765d0a9
Showing 4 changed files with 74 additions and 61 deletions.
54 changes: 54 additions & 0 deletions oras/tests/conftest.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
import os
from dataclasses import dataclass

import pytest


@dataclass
class TestCredentials:
with_auth: bool
user: str
password: str


@pytest.fixture(scope="module")
def registry():
host = os.environ.get("ORAS_HOST")
port = os.environ.get("ORAS_PORT")

if not host or not port:
pytest.skip(
"You must export ORAS_HOST and ORAS_PORT"
" for a running registry before running tests."
)

return f"{host}:{port}"


@pytest.fixture(scope="module")
def credentials(request):
with_auth = os.environ.get("ORAS_AUTH") == "true"
user = os.environ.get("ORAS_USER", "myuser")
pwd = os.environ.get("ORAS_PASS", "mypass")

if with_auth and not user or not pwd:
pytest.skip("To test auth you need to export ORAS_USER and ORAS_PASS")

if request.node.get_closest_marker("with_auth"):
if request.node.get_closest_marker("with_auth").args[0] != with_auth:
if with_auth:
pytest.skip("test requires un-authenticated access to registry")
else:
pytest.skip("test requires authenticated access to registry")

return TestCredentials(with_auth, user, pwd)


@pytest.fixture(scope="module")
def target(registry):
return f"{registry}/dinosaur/artifact:v1"


@pytest.fixture(scope="module")
def target_dir(registry):
return f"{registry}/dinosaur/directory:v1"
47 changes: 13 additions & 34 deletions oras/tests/test_oras.py
Original file line number Diff line number Diff line change
@@ -4,61 +4,40 @@

import os
import shutil
import sys

import pytest

import oras.client

here = os.path.abspath(os.path.dirname(__file__))

registry_host = os.environ.get("ORAS_HOST")
registry_port = os.environ.get("ORAS_PORT")
with_auth = os.environ.get("ORAS_AUTH") == "true"
oras_user = os.environ.get("ORAS_USER", "myuser")
oras_pass = os.environ.get("ORAS_PASS", "mypass")


def setup_module(module):
"""
Ensure the registry port and host is in the environment.
"""
if not registry_host or not registry_port:
sys.exit(
"You must export ORAS_HOST and ORAS_PORT for a running registry before running tests."
)
if with_auth and not oras_user or not oras_pass:
sys.exit("To test auth you need to export ORAS_USER and ORAS_PASS")


registry = f"{registry_host}:{registry_port}"
target = f"{registry}/dinosaur/artifact:v1"
target_dir = f"{registry}/dinosaur/directory:v1"


def test_basic_oras():
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()


@pytest.mark.skipif(not with_auth, reason="basic auth is needed for login/logout")
def test_login_logout():
@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=oras_user, password=oras_pass, insecure=True
hostname=registry,
username=credentials.user,
password=credentials.password,
insecure=True,
)
assert res["Status"] == "Login Succeeded"
client.logout(registry)


@pytest.mark.skipif(with_auth, reason="token auth is needed for push and pull")
def test_basic_push_pull(tmp_path):
@pytest.mark.with_auth(False)
def test_basic_push_pull(tmp_path, registry, credentials, target):
"""
Basic tests for oras (without authentication)
"""
@@ -88,8 +67,8 @@ def test_basic_push_pull(tmp_path):
assert res.status_code == 201


@pytest.mark.skipif(with_auth, reason="token auth is needed for push and pull")
def test_get_delete_tags(tmp_path):
@pytest.mark.with_auth(False)
def test_get_delete_tags(tmp_path, registry, credentials, target):
"""
Test creationg, getting, and deleting tags.
"""
@@ -139,8 +118,8 @@ def test_get_many_tags():
assert len(tags) == 10


@pytest.mark.skipif(with_auth, reason="token auth is needed for push and pull")
def test_directory_push_pull(tmp_path):
@pytest.mark.with_auth(False)
def test_directory_push_pull(tmp_path, registry, credentials, target_dir):
"""
Test push and pull for directory
"""
31 changes: 4 additions & 27 deletions oras/tests/test_provider.py
Original file line number Diff line number Diff line change
@@ -3,7 +3,6 @@
__license__ = "Apache-2.0"

import os
import sys
from pathlib import Path

import pytest
@@ -15,35 +14,13 @@

here = os.path.abspath(os.path.dirname(__file__))

registry_host = os.environ.get("ORAS_HOST")
registry_port = os.environ.get("ORAS_PORT")
with_auth = os.environ.get("ORAS_AUTH") == "true"
oras_user = os.environ.get("ORAS_USER", "myuser")
oras_pass = os.environ.get("ORAS_PASS", "mypass")


def setup_module(module):
"""
Ensure the registry port and host is in the environment.
"""
if not registry_host or not registry_port:
sys.exit(
"You must export ORAS_HOST and ORAS_PORT for a running registry before running tests."
)
if with_auth and not oras_user or not oras_pass:
sys.exit("To test auth you need to export ORAS_USER and ORAS_PASS")


registry = f"{registry_host}:{registry_port}"
target = f"{registry}/dinosaur/artifact:v1"
target_dir = f"{registry}/dinosaur/directory:v1"


@pytest.mark.skipif(with_auth, reason="token auth is needed for push and pull")
def test_annotated_registry_push(tmp_path):
@pytest.mark.with_auth(False)
def test_annotated_registry_push(tmp_path, registry, credentials, target):
"""
Basic tests for oras push with annotations
"""

# Direct access to registry functions
remote = oras.provider.Registry(hostname=registry, insecure=True)
client = oras.client.OrasClient(hostname=registry, insecure=True)
@@ -84,7 +61,7 @@ def test_annotated_registry_push(tmp_path):
)


def test_parse_manifest():
def test_parse_manifest(registry):
"""
Test parse manifest function.
3 changes: 3 additions & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
@@ -8,3 +8,6 @@ skip = []

[tool.mypy]
mypy_path = ["oras", "examples"]

[tool.pytest.ini_options]
markers = ["with_auth: mark for tests requiring authenticated registry access (or not)"]

0 comments on commit 765d0a9

Please sign in to comment.