Skip to content

Commit 1a87bd3

Browse files
committed
Add test scripts, CI workflow, and Docker Compose test configuration
1 parent 4f2717c commit 1a87bd3

File tree

5 files changed

+256
-9
lines changed

5 files changed

+256
-9
lines changed

.github/workflows/test.yml

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
name: GenAI App Tests
2+
3+
on:
4+
push:
5+
branches: [ main ]
6+
pull_request:
7+
branches: [ main ]
8+
9+
jobs:
10+
integration-tests:
11+
runs-on: ubuntu-latest
12+
13+
steps:
14+
- uses: actions/checkout@v3
15+
16+
- name: Set up Go
17+
uses: actions/setup-go@v4
18+
with:
19+
go-version: '1.20'
20+
21+
- name: Install dependencies
22+
run: |
23+
cd tests
24+
go mod download
25+
go run github.com/playwright-community/playwright-go/cmd/playwright install --with-deps
26+
27+
- name: Run short tests
28+
run: |
29+
cd tests
30+
go test -v ./integration -short
31+
32+
- name: Run API tests
33+
if: success() || failure() # Run even if previous step failed
34+
run: |
35+
cd tests
36+
go test -v ./integration -run TestGenAIAppIntegration
37+
38+
- name: Run quality tests
39+
if: success() || failure() # Run even if previous step failed
40+
run: |
41+
cd tests
42+
go test -v ./integration -run TestGenAIQuality
43+
44+
- name: Clean up
45+
if: always() # Always run cleanup
46+
run: |
47+
docker network prune -f --filter "name=genai-*"
48+
docker container prune -f --filter "name=test-*"

tests/README.md

