diff --git a/.github/workflows/ci.yaml b/.github/workflows/ci.yaml index 63402317..9d5b7373 100644 --- a/.github/workflows/ci.yaml +++ b/.github/workflows/ci.yaml @@ -117,6 +117,8 @@ jobs: test-containers: name: tox runs-on: ubuntu-latest + env: + K3S_CLUSTER_NAME: "test-k3s" needs: gentestmatrix strategy: fail-fast: false @@ -400,6 +402,13 @@ jobs: echo -e "$(getent ahostsv4 $host | grep STREAM | cut -d' ' -f1 | head -n 1)\t$host" | sudo tee -a /etc/hosts done + - uses: AbsaOSS/k3d-action@v2 + name: "Create Single node k3d Cluster" + if: ${{ matrix.toxenv == 'kubectl' }} + with: + cluster-name: ${{ env.K3S_CLUSTER_NAME }} + args: --agents 1 + - name: Run the tests for docker run: python3 -m tox -e ${{ matrix.toxenv }} -- -n 3 --reruns 3 --durations=25 --durations-min=600.0 --pytest-container-log-level=debug env: diff --git a/bci_tester/data.py b/bci_tester/data.py index 54deca3b..a10c2abd 100755 --- a/bci_tester/data.py +++ b/bci_tester/data.py @@ -842,6 +842,23 @@ def create_BCI( forwarded_ports=[PortForwarding(container_port=80)], ) +_KUBECTL_VERSION_OS_MATRIX: Tuple[Tuple[str, Tuple[str, ...]], ...] = ( + ("1.28", ("15.6", "tumbleweed")), + ("1.30", ("15.7")), + ("1.29", ("tumbleweed")), + ("1.30", ("tumbleweed")), + ("1.31", ("tumbleweed")), +) + +KUBECTL_CONTAINERS = [ + create_BCI( + build_tag=f"{APP_CONTAINER_PREFIX}/:{kubectl_ver}", + bci_type=ImageType.APPLICATION, + available_versions=os_versions, + ) + for kubectl_ver, os_versions in _KUBECTL_VERSION_OS_MATRIX +] + if OS_VERSION in ("16.0",): KERNEL_MODULE_CONTAINER = create_BCI( build_tag=f"{BCI_CONTAINER_PREFIX}/bci-sle16-kernel-module-devel:{OS_CONTAINER_TAG}", @@ -1031,6 +1048,7 @@ def create_BCI( + RUST_CONTAINERS + SPACK_CONTAINERS + (DOTNET_CONTAINERS if LOCALHOST.system_info.arch == "x86_64" else []) + + KUBECTL_CONTAINERS ) #: all containers with zypper and with the flag to launch them as root @@ -1113,6 +1131,7 @@ def create_BCI( + RUBY_CONTAINERS + RUST_CONTAINERS + SPACK_CONTAINERS + + KUBECTL_CONTAINERS ) ACC_CONTAINERS = POSTGRESQL_CONTAINERS diff --git a/tests/test_kubectl.py b/tests/test_kubectl.py new file mode 100644 index 00000000..728b9efd --- /dev/null +++ b/tests/test_kubectl.py @@ -0,0 +1,51 @@ +import os + +import pytest +from pytest_container import DerivedContainer +from pytest_container.container import ContainerLauncher +from pytest_container.runtime import OciRuntimeBase + +from bci_tester.data import KUBECTL_CONTAINERS + + +@pytest.mark.parametrize("ctr_image", KUBECTL_CONTAINERS) +def test_kubectl( + host, + container_runtime: OciRuntimeBase, + pytestconfig: pytest.Config, + ctr_image: DerivedContainer, +) -> None: + engine = container_runtime.runner_binary + cluster_name = os.getenv("K3S_CLUSTER_NAME") + control_plane = "k3d-" + cluster_name + "-server-0" + host.run_expect([0], f"k3d kubeconfig get {cluster_name} > kubeconfig") + format_template = ( + "'{{range .NetworkSettings.Networks}}{{.IPAddress}}{{end}}'" + ) + kind_ip = host.check_output( + f"{engine} inspect -f {format_template} {control_plane}" + ) + host.run_expect( + [0], + rf'sed -i "s/server: https:\/\/.*:[0-9]*/server: https:\/\/{kind_ip}:6443/g" kubeconfig', + ) + + kubectl_ctr = DerivedContainer( + base=ctr_image, + # below base image was used for testing as we haven't published kubectl image yet + # base="registry.opensuse.org/home/defolos/bci/staging/tumbleweed/tumbleweed-2060/containerfile/opensuse/kubectl:latest", + containerfile="COPY kubeconfig /root/.kube/config", + custom_entry_point="/bin/bash", + extra_launch_args=["--network=" + "k3d-" + cluster_name], + ) + + with ContainerLauncher.from_pytestconfig( + kubectl_ctr, container_runtime, pytestconfig + ) as kubectl_launcher: + kubectl_launcher.launch_container() + kubectl_con = kubectl_launcher.container_data.connection + nodes = ( + kubectl_con.check_output("kubectl get nodes").strip().splitlines() + ) + assert len(nodes) == 2 + assert control_plane == nodes[1].split()[0] diff --git a/tox.ini b/tox.ini index 4e4adf9c..13fcf216 100644 --- a/tox.ini +++ b/tox.ini @@ -1,5 +1,5 @@ [tox] -envlist = {py36,py39,py310,py311,py312,py313}-unit, all, base, cosign, fips, init, dotnet, python, ruby, node, go, openjdk, openjdk_devel, rust, php, busybox, 389ds, metadata, minimal, multistage, repository, doc, lint, get_urls, pcp, distribution, postgres, git, helm, nginx, kernel_module, mariadb, tomcat, spack, gcc, prometheus, grafana, kiwi, postfix, ai +envlist = {py36,py39,py310,py311,py312,py313}-unit, all, base, cosign, fips, init, dotnet, python, ruby, node, go, openjdk, openjdk_devel, rust, php, busybox, 389ds, metadata, minimal, multistage, repository, doc, lint, get_urls, pcp, distribution, postgres, git, helm, nginx, kernel_module, mariadb, tomcat, spack, gcc, prometheus, grafana, kiwi, postfix, ai, kubectl skip_missing_interpreters = True [common]