Skip to content

Commit

Permalink
Merge pull request #66 from my-opencode/alpha-actions
Browse files Browse the repository at this point in the history
Alpha actions: integration tests
  • Loading branch information
my-opencode authored Aug 16, 2024
2 parents 7581d70 + 223a7fb commit d9d3ff0
Show file tree
Hide file tree
Showing 7 changed files with 55 additions and 4 deletions.
37 changes: 37 additions & 0 deletions .github/workflows/test.integration.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
name: Integration Tests
run-name: ${{ github.actor }} is running integration tests

on: [push]

jobs:
Integration-Tests:
runs-on: ubuntu-22.04
env:
MYSQL_ROOT_USER: root
MYSQL_ROOT_PASSWORD: root
MYSQL_DATABASE: demodb
MYSQL_USER: demouser
MYSQL_PASSWORD: demopswd
MYSQL_TCP_PORT: 3306
MYSQL_HP: --host=localhost --port=3306
steps:
- name: Copy project directory
uses: actions/checkout@v4
- name: Start MySQL
run: sudo /etc/init.d/mysql start && mysql -e 'CREATE DATABASE ${{ env.MYSQL_DATABASE }};' -u${{ env.MYSQL_ROOT_USER }} -p${{ env.MYSQL_ROOT_PASSWORD }} ${{env.MYSQL_HP}}
- name: Create MySQL user without host
run: sudo mysql -e 'CREATE USER ${{ env.MYSQL_USER }} IDENTIFIED BY "${{ env.MYSQL_PASSWORD }}";' -u${{ env.MYSQL_ROOT_USER }} -p${{ env.MYSQL_ROOT_PASSWORD }} ${{env.MYSQL_HP}}
- name: Apply MySQL schema
run: sudo mysql ${{ env.MYSQL_HP }} -u${{ env.MYSQL_ROOT_USER }} -p${{ env.MYSQL_ROOT_PASSWORD }} ${{ env.MYSQL_DATABASE }} < ${{ github.workspace }}/docker-entrypoint-initdb.d/001-database-model.sql
- name: Populate MySQL
run: sudo mysql ${{ env.MYSQL_HP }} -u${{ env.MYSQL_ROOT_USER }} -p${{ env.MYSQL_ROOT_PASSWORD }} ${{ env.MYSQL_DATABASE }} < ${{ github.workspace }}/docker-entrypoint-initdb.d/002-database-state-insert.sql
- run: echo "Running Integration Tests in ${{ runner.os }} for branch ${{ github.ref }}."
- name: Set Node.js
uses: actions/setup-node@v4
with:
node-version: '22.x'
- name: Install dependencies
run: cd back && npm ci
- name: Launch Integration Tests
run: cd back && npm run test:integration
- run: echo "Test completed with status ${{ job.status }}."
5 changes: 5 additions & 0 deletions back/database/actiondb.env
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
MYSQL_ROOT_PASSWORD=root
MYSQL_DATABASE=demodb
MYSQL_USER=demouser
MYSQL_PASSWORD=demopswd
MYSQL_TCP_PORT=3306
2 changes: 1 addition & 1 deletion back/database/config.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import fs from "fs";
import { PoolOptions } from "mysql2";
const envFile = process.env.NODE_ENV === `production` ? `.env` : `devdb.env`;
const envFile = process.env.GITHUB_STATE ? `actiondb.env` : process.env.NODE_ENV === `production` ? `.env` : `devdb.env`;
/**
* Sync function to load database environment variables from file.
* Mutates process.env
Expand Down
8 changes: 7 additions & 1 deletion back/database/connector.integration.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,12 @@ import connector, { waitForDbServer } from "./connector";
import { describe, it, after, before } from "node:test";
import * as assert from "node:assert";
import mysql, { FieldPacket, RowDataPacket } from "mysql2/promise";
import Logger from "../lib/winston";

const logger = Logger(`connector.test`);
logger.log(`debug`, `environment is ${JSON.stringify(process.env)}`);

const waitForDbServerTime = process.env.GITHUB_STATE ? 10 : 60;

// from mysql2/lib/constants/types.js
const typeDict: { [key: number]: string } = {
Expand Down Expand Up @@ -63,7 +69,7 @@ describe(`Database connector`, async function () {
before(async function () {
// Waiting for the database server
// TODO add env flag to trigger this behavior
await waitForDbServer();
await waitForDbServer(waitForDbServerTime);
pool = connector();
});
after(function () {
Expand Down
1 change: 1 addition & 0 deletions back/database/connector.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ const logger = Logger(`connector`);
* @returns {Pool}
*/
export default function connector(): Pool {
logger.log(`debug`, `Database configuration is: ${JSON.stringify(databaseConfig)}`);
const pool = mysql.createPool(databaseConfig);
return pool;
}
Expand Down
3 changes: 2 additions & 1 deletion back/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,8 @@
"description": "Back End for a product management demo",
"main": "server.js",
"scripts": {
"test:unit":"export LOG_LEVEL=verbose; npx tsx --test --test-concurrency=1 **/*.unit.test.ts || true",
"test:integration":"export LOG_LEVEL=silly; npx tsx --test --test-concurrency=1 **/*.integration.test.ts",
"test:unit":"export LOG_LEVEL=verbose; npx tsx --test --test-concurrency=1 **/*.unit.test.ts",
"pretest": "echo 'Booting Database' && sudo docker-compose -f test-db.yaml --profile test up -d -V",
"test": "export LOG_LEVEL=verbose; npm run clearlogs && npx tsx --test --test-concurrency=1 || true",
"posttest": "echo 'Closing Database' && sudo docker-compose -f test-db.yaml down --volumes",
Expand Down
3 changes: 2 additions & 1 deletion back/server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import AppSymbols from "./AppSymbols";
import setCors from "./middleware/cors";
const logger = Logger(`server`);
const PORT = process.env.APP_PORT ? parseInt(process.env.APP_PORT) : 3000;
const waitForDbServerTime = process.env.GITHUB_STATE ? 10 : 60;
export interface StartServerOptions {
skipDatabase?: boolean;
skipRoutes?: boolean;
Expand All @@ -31,7 +32,7 @@ export default async function startServer(options?: StartServerOptions) {

if (!options?.skipDatabase) {
logger.log(`debug`, `Connecting DB.`);
await waitForDbServer();
await waitForDbServer(waitForDbServerTime);
const connectionPool = connector();
logger.log(`debug`, `Connected DB.`);
disconnectDatabase = function () {
Expand Down

0 comments on commit d9d3ff0

Please sign in to comment.