From 12735c29ccdc7723cd6f0032b1e957bd4d49c204 Mon Sep 17 00:00:00 2001 From: Konstanty Karagiorgis Date: Wed, 5 Jun 2024 15:02:09 +0200 Subject: [PATCH] #28: Removed leftovers of the on-host integration tests --- kubernetes/raw/kind/nps.yaml | 94 ----------------- tests/conftest.py | 45 +------- tests/fixtures.py | 196 +---------------------------------- tests/setup.py | 1 - 4 files changed, 2 insertions(+), 334 deletions(-) delete mode 100644 kubernetes/raw/kind/nps.yaml diff --git a/kubernetes/raw/kind/nps.yaml b/kubernetes/raw/kind/nps.yaml deleted file mode 100644 index a7cd81c..0000000 --- a/kubernetes/raw/kind/nps.yaml +++ /dev/null @@ -1,94 +0,0 @@ -# At some point the support for that should be bundled in KIND, for now copied from this PR: -# https://github.com/kubernetes-sigs/kind/pull/3611/files ---- -kind: ClusterRole -apiVersion: rbac.authorization.k8s.io/v1 -metadata: - name: kube-network-policies -rules: - - apiGroups: - - "" - resources: - - pods - - namespaces - verbs: - - list - - watch - - apiGroups: - - "networking.k8s.io" - resources: - - networkpolicies - verbs: - - list - - watch ---- -kind: ClusterRoleBinding -apiVersion: rbac.authorization.k8s.io/v1 -metadata: - name: kube-network-policies -roleRef: - apiGroup: rbac.authorization.k8s.io - kind: ClusterRole - name: kube-network-policies -subjects: -- kind: ServiceAccount - name: kube-network-policies - namespace: kube-system ---- -apiVersion: v1 -kind: ServiceAccount -metadata: - name: kube-network-policies - namespace: kube-system ---- -apiVersion: apps/v1 -kind: DaemonSet -metadata: - name: kube-network-policies - namespace: kube-system - labels: - tier: node - app: kube-network-policies - k8s-app: kube-network-policies -spec: - selector: - matchLabels: - app: kube-network-policies - template: - metadata: - labels: - tier: node - app: kube-network-policies - k8s-app: kube-network-policies - spec: - hostNetwork: true - dnsPolicy: ClusterFirst - nodeSelector: - kubernetes.io/os: linux - tolerations: - - operator: Exists - effect: NoSchedule - serviceAccountName: kube-network-policies - containers: - - name: kube-network-policies - image: registry.k8s.io/networking/kube-network-policies:v0.2.0 - args: - - /bin/netpol - - -v - - "2" - volumeMounts: - - name: lib-modules - mountPath: /lib/modules - readOnly: true - resources: - requests: - cpu: "100m" - memory: "50Mi" - securityContext: - privileged: true - capabilities: - add: ["NET_ADMIN"] - volumes: - - name: lib-modules - hostPath: - path: /lib/modules diff --git a/tests/conftest.py b/tests/conftest.py index 64a1823..e11299c 100644 --- a/tests/conftest.py +++ b/tests/conftest.py @@ -6,7 +6,7 @@ import pytest -from .fixtures import Client, Helm, K3dCluster, Kubectl, MockServer, MySQLServer, Server, Curl +from .fixtures import Helm, K3dCluster, Kubectl, MockServer, MySQLServer, Curl logger = logging.getLogger(__name__) @@ -34,49 +34,6 @@ def run_process(process, **kwargs): return rt -@pytest.fixture(scope="session") -def executable(): - tmp = None - try: - tmp = tempfile.NamedTemporaryFile(prefix="wormhole", delete=False) - run_process(["go", "build", "-o", tmp.name, "main.go"]) - yield tmp.name - finally: - os.unlink(tmp.name) - - -@pytest.fixture() -def client(executable, server, tmpdir): - c_subdir = tmpdir.mkdir("client") - c = Client( - executable, - server, - c_subdir.mkdir("state-manager"), - c_subdir.mkdir("nginx"), - c_subdir.mkdir("wireguard").join("wg0.conf"), - ) - try: - yield c.start() - finally: - c.stop() - - -@pytest.fixture() -def server(executable, tmpdir): - s_subdir = tmpdir.mkdir("server") - server = Server( - executable, - s_subdir.mkdir("state-manager"), - s_subdir.mkdir("nginx"), - s_subdir.mkdir("wireguard").join("wg0.conf"), - ) - try: - server.start() - yield server - finally: - server.stop() - - @pytest.fixture() def mysql(): mysql = MySQLServer() diff --git a/tests/fixtures.py b/tests/fixtures.py index bd847a8..63431c8 100644 --- a/tests/fixtures.py +++ b/tests/fixtures.py @@ -1,15 +1,10 @@ import json import os import shutil -import signal import subprocess -import socket -import uuid from contextlib import contextmanager -import tempfile import psutil -import pymysql import requests from retry import retry @@ -19,172 +14,6 @@ def run_process(command, *args, **kwargs): return subprocess.run(command, *args, **kwargs) -class Server: - def __init__( - self, - executable, - state_manager_path="/tmp/server-state-manager", - nginx_confd_path="/tmp/server-nginx-confd", - wireguard_config_path="/tmp/server-wireguard/wg0.conf", - wireguard_address="0.0.0.0", - wireguard_subnet="24", - metrics_port=8090, - ): - self.executable = executable - self.state_manager_path = state_manager_path - self.nginx_confd_path = nginx_confd_path - self.wireguard_config_path = wireguard_config_path - self.metrics_port = metrics_port - self.wireguard_address = wireguard_address - self.wireguard_subnet = wireguard_subnet - self.process = None - - def start(self): - cmd = [ - self.executable, - "--debug", - "--metrics", - "--metrics-port", - str(self.metrics_port), - "server", - "--name", - uuid.uuid4().hex, - "--directory-state-manager-path", - self.state_manager_path, - "--nginx-confd-path", - self.nginx_confd_path, - "--wg-config", - self.wireguard_config_path, - "--wg-public-host", - self.wireguard_address, - "--wg-internal-host", - self.wireguard_address, - "--wg-subnet-mask", - self.wireguard_subnet, - "--invite-token", - "123123", - ] - print(" ".join([str(i) for i in cmd])) - self.process = subprocess.Popen( - cmd, - shell=False, - ) - - @retry(delay=0.1, tries=50) - def _check_if_is_already_opened(): - with socket.socket(socket.AF_INET, socket.SOCK_STREAM) as s: - try: - s.connect(('localhost', self.metrics_port)) - return True - except (ConnectionRefusedError, OSError): - raise Exception("Port is not open yet") - - _check_if_is_already_opened() - - return self - - def stop(self): - return_code = self.process.poll() - if return_code is None: - return os.kill(self.process.pid, signal.SIGINT) - - def admin(self, path): - return ( - f'http://localhost:{self.admin_port}/{path if not path.startswith("/") else path[1:]}' - ) - - -class MySQLServer: - def __init__(self): - self.container_id = f"mysql-{uuid.uuid4().hex}" - self.host = "localhost" - self.port = 3306 - self.user = "root" - self.password = "123123" - - def start(self): - process = run_process( - [ - "docker", - "run", - "--rm", - "-d", - "--network=host", - "--name", - self.container_id, - "-e", - f"MYSQL_ROOT_PASSWORD={self.password}", - "mysql:latest", - ], - shell=False, - stdout=subprocess.PIPE, - stderr=subprocess.PIPE, - check=True, - ) - - self.container_id = process.stdout.decode().strip() - - @retry(delay=2, tries=120) - def _check_if_mysql_already_listens(): - pymysql.connect(host=self.host, user=self.user, password=self.password) - - _check_if_mysql_already_listens() - - def stop(self): - run_process( - ["docker", "rm", "-f", self.container_id], - shell=False, - stdout=subprocess.PIPE, - stderr=subprocess.PIPE, - check=True, - ) - - -class Client: - def __init__( - self, - executable, - server, - state_manager_path="/tmp/client-state-manager", - nginx_confd_path="/tmp/client-nginx-confd", - wireguard_config_path="/tmp/client-wireguard/wg0.conf", - metrics_port=8091, - ): - self.executable = executable - self.server = server - self.state_manager_path = state_manager_path - self.nginx_confd_path = nginx_confd_path - self.wireguard_config_path = wireguard_config_path - self.metrics_port = metrics_port - self.process = None - - def start(self): - command = [ - self.executable, - "--metrics", - "--metrics-port", - str(self.metrics_port), - "client", - "--name", - uuid.uuid4().hex, - "--nginx-confd-path", - self.nginx_confd_path, - "--wg-config", - self.wireguard_config_path, - "--directory-state-manager-path", - self.state_manager_path, - "--invite-token", - "123123", - ] - self.process = subprocess.Popen(command, shell=False) - return self - - def stop(self): - return_code = self.process.poll() - if return_code is None: - return os.kill(self.process.pid, signal.SIGINT) - - class MockServer: def __init__(self, kubectl, wormhole_image): self.namespace = "nginx" @@ -240,29 +69,6 @@ def _call(self, pod, command, max_time_seconds=None): ) -@contextmanager -def launched_in_background(process): - try: - process.start() - yield process - finally: - process.stop() - - -def get_number_of_running_goroutines(port=8090): - return int( - [ - metrics - for metrics in requests.get(f"http://localhost:{port}/metrics").text.split("\n") - if metrics.strip().startswith("go_goroutines") - ][0].split(" ")[1] - ) - - -def get_number_of_opened_files(process_owner): - return len(psutil.Process(pid=process_owner.process.pid).open_files()) - - class Kubectl: def __init__(self, cluster): self.cluster = cluster @@ -479,7 +285,7 @@ class Services: @classmethod def count(cls, kubectl, namespace): return len(kubectl.json(["get", "svc", "-n", namespace])["items"]) - + @classmethod def names(cls, kubectl, namespace): return [item["metadata"]["name"] for item in kubectl.json(["get", "svc", "-n", namespace])["items"]] diff --git a/tests/setup.py b/tests/setup.py index 4a25f90..0daad20 100644 --- a/tests/setup.py +++ b/tests/setup.py @@ -13,7 +13,6 @@ "cryptography==36.0.2", "pytest==7.0.1", "psutil==5.9.0", - "PyMySQL==1.0.2", "requests==2.27.1", "retry==0.9.2", ],