Skip to content

Commit

Permalink
Switch to external harness
Browse files Browse the repository at this point in the history
The rock tests are expected to share as significant amount of code
(e.g. k8s harness, various helpers), which was moved to a separate
project to avoid duplication.

While at it, we're fixing the linter and code formatter jobs.
  • Loading branch information
petrutlucian94 committed Jul 16, 2024
1 parent 13d3695 commit f32d7c8
Show file tree
Hide file tree
Showing 19 changed files with 82 additions and 1,355 deletions.
1 change: 1 addition & 0 deletions .gitattributes
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
* text eol=lf
2 changes: 2 additions & 0 deletions tests/.copyright.tmpl
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
Copyright ${years} ${owner}.
See LICENSE file for licensing details
137 changes: 1 addition & 136 deletions tests/integration/conftest.py
Original file line number Diff line number Diff line change
@@ -1,136 +1 @@
#
# Copyright 2024 Canonical, Ltd.
#
import logging
from pathlib import Path
from typing import Generator, List

import pytest
from test_util import config, harness, util

LOG = logging.getLogger(__name__)


def _harness_clean(h: harness.Harness):
"Clean up created instances within the test harness."

if config.SKIP_CLEANUP:
LOG.warning(
"Skipping harness cleanup. "
"It is your job now to clean up cloud resources"
)
else:
LOG.debug("Cleanup")
h.cleanup()


@pytest.fixture(scope="module")
def h() -> harness.Harness:
LOG.debug("Create harness for %s", config.SUBSTRATE)
if config.SUBSTRATE == "local":
h = harness.LocalHarness()
elif config.SUBSTRATE == "lxd":
h = harness.LXDHarness()
elif config.SUBSTRATE == "multipass":
h = harness.MultipassHarness()
elif config.SUBSTRATE == "juju":
h = harness.JujuHarness()
else:
raise harness.HarnessError(
"TEST_SUBSTRATE must be one of: local, lxd, multipass, juju"
)

yield h

_harness_clean(h)


def pytest_configure(config):
config.addinivalue_line(
"markers",
"node_count: Mark a test to specify how many instance nodes need to be created\n"
"disable_k8s_bootstrapping: By default, the first k8s node is bootstrapped. This marker disables that.",
)


@pytest.fixture(scope="function")
def node_count(request) -> int:
node_count_marker = request.node.get_closest_marker("node_count")
if not node_count_marker:
return 1
node_count_arg, *_ = node_count_marker.args
return int(node_count_arg)


@pytest.fixture(scope="function")
def disable_k8s_bootstrapping(request) -> int:
return bool(request.node.get_closest_marker("disable_k8s_bootstrapping"))


@pytest.fixture(scope="function")
def instances(
h: harness.Harness, node_count: int, tmp_path: Path, disable_k8s_bootstrapping: bool
) -> Generator[List[harness.Instance], None, None]:
"""Construct instances for a cluster.
Bootstrap and setup networking on the first instance, if `disable_k8s_bootstrapping` marker is not set.
"""
if not config.SNAP_CHANNEL:
pytest.fail("Set TEST_SNAP_CHANNEL to the channel of the k8s snap to install.")

if node_count <= 0:
pytest.xfail("Test requested 0 or fewer instances, skip this test.")

LOG.info(f"Creating {node_count} instances")
instances: List[harness.Instance] = []

for _ in range(node_count):
# Create <node_count> instances and setup the k8s snap in each.
instance = h.new_instance()
instances.append(instance)
util.setup_k8s_snap(instance)

if not disable_k8s_bootstrapping:
first_node, *_ = instances
first_node.exec(["k8s", "bootstrap"])

yield instances

if config.SKIP_CLEANUP:
LOG.warning("Skipping clean-up of instances, delete them on your own")
return

# Cleanup after each test.
# We cannot execute _harness_clean() here as this would also
# remove the module_instance.
for instance in instances:
h.delete_instance(instance.id)


