Skip to content

Commit

Permalink
tests: Add containerd cfg dir marker
Browse files Browse the repository at this point in the history
One of the tests uses a custom containerd base dir. We'll add a
marker so that the registry mirror settings can be applied to that
containerd folder.
  • Loading branch information
petrutlucian94 committed Dec 10, 2024
1 parent fa5ad21 commit d506027
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 8 deletions.
13 changes: 12 additions & 1 deletion tests/integration/tests/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -139,6 +139,7 @@ def pytest_configure(config):
"bootstrap_config: Provide a custom bootstrap config to the bootstrapping node.\n"
"disable_k8s_bootstrapping: By default, the first k8s node is bootstrapped. This marker disables that.\n"
"no_setup: No setup steps (pushing snap, bootstrapping etc.) are performed on any node for this test.\n"
"containerd_cfgdir: The instance containerd config directory, defaults to /etc/containerd."
"network_type: Specify network type to use for the infrastructure (IPv4, Dualstack or IPv6).\n"
"etcd_count: Mark a test to specify how many etcd instance nodes need to be created (None by default)\n"
"node_count: Mark a test to specify how many instance nodes need to be created\n"
Expand Down Expand Up @@ -192,6 +193,15 @@ def network_type(request) -> Union[str, None]:
return network_type


@pytest.fixture(scope="function")
def containerd_cfgdir(request) -> str:
marker = request.node.get_closest_marker("containerd_cfgdir")
if not marker:
return "/etc/containerd"
cfgdir, *_ = marker.args
return cfgdir


@pytest.fixture(scope="function")
def instances(
h: harness.Harness,
Expand All @@ -200,6 +210,7 @@ def instances(
tmp_path: Path,
disable_k8s_bootstrapping: bool,
no_setup: bool,
containerd_cfgdir: str,
bootstrap_config: Union[str, None],
request,
network_type: str,
Expand Down Expand Up @@ -241,7 +252,7 @@ def instances(
util.setup_k8s_snap(instance, tmp_path, snap)

if config.USE_LOCAL_MIRROR:
registry.apply_configuration(instance)
registry.apply_configuration(instance, containerd_cfgdir)

if not disable_k8s_bootstrapping and not no_setup:
first_node, *_ = instances
Expand Down
7 changes: 3 additions & 4 deletions tests/integration/tests/test_cleanup.py
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ def test_node_cleanup(instances: List[harness.Instance], tmp_path):

@pytest.mark.node_count(2)
@pytest.mark.disable_k8s_bootstrapping()
@pytest.mark.containerd_cfgdir("/home/ubuntu/etc/containerd")
@pytest.mark.tags(tags.NIGHTLY)
def test_node_cleanup_new_containerd_path(instances: List[harness.Instance]):
main = instances[0]
Expand Down Expand Up @@ -70,12 +71,10 @@ def test_node_cleanup_new_containerd_path(instances: List[harness.Instance]):
for p in CONTAINERD_PATHS
]

# We expect containerd to use our custom paths instead of the default ones.
# However, the test fixture places registry configuration in /etc/containerd
# and /run/containerd gets created but isn't actually used (requires further
# /run/containerd gets created but isn't actually used (requires further
# investigation).
exp_missing_paths = [
"/etc/containerd/config.toml",
"/etc/containerd",
"/run/containerd/containerd.sock",
"/var/lib/containerd",
]
Expand Down
8 changes: 5 additions & 3 deletions tests/integration/tests/test_util/registry.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
# Copyright 2024 Canonical, Ltd.
#
import logging
import os
from pathlib import Path
from string import Template
from typing import List, Optional
Expand Down Expand Up @@ -165,21 +166,22 @@ def ip(self) -> str:
return self._ip

# Configure the specified instance to use this registry mirror.
def apply_configuration(self, instance):
def apply_configuration(self, instance, containerd_basedir="/etc/containerd"):
for mirror in self.mirrors:
substitutes = {
"IP": self.ip,
"PORT": mirror.port,
}

instance.exec(["mkdir", "-p", f"/etc/containerd/hosts.d/{mirror.name}"])
mirror_dir = os.path.join(containerd_basedir, "hosts.d", mirror.name)
instance.exec(["mkdir", "-p", mirror_dir])

with open(config.REGISTRY_DIR / "hosts.toml", "r") as registry_template:
src = Template(registry_template.read())
instance.exec(
[
"dd",
f"of=/etc/containerd/hosts.d/{mirror.name}/hosts.toml",
f"of={mirror_dir}/hosts.toml",
],
input=str.encode(src.substitute(substitutes)),
)

0 comments on commit d506027

Please sign in to comment.