Skip to content

Commit bdadb9e

Browse files
committed
test(secrets): add tests for helpers
1 parent cb4b05b commit bdadb9e

File tree

7 files changed

+353
-299
lines changed

7 files changed

+353
-299
lines changed

lib/addNewMask.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ async function updateMasks(secret) {
1818
console.log(`successfully updated masks with secret: ${secret.key}`);
1919
process.exit(0);
2020
} catch (error) {
21-
console.error(`could not create mask for secret: ${secret.key}, due to error: ${error}`);
21+
console.error(`could not create mask for secret: ${secret.key}. Error: ${error}`);
2222
process.exit(1);
2323
}
2424
}

lib/const.js

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,8 @@
1-
const serverAddressPath = '/root/cf-runtime/SERVER_ADDRESS';
1+
const { tmpdir } = require('node:os');
2+
const { resolve } = require('node:path');
3+
4+
const SERVER_ADDRESS_PATH = resolve(tmpdir(), 'LOGGER_SERVER_ADDRESS');
25

36
module.exports = {
4-
serverAddressPath,
7+
SERVER_ADDRESS_PATH,
58
};

lib/helpers.js

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
1-
const { stat, writeFile, readFile } = require('fs/promises');
1+
const { stat, writeFile, readFile } = require('node:fs/promises');
22
const path = require('path');
33
const logger = require('cf-logs').Logger('codefresh:containerLogger');
44
const getPromiseWithResolvers = require('core-js-pure/es/promise/with-resolvers');
55
const { BuildFinishedSignalFilename } = require('./enums');
6-
const { serverAddressPath } = require('./const');
6+
const { SERVER_ADDRESS_PATH } = require('./const');
77

88
const checkFileInterval = 1000;
99

@@ -35,19 +35,18 @@ function watchForBuildFinishedSignal() {
3535
return deferred.promise;
3636
}
3737

