Skip to content

Commit

Permalink
chore(infra): add e2e for local
Browse files Browse the repository at this point in the history
  • Loading branch information
mildronize committed May 16, 2024
1 parent a792187 commit 420e991
Show file tree
Hide file tree
Showing 6 changed files with 123 additions and 15 deletions.
37 changes: 36 additions & 1 deletion .github/workflows/e2e.yml
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,41 @@ jobs:
path: examples/with-${{ matrix.runtime }}/.nmt/dist
retention-days: 1

e2e-local:
runs-on: ubuntu-latest
needs:
- build
- get-matrix
timeout-minutes: 10
strategy:
matrix:
include: ${{fromJson(needs.get-matrix.outputs.matrix)}}

steps:
- name: Download Artifact
uses: actions/download-artifact@v4
with:
name: ${{ matrix.resource_identifier_key }}
- uses: actions/checkout@v4
- uses: oven-sh/setup-bun@v1

- name: Use Node.js ${{ matrix.version }}
if: matrix.runtime == 'node'
uses: actions/setup-node@v4
with:
node-version: ${{ matrix.version }}

- uses: pnpm/action-setup@v3
name: Install pnpm
with:
version: ${{ env.pnpm_version }}
- run: pnpm install

- name: Build the project
run: pnpm exec nx run @infra/azure-functions:test:e2e-local
env:
RUNTIME: ${{ matrix.runtime }}

deploy:
runs-on: ubuntu-latest
needs:
Expand Down Expand Up @@ -140,7 +175,7 @@ jobs:
- run: pnpm install

- name: Run E2E tests
run: pnpm exec nx run @infra/azure-functions:test
run: pnpm exec nx run @infra/azure-functions:test:e2e-remote
env:
AZURE_FUNCTIONS_URL: https://nmt-e2e-${{ matrix.target }}-${{ secrets[matrix.resource_identifier_key] }}.azurewebsites.net
AZURE_FUNCTIONS_API_KEY: ${{ secrets.AZURE_FUNCTIONS_HOST_KEY }}
4 changes: 3 additions & 1 deletion infra/azure-functions/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,15 @@
"test-os": "tsx src/test-os.ts",
"apply": "bun run src/main.ts",
"plan": "PLAN_MODE=true bun run src/main.ts",
"test": "bun test --timeout 120000",
"test:e2e-remote": "bun test --timeout 120000 -t e2e-remote",
"test:e2e-local": "bun test --timeout 120000 -t e2e-local",
"github-actions": "bun run src/github-actions.ts",
"lint": "tsc --noEmit && eslint ./src && prettier -c src",
"lint:fix": "eslint --fix ./src && prettier -w src"
},
"dependencies": {
"@actions/core": "^1.10.1",
"execa": "^9.1.0",
"nammatham": "workspace:*"
},
"devDependencies": {
Expand Down
57 changes: 57 additions & 0 deletions infra/azure-functions/src/e2e-local.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
import { test, expect, describe, beforeAll, afterAll } from 'bun:test';
import { execa, type ResultPromise } from 'execa';

const url = 'http://localhost:7071';
const healthPath = '/api/SimpleHttpTrigger';

function waitForServer(url: string) {
return new Promise<void>((resolve, reject) => {
const checkServer = async () => {
try {
const response = await fetch(url);
if (response.ok) {
resolve();
} else {
setTimeout(checkServer, 1000);
}
} catch (error) {
setTimeout(checkServer, 1000);
}
};
checkServer();
});
}

describe('e2e-local', () => {
let serverProcess:
| ResultPromise<{
stdout: 'inherit';
killSignal: 'SIGKILL';
}>
| undefined;
let serverLogs = '';

beforeAll(async () => {
// Place Azure Functions into the current directory
serverProcess = execa({ stdout: 'inherit', killSignal: 'SIGKILL' })`func start --verbose`;
// Wait for the server to be ready
await waitForServer(new URL(healthPath, url).toString());
});

afterAll(async () => {
if (serverProcess) {
console.log('Killing server process');
serverProcess.kill();
}
});

test('should return 200 OK', async () => {
try {
const response = await fetch(new URL('/api/SimpleHttpTrigger', url).toString());
expect(response.status).toBe(200);
} catch (error) {
console.error(serverLogs); // Print server logs on failure
throw new Error('Server did not respond with 200 OK');
}
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,11 @@ const apiKey = process.env.AZURE_FUNCTIONS_API_KEY;
if (!url) throw new Error('AZURE_FUNCTIONS_URL not set');

Check failure on line 6 in infra/azure-functions/src/e2e-remote.test.ts

View workflow job for this annotation

GitHub Actions / e2e-local (ubuntu-latest, node, latest, node18-linux-x64, true, RESOURCE_IDENTIFIER_NODE18_LINUX_...

error: AZURE_FUNCTIONS_URL not set

at /home/runner/work/nammatham/nammatham/infra/azure-functions/src/e2e-remote.test.ts:6:17 at :2:1

Check failure on line 6 in infra/azure-functions/src/e2e-remote.test.ts

View workflow job for this annotation

GitHub Actions / e2e-local (macos-latest, node, latest, node18-macos-arm64, false, RESOURCE_IDENTIFIER_NODE18_MACO...

error: AZURE_FUNCTIONS_URL not set

at /home/runner/work/nammatham/nammatham/infra/azure-functions/src/e2e-remote.test.ts:6:17 at :2:1

Check failure on line 6 in infra/azure-functions/src/e2e-remote.test.ts

View workflow job for this annotation

GitHub Actions / e2e-local (windows-latest, bun, latest, bun-win-x64, true, RESOURCE_IDENTIFIER_BUN_WIN_X64)

error: AZURE_FUNCTIONS_URL not set

at /home/runner/work/nammatham/nammatham/infra/azure-functions/src/e2e-remote.test.ts:6:17 at :2:1

Check failure on line 6 in infra/azure-functions/src/e2e-remote.test.ts

View workflow job for this annotation

GitHub Actions / e2e-local (macos-latest, bun, latest, bun-macos-x64, false, RESOURCE_IDENTIFIER_BUN_MACOS_X64)

error: AZURE_FUNCTIONS_URL not set

at /home/runner/work/nammatham/nammatham/infra/azure-functions/src/e2e-remote.test.ts:6:17 at :2:1
if (!apiKey) throw new Error('AZURE_FUNCTIONS_API_KEY not set');

test('e2e', async () => {
test('e2e-remote', async () => {
// const response = await supertest(url).get(`/api/SimpleHttpTrigger?code=${apiKey}`);
const response = await fetch(new URL(`/api/SimpleHttpTrigger?code=${apiKey}`, url).toString(), {
method: 'GET',
});

expect(response.status).toBe(200);
});
33 changes: 21 additions & 12 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 4 additions & 0 deletions vitest.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,10 @@ import { configDefaults, defineConfig } from 'vitest/config'
export default defineConfig({

test: {
exclude: [
...configDefaults.exclude,
'infra/**',
],
coverage: {
exclude: [
...configDefaults.coverage.exclude ?? [],
Expand Down

0 comments on commit 420e991

Please sign in to comment.