-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathintegrationTests.ts
More file actions
175 lines (148 loc) · 4.69 KB
/
integrationTests.ts
File metadata and controls
175 lines (148 loc) · 4.69 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
/* eslint-disable no-console */
// Run with: npx babel-node status.js
import chalk from 'chalk';
import { spawn, spawnSync } from 'child_process';
import Docker from 'dockerode';
import find from 'find-process';
import fs from 'fs';
import glob from 'glob';
import yaml from 'js-yaml';
import fetch from 'node-fetch';
import Knex from './src/server/getSingletonKnex';
const REFRESH_INTERVAL = 5000;
// Todo: add status of all the gboxes
const isDockerUp = async () => {
try {
const docker = new Docker({ socketPath: '/var/run/docker.sock' });
await docker.listContainers();
return true;
} catch (err) {
return false;
}
};
const isPostgresUp = async (knex) => {
try {
// Run arbitrary query to check status.
await knex.select('*').from('gbox');
return true;
} catch (err) {
return false;
}
};
const isProcessRunning = async (name) => {
try {
const list = await find('name', name);
return list.length > 0;
} catch (err) {
return false;
}
};
const getGboxName = (filename) => filename.replace(/^.*\/([^/]+)\.yaml$/, '$1');
const getDockerCommandToRun = (filename) => {
const spec = yaml.safeLoad(fs.readFileSync(filename, 'utf8')) as any;
const backend = spec.endpoints.backend;
const command = [
`docker run -i --rm --name ${getGboxName(filename)} -e GRANATUM_SWD=/data`,
backend.image,
backend.cmd,
].join(' ');
console.log(command);
return command;
};
// todo: debug. why aren't some docker instances closing?
// https://hackernoon.com/another-reason-why-your-docker-containers-may-be-slow-d37207dec27f
const testGboxAsync = (filename) =>
new Promise((resolve) => {
try {
const command = getDockerCommandToRun(filename);
const commandWords = command.split(' ');
const execution = spawn(commandWords[0], commandWords.slice(1));
execution.stderr.on('data', (err) => {
console.log(`${getGboxName(filename)} stderr: ${err}`);
});
execution.on('close', (exitCode) => {
if (exitCode > 0) {
console.log(`${getGboxName(filename)} exitCode ${exitCode}`);
resolve(false);
}
resolve(true);
});
} catch (err) {
console.log(err);
resolve(false);
}
});
const testGbox = async (filename) => {
try {
const command = getDockerCommandToRun(filename);
const commandWords = command.split(' ');
const execution = spawnSync(commandWords[0], commandWords.slice(1));
const exitCode = execution.status;
if (exitCode > 0) {
throw new Error(execution.stderr.toString());
}
return true;
} catch (err) {
// console.log(err);
return false;
}
};
const printTest = (testName, testResult, suffix = `up`) => {
console.log(testResult ? chalk.green(`${testName} is ${suffix}`) : chalk.red(`${testName} is NOT ${suffix}`));
};
const testGboxes = async () => {
const gboxYamls = glob.sync('../gboxes/gboxSpecs/*.yaml');
const promises = gboxYamls.map(async (filename) =>
testGboxAsync(filename).then((result) => {
printTest(`Gbox: ${getGboxName(filename)}`, result);
}),
);
await Promise.all(promises);
};
const runTests = async () => {
const knex = await Knex();
printTest('Postgres/Node Connection', await isPostgresUp(knex));
printTest('Docker', await isDockerUp());
printTest('TaskRunner', await isProcessRunning('taskRunner'));
printTest('WebApp', await isProcessRunning('granatumWebApp'));
// Todo: be smarter about what ports to check
const ports = [80, 443, 3000, 34567];
ports.forEach(async (port) => {
try {
const url = port === 443 ? 'https://granatum.garmiregroup.org' : `http://localhost:${port}/index.html`;
const res = await fetch(url, {
redirect: 'manual',
});
printTest('WebApp', true, `listening on port ${port}`);
const text = await res.text();
printTest('WebApp', text.includes('Granatum'), `working properly on port ${port}`);
} catch (err) {
printTest('WebApp', false, `listening on port ${port}`);
}
});
if (process.argv.includes('--gbox')) {
testGboxes();
}
knex.destroy();
};
const delay = (t) =>
new Promise((resolve) => {
setTimeout(() => {
resolve();
}, t);
});
setImmediate(async () => {
const animateFrames = '\\-/|'.split('');
let animateFrameNum = 0;
// eslint-disable-next-line no-constant-condition
while (true) {
// eslint-disable-next-line
console.log('\x1B[2J\x1B[0f');
animateFrameNum = (animateFrameNum + 1) % animateFrames.length;
console.log(`${animateFrames[animateFrameNum]} Refreshing in ${REFRESH_INTERVAL / 1000} seconds ...`);
console.log('');
runTests();
// eslint-disable-next-line no-await-in-loop
await delay(REFRESH_INTERVAL);
}
});