Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[WIP] e2e tests #20

Open
wants to merge 15 commits into
base: main
Choose a base branch
from
87 changes: 87 additions & 0 deletions .github/workflows/tunnel-tests.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
# This workflow will build a golang project
# For more information see: https://docs.github.com/en/actions/automating-builds-and-tests/building-and-testing-go

name: Tunnel tests

on:
push:
branches: ["main"]
# paths:
# - "admin/**"
# - "tunnel/**"
pull_request:
branches: ["main"]
# paths:
# - "admin/**"
# - "tunnel/**"

permissions:
contents: read

jobs:
run-tunnel-tests:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v3

- name: Start the services
run: |
cp ./e2e/.env .env
docker-compose -f docker-compose.dev.yaml up -d
sleep 5

- name: Set up Python 3.12
uses: actions/setup-python@v3
with:
python-version: "3.12"
cache: "pip"
cache-dependency-path: e2e/requirements.txt

- name: Setup test server
working-directory: ./e2e
run: |
python -m pip install --upgrade pip
pip install -r requirements.txt

- name: Start test server
working-directory: ./e2e
run: |
uvicorn server:app --host 0.0.0.0 --port 9000 &
sleep 3

- name: Setup client
uses: pnpm/action-setup@v3
with:
version: 8.7.5
run_install: false

- name: Install client dependencies
run: pnpm --dir tunnel/internal/client/dashboard/ui install

- name: Build client
run: pnpm --dir tunnel/internal/client/dashboard/ui build

