Skip to content

Commit

Permalink
#28: Removed leftovers of the on-host integration tests
Browse files Browse the repository at this point in the history
  • Loading branch information
glothriel committed Jun 5, 2024
1 parent 9dd19e3 commit 12735c2
Show file tree
Hide file tree
Showing 4 changed files with 2 additions and 334 deletions.
94 changes: 0 additions & 94 deletions kubernetes/raw/kind/nps.yaml

This file was deleted.

45 changes: 1 addition & 44 deletions tests/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -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__)

Expand Down Expand Up @@ -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()
Expand Down
196 changes: 1 addition & 195 deletions tests/fixtures.py
Original file line number Diff line number Diff line change
@@ -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

Expand All @@ -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"
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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"]]
1 change: 0 additions & 1 deletion tests/setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -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",
],
Expand Down

0 comments on commit 12735c2

Please sign in to comment.