Lines changed: 27 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,28 @@ go test -v ./tests/integration -run TestGenAIAppIntegration
5252
go test -v ./tests/integration -short
5353
```
5454

55+
Alternatively, you can use the provided Makefile:
56+
57+
```bash
58+
# Setup dependencies
59+
make -C tests setup
60+
61+
# Run all tests
62+
make -C tests test
63+
64+
# Run specific test categories
65+
make -C tests test-api
66+
make -C tests test-frontend
67+
make -C tests test-performance
68+
make -C tests test-quality
69+
70+
# Run tests in short mode
71+
make -C tests test-short
72+
73+
# Clean up test artifacts
74+
make -C tests clean
75+
```
76+
5577
## Test Environment
5678

5779
The tests create isolated Docker containers for:
@@ -62,18 +84,14 @@ The tests create isolated Docker containers for:
6284

6385
All containers are connected via a dedicated Docker network to ensure proper communication.
6486

65-
## Customizing Tests
87+
## Test Reports
88+
89+
Test results are displayed in the console. Screenshots for UI tests are saved to the system's temporary directory for visual inspection when failures occur.
90+
91+
## Extending the Tests
6692

6793
To add new test cases or modify existing ones:
6894

6995
- For API tests, add new test functions to `api_test.go`
7096
- For UI tests, add new test steps to the Playwright tests in `frontend_test.go`
7197
- For quality evaluation, add new test cases to the `testCases` array in `quality_test.go`
72-
73-
## Best Practices
74-
75-
1. Keep tests isolated - each test should run independently
76-
2. Use descriptive test names that indicate what's being tested
77-
3. Include both positive and negative test cases
78-
4. For UI tests, add screenshots to help debug failures
79-
5. For LLM quality tests, focus on pattern matching rather than exact text comparison

tests/docker-compose.test.yml

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
version: '3.8'
2+
3+
services:
4+
ollama:
5+
image: ilopezluna/llama3.2:0.5.4-1b
6+
ports:
7+
- "11434:11434"
8+
healthcheck:
9+
test:
10+
[
11+
'CMD-SHELL',
12+
'bash',
13+
'-c',
14+
"{ printf >&3 'GET / HTTP/1.0\\r\\n\\r\\n'; cat <&3; } 3<>/dev/tcp/localhost/11434 | grep 'Ollama is' || exit 1",
15+
]
16+
interval: 10s
17+
timeout: 5s
18+
retries: 3
19+
start_period: 10s
20+
networks:
21+
- test-network
22+
23+
backend:
24+
build:
25+
context: ..
26+
target: backend
27+
environment:
28+
- BASE_URL=http://ollama:11434/engines/llama.cpp/v1/
29+
- MODEL=ignaciolopezluna020/llama3.2:1b
30+
- API_KEY=ollama
31+
ports:
32+
- "8080:8080"
33+
depends_on:
34+
ollama:
35+
condition: service_healthy
36+
networks:
37+
- test-network
38+
healthcheck:
39+
test: ['CMD', 'wget', '-qO-', 'http://localhost:8080/health']
40+
interval: 3s
41+
timeout: 3s
42+
retries: 3
43+
44+
frontend:
45+
build:
46+
context: ../frontend
47+
ports:
48+
- "3000:3000"
49+
environment:
50+
- REACT_APP_API_URL=http://backend:8080
51+
- VITE_API_URL=http://backend:8080
52+
depends_on:
53+
backend:
54+
condition: service_healthy
55+
networks:
56+
- test-network
57+
58+
networks:
59+
test-network:
60+
driver: bridge
Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
package integration
2+
3+
import (
4+
"fmt"
5+
"os"
6+
"os/exec"
7+
"testing"
8+
"time"
9+
)
10+
11+
// TestWithDockerCompose runs tests using docker-compose instead of testcontainers
12+
// This is useful for CI environments or when you want to debug the tests
13+
func TestWithDockerCompose(t *testing.T) {
14+
if os.Getenv("USE_DOCKER_COMPOSE") != "true" {
15+
t.Skip("Skipping docker-compose tests. Set USE_DOCKER_COMPOSE=true to run these tests.")
16+
}
17+
18+
// Start the environment with docker-compose
19+
cmd := exec.Command("docker-compose", "-f", "../docker-compose.test.yml", "up", "-d")
20+
cmd.Stdout = os.Stdout
21+
cmd.Stderr = os.Stderr
22+
23+
if err := cmd.Run(); err != nil {
24+
t.Fatalf("Failed to start docker-compose: %s", err)
25+
}
26+
defer func() {
27+
// Clean up after tests
28+
cmd := exec.Command("docker-compose", "-f", "../docker-compose.test.yml", "down")
29+
cmd.Stdout = os.Stdout
30+
cmd.Stderr = os.Stderr
31+
cmd.Run()
32+
}()
33+
34+
// Wait for services to be ready
35+
t.Log("Waiting for services to be ready...")
36+
time.Sleep(30 * time.Second)
37+
38+
// Define test URLs
39+
backendURL := "http://localhost:8080"
40+
frontendURL := "http://localhost:3000"
41+
42+
// Run tests
43+
t.Run("API_Health", func(t *testing.T) {
44+
testHealthEndpoint(t, backendURL)
45+
})
46+
47+
t.Run("API_Chat", func(t *testing.T) {
48+
testChatEndpoint(t, backendURL)
49+
})
50+
51+
t.Run("API_Streaming", func(t *testing.T) {
52+
testStreamingResponse(t, backendURL)
53+
})
54+
55+
// Uncomment to run browser tests if needed
56+
// t.Run("UI_Tests", func(t *testing.T) {
57+
// runPlaywrightTests(t, frontendURL)
58+
// })
59+
60+
t.Log("Tests completed successfully!")
61+
}

tests/scripts/run_tests.sh

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
#!/bin/bash
2+
3+
# Script to run integration tests
4+
5+
set -e
6+
7+
cd "$(dirname "$0")/../"
8+
9+
# Ensure dependencies are installed
10+
go mod tidy
11+
echo "Installing Playwright dependencies..."
12+
go run github.com/playwright-community/playwright-go/cmd/playwright install --with-deps
13+
14+
# Parse arguments
15+
RUN_MODE="all"
16+
if [ "$1" == "short" ]; then
17+
RUN_MODE="short"
18+
elif [ "$1" == "api" ]; then
19+
RUN_MODE="api"
20+
elif [ "$1" == "frontend" ]; then
21+
RUN_MODE="frontend"
22+
elif [ "$1" == "quality" ]; then
23+
RUN_MODE="quality"
24+
elif [ "$1" == "performance" ]; then
25+
RUN_MODE="performance"
26+
elif [ "$1" == "compose" ]; then
27+
RUN_MODE="compose"
28+
fi
29+
30+
# Run tests based on mode
31+
if [ "$RUN_MODE" == "short" ]; then
32+
echo "Running tests in short mode..."
33+
go test -v ./integration -short
34+
elif [ "$RUN_MODE" == "api" ]; then
35+
echo "Running API tests..."
36+
go test -v ./integration -run TestGenAIAppIntegration
37+
elif [ "$RUN_MODE" == "frontend" ]; then
38+
echo "Running frontend tests..."
39+
go test -v ./integration -run TestFrontendIntegration
40+
elif [ "$RUN_MODE" == "quality" ]; then
41+
echo "Running quality tests..."
42+
go test -v ./integration -run TestGenAIQuality
43+
elif [ "$RUN_MODE" == "performance" ]; then
44+
echo "Running performance tests..."
45+
go test -v ./integration -run TestGenAIPerformance
46+
elif [ "$RUN_MODE" == "compose" ]; then
47+
echo "Running tests using docker-compose..."
48+
export USE_DOCKER_COMPOSE=true
49+
go test -v ./integration -run TestWithDockerCompose
50+
else
51+
echo "Running all tests..."
52+
go test -v ./integration
53+
fi
54+
55+
# Clean up
56+
echo "Cleaning up test resources..."
57+
docker network prune -f --filter "name=genai-*" || true
58+
docker container prune -f --filter "name=test-*" || true
59+
60+
echo "Tests completed!"

0 commit comments

Comments
 (0)