Skip to content

Commit

Permalink
Add script to run ndc tests (#41)
Browse files Browse the repository at this point in the history
* add a npm script to run ndc-tests

* generate configuration before starting the server

* test out github workflow

* try with ubuntu-latest

* add debug statement

* debug

* remove the loging into GHCR
  • Loading branch information
codingkarthik authored Sep 11, 2024
1 parent 0314eec commit 83ce105
Show file tree
Hide file tree
Showing 3 changed files with 101 additions and 33 deletions.
46 changes: 13 additions & 33 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -6,56 +6,36 @@ on:
jobs:
unit_tests:
name: Run NDC tests
runs-on: ubuntu-20.04
runs-on: ubuntu-latest
steps:
- name: Checkout (GitHub)
uses: actions/checkout@v3
with:
path: cosmos

- name: Log in to GitHub Container Registry 📦
uses: docker/login-action@v2
with:
registry: ghcr.io
username: ${{ github.actor }}
password: ${{ secrets.GITHUB_TOKEN }}

- name: Build connector
run: |
cd cosmos
npm install
npm run build
- name: Generate the connector configuration
env:
AZURE_COSMOS_KEY: ${{ secrets.AZURE_COSMOS_KEY }}
AZURE_COSMOS_ENDPOINT: ${{ secrets.AZURE_COSMOS_ENDPOINT }}
AZURE_COSMOS_DB_NAME: ${{ secrets.AZURE_COSMOS_DB_NAME }}
- name: Download NDC Test Binary
run: |
cd cosmos
chmod +x ./dist/cli/index.js
./dist/cli/index.js update
curl -L https://github.com/hasura/ndc-spec/releases/download/v0.1.6/ndc-test-x86_64-unknown-linux-gnu -o ndc-test
chmod +x ndc-test
./ndc-test --version # Optional: Verify the binary works
sudo mv ndc-test /usr/local/bin/
- name: Start connector
- name: Verify the ndc-test binary is accessible
run: |
ndc-test -V
- name: Run tests
env:
AZURE_COSMOS_KEY: ${{ secrets.AZURE_COSMOS_KEY }}
AZURE_COSMOS_ENDPOINT: ${{ secrets.AZURE_COSMOS_ENDPOINT }}
AZURE_COSMOS_DB_NAME: ${{ secrets.AZURE_COSMOS_DB_NAME }}
run: |
cd cosmos
export AZURE_COSMOS_KEY=Bh3EVxRH6BsUnger4tfXkKAvUenZhVosnvNpk185PyYZ9wd4qZO1kf7Y6hvERc7EUUJUE9j8RvDNACDbsgKqLg==
export AZURE_COSMOS_ENDPOINT=https://test-azure-cosmos-one.documents.azure.com:443/
export AZURE_COSMOS_DB_NAME=azure-cosmos-one
export HASURA_CONFIGURATION_DIRECTORY="."
npm run start serve -- --configuration . &
- name: Checkout ndc-spec
uses: actions/checkout@v3
with:
repository: hasura/ndc-spec
path: ndc-spec
ref: 'v0.1.6'

- name: Run ndc-test
working-directory: ndc-spec
run: cargo run --bin ndc-test -- replay --endpoint http://0.0.0.0:8080 --snapshots-dir ../cosmos/ndc-test-snapshots
npm run ndc-test
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
},
"scripts": {
"test": "mocha",
"ndc-test": "node script/test-setup.js",
"build": "rimraf ./build && tsc",
"start": "npm run build && ts-node ./src/index.ts",
"install-bin": "tsc && npm install -g",
Expand Down
87 changes: 87 additions & 0 deletions script/test-setup.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
import { exec, execSync } from 'child_process';
import net from 'net';
import path from 'path';
import fs from 'fs';
import { fileURLToPath } from 'url';

const __filename = fileURLToPath(import.meta.url);
const __dirname = path.dirname(__filename);

function findVacantPort(startPort = 8000) {
return new Promise((resolve, reject) => {
const server = net.createServer();
server.listen(startPort, () => {
const port = server.address().port;
server.close(() => resolve(port));
});
server.on('error', (err) => {
if (err.code === 'EADDRINUSE') {
findVacantPort(startPort + 1).then(resolve, reject);
} else {
reject(err);
}
});
});
}

async function runTests() {
const projectRoot = path.resolve(__dirname, '..');
const logFile = path.join(projectRoot, 'server-output.log');
let serverProcess = null;

try {
console.log('Generating project configuration...');
execSync('npm run cli update', { stdio: 'inherit', cwd: projectRoot });
console.log('Configuration generated successfully.');

const port = await findVacantPort();
console.log(`Found vacant port: ${port}`);

const startCommand = `npm run start serve -- --configuration . --port ${port} | jq -R -r '. as $line | try fromjson catch $line'`;
const testCommand = `ndc-test replay --endpoint http://0.0.0.0:${port} --snapshots-dir ${path.join(projectRoot, 'ndc-test-snapshots')}`;

console.log('Starting server...');
serverProcess = exec(startCommand, { cwd: projectRoot });

// Save server output to file
const logStream = fs.createWriteStream(logFile);
serverProcess.stdout.pipe(logStream);
serverProcess.stderr.pipe(logStream);

// Wait for server to start (adjust timeout as needed)
await new Promise(resolve => setTimeout(resolve, 5000));

console.log('Running tests...');
execSync(testCommand, { stdio: 'inherit', cwd: projectRoot });

console.log('Tests completed successfully.');
} catch (error) {
console.error('An error occurred:', error);

// Output contents of log file
console.error('Server output:');
try {
const logContents = fs.readFileSync(logFile, 'utf8');
console.error(logContents);
} catch (readError) {
console.error('Failed to read server output log:', readError);
}
} finally {
// Terminate the server process
if (serverProcess) {
console.log('Terminating server process...');
serverProcess.kill('SIGTERM');
}

// Clean up log file
try {
fs.unlinkSync(logFile);
} catch (unlinkError) {
console.error('Failed to remove log file:', unlinkError);
}

console.log('Test setup script completed.');
}
}

runTests().then(() => process.exit(0)).catch(() => process.exit(1));

0 comments on commit 83ce105

Please sign in to comment.