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鈥檒l occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update testing infrastructure #266

Open
wants to merge 6 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
109 changes: 106 additions & 3 deletions .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -17,18 +17,121 @@ jobs:
- macos-latest

steps:
- uses: actions/checkout@v3
- uses: actions/checkout@v4

- name: Setup (on MacOS)
if: runner.os == 'macOS'
run: |
# install and up
brew install redis
brew services start postgresql
brew services start redis

# wait for ready
until pg_isready -h localhost -p 5432 && redis-cli ping; do sleep 5; done

# setup db
createuser -s postgres
cd database
bash setup.sh

- name: Setup (on Windows)
if: runner.os == 'Windows'
run: |
# postgres
echo "unix_socket_directories = ''" >> "$PGDATA/postgresql.conf"
echo "port = 5432" >> "$PGDATA/postgresql.conf"
$PG_BIN = "C:\Program Files\PostgreSQL\14\bin"
$env:PATH = "$PG_BIN;$env:PATH"

# start and wait
pg_ctl start
$timeout = 120
$elapsedTime = 0
Write-Host "Waiting for PostgreSQL to start"
while ($elapsedTime -lt $timeout) {
$pgIsReadyResult = & $PG_BIN\pg_isready.exe -h localhost -p 5432 -t 1
if ($pgIsReadyResult -eq "localhost:5432 - accepting connections") {
Write-Host "PostgreSQL is ready"
break
}
Start-Sleep -Seconds 5
$elapsedTime += 5
}

# setup db
cd database
bash setup.sh

# redis
$URL = "https://github.com/redis-windows/redis-windows/releases/download/7.0.14/Redis-7.0.14-Windows-x64-with-Service.tar.gz"
$outputFolder = "C:\redis"

if (-not (Test-Path $outputFolder)) {
New-Item -ItemType Directory -Path $outputFolder | Out-Null
}

Invoke-WebRequest -Uri $URL -OutFile "redis.tar.gz"
tar -xf "redis.tar.gz" -C $outputFolder
$innerFolder = Get-ChildItem -Path $outputFolder | Select-Object -First 1
Move-Item -Path "$outputFolder\$($innerFolder.Name)\*" -Destination $outputFolder -Force
Remove-Item -Path "$outputFolder\$($innerFolder.Name)" -Force

# start redis
cd $outputFolder
sc.exe create Redis binpath=C:\redis\RedisService.exe start= auto
net start Redis

# wait for redis
Write-Host "Waiting for Redis to start"
while ($elapsedTime -lt $timeout) {
$redisPingResult = & C:\redis\redis-cli.exe ping
if ($redisPingResult -eq "PONG") {
Write-Host "Redis is ready"
break
}
Start-Sleep -Seconds 5
$elapsedTime += 5
}

if ($elapsedTime -ge $timeout) {
Write-Host "Timeout waiting for services to start."
exit 1
}

Write-Host "Services are ready"

- name: Setup (on Linux)
if: runner.os == 'Linux'
run: |
docker-compose -f test-docker-compose.yml up -d pg-example redis-example
while [[ "$(docker inspect --format='{{.State.Health.Status}}' pg-example)" != "healthy" ]]; do
echo "Waiting for PostgreSQL container to become healthy..."
sleep 5
done
while [[ "$(docker inspect --format='{{.State.Health.Status}}' redis-example)" != "healthy" ]]; do
echo "Waiting for Redis container to become healthy..."
sleep 5
done

- name: Use Node.js ${{ matrix.node }}
uses: actions/setup-node@v3
uses: actions/setup-node@v4
with:
node-version: ${{ matrix.node }}
- uses: actions/cache@v3

- uses: actions/cache@v4
with:
path: ~/.npm
key: ${{ runner.os }}-node-${{ hashFiles('**/package-lock.json') }}
restore-keys: |
${{ runner.os }}-node-

- run: npm ci

- run: npm run dotest
env:
MODE: test

- name: Stop containers for Linux
if: runner.os == 'Linux'
run: docker-compose down
1 change: 1 addition & 0 deletions .prettierignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
application/tasks/
package-lock.json
package.json
test-docker-compose.yml
2 changes: 1 addition & 1 deletion Dockerfile
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
FROM node:16-alpine
FROM node:20-alpine
WORKDIR /usr/server
COPY . .
RUN npm ci --only=production
Expand Down
9 changes: 9 additions & 0 deletions application/config/redis.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
({
apiRedis: {
socket: {
host: process.env.REDIS_HOST ?? '127.0.0.1',
port: process.env.REDIS_PORT ?? 6379,
},
password: process.env.PASS,
},
});
4 changes: 2 additions & 2 deletions application/db/redis/start.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,13 @@ async () => {
if (application.worker.id === 'W1') {
console.debug('Connect to redis');
}
const client = npm.redis.createClient();
const client = npm.redis.createClient(config.redis.apiRedis);
db.redis.client = client;
client.on('error', () => {
if (application.worker.id === 'W1') {
console.warn('No redis service detected, so quit client');
}
client.quit();
});
//await client.connect();
await client.connect();
};
12 changes: 11 additions & 1 deletion docker-compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ services:
container_name: api-example
environment:
- DB_HOST=pg-example
- REDIS_HOST=redis-example
volumes:
- ./application:/usr/server/application
depends_on:
Expand All @@ -19,7 +20,7 @@ services:
restart: always

pg-example:
image: postgres:14.1-alpine3.15
image: postgres:16-alpine
container_name: pg-example
environment:
- POSTGRES_USER=marcus
Expand All @@ -33,6 +34,15 @@ services:
- 127.0.0.1:5432:5432
restart: always

redis-example:
image: redis:alpine
container_name: redis-example
volumes:
- ./data/redis-example:/data
ports:
- 127.0.0.1:6379:6379
restart: always

networks:
default:
name: api-example-network
37 changes: 37 additions & 0 deletions test-docker-compose.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
version: '3'

services:
pg-example:
image: postgres:16-alpine
container_name: pg-example
environment:
- POSTGRES_USER=marcus
- POSTGRES_PASSWORD=marcus
- POSTGRES_DB=application
volumes:
- ./database/structure.sql:/docker-entrypoint-initdb.d/1.sql
- ./database/data.sql:/docker-entrypoint-initdb.d/2.sql
ports:
- 127.0.0.1:5432:5432
restart: always
healthcheck:
test: ["CMD", "pg_isready", "-q", "-h", "localhost", "-p", "5432", "-U", "marcus"]
interval: 10s
timeout: 5s
retries: 3

redis-example:
image: redis:alpine
container_name: redis-example
ports:
- 127.0.0.1:6379:6379
restart: always
healthcheck:
test: ["CMD", "redis-cli", "ping"]
interval: 10s
timeout: 5s
retries: 3

networks:
default:
name: api-example-network