- name: Build portr binary
working-directory: ./tunnel
run: |
go build -ldflags="-s -w" -o ../e2e/portr cmd/portr/*.go

- name: Setup test data
working-directory: ./e2e
run: |
make load-pg-data

- name: Start the tunnel
working-directory: ./e2e
run: |
./portr --config client.e2e.yaml http 9000 --subdomain test-tunnel-server &
sleep 3

- name: Add hosts to /etc/hosts
run: |
sudo echo "127.0.0.1 test-tunnel-server.localhost" | sudo tee -a /etc/hosts

- name: Run tests
working-directory: ./e2e
run: |
pytest
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -11,4 +11,5 @@ node_modules
.mypy_cache
data/
dist
!tunnel/internal/client/dashboard/dist
!tunnel/internal/client/dashboard/dist
!e2e/.env
28 changes: 22 additions & 6 deletions docker-compose.dev.yaml
Original file line number Diff line number Diff line change
@@ -1,35 +1,51 @@
services:
admin:
container_name: portr_admin
build:
context: admin
ports:
- 8000:8000
depends_on:
- postgres
postgres:
condition: service_healthy
env_file: .env
healthcheck:
test: ["CMD", "curl", "-f", "http://localhost:8000/api/v1/healthcheck"]
interval: 10s
timeout: 5s
retries: 5

tunnel:
container_name: portr_tunnel
build:
context: tunnel
command: ["start"]
ports:
- 2222:2222
- 8001:8001
depends_on:
- admin
- postgres
admin:
condition: service_healthy
postgres:
condition: service_healthy
env_file: .env

postgres:
container_name: portr_postgres
image: postgres:16.2
environment:
POSTGRES_USER: postgres
POSTGRES_PASSWORD: postgres
POSTGRES_DB: postgres
POSTGRES_USER: $POSTGRES_USER
POSTGRES_PASSWORD: $POSTGRES_PASSWORD
POSTGRES_DB: $POSTGRES_DB
ports:
- 5432:5432
volumes:
- postgres_data:/var/lib/postgresql/data
healthcheck:
test: ["CMD-SHELL", "pg_isready -U $POSTGRES_USER"]
interval: 10s
timeout: 5s
retries: 5

volumes:
postgres_data: {}
11 changes: 11 additions & 0 deletions e2e/.env
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
PORTR_DB_URL="postgres://postgres:postgres@postgres:5432/postgres"
PORTR_DOMAIN=localhost:8000
PORTR_ADMIN_GITHUB_CLIENT_ID=""
PORTR_ADMIN_GITHUB_CLIENT_SECRET=""
PORTR_SERVER_URL=localhost:8000
PORTR_SSH_URL=localhost:2222
PORTR_TUNNEL_USE_LOCALHOST=true
PORTR_ADMIN_ENCRYPTION_KEY="mj-qoeMhLQp_cHnMU9nsLfCMnNkZ6XBcFefy4VxzOe8="
POSTGRES_USER=postgres
POSTGRES_PASSWORD=postgres
POSTGRES_DB=postgres
10 changes: 10 additions & 0 deletions e2e/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
# python generated files
__pycache__/
*.py[oc]
build/
dist/
wheels/
*.egg-info

# venv
.venv
1 change: 1 addition & 0 deletions e2e/.python-version
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
3.12.1
2 changes: 2 additions & 0 deletions e2e/Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
load-pg-data:
docker exec portr_postgres psql -U postgres postgres < portr_e2e.sql
12 changes: 12 additions & 0 deletions e2e/client.e2e.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
server_url: localhost:8000
ssh_url: localhost:2222
tunnel_url: localhost:8001
secret_key: portr_gpb2sW2uWmN3TASvzI6PzAmuqem0VrKwc9o7
use_localhost: true
tunnels:
- name: portr
subdomain: portr
port: 4321
- name: e2e
subdomain: e2e
port: 8000
132 changes: 132 additions & 0 deletions e2e/portr_e2e.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,132 @@
--
-- PostgreSQL database dump
--

-- Dumped from database version 16.2 (Debian 16.2-1.pgdg120+2)
-- Dumped by pg_dump version 16.2 (Debian 16.2-1.pgdg120+2)

SET statement_timeout = 0;
SET lock_timeout = 0;
SET idle_in_transaction_session_timeout = 0;
SET client_encoding = 'UTF8';
SET standard_conforming_strings = on;
SELECT pg_catalog.set_config('search_path', '', false);
SET check_function_bodies = false;
SET xmloption = content;
SET client_min_messages = warning;
SET row_security = off;

--
-- Data for Name: team; Type: TABLE DATA; Schema: public; Owner: postgres
--

COPY public.team (id, created_at, updated_at, name, slug) FROM stdin;
1 2024-03-30 14:13:32.763932+00 2024-03-30 14:13:32.763961+00 portr portr
\.


--
-- Data for Name: user; Type: TABLE DATA; Schema: public; Owner: postgres
--

COPY public."user" (id, created_at, updated_at, email, first_name, last_name, is_superuser) FROM stdin;
1 2024-03-30 14:13:28.931274+00 2024-03-30 14:13:28.931326+00 [email protected] \N \N t
\.


--
-- Data for Name: team_users; Type: TABLE DATA; Schema: public; Owner: postgres
--

COPY public.team_users (id, created_at, updated_at, secret_key, role, team_id, user_id) FROM stdin;
1 2024-03-30 14:13:32.766736+00 2024-03-30 14:13:32.766754+00 portr_gpb2sW2uWmN3TASvzI6PzAmuqem0VrKwc9o7 admin 1 1
\.


--
-- Data for Name: connection; Type: TABLE DATA; Schema: public; Owner: postgres
--

COPY public.connection (created_at, updated_at, id, type, subdomain, port, status, started_at, closed_at, created_by_id, team_id) FROM stdin;
\.


--
-- Data for Name: githubuser; Type: TABLE DATA; Schema: public; Owner: postgres
--

COPY public.githubuser (id, github_id, github_access_token, github_avatar_url, user_id) FROM stdin;
1 18011385 ghu_uverybigrandomstringxB31LvIa https://avatars.githubusercontent.com/u/18011385?v=4 1
\.


--
-- Data for Name: instancesettings; Type: TABLE DATA; Schema: public; Owner: postgres
--

COPY public.instancesettings (id, created_at, updated_at, smtp_enabled, smtp_host, smtp_port, smtp_username, smtp_password, from_address, add_user_email_subject, add_user_email_body, updated_by_id) FROM stdin;
1 2024-03-30 14:12:43.843191+00 2024-03-30 14:12:43.843204+00 f \N \N \N \N \N You've been added to team {{teamName}} on Portr! Hello {{email}}\n\nYou've been added to team "{{teamName}}" on Portr.\n\nGet started by signing in with your github account at {{dashboardUrl}} \N
\.


--
-- Data for Name: session; Type: TABLE DATA; Schema: public; Owner: postgres
--

COPY public.session (id, created_at, updated_at, token, expires_at, user_id) FROM stdin;
1 2024-03-30 14:13:28.946702+00 2024-03-30 14:13:28.946732+00 XV6ZpcwMnvxuYEkdOTS0mbJIYDyy6Bx6 2024-04-06 14:13:28.945745+00 1
\.


--
-- Name: aerich_id_seq; Type: SEQUENCE SET; Schema: public; Owner: postgres
--

SELECT pg_catalog.setval('public.aerich_id_seq', 1, true);


--
-- Name: githubuser_id_seq; Type: SEQUENCE SET; Schema: public; Owner: postgres
--

SELECT pg_catalog.setval('public.githubuser_id_seq', 1, true);


--
-- Name: instancesettings_id_seq; Type: SEQUENCE SET; Schema: public; Owner: postgres
--

SELECT pg_catalog.setval('public.instancesettings_id_seq', 1, true);


--
-- Name: session_id_seq; Type: SEQUENCE SET; Schema: public; Owner: postgres
--

SELECT pg_catalog.setval('public.session_id_seq', 1, true);


--
-- Name: team_id_seq; Type: SEQUENCE SET; Schema: public; Owner: postgres
--

SELECT pg_catalog.setval('public.team_id_seq', 1, true);


--
-- Name: team_users_id_seq; Type: SEQUENCE SET; Schema: public; Owner: postgres
--

SELECT pg_catalog.setval('public.team_users_id_seq', 1, true);


--
-- Name: user_id_seq; Type: SEQUENCE SET; Schema: public; Owner: postgres
--

SELECT pg_catalog.setval('public.user_id_seq', 1, true);


--
-- PostgreSQL database dump complete
--
4 changes: 4 additions & 0 deletions e2e/requirements.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
fastapi==0.110.0
pytest==8.1.1
uvicorn==0.29.0
requests==2.31.0
13 changes: 13 additions & 0 deletions e2e/server.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
from fastapi import FastAPI
import uvicorn

app = FastAPI()


@app.get("/")
def read_root():
return {"Hello": "World"}


if __name__ == "__main__":
uvicorn.run(app, host="0.0.0.0", port=9000)
7 changes: 7 additions & 0 deletions e2e/test_server.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
import requests # type: ignore


def test_json_api():
response = requests.get("http://test-tunnel-server.localhost:8001")
assert response.status_code == 200
assert response.json() == {"Hello": "World"}
Loading