Skip to content

Commit 6a72a7b

Browse files
committed
Merge branch 'devDocker_v2' into Haoyu/grafana
2 parents 90c431a + 22d1049 commit 6a72a7b

File tree

137 files changed

+25001
-4231
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

137 files changed

+25001
-4231
lines changed

__backend-tests__/alert.test.js

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
// const mongoose = require('mongoose');
2+
// const alert = require('../../chronos_npm_package/controllers/alert');
3+
// const axios = require('axios');
4+
// const MockAdapter = require('axios-mock-adapter');
5+
6+
// const mockAxios = new MockAdapter(axios);
7+
8+
// const consoleLogSpy = jest.spyOn(console, 'log');
9+
// jest.spyOn(console, 'error').mockImplementation(() => {});
10+
// // jest.mock('axios', () => {
11+
// // return {
12+
// // post: jest.fn(),
13+
// // };
14+
// // });
15+
16+
17+
18+
// describe('alert.sendSlack', () => {
19+
// beforeEach(() => {
20+
// mockAxios.reset();
21+
// jest.clearAllMocks();
22+
// });
23+
24+
// test('should send Slack message with the correct data', async () => {
25+
// const code = 500;
26+
// const slackSettings = {
27+
// webhook: 'https://example.com/slack-webhook',
28+
// };
29+
// const message = 'Internal server error';
30+
// const expectedData = { text: `${code}, ${message}, ${Date.now()}` };
31+
// mockAxios.onPost(slackSettings.webhook).reply(200, 'Status Code >= 400...\nError message sent');
32+
// await alert.sendSlack(code, message, slackSettings);
33+
34+
// expect(axios.post).toHaveBeenCalledWith(slackSettings.webhook, expectedData, expect.any(Object));
35+
// expect(consoleLogSpy).toHaveBeenCalledWith('Status Code >= 400...\nError message sent');
36+
// });
37+
// });
Lines changed: 205 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,205 @@
1+
const { EcoTwoTone } = require('@material-ui/icons');
2+
const Chronos = require('../chronos_npm_package/chronos.js');
3+
const helpers = require('../chronos_npm_package/controllers/utilities.js');
4+
const hpropagate = require('hpropagate');
5+
const mongo = require('../chronos_npm_package/controllers/mongo.js');
6+
const postgres = require('../chronos_npm_package/controllers/postgres.js');
7+
8+
// Mock the utilities module functions
9+
jest.mock('../chronos_npm_package/controllers/utilities.js', () => ({
10+
validateInput: jest.fn(config => config),
11+
addNotifications: jest.fn(config => config),
12+
testMetricsQuery: jest.fn(config => config),
13+
getMetricsURI: jest.fn(config => config),
14+
}));
15+
16+
// mock propogate from Chronos
17+
jest.mock('hpropagate');
18+
19+
//mock fns found in track
20+
jest.mock('../chronos_npm_package/controllers/mongo.js', () => ({
21+
connect: jest.fn(config => config),
22+
services: jest.fn(config => config),
23+
docker: jest.fn(config => config),
24+
health: jest.fn(config => config),
25+
communications: jest.fn(config => config),
26+
serverQuery: jest.fn(config => config),
27+
}));
28+
29+
jest.mock('../chronos_npm_package/controllers/postgres.js', () => ({
30+
connect: jest.fn(config => config),
31+
services: jest.fn(config => config),
32+
docker: jest.fn(config => config),
33+
health: jest.fn(config => config),
34+
communications: jest.fn(config => config),
35+
serverQuery: jest.fn(config => config),
36+
}));
37+
38+
describe('Chronos Config', () => {
39+
afterEach(() => {
40+
// Clear mock function calls after each test
41+
jest.clearAllMocks();
42+
});
43+
44+
test('should throw an error if config is undefined', () => {
45+
expect(() => new Chronos()).toThrow('Chronos config is undefined');
46+
});
47+
48+
test('should call utilities functions with the correct parm', () => {
49+
const config = {
50+
microservice: 'test',
51+
interval: 300,
52+
mode: 'micro',
53+
dockerized: false,
54+
database: {
55+
connection: 'REST',
56+
type: process.env.CHRONOS_DB,
57+
URI: process.env.CHRONOS_URI,
58+
},
59+
notifications: [],
60+
};
61+
62+
const chronos = new Chronos(config);
63+
64+
// Ensure the config property is correctly set in the instance
65+
expect(chronos.config).toEqual(config);
66+
// Ensure the constructor called the validateInput and addNotifications functions
67+
expect(helpers.validateInput).toHaveBeenCalledWith(config);
68+
expect(helpers.addNotifications).toHaveBeenCalledWith(config);
69+
});
70+
71+
describe('propagate', () => {
72+
test('should check if propagate func properply calls hpropagate', () => {
73+
const config = {
74+
microservice: 'test',
75+
interval: 300,
76+
mode: 'micro',
77+
dockerized: false,
78+
database: {
79+
connection: 'REST',
80+
type: process.env.CHRONOS_DB,
81+
URI: process.env.CHRONOS_URI,
82+
},
83+
notifications: [],
84+
};
85+
86+
const chronos = new Chronos(config);
87+
chronos.propagate();
88+
expect(hpropagate).toHaveBeenCalledWith({ propagateInResponses: true });
89+
});
90+
});
91+
92+
describe('track', () => {
93+
test('should check if track function for MongoDB works', () => {
94+
//check if we can destructure database and dockerized from config
95+
const config = {
96+
microservice: 'test',
97+
interval: 300,
98+
mode: 'micro',
99+
dockerized: true,
100+
database: {
101+
connection: 'REST',
102+
type: 'MongoDB',
103+
URI: process.env.CHRONOS_URI,
104+
},
105+
notifications: [],
106+
};
107+
const { database, dockerized } = config;
108+
const falseDock = { dockerized: false };
109+
const chronos = new Chronos(config);
110+
chronos.track();
111+
if (database.type === 'MongoDB') {
112+
expect(mongo.connect).toHaveBeenCalledWith(config);
113+
expect(mongo.services).toHaveBeenCalledWith(config);
114+
if (dockerized) expect(mongo.docker).toHaveBeenCalledWith(config);
115+
if (!falseDock) expect(mongo.health).toHaveBeenCalledWith(config);
116+
if (database.connection === 'REST') expect(mongo.communications).not.toBeUndefined();
117+
}
118+
});
119+
test('should check if track function for Postgres works', () => {
120+
//check if we can destructure database and dockerized from config
121+
const config = {
122+
microservice: 'test',
123+
interval: 300,
124+
mode: 'micro',
125+
dockerized: true,
126+
database: {
127+
connection: 'REST',
128+
type: 'PostgreSQL',
129+
URI: process.env.CHRONOS_URI,
130+
},
131+
notifications: [],
132+
};
133+
const { database, dockerized } = config;
134+
const falseDock = { dockerized: false };
135+
const chronos = new Chronos(config);
136+
chronos.track();
137+
if (database.type === 'PostgreSQL') {
138+
expect(postgres.connect).toHaveBeenCalledWith(config);
139+
expect(postgres.services).toHaveBeenCalledWith(config);
140+
if (dockerized) expect(postgres.docker).toHaveBeenCalledWith(config);
141+
if (!falseDock) expect(postgres.health).toHaveBeenCalledWith(config);
142+
if (database.connection === 'REST') expect(postgres.communications).not.toBeUndefined();
143+
}
144+
});
145+
});
146+
describe('kafka', () => {
147+
test('should check if kafka is functional', async () => {
148+
const config = {
149+
microservice: 'test',
150+
interval: 300,
151+
mode: 'micro',
152+
dockerized: true,
153+
database: {
154+
connection: 'REST',
155+
type: 'MongoDB',
156+
URI: process.env.CHRONOS_URI,
157+
},
158+
notifications: [],
159+
};
160+
const { database, dockerized } = config;
161+
const falseDock = { dockerized: false };
162+
const chronos = new Chronos(config);
163+
chronos.kafka();
164+
await helpers.testMetricsQuery(config);
165+
if (database.type === 'MongoDB') {
166+
expect(mongo.connect).toHaveBeenCalledWith(config);
167+
expect(mongo.serverQuery).toHaveBeenCalledWith(config);
168+
}
169+
if (database.type === 'PostgreSQL') {
170+
expect(postgres.connect).toHaveBeenCalledWith(config);
171+
expect(postgres.serverQuery).toHaveBeenCalledWith(config);
172+
}
173+
});
174+
});
175+
176+
describe('kubernetes', () => {
177+
test('should check if kubernetes is functional', async () => {
178+
const config = {
179+
microservice: 'test',
180+
interval: 300,
181+
mode: 'micro',
182+
dockerized: true,
183+
database: {
184+
connection: 'REST',
185+
type: 'MongoDB',
186+
URI: process.env.CHRONOS_URI,
187+
},
188+
notifications: [],
189+
};
190+
const { database, dockerized } = config;
191+
const falseDock = { dockerized: false };
192+
const chronos = new Chronos(config);
193+
chronos.kubernetes();
194+
await helpers.testMetricsQuery(config);
195+
if (database.type === 'MongoDB') {
196+
expect(mongo.connect).toHaveBeenCalledWith(config);
197+
expect(mongo.serverQuery).toHaveBeenCalledWith(config);
198+
}
199+
if (database.type === 'PostgreSQL') {
200+
expect(postgres.connect).toHaveBeenCalledWith(config);
201+
expect(postgres.serverQuery).toHaveBeenCalledWith(config);
202+
}
203+
});
204+
});
205+
});