38-
const persistServerAddress = async (serverAddress) => {
38+
const saveServerAddress = async (serverAddress) => {
3939
try {
40-
await writeFile(serverAddressPath, serverAddress, { encoding: 'utf8' });
40+
await writeFile(SERVER_ADDRESS_PATH, serverAddress, { encoding: 'utf8' });
4141
} catch (error) {
42-
logger.error(`Failed to persist server address: ${error}`);
42+
logger.error(`Failed to save server address: ${error}`);
4343
throw error;
4444
}
4545
};
4646

4747
const getServerAddress = async () => {
4848
try {
49-
const serverAddress = await readFile(serverAddressPath, { encoding: 'utf8' });
50-
return serverAddress;
49+
return await readFile(SERVER_ADDRESS_PATH, { encoding: 'utf8' });
5150
} catch (error) {
5251
logger.error(`Failed to read server address: ${error}`);
5352
throw error;
@@ -61,6 +60,6 @@ module.exports = {
6160
*/
6261
getPromiseWithResolvers,
6362
watchForBuildFinishedSignal,
64-
persistServerAddress,
63+
saveServerAddress,
6564
getServerAddress,
6665
};

lib/logger.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ const { ContainerStatus } = require('./enums');
1111
const { LoggerStrategy } = require('./enums');
1212
const { ContainerHandlingStatus } = require('./enums');
1313
const ContainerLogger = require('./ContainerLogger');
14-
const { getPromiseWithResolvers, persistServerAddress } = require('./helpers');
14+
const { getPromiseWithResolvers, saveServerAddress } = require('./helpers');
1515

1616
const initialState = {
1717
pid: process.pid, status: 'init', lastLogsDate: new Date(), failedHealthChecks: [], restartCounter: 0, containers: {}
@@ -382,7 +382,7 @@ class Logger {
382382
});
383383

384384
const address = await server.listen({ host, port });
385-
await persistServerAddress(address);
385+
await saveServerAddress(address);
386386
logger.info(`listening for engine updates on ${address}`);
387387
} catch (error) {
388388
logger.error(`could not start server for engine updates due to error: ${error}`);

package.json

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@
2323
"q": "^1.5.1"
2424
},
2525
"devDependencies": {
26-
"chai": "^4.3.6",
26+
"chai": "^4.4.1",
2727
"eslint": "^7.32.0",
2828
"eslint-config-airbnb": "^18.2.1",
2929
"eslint-plugin-chai-friendly": "^0.6.0",
@@ -34,10 +34,10 @@
3434
"eslint-plugin-promise": "^4.3.1",
3535
"eslint-plugin-react": "^7.29.4",
3636
"isparta": "^4.1.1",
37-
"mocha": "^8.4.0",
37+
"mocha": "^10.6.0",
3838
"proxyquire": "^1.8.0",
3939
"shelljs": "^0.6.1",
40-
"sinon": "^7.5.0",
40+
"sinon": "^18.0.0",
4141
"sinon-chai": "^3.7.0"
4242
},
4343
"engines": {

test/helpers.unit.spec.js

Lines changed: 97 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,97 @@
1+
const proxyquire = require('proxyquire').noCallThru();
2+
const chai = require('chai');
3+
const sinon = require('sinon');
4+
5+
const expect = chai.expect;
6+
const stubFsPromises = {
7+
writeFile: sinon.stub(),
8+
readFile: sinon.stub(),
9+
};
10+
const stubLogger = {
11+
debug: sinon.stub(),
12+
log: sinon.stub(),
13+
error: sinon.stub(),
14+
};
15+
16+
describe('helpers', () => {
17+
afterEach(() => {
18+
for (const stub in stubFsPromises) {
19+
stubFsPromises[stub].resetHistory();
20+
}
21+
for (const stub in stubLogger) {
22+
stubLogger[stub].resetHistory();
23+
}
24+
});
25+
26+
describe('saveServerAddress() and getServerAddress()', () => {
27+
describe('saveServerAddress()', () => {
28+
it('should write the server address to the file', async () => {
29+
const SERVER_ADDRESS_PATH = 'little/bobby/tables/we/call/him';
30+
const serverAddress = 'foo';
31+
const { saveServerAddress } = proxyquire('../lib/helpers.js', {
32+
'node:fs/promises': stubFsPromises,
33+
'cf-logs': { Logger: () => stubLogger },
34+
'./const': { SERVER_ADDRESS_PATH },
35+
});
36+
await saveServerAddress(serverAddress);
37+
expect(stubFsPromises.writeFile).to.have.been.calledOnceWithExactly(SERVER_ADDRESS_PATH, serverAddress, { encoding: 'utf8' });
38+
});
39+
40+
it('should fail if the file cannot be written', async () => {
41+
const { saveServerAddress } = proxyquire('../lib/helpers.js', {
42+
'node:fs/promises': stubFsPromises,
43+
'cf-logs': { Logger: () => stubLogger },
44+
});
45+
const expectedError = new Error('oh no');
46+
stubFsPromises.writeFile.rejects(expectedError);
47+
try {
48+
await saveServerAddress('foo');
49+
expect.fail('should have thrown an error');
50+
} catch (error) {
51+
expect(error).to.be.equal(expectedError);
52+
expect(stubLogger.error).to.have.been.calledOnceWithExactly(`Failed to save server address: ${expectedError}`);
53+
}
54+
});
55+
});
56+
57+
describe('getServerAddress()', () => {
58+
it('should read the server address from the file', async () => {
59+
const SERVER_ADDRESS_PATH = 'little/bobby/tables/we/call/him';
60+
const serverAddress = 'foo';
61+
const { getServerAddress } = proxyquire('../lib/helpers.js', {
62+
'node:fs/promises': stubFsPromises,
63+
'cf-logs': { Logger: () => stubLogger },
64+
'./const': { SERVER_ADDRESS_PATH },
65+
});
66+
stubFsPromises.readFile.resolves(serverAddress);
67+
const result = await getServerAddress();
68+
expect(result).to.be.equal(serverAddress);
69+
expect(stubFsPromises.readFile).to.have.been.calledOnceWithExactly(SERVER_ADDRESS_PATH, { encoding: 'utf8' });
70+
});
71+
72+
it('should fail if the file cannot be read', async () => {
73+
const { getServerAddress } = proxyquire('../lib/helpers.js', {
74+
'node:fs/promises': stubFsPromises,
75+
'cf-logs': { Logger: () => stubLogger },
76+
});
77+
const expectedError = new Error('oh no');
78+
stubFsPromises.readFile.rejects(expectedError);
79+
try {
80+
await getServerAddress();
81+
expect.fail('should have thrown an error');
82+
} catch (error) {
83+
expect(error).to.be.equal(expectedError);
84+
expect(stubLogger.error).to.have.been.calledOnceWithExactly(`Failed to read server address: ${expectedError}`);
85+
}
86+
});
87+
});
88+
89+
it('should write/read to/from the same location', async () => {
90+
const { saveServerAddress, getServerAddress } = require('../lib/helpers.js');
91+
const serverAddress = 'http://g.codefresh.io';
92+
await saveServerAddress(serverAddress);
93+
const result = await getServerAddress();
94+
expect(result).to.be.equal(serverAddress);
95+
});
96+
});
97+
});

0 commit comments

Comments
 (0)