Skip to content

Commit 530072b

Browse files
committed
implement tests with kubetest2
1 parent b012777 commit 530072b

File tree

6 files changed

+191
-10
lines changed

6 files changed

+191
-10
lines changed

index.js

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,13 @@ app.get('/', (req, res, next) => {
6464
res.json({
6565
paths: routes.flat(Math.Infinity).filter((e) => e).map((e) => e.path).flat(Math.Infinity)
6666
});
67+
});
68+
69+
app.get('/ping', (req, res) => {
70+
if (db.connectionStatus() === 1) {
71+
return res.sendStatus(200);
72+
}
73+
return res.sendStatus(502);
6774
})
6875

6976
app.use((req, res) => {

package-lock.json

Lines changed: 72 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,14 +4,14 @@
44
"description": "",
55
"main": "index.js",
66
"scripts": {
7-
"test": "echo \"Error: no test specified\" && exit 1",
8-
"start": "./start.sh",
9-
"test": "kubetest2 noop --kubeconfig=./test-config --test=ginkgo"
7+
"test": "node ./test/test.js",
8+
"start": "./start.sh"
109
},
1110
"author": "",
1211
"license": "ISC",
1312
"dependencies": {
1413
"@wesleytodd/openapi": "^1.0.0",
14+
"axios": "^1.6.8",
1515
"docker-cli-js": "^2.10.0",
1616
"dotenv": "^16.4.5",
1717
"express": "^4.19.1",

test-config

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2,17 +2,17 @@ apiVersion: v1
22
clusters:
33
- cluster:
44
insecure-skip-tls-verify: true
5-
server: http://host.docker.internal:8080
6-
name: host.docker.internal:8080
5+
server: http://localhost:8080
6+
name: localhost:8080
77
contexts:
88
- context:
9-
cluster: host.docker.internal:8080
10-
user: admin/host.docker.internal:8080
11-
name: /host.docker.internal:8080/admin
12-
current-context: /host.docker.internal:8080/admin
9+
cluster: localhost:8080
10+
user: admin/localhost:8080
11+
name: /localhost:8080/admin
12+
current-context: /localhost:8080/admin
1313
kind: Config
1414
preferences: {}
1515
users:
16-
- name: admin/host.docker.internal:8080
16+
- name: admin/localhost:8080
1717
user:
1818
token: token

test/templates/node.json

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
{
2+
"kind": "Node",
3+
"apiVersion": "v1",
4+
"metadata": {
5+
"name": "${ip}",
6+
"labels": {
7+
"name": "my-first-k8s-node"
8+
}
9+
},
10+
"status": {
11+
"capacity": {
12+
"cpu": "1",
13+
"ephemeral-storage": "15625000Ki",
14+
"hugepages-1Gi": "0",
15+
"hugepages-2Mi": "0",
16+
"memory": "15625000Ki",
17+
"pods": "250"
18+
},
19+
"allocatable": {},
20+
"conditions": [],
21+
"addresses": [
22+
{
23+
"type": "ExternalIP",
24+
"address": "$ip"
25+
},
26+
{
27+
"type": "InternalIP",
28+
"address": "$ip"
29+
},
30+
{
31+
"type": "Hostname",
32+
"address": "$ip"
33+
}
34+
]
35+
}
36+
}

test/test.js

Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
const {
2+
stopContainer,
3+
killContainer,
4+
removeContainer,
5+
getContainerIP,
6+
runImage,
7+
} = require('../functions.js');
8+
9+
const nodeSpec = require('./templates/node.json');
10+
const axios = require('axios');
11+
const { spawn } = require('child_process');
12+
13+
(async () => {
14+
let nodeName = 'worker-node-1';
15+
await runImage('ubuntu', nodeName);
16+
let ip = JSON.parse((await getContainerIP(nodeName)).raw)[0]?.NetworkSettings.Networks.bridge.IPAddress;
17+
nodeSpec.metadata.name = ip;
18+
nodeSpec.metadata.labels.name = nodeName;
19+
nodeSpec.status.addresses = [{
20+
"type": "ExternalIP",
21+
"address": ip,
22+
}, {
23+
"type": "InternalIP",
24+
"address": ip,
25+
}, {
26+
"type": "Hostname",
27+
"address": ip,
28+
}];
29+
30+
let server = spawn('npm', ['start']);
31+
32+
server.stdout.on('data', (data) => console.log(`[${new Date()}] [server] ${data}`));
33+
34+
server.stderr.on('data', (data) => console.log(`[${new Date()}] [server] ${data}`));
35+
36+
server.on('exit', async (code) => {
37+
console.log(`[server] closed`);
38+
await killContainer(nodeName);
39+
await removeContainer(nodeName);
40+
process.exit(0);
41+
});
42+
43+
await new Promise((resolve, reject) => {
44+
let i = setInterval(() => {
45+
axios.get('http://localhost:8080/ping')
46+
.then(() => {
47+
clearInterval(i);
48+
resolve();
49+
})
50+
.catch(() => { });
51+
}, 10000);
52+
});
53+
54+
await axios.post('http://localhost:8080/api/v1/nodes', nodeSpec);
55+
let test = spawn('kubetest2', ['noop', '--kubeconfig=./test-config', '--v', '10', '--test=ginkgo'])
56+
57+
test.stdout.on('data', (data) => console.log(`[${new Date()}] [test] ${data}`));
58+
59+
test.stderr.on('data', (data) => console.log(`[${new Date()}] [test] ${data}`));
60+
61+
test.on('close', async (code) => {
62+
console.log(`[test] closed`);
63+
await axios.delete(`http://localhost:8080/api/v1/nodes/${ip}`);
64+
server.kill('SIGKILL');
65+
});
66+
})();

0 commit comments

Comments
 (0)