From 19c63bba60ffb457a2a0376c54c678dd36431bf0 Mon Sep 17 00:00:00 2001 From: Anonymous <> Date: Sun, 9 Jun 2024 20:59:00 +0800 Subject: [PATCH] add test suite --- docker-compose.yml | 1 + test/Dockerfile | 7 +++++++ test/docker-compose.yml | 25 +++++++++++++++++++++++++ test/mirrors_test.py | 20 ++++++++++++++++++++ test/utils.py | 27 +++++++++++++++++++++++++++ 5 files changed, 80 insertions(+) create mode 100644 test/Dockerfile create mode 100644 test/docker-compose.yml create mode 100644 test/mirrors_test.py create mode 100644 test/utils.py diff --git a/docker-compose.yml b/docker-compose.yml index ce5f3b0..29f173f 100644 --- a/docker-compose.yml +++ b/docker-compose.yml @@ -29,4 +29,5 @@ services: restart: unless-stopped networks: app: + name: lightmirrors_app driver: bridge \ No newline at end of file diff --git a/test/Dockerfile b/test/Dockerfile new file mode 100644 index 0000000..f6b958f --- /dev/null +++ b/test/Dockerfile @@ -0,0 +1,7 @@ +FROM alpine:3 + +RUN apk update && apk add python3 py3-pip docker-cli + +WORKDIR /app + +CMD tail -f /dev/null \ No newline at end of file diff --git a/test/docker-compose.yml b/test/docker-compose.yml new file mode 100644 index 0000000..eb9ace6 --- /dev/null +++ b/test/docker-compose.yml @@ -0,0 +1,25 @@ +services: + test: + image: lightmirrors/test + build: + context: . + dockerfile: Dockerfile + volumes: + - .:/app + - /var/run/docker.sock:/var/run/docker.sock + networks: + - lightmirrors_app + external_links: + - lightmirrors:aria2.local.homeinfra.org + - lightmirrors:docker.local.homeinfra.org + - lightmirrors:pypi.local.homeinfra.org + - lightmirrors:torch.local.homeinfra.org + - lightmirrors:npm.local.homeinfra.org + - lightmirrors:ubuntu.local.homeinfra.org + - lightmirrors:debian.local.homeinfra.org + - lightmirrors:proxy.local.homeinfra.org + - lightmirrors:github.local.homeinfra.org + - lightmirrors:alpine.local.homeinfra.org +networks: + lightmirrors_app: + external: true \ No newline at end of file diff --git a/test/mirrors_test.py b/test/mirrors_test.py new file mode 100644 index 0000000..60ca621 --- /dev/null +++ b/test/mirrors_test.py @@ -0,0 +1,20 @@ +import unittest + +from utils import call + +PYPI_HOST = "pypi.local.homeinfra.org" +PYPI_INDEX = f"http://{PYPI_HOST}/simple" +TORCH_HOST = "torch.local.homeinfra.org" +TORCH_INDEX = f"http://{TORCH_HOST}/whl" + + +class TestPypi(unittest.TestCase): + + def test_pypi_http(self): + call(f"pip download -i {PYPI_INDEX} django --trusted-host {PYPI_HOST} --dest /tmp/pypi/") + + def test_torch_http(self): + call(f"pip download -i {TORCH_INDEX} tqdm --trusted-host {TORCH_HOST} --dest /tmp/torch/") + + def test_docker_pull(self): + call(f"docker pull docker.local.homeinfra.org/alpine:3.12") diff --git a/test/utils.py b/test/utils.py new file mode 100644 index 0000000..4edc728 --- /dev/null +++ b/test/utils.py @@ -0,0 +1,27 @@ +import os +import subprocess +from pathlib import Path + +test_dir = Path(__file__).parent +root_dir = Path(__file__).parent.parent + + +def call(cmd): + print(f">> {cmd}") + p = subprocess.Popen(cmd, shell=True, stdin=subprocess.PIPE, stdout=subprocess.PIPE, stderr=subprocess.PIPE) + stdout, stderr = p.communicate() + assert p.returncode == 0, f"Error: {stderr.decode()}" + print(">>", stdout.decode()) + return stdout.decode(), stderr.decode() + + +class SetupMirrors(): + def __enter__(self): + os.chdir(root_dir) + call("docker-compose up -d") + return self + + def __exit__(self, exc_type, exc_val, exc_tb): + call("docker-compose down") + os.chdir(test_dir) + return False