Skip to content

Commit

Permalink
tests: Move utils_pod fixture to conftest
Browse files Browse the repository at this point in the history
  • Loading branch information
TeddyAndrieux committed Feb 4, 2022
1 parent a5fce8f commit b5cf882
Show file tree
Hide file tree
Showing 4 changed files with 49 additions and 58 deletions.
43 changes: 43 additions & 0 deletions tests/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -228,6 +228,49 @@ def utils_image(registry_address, version):
)


@pytest.fixture
def utils_manifest(utils_image):
manifest_file = pathlib.Path(__file__).parent.resolve() / "files" / "utils.yaml"

with open(manifest_file, encoding="utf-8") as fd:
manifest = yaml.safe_load(fd)

manifest["spec"]["containers"][0]["image"] = utils_image

return manifest


@pytest.fixture
def utils_pod(k8s_client, utils_manifest):
# Create the Pod
pod_name = utils_manifest["metadata"]["name"]

pod_k8s_client = k8s_client.resources.get(api_version="v1", kind="Pod")

pod_k8s_client.create(body=utils_manifest, namespace="default")

# Wait for the Pod to be ready
utils.retry(
kube_utils.check_pod_status(
k8s_client, name=pod_name, namespace="default", state="Running"
),
times=10,
wait=12,
name="wait for Pod '{}'".format(pod_name),
)

yield pod_name

# Clean-up resources
pod_k8s_client.delete(
name=pod_name,
namespace="default",
body=kubernetes.client.V1DeleteOptions(
grace_period_seconds=0, # Force deletion instantly
),
)


@pytest.fixture
def ssh_config(request):
return request.config.getoption("--ssh-config")
Expand Down
File renamed without changes.
45 changes: 0 additions & 45 deletions tests/post/steps/test_dns.py
Original file line number Diff line number Diff line change
@@ -1,50 +1,5 @@
import os

from kubernetes import client
import pytest
from pytest_bdd import scenario, then, parsers
import yaml

from tests import kube_utils
from tests import utils


@pytest.fixture
def utils_pod(k8s_client, utils_image):
# Create the Pod
manifest_file = os.path.join(
os.path.realpath(os.path.dirname(__file__)), "files", "utils.yaml"
)
with open(manifest_file, encoding="utf-8") as fd:
manifest = yaml.safe_load(fd)

manifest["spec"]["containers"][0]["image"] = utils_image
pod_name = manifest["metadata"]["name"]

pod_k8s_client = k8s_client.resources.get(api_version="v1", kind="Pod")

pod_k8s_client.create(body=manifest, namespace="default")

# Wait for the Pod to be ready
utils.retry(
kube_utils.check_pod_status(
k8s_client, name=pod_name, namespace="default", state="Running"
),
times=10,
wait=12,
name="wait for Pod '{}'".format(pod_name),
)

yield pod_name

# Clean-up resources
pod_k8s_client.delete(
name=pod_name,
namespace="default",
body=client.V1DeleteOptions(
grace_period_seconds=0, # Force deletion instantly
),
)


# Scenarios
Expand Down
19 changes: 6 additions & 13 deletions tests/post/steps/test_seccomp.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,32 +24,25 @@ def test_seccomp(host):
"and annotations "
"{'seccomp.security.alpha.kubernetes.io/pod': 'runtime/default'}"
)
def create_utils_pod(utils_pod):
def create_utils_pod(seccomp_pod):
pass


@pytest.fixture
def utils_pod(k8s_client, utils_image):
manifest_file = os.path.join(
os.path.realpath(os.path.dirname(__file__)), "files", "utils.yaml"
)
with open(manifest_file, encoding="utf-8") as fd:
manifest = yaml.safe_load(fd)

def seccomp_pod(k8s_client, utils_manifest):
pod_name = "test-seccomp1"

manifest["spec"]["containers"][0]["image"] = utils_image
manifest["metadata"]["name"] = pod_name
manifest["metadata"]["annotations"] = {
utils_manifest["metadata"]["name"] = pod_name
utils_manifest["metadata"]["annotations"] = {
"seccomp.security.alpha.kubernetes.io/pod": "runtime/default",
}
manifest["metadata"]["labels"] = {
utils_manifest["metadata"]["labels"] = {
"test": "seccomp1",
}

pod_k8s_client = k8s_client.resources.get(api_version="v1", kind="Pod")

pod_k8s_client.create(body=manifest, namespace="default")
pod_k8s_client.create(body=utils_manifest, namespace="default")

try:
yield pod_name
Expand Down

0 comments on commit b5cf882

Please sign in to comment.