Skip to content

Commit

Permalink
Provide pytest marker to set custom bootstrap config (#570)
Browse files Browse the repository at this point in the history
---------

Co-authored-by: Mateo Florido <32885896+mateoflorido@users.noreply.github.com>
  • Loading branch information
bschimke95 and mateoflorido committed Jul 31, 2024
1 parent f7167b5 commit fbc4fe2
Show file tree
Hide file tree
Showing 5 changed files with 51 additions and 46 deletions.
42 changes: 35 additions & 7 deletions tests/integration/tests/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
#
import logging
from pathlib import Path
from typing import Generator, List
from typing import Generator, List, Union

import pytest
from test_util import config, harness, util
Expand Down Expand Up @@ -49,9 +49,11 @@ def h() -> harness.Harness:
def pytest_configure(config):
config.addinivalue_line(
"markers",
"node_count: Mark a test to specify how many instance nodes need to be created\n"
"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"
"etcd_count: Mark a test to specify how many etcd instance nodes need to be created (None by default)\n",
"dualstack: Support dualstack on the instances.\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 All @@ -65,13 +67,32 @@ def node_count(request) -> int:


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


@pytest.fixture(scope="function")
def bootstrap_config(request) -> Union[str, None]:
bootstrap_config_marker = request.node.get_closest_marker("bootstrap_config")
if not bootstrap_config_marker:
return None
config, *_ = bootstrap_config_marker.args
return config


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


@pytest.fixture(scope="function")
def instances(
h: harness.Harness, node_count: int, tmp_path: Path, disable_k8s_bootstrapping: bool
h: harness.Harness,
node_count: int,
tmp_path: Path,
disable_k8s_bootstrapping: bool,
bootstrap_config: Union[str, None],
dualstack: bool,
) -> Generator[List[harness.Instance], None, None]:
"""Construct instances for a cluster.
Expand All @@ -90,13 +111,20 @@ def instances(

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

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

if bootstrap_config is not None:
first_node.exec(
["k8s", "bootstrap", "--file", "-"],
input=str.encode(bootstrap_config),
)
else:
first_node.exec(["k8s", "bootstrap"])

yield instances

Expand Down
17 changes: 4 additions & 13 deletions tests/integration/tests/test_control_plane_taints.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,28 +6,19 @@
from typing import List

import pytest
import yaml
from test_util import harness, util

LOG = logging.getLogger(__name__)


@pytest.mark.node_count(1)
@pytest.mark.disable_k8s_bootstrapping()
@pytest.mark.bootstrap_config(
'control-plane-taints: ["node-role.kubernetes.io/control-plane:NoSchedule"]'
)
def test_control_plane_taints(instances: List[harness.Instance]):
k8s_instance = instances[0]

bootstrap_conf = yaml.safe_dump(
{"control-plane-taints": ["node-role.kubernetes.io/control-plane:NoSchedule"]}
)

k8s_instance.exec(
["dd", "of=/root/config.yaml"],
input=str.encode(bootstrap_conf),
)

k8s_instance.exec(["k8s", "bootstrap", "--file", "/root/config.yaml"])
retries = 10

while retries and not (nodes := util.get_nodes(k8s_instance)):
LOG.info("Waiting for Nodes")
time.sleep(3)
Expand Down
21 changes: 7 additions & 14 deletions tests/integration/tests/test_dualstack.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
#
import logging
from ipaddress import IPv4Address, IPv6Address, ip_address
from pathlib import Path
from typing import List

import pytest
from test_util import config, harness, util
Expand All @@ -12,19 +12,12 @@


@pytest.mark.node_count(1)
def test_dualstack(h: harness.Harness, tmp_path: Path):
snap_path = (tmp_path / "k8s.snap").as_posix()
main = h.new_instance(dualstack=True)
util.setup_k8s_snap(main, snap_path)

bootstrap_config = (config.MANIFESTS_DIR / "bootstrap-dualstack.yaml").read_text()

main.exec(
["k8s", "bootstrap", "--file", "-"],
input=str.encode(bootstrap_config),
)
util.wait_until_k8s_ready(main, [main])

@pytest.mark.bootstrap_config(
(config.MANIFESTS_DIR / "bootstrap-dualstack.yaml").read_text()
)
@pytest.mark.dualstack()
def test_dualstack(instances: List[harness.Instance]):
main = instances[0]
dualstack_config = (config.MANIFESTS_DIR / "nginx-dualstack.yaml").read_text()

# Deploy nginx with dualstack service
Expand Down
15 changes: 4 additions & 11 deletions tests/integration/tests/test_smoke.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,25 +8,18 @@
from typing import List

import pytest
from test_util import config, harness, util
from test_util import config, harness

LOG = logging.getLogger(__name__)


@pytest.mark.node_count(1)
@pytest.mark.disable_k8s_bootstrapping()
@pytest.mark.bootstrap_config(
(config.MANIFESTS_DIR / "bootstrap-smoke.yaml").read_text()
)
def test_smoke(instances: List[harness.Instance]):
instance = instances[0]

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

instance.exec(["k8s", "bootstrap", "--file", bootstrap_smoke_config_path])
util.wait_until_k8s_ready(instance, [instance])

# Verify the functionality of the k8s config command during the smoke test.
# It would be excessive to deploy a cluster solely for this purpose.
result = instance.exec(
Expand Down
2 changes: 1 addition & 1 deletion tests/integration/tests/test_util/util.py
Original file line number Diff line number Diff line change
Expand Up @@ -139,7 +139,7 @@ def setup_k8s_snap(instance: harness.Instance, snap_path: Path):
def wait_until_k8s_ready(
control_node: harness.Instance,
instances: List[harness.Instance],
retries: int = 15,
retries: int = 30,
delay_s: int = 5,
):
"""
Expand Down

0 comments on commit fbc4fe2

Please sign in to comment.