Skip to content

Commit

Permalink
Adds Whereabouts integration test
Browse files Browse the repository at this point in the history
Whereabouts depends on Multus, which means that the integration
test is deploying Multus as well.
  • Loading branch information
claudiubelu committed Jul 17, 2024
1 parent c015eda commit b7b0567
Show file tree
Hide file tree
Showing 6 changed files with 197 additions and 6 deletions.
5 changes: 5 additions & 0 deletions tests/integration/conftest.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
#
# Copyright 2024 Canonical, Ltd.
#

pytest_plugins = ["k8s_test_harness.plugin"]
134 changes: 132 additions & 2 deletions tests/integration/test_whereabouts.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,136 @@
# Copyright 2024 Canonical, Ltd.
#

import logging
import os
from pathlib import Path

def test_integration_whereabouts():
assert False, "Integration tests are not yet implemented yet"
from k8s_test_harness import harness
from k8s_test_harness.util import exec_util

LOG = logging.getLogger(__name__)

DIR = Path(__file__).absolute().parent
MANIFESTS_DIR = DIR / ".." / "templates"


def _get_whereabouts_helm_cmd():
images = [
{"variable": "ROCK_WHEREABOUTS", "prefix": None},
]

helm_command = [
"k8s",
"helm",
"install",
"whereabouts",
"oci://registry-1.docker.io/bitnamicharts/whereabouts",
"--namespace",
"whereabouts",
"--create-namespace",
]

for image in images:
image_uri = os.getenv(image["variable"])
assert image_uri is not None, f"{image['variable']} is not set"
image_split = image_uri.split(":")
image_name = image_split[0]

# This helm charts requires setting the image registry separately.
parts = image_name.split("/")
if len(parts) > 1:
image_name = "/".join(parts[1:])

prefix = ""
if image["prefix"]:
prefix = f"{image['prefix']}."

helm_command += [
"--set",
f"{prefix}image.repository={image_name}",
"--set",
f"{prefix}image.tag={image_split[1]}",
"--set",
"{prefix}securityContext.runAsUser=584792",
]

# Get the registry name from the image URI.
registry = "docker.io"
image_uri = os.getenv(images[0]["variable"])
parts = image_uri.split("/")
if len(parts) > 1:
registry = parts[0]
helm_command += [
"--set",
f"image.registry={registry}",
]

return helm_command


def _get_multus_helm_cmd():
return [
"k8s",
"helm",
"install",
"multus",
"oci://registry-1.docker.io/bitnamicharts/multus-cni",
"--namespace",
"kube-system",
]


def _wait_for_daemonset(instance, name, namespace="default"):
exec_util.stubbornly(retries=3, delay_s=1).on(instance).exec(
[
"k8s",
"kubectl",
"rollout",
"status",
"--namespace",
namespace,
"daemonset.apps",
name,
"--timeout",
"60s",
]
)


def _wait_for_resource(
instance, resource_type, name, namespace="default", condition="Available"
):
exec_util.stubbornly(retries=3, delay_s=1).on(instance).exec(
[
"k8s",
"kubectl",
"wait",
"--namespace",
namespace,
f"--for=condition={condition}",
resource_type,
name,
"--timeout",
"60s",
]
)


def test_integration_whereabouts(module_instance: harness.Instance):
# We also need multus in order to test out whereabouts.
# It should become Available if everything is fine with it.
module_instance.exec(_get_multus_helm_cmd())
_wait_for_daemonset(module_instance, "multus-multus-cni", "kube-system")

module_instance.exec(_get_whereabouts_helm_cmd())
_wait_for_daemonset(module_instance, "whereabouts", "whereabouts")

# Create a NetworkAttachmentDefinition and a deployment requiring it.
for filename in ["whereabouts-net-definition.yaml", "deployment.yaml"]:
manifest = MANIFESTS_DIR / filename
module_instance.exec(
["k8s", "kubectl", "apply", "-f", "-"],
input=Path(manifest).read_bytes(),
)

_wait_for_resource(module_instance, "deployment.apps", "netshoot-deployment")
1 change: 1 addition & 0 deletions tests/requirements-test.txt
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,4 @@ coverage[toml]==7.2.5
pytest==7.3.1
PyYAML==6.0.1
tenacity==8.2.3
git+https://github.com/canonical/k8s-test-harness.git@main
25 changes: 25 additions & 0 deletions tests/templates/deployment.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
apiVersion: apps/v1
kind: Deployment
metadata:
name: netshoot-deployment
labels:
app: netshoot-deployment
spec:
replicas: 1
selector:
matchLabels:
app: netshoot-pod
template:
metadata:
annotations:
k8s.v1.cni.cncf.io/networks: whereabouts-conf
labels:
app: netshoot-pod
spec:
containers:
- name: netshoot
image: nicolaka/netshoot
command:
- sleep
- "3600"
imagePullPolicy: IfNotPresent
22 changes: 22 additions & 0 deletions tests/templates/whereabouts-net-definition.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
apiVersion: "k8s.cni.cncf.io/v1"
kind: NetworkAttachmentDefinition
metadata:
name: whereabouts-conf
spec:
config: '{
"cniVersion": "0.3.1",
"plugins": [
{
"type": "macvlan",
"capabilities": { "ips": true },
"master": "eth0",
"mode": "bridge",
"ipam": {
"type": "whereabouts",
"range": "192.168.20.0/24",
"range_start": "192.168.20.10",
"range_end": "192.168.20.100",
"gateway": "192.168.20.1"
}
} ]
}'
16 changes: 12 additions & 4 deletions tests/tox.ini
Original file line number Diff line number Diff line change
Expand Up @@ -48,11 +48,19 @@ pass_env =
ROCK_*

[testenv: integration]
allowlist_externals =
echo
description = Run integration tests
deps = -r {tox_root}/requirements-test.txt
commands =
# TODO: Implement integration tests here
echo "WARNING: This is a placeholder test - no test is implemented here."
pytest -v \
--maxfail 1 \
--tb native \
--log-cli-level DEBUG \
--disable-warnings \
{posargs} \
{tox_root}/integration
pass_env =
TEST_*
ROCK_*

[flake8]
max-line-length = 120
Expand Down

0 comments on commit b7b0567

Please sign in to comment.