Browse Source

simple e2e test

tags/0.1
Henning Jacobs 4 months ago
parent
commit
0eaa62fe8d
5 changed files with 167 additions and 4 deletions
  1. +1
    -0
      .gitignore
  2. +2
    -2
      Makefile
  3. +83
    -0
      tests/e2e/conftest.py
  4. +71
    -0
      tests/e2e/deployment.yaml
  5. +10
    -2
      tests/e2e/test_view.py

+ 1
- 0
.gitignore View File

@@ -4,3 +4,4 @@ dist/
kind
kubectl
docs/_build/
.kube

+ 2
- 2
Makefile View File

@@ -30,8 +30,8 @@ test.unit:
poetry run coverage report

.PHONY: test.e2e
test.e2e: kind kubectl
env TEST_KIND=./kind TEST_KUBECTL=./kubectl TEST_OPERATOR_IMAGE=$(OPERATOR_IMAGE) \
test.e2e: kind kubectl docker
env TEST_KIND=./kind TEST_KUBECTL=./kubectl TEST_IMAGE=$(IMAGE):$(TAG) \
poetry run pytest -v -r=a \
--log-cli-level info \
--log-cli-format '%(asctime)s %(levelname)s %(message)s' \

+ 83
- 0
tests/e2e/conftest.py View File

@@ -0,0 +1,83 @@
import pykube
import yaml
import threading
from tempfile import NamedTemporaryFile
import requests
import logging
from pytest import fixture
from pathlib import Path
import os

from subprocess import check_output, run


@fixture(scope="class")
def cluster() -> dict:
kind = "./kind"
cluster_name = "kube-web-view-e2e"

cluster_exists = False
out = check_output([kind, "get", "clusters"], encoding="utf-8")
for name in out.splitlines():
if name == cluster_name:
cluster_exists = True

if not cluster_exists:
logging.info(f"Creating cluster {cluster_name!r} (usually ~1.5m) ...")
run(
[kind, "create", "cluster", "--name", cluster_name, "--wait", "2m"],
check=True,
)

kubeconfig = check_output(
[kind, "get", "kubeconfig-path", "--name", cluster_name], encoding="utf-8"
).strip()

def kubectl(*args: str, **kwargs):
return run(
["./kubectl", *args], check=True, env={"KUBECONFIG": kubeconfig}, **kwargs
)

docker_image = os.getenv("TEST_IMAGE")
logging.info("Loading Docker image in cluster (usually ~5s) ...")
run(
[kind, "load", "docker-image", "--name", cluster_name, docker_image], check=True
)

logging.info("Deploying kube-web-view ...")
deployment_manifests_path = Path(__file__).parent / "deployment.yaml"

with NamedTemporaryFile(mode="w+") as tmp:
with deployment_manifests_path.open() as f:
resources = list(yaml.safe_load_all(f))
dep = resources[-1]
assert (
dep["kind"] == "Deployment" and dep["metadata"]["name"] == "kube-web-view"
)
dep["spec"]["template"]["spec"]["containers"][0]["image"] = docker_image
yaml.dump_all(documents=resources, stream=tmp)
kubectl("apply", "-f", tmp.name)

logging.info("Waiting for rollout ...")
kubectl("rollout", "status", "deployment/kube-web-view")

def proxy():
kubectl("proxy", "--port=8011")

proxy_url = "http://localhost:8011/"
threading.Thread(target=proxy, daemon=True).start()
logging.info(f"Waiting for proxy {proxy_url} ...")
while True:
try:
requests.get(proxy_url)
except:
pass
else:
break

return {"proxy_url": proxy_url}


@fixture(scope="class")
def populated_cluster(cluster):
return cluster

+ 71
- 0
tests/e2e/deployment.yaml View File

@@ -0,0 +1,71 @@
---
apiVersion: v1
kind: ServiceAccount
metadata:
namespace: "default"
name: kube-web-view-account
---
apiVersion: rbac.authorization.k8s.io/v1beta1
kind: ClusterRole
metadata:
name: kube-web-view-cluster-role
rules:

- apiGroups:
- '*'
resources:
- '*'
verbs: [list, get]
- nonResourceURLs:
- '*'
verbs: [list, get]

---
apiVersion: rbac.authorization.k8s.io/v1beta1
kind: ClusterRoleBinding
metadata:
name: kube-web-view-rolebinding-cluster
roleRef:
apiGroup: rbac.authorization.k8s.io
kind: ClusterRole
name: kube-web-view-cluster-role
subjects:
- kind: ServiceAccount
name: kube-web-view-account
namespace: "default"
---
apiVersion: v1
kind: Service
metadata:
name: kube-web-view
spec:
selector:
application: kube-web-view
type: ClusterIP
ports:
- port: 80
protocol: TCP
targetPort: 8080
---
apiVersion: apps/v1
kind: Deployment
metadata:
name: kube-web-view
labels:
application: kube-web-view
spec:
replicas: 1
selector:
matchLabels:
application: kube-web-view
template:
metadata:
labels:
application: kube-web-view
spec:
serviceAccountName: kube-web-view-account
containers:
- name: kube-web-view-container
image: TO_BE_REPLACED_BY_E2E_TEST
imagePullPolicy: IfNotPresent # For our E2E tests.
env: []

+ 10
- 2
tests/e2e/test_view.py View File

@@ -1,2 +1,10 @@
def test_view():
pass
import requests


def test_view(populated_cluster):
proxy_url = populated_cluster["proxy_url"]
response = requests.get(
f"{proxy_url}/api/v1/namespaces/default/services/kube-web-view/proxy/clusters/default/namespaces/default/deployments/kube-web-view"
)
response.raise_for_status()
assert "application=kube-web-view" in response.text

Loading…
Cancel
Save