__backend-tests__/healthHelpers.test.js

Whitespace-only changes.

__backend-tests__/jest.config.js

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
module.exports = {
2+
// testEnvironment: 'node', // Use the Node.js environment for testing
3+
// roots: ['<rootDir>/controllers'], // Set the root directory for test files
4+
5+
// testRegex: '(/tests/.*|(\\.|/)(test|spec))\\.(jsx?|tsx?)$',
6+
7+
// // Code coverage settings
8+
// collectCoverage: true,
9+
// coverageDirectory: 'coverage',
10+
11+
// // Specify the test path patterns to ignore frontend tests
12+
// testPathIgnorePatterns: ['/node_modules/', '/__tests__/'],
13+
// };
14+
15+
// =======
16+
roots: ['<rootDir>'], // Set the root directory for test files (adjust this path to your test folder)
17+
18+
testRegex: '(/tests/.*|(\\.|/)(test|spec))\\.(jsx?|tsx?)$',
19+
20+
// Code coverage settings
21+
collectCoverage: true,
22+
coverageDirectory: 'coverage',
23+
24+
// Specify the test path patterns to ignore (frontend tests)
25+
testPathIgnorePatterns: ['/node_modules/', '/__tests__/'],
26+
};
27+

__backend-tests__/mockdbsetup.js

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
const mongoose = require("mongoose");
2+
const { MongoMemoryServer } = require("mongodb-memory-server");
3+
4+
let mongo = null;
5+
6+
const connectDB = async () => {
7+
mongo = await MongoMemoryServer.create();
8+
const uri = mongo.getUri();
9+
10+
await mongoose.connect(uri, {
11+
useNewUrlParser: true,
12+
useUnifiedTopology: true,
13+
});
14+
};
15+
16+
const dropDB = async () => {
17+
if (mongo) {
18+
await mongoose.connection.dropDatabase();
19+
await mongoose.connection.close();
20+
await mongo.stop();
21+
}
22+
};
23+
24+
const dropCollections = async () => {
25+
if (mongo) {
26+
const collections = await mongoose.connection.db.collections();
27+
for (let collection of collections) {
28+
await collection.deleteMany();
29+
}
30+
}
31+
};
32+
33+
34+
module.exports = { connectDB, dropDB, dropCollections}

0 commit comments

Comments
 (0)