forked from prasmussen/glot-containers
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathrun_tests_and_push.js
More file actions
84 lines (67 loc) · 2.15 KB
/
run_tests_and_push.js
File metadata and controls
84 lines (67 loc) · 2.15 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
const glob = require('glob');
const fs = require('fs');
const { exec } = require("child_process");
const IMAGE_PREFIX = 'docker.pkg.github.com/techulus/glot-containers';
const runCommand = async (cmd) => {
return new Promise((resolve, reject) => {
exec(cmd, (error, stdout, stderr) => {
if (error) {
reject(error);
return;
}
if (stderr) {
reject(stderr);
return;
}
resolve(stdout);
})
});
};
const getFiles = async (path) => {
return new Promise((resolve, reject) => {
glob(path, function (err, files) {
if (err) return reject(err);
resolve(files);
});
});
};
runTest = async () => {
try {
const imageFiles = await getFiles("**/Dockerfile");
// console.log("images", imageFiles)
let file, folder, testFiles;
for (let i = 0; i < imageFiles.length; i++) {
file = imageFiles[i]
folder = file.replace('/Dockerfile', '')
testFiles = await getFiles(folder + "/**/payload.json");
resultFiles = await getFiles(folder + "/**/result.json");
// console.log('tests', testFiles)
const imageName = [IMAGE_PREFIX, folder.replace('/', ':')].join('/');
for (let j = 0; j < testFiles.length; j++) {
// ======= build
console.log('build', imageName)
await runCommand(`docker build --pull ./${folder} -t ${imageName}`);
// ======= run
console.log('run', imageName)
const result = await runCommand(`docker run -i --rm ${imageName} < ${testFiles[j]}`);
console.log('result', result);
const expected = fs.readFileSync(resultFiles[j], "utf8");
console.log('expected', expected);
// ======= test
if (result === expected) {
console.log("test passed")
// ======= push
console.log("push image")
const remoteResponse = await runCommand(`docker push ${imageName}`);
console.log("push image done", remoteResponse)
} else {
console.log("test failed")
process.exit(1);
}
}
}
} catch (err) {
return console.error('failed to run tests', err);
}
};
runTest();