add test suite

This commit is contained in:
Anonymous 2024-06-09 20:59:00 +08:00
parent 2c84cffa55
commit 19c63bba60
5 changed files with 80 additions and 0 deletions

View File

@ -29,4 +29,5 @@ services:
restart: unless-stopped
networks:
app:
name: lightmirrors_app
driver: bridge

7
test/Dockerfile Normal file
View File

@ -0,0 +1,7 @@
FROM alpine:3
RUN apk update && apk add python3 py3-pip docker-cli
WORKDIR /app
CMD tail -f /dev/null

25
test/docker-compose.yml Normal file
View File

@ -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

20
test/mirrors_test.py Normal file
View File

@ -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")

27
test/utils.py Normal file
View File

@ -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