@pytest.fixture(scope="module")
def module_instance(
h: harness.Harness, tmp_path_factory: pytest.TempPathFactory,
request
) -> Generator[harness.Instance, None, None]:
"""Constructs and bootstraps an instance that persists over a test session.
Bootstraps the instance with all k8sd features enabled to reduce testing time.
"""
LOG.info("Setup node and enable all features")

instance = h.new_instance()
util.setup_k8s_snap(instance)
request.addfinalizer(lambda: util.purge_k8s_snap(instance))

bootstrap_config_path = "/home/ubuntu/bootstrap-session.yaml"
instance.send_file(
(config.MANIFESTS_DIR / "bootstrap-session.yaml").as_posix(),
bootstrap_config_path,
)

instance.exec(["k8s", "bootstrap", "--file", bootstrap_config_path])
util.wait_until_k8s_ready(instance, [instance])
util.wait_for_network(instance)
util.wait_for_dns(instance)

yield instance
pytest_plugins = ["k8s_test_harness.conftest"]
47 changes: 30 additions & 17 deletions tests/integration/test_multus_v3_8.py
Original file line number Diff line number Diff line change
@@ -1,32 +1,39 @@
#
# Copyright 2024 Canonical, Ltd.
# See LICENSE file for licensing details
#
import os
import pathlib

from test_util import harness, util
from k8s_test_harness import harness
from k8s_test_harness.util import exec_util


def test_multus_deployment(
tmp_path: pathlib.Path,
module_instance: harness.Instance):
def test_multus_deployment(tmp_path: pathlib.Path, module_instance: harness.Instance):
clone_path = tmp_path / "multus"
clone_path.mkdir()

clone_command = [
"git", "clone", "https://github.com/k8snetworkplumbingwg/helm-charts",
"--depth", "1",
str(clone_path.absolute())
"git",
"clone",
"https://github.com/k8snetworkplumbingwg/helm-charts",
"--depth",
"1",
str(clone_path.absolute()),
]
module_instance.exec(clone_command)

chart_path = clone_path / 'multus'
chart_path = clone_path / "multus"

helm_command = [
"sudo", "k8s",
"helm", "install", "multus-cni",
"sudo",
"k8s",
"helm",
"install",
"multus-cni",
str(chart_path.absolute()),
"--namespace", "kube-system",
"--namespace",
"kube-system",
]

image_uri = os.getenv("ROCK_MULTUS_V3_8")
Expand All @@ -44,12 +51,18 @@ def test_multus_deployment(

module_instance.exec(helm_command)

util.stubbornly(retries=3, delay_s=1).on(module_instance).exec(
exec_util.stubbornly(retries=3, delay_s=1).on(module_instance).exec(
[
"sudo", "k8s",
"kubectl", "rollout", "status",
"daemonset", "multus-cni-multus-ds",
"--namespace", "kube-system",
"--timeout", "60s",
"sudo",
"k8s",
"kubectl",
"rollout",
"status",
"daemonset",
"multus-cni-multus-ds",
"--namespace",
"kube-system",
"--timeout",
"60s",
]
)
5 changes: 3 additions & 2 deletions tests/integration/test_multus_v4_0_2.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,8 @@
#
import os

from test_util import harness, util
from k8s_test_harness import harness
from k8s_test_harness.util import exec_util


def test_multus_deployment(module_instance: harness.Instance):
Expand All @@ -30,7 +31,7 @@ def test_multus_deployment(module_instance: harness.Instance):

module_instance.exec(helm_command)

util.stubbornly(retries=3, delay_s=1).on(module_instance).exec(
exec_util.stubbornly(retries=3, delay_s=1).on(module_instance).exec(
[
"sudo", "k8s",
"kubectl", "rollout", "status",
Expand Down
63 changes: 0 additions & 63 deletions tests/integration/test_util/config.py

This file was deleted.

18 changes: 0 additions & 18 deletions tests/integration/test_util/harness/__init__.py

This file was deleted.

Loading

0 comments on commit f32d7c8

Please sign in to comment.