Skip to content

Commit 643b2af

Browse files
committed
fix: tests for isReady.js
1 parent d2828e2 commit 643b2af

File tree

2 files changed

+61
-19
lines changed

2 files changed

+61
-19
lines changed

lib/isReady.js

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ function isContainerLoggerReady(state) {
3131
return isReady;
3232
}
3333

34-
(async () => {
34+
const isReady = async () => {
3535
const containerId = process.argv[2];
3636
const state = JSON.parse(readFileSync('./lib/state.json').toString('utf-8'));
3737
let isReady = false;
@@ -43,4 +43,10 @@ function isContainerLoggerReady(state) {
4343

4444
await terminate();
4545
process.exit(isReady ? 0 : 1);
46-
})();
46+
};
47+
48+
if (require.main === module) {
49+
isReady();
50+
} else {
51+
module.exports = { isReady };
52+
}

test/isReady.unit.spec.js

Lines changed: 53 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -18,67 +18,103 @@ describe('isReady script', () => {
1818
process.argv = orgArgs;
1919
});
2020
describe('Container Logger Checks', () => {
21-
it('Should check exit with 0 code if container logger is ready', () => {
21+
it('Should check exit with 0 code if container logger is ready', async () => {
2222
const state = JSON.stringify({ status: 'ready', containers: {} })
2323
process.argv = [];
24-
proxyquire('../lib/isReady.js', {
24+
const terminateSpy = sinon.spy();
25+
const { isReady } = proxyquire('../lib/isReady.js', {
26+
'@codefresh-io/cf-telemetry/init': { terminate: terminateSpy },
2527
'fs': {
2628
readFileSync: () => Buffer.from(state),
2729
},
2830
});
31+
await isReady();
32+
expect(terminateSpy).to.have.been.calledOnce;
2933
expect(process.exit).to.have.been.calledOnceWith(0);
3034
});
31-
it('Should check exit with 1 code if container logger is not ready', () => {
35+
it('Should check exit with 1 code if container logger is not ready', async () => {
3236
const state = JSON.stringify({ status: 'notReady', containers: {} })
3337
process.argv = [];
34-
proxyquire('../lib/isReady.js', {
38+
const terminateSpy = sinon.spy();
39+
const { isReady } = proxyquire('../lib/isReady.js', {
40+
'@codefresh-io/cf-telemetry/init': { terminate: terminateSpy },
3541
'fs': {
3642
readFileSync: () => Buffer.from(state),
3743
},
3844
});
45+
await isReady();
46+
expect(terminateSpy).to.have.been.calledOnce;
3947
expect(process.exit).to.have.been.calledOnceWith(1);
4048
});
4149
});
4250
describe('Container Checks', () => {
43-
it('Should check exit with 0 code if container is ready', () => {
44-
const state = JSON.stringify({ status: 'ready', containers: { 'container-id': { status: ContainerHandlingStatus.LISTENING } } })
51+
it('Should check exit with 0 code if container is ready', async () => {
52+
const state = JSON.stringify({
53+
status: 'ready',
54+
containers: {'container-id': {status: ContainerHandlingStatus.LISTENING}}
55+
})
4556
process.argv = ['foo', 'bar', 'container-id'];
46-
proxyquire('../lib/isReady.js', {
57+
const terminateSpy = sinon.spy();
58+
const {isReady} = proxyquire('../lib/isReady.js', {
59+
'@codefresh-io/cf-telemetry/init': {terminate: terminateSpy},
4760
'fs': {
4861
readFileSync: () => Buffer.from(state),
4962
},
5063
});
64+
await isReady();
65+
expect(terminateSpy).to.have.been.calledOnce;
5166
expect(process.exit).to.have.been.calledOnceWith(0);
5267
});
53-
it('Should check exit with 0 code if container is waiting for start status', () => {
54-
const state = JSON.stringify({ status: 'ready', containers: { 'container-id': { status: ContainerHandlingStatus.WAITING_FOR_START } } })
68+
it('Should check exit with 0 code if container is waiting for start status', async () => {
69+
const state = JSON.stringify({
70+
status: 'ready',
71+
containers: {'container-id': {status: ContainerHandlingStatus.WAITING_FOR_START}}
72+
})
5573
process.argv = ['foo', 'bar', 'container-id'];
56-
proxyquire('../lib/isReady.js', {
74+
const terminateSpy = sinon.spy();
75+
const {isReady} = proxyquire('../lib/isReady.js', {
76+
'@codefresh-io/cf-telemetry/init': {terminate: terminateSpy},
5777
'fs': {
5878
readFileSync: () => Buffer.from(state),
5979
},
6080
});
81+
await isReady();
82+
expect(terminateSpy).to.have.been.calledOnce;
6183
expect(process.exit).to.have.been.calledOnceWith(0);
6284
});
63-
it('Should check exit with 0 code if container is finished status', () => {
64-
const state = JSON.stringify({ status: 'ready', containers: { 'container-id': { status: ContainerHandlingStatus.FINISHED } } })
85+
it('Should check exit with 0 code if container is finished status', async () => {
86+
const state = JSON.stringify({
87+
status: 'ready',
88+
containers: {'container-id': {status: ContainerHandlingStatus.FINISHED}}
89+
})
6590
process.argv = ['foo', 'bar', 'container-id'];
66-
proxyquire('../lib/isReady.js', {
91+
const terminateSpy = sinon.spy();
92+
const {isReady} = proxyquire('../lib/isReady.js', {
93+
'@codefresh-io/cf-telemetry/init': {terminate: terminateSpy},
6794
'fs': {
6895
readFileSync: () => Buffer.from(state),
6996
},
7097
});
98+
await isReady();
99+
expect(terminateSpy).to.have.been.calledOnce;
71100
expect(process.exit).to.have.been.calledOnceWith(0);
72101
});
73-
it('Should check exit with 1 code if container is not ready', () => {
74-
const state = JSON.stringify({ status: 'ready', containers: { 'container-id': { status: ContainerHandlingStatus.INITIALIZING } } })
102+
it('Should check exit with 1 code if container is not ready', async () => {
103+
const state = JSON.stringify({
104+
status: 'ready',
105+
containers: {'container-id': {status: ContainerHandlingStatus.INITIALIZING}}
106+
})
75107
process.argv = ['foo', 'bar', 'container-id'];
76-
proxyquire('../lib/isReady.js', {
108+
const terminateSpy = sinon.spy();
109+
const {isReady} = proxyquire('../lib/isReady.js', {
110+
'@codefresh-io/cf-telemetry/init': {terminate: terminateSpy},
77111
'fs': {
78112
readFileSync: () => Buffer.from(state),
79113
},
80114
});
115+
await isReady();
116+
expect(terminateSpy).to.have.been.calledOnce;
81117
expect(process.exit).to.have.been.calledOnceWith(1);
82118
});
83119
});
84-
});
120+
});

0 commit comments

Comments
 (0)