Skip to content

Commit

Permalink
test case add for kubectl container
Browse files Browse the repository at this point in the history
  • Loading branch information
rcmadhankumar committed Dec 19, 2024
1 parent ca8c2d0 commit 0213351
Show file tree
Hide file tree
Showing 4 changed files with 80 additions and 1 deletion.
9 changes: 9 additions & 0 deletions .github/workflows/ci.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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:
Expand Down
19 changes: 19 additions & 0 deletions bci_tester/data.py
Original file line number Diff line number Diff line change
Expand Up @@ -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}",
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -1113,6 +1131,7 @@ def create_BCI(
+ RUBY_CONTAINERS
+ RUST_CONTAINERS
+ SPACK_CONTAINERS
+ KUBECTL_CONTAINERS
)

ACC_CONTAINERS = POSTGRESQL_CONTAINERS
Expand Down
51 changes: 51 additions & 0 deletions tests/test_kubectl.py
Original file line number Diff line number Diff line change
@@ -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]
2 changes: 1 addition & 1 deletion tox.ini
Original file line number Diff line number Diff line change
@@ -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]
Expand Down

0 comments on commit 0213351

Please sign in to comment.