|
1 |
| -//const mongoose = require('mongoose'); |
| 1 | +const mongoose = require('mongoose'); |
2 | 2 | const mongo = require('../../chronos_npm_package/controllers/mongo');
|
3 | 3 | const ServicesModel = require('../../chronos_npm_package/models/ServicesModel');
|
| 4 | +const CommunicationModel = require('../../chronos_npm_package/models/CommunicationModel'); |
| 5 | +const { connectDB, dropDB, dropCollections } = require('./testdbsetup'); |
| 6 | +const alert = require('../../chronos_npm_package/controllers/alert'); |
| 7 | +const ContainerInfo = require('../../chronos_npm_package/models/ContainerInfo'); |
| 8 | + |
4 | 9 | require('dotenv').config();
|
5 | 10 |
|
6 |
| -jest.mock('mongoose', () => { |
7 |
| - return { |
8 |
| - connect: jest.fn().mockResolvedValue(undefined), |
9 |
| - }; |
| 11 | +const db = process.env.DB_URI; |
| 12 | + |
| 13 | +beforeAll(async () => { |
| 14 | + await connectDB(); |
| 15 | +}); |
| 16 | + |
| 17 | +afterAll(async () => { |
| 18 | + await dropDB(); |
10 | 19 | });
|
11 | 20 |
|
12 | 21 | jest.spyOn(console, 'log').mockImplementation(() => {});
|
13 | 22 |
|
| 23 | +jest.mock('../../chronos_npm_package/controllers/alert'); |
| 24 | + |
| 25 | +jest.useFakeTimers(); |
| 26 | + |
| 27 | +jest.spyOn(global, 'setInterval'); |
| 28 | + |
| 29 | +jest.mock('../../chronos_npm_package/controllers/healthHelpers', () => { |
| 30 | + return [ |
| 31 | + { |
| 32 | + time: Date.now(), |
| 33 | + metric: 'testMetric', |
| 34 | + value: 10, |
| 35 | + category: 'testCategory', |
| 36 | + }, |
| 37 | + { |
| 38 | + time: Date.now(), |
| 39 | + metric: 'testMetric2', |
| 40 | + value: 12, |
| 41 | + category: 'testCategory2', |
| 42 | + }, |
| 43 | + ]; |
| 44 | +}); |
| 45 | + |
| 46 | +jest.mock('../../chronos_npm_package/controllers/mongo', () => ({ |
| 47 | + ...jest.requireActual('../../chronos_npm_package/controllers/mongo'), |
| 48 | + addMetrics: jest.fn(), |
| 49 | + getSavedMetricsLength: jest.fn(), |
| 50 | +})); |
| 51 | + |
| 52 | +const HealthModel = { |
| 53 | + insertMany: jest.fn(() => Promise.resolve()), |
| 54 | +}; |
| 55 | + |
| 56 | +const HealthModelFunc = jest.fn(() => HealthModel); |
| 57 | + |
14 | 58 | describe('mongo.connect', () => {
|
15 | 59 | beforeEach(() => {
|
16 | 60 | jest.clearAllMocks();
|
17 | 61 | });
|
| 62 | + beforeEach(() => { |
| 63 | + jest.clearAllMocks(); |
| 64 | + }); |
18 | 65 |
|
19 | 66 | test('should connect to MongoDB database', async () => {
|
20 |
| - const databaseURI = 'mongodb://localhost:27017/testdb'; |
21 |
| - await mongo.connect({ databaseURI }); |
22 |
| - expect(mongoose.connect).toHaveBeenCalledWith(databaseURI); |
| 67 | + await mongo.connect({ database: { URI: db } }); |
| 68 | + |
| 69 | + //expect(mongoose.connect).toHaveBeenCalledWith(db); |
23 | 70 | expect(console.log).toHaveBeenCalledWith(
|
24 | 71 | expect.stringContaining('MongoDB database connected at')
|
25 | 72 | );
|
26 | 73 | });
|
27 | 74 |
|
28 |
| - test('should handle connection error', async () => { |
29 |
| - const errorMessage = 'Connection failed'; |
30 |
| - const databaseURI = 'mongodb://localhost:27017/testdb'; |
| 75 | + test('should handle connection errors', async () => { |
| 76 | + const testDb = 'test'; |
| 77 | + await mongo.connect({ database: { URI: testDb } }); |
31 | 78 |
|
32 |
| - jest.spyOn(mongoose, 'connect').mockRejectedValueOnce(new Error(errorMessage)); |
33 |
| - |
34 |
| - await mongo.connect({ databaseURI }); |
35 |
| - expect(mongoose.connect).toHaveBeenCalledWith(databaseURI); |
36 |
| - expect(console.log).toHaveBeenCalledWith(expect.stringContaining(errorMessage)); |
| 79 | + expect(console.log).toHaveBeenCalledWith( |
| 80 | + expect.stringContaining('Error connecting to MongoDB:'), |
| 81 | + expect.any(String) |
| 82 | + ); |
37 | 83 | });
|
38 | 84 | });
|
39 | 85 |
|
40 | 86 | describe('mongo.services', () => {
|
41 | 87 | beforeEach(() => {
|
42 | 88 | jest.clearAllMocks();
|
43 | 89 | });
|
| 90 | + beforeEach(() => { |
| 91 | + jest.clearAllMocks(); |
| 92 | + }); |
| 93 | + |
| 94 | + afterEach(async () => { |
| 95 | + await dropCollections(); |
| 96 | + }); |
| 97 | + |
| 98 | + test('should create a new document', async () => { |
| 99 | + await mongo.services({ microservice: 'test2', interval: 'test2' }); |
| 100 | + const savedService = await ServicesModel.findOne({ microservice: 'test2', interval: 'test2' }); |
| 101 | + expect(savedService).toBeDefined(); // The document should be defined if it exists |
| 102 | + }); |
| 103 | +}); |
| 104 | + |
| 105 | +describe('mongo.communications', () => { |
| 106 | + afterEach(async () => { |
| 107 | + await dropCollections(); |
| 108 | + }); |
| 109 | + |
| 110 | + test('should record request cycle and save communication to the database', async () => { |
| 111 | + const req = {}; |
| 112 | + const res = { |
| 113 | + statusCode: 200, |
| 114 | + statusMessage: 'OK', |
| 115 | + getHeaders: () => ({ 'x-correlation-id': 'correlation-id-123' }), |
| 116 | + on: jest.fn((event, callback) => { |
| 117 | + if (event === 'finish') { |
| 118 | + callback(); |
| 119 | + } |
| 120 | + }), |
| 121 | + }; |
| 122 | + const middleware = mongo.communications({ microservice: 'test3', slack: null, email: null }); |
| 123 | + await middleware(req, res, () => {}); |
| 124 | + const savedCommunication = await CommunicationModel.findOne({ microservice: 'test3' }); |
| 125 | + expect(savedCommunication).toBeDefined(); // The document should be defined if it exists |
| 126 | + }); |
| 127 | + |
| 128 | + test('should send an alert', async () => { |
| 129 | + const req = {}; |
| 130 | + const res = { |
| 131 | + statusCode: 400, |
| 132 | + statusMessage: 'Not Found', |
| 133 | + getHeaders: () => ({ 'x-correlation-id': 'correlation-id-123' }), |
| 134 | + on: jest.fn((event, callback) => { |
| 135 | + if (event === 'finish') { |
| 136 | + callback(); |
| 137 | + } |
| 138 | + }), |
| 139 | + }; |
| 140 | + const middleware = mongo.communications({ |
| 141 | + microservice: 'test4', |
| 142 | + slack: 'slackTest', |
| 143 | + email: 'emailTest', |
| 144 | + }); |
| 145 | + await middleware(req, res, () => {}); |
| 146 | + expect(alert.sendSlack).toHaveBeenCalledTimes(1); |
| 147 | + expect(alert.sendEmail).toHaveBeenCalledTimes(1); |
| 148 | + expect(alert.sendSlack).toHaveBeenCalledWith(res.statusCode, res.statusMessage, 'slackTest'); |
| 149 | + expect(alert.sendEmail).toHaveBeenCalledWith(res.statusCode, res.statusMessage, 'emailTest'); |
| 150 | + }); |
| 151 | +}); |
| 152 | + |
| 153 | +describe('mongo.health', () => { |
| 154 | + beforeEach(() => { |
| 155 | + jest.clearAllMocks(); |
| 156 | + jest.useFakeTimers(); |
| 157 | + }); |
44 | 158 |
|
45 |
| - test('should create a new document', async () => {}); |
| 159 | + afterEach(() => { |
| 160 | + jest.clearAllTimers(); |
| 161 | + }); |
| 162 | + |
| 163 | + test('should collect data after the set interval', async () => { |
| 164 | + await mongo.health({ microservice: 'mongo.health test', interval: 1000, mode: 'testMode' }); |
| 165 | + jest.advanceTimersByTime(1000); |
| 166 | + expect(setInterval).toHaveBeenCalledWith(expect.any(Function), 1000); |
| 167 | + expect(collectHealthData).toHaveBeenCalled(); |
| 168 | + }); |
| 169 | + |
| 170 | + test('should save metrics to database', async () => { |
| 171 | + const mockData = [ |
| 172 | + { |
| 173 | + time: Date.now(), |
| 174 | + metric: 'testMetric', |
| 175 | + value: 10, |
| 176 | + category: 'testCategory', |
| 177 | + }, |
| 178 | + { |
| 179 | + time: Date.now(), |
| 180 | + metric: 'testMetric2', |
| 181 | + value: 12, |
| 182 | + category: 'testCategory2', |
| 183 | + }, |
| 184 | + ]; |
| 185 | + await mongo.health({ microservice: 'mongo.health test', interval: 1000, mode: 'testMode' }); |
| 186 | + jest.advanceTimersByTime(1000); |
| 187 | + expect(collectHealthData).toHaveReturnedWith(mockData); |
| 188 | + expect(HealthModelFunc).toHaveBeenCalled(); |
| 189 | + expect(HealthModel).toHaveBeenCalled(); |
| 190 | + }); |
46 | 191 | });
|
| 192 | + |
| 193 | +describe('mongo.docker', () => { |
| 194 | + beforeEach(async () => { |
| 195 | + jest.clearAllMocks(); |
| 196 | + jest.useFakeTimers(); |
| 197 | + }); |
| 198 | + |
| 199 | + afterEach(async () => { |
| 200 | + await dropCollections(); |
| 201 | + jest.clearAllTimers(); |
| 202 | + }); |
| 203 | + |
| 204 | + test('should collect docker container information', async () => { |
| 205 | + const microservice = 'mongo.docker test'; |
| 206 | + const mockContainerData = { |
| 207 | + containername: microservice, |
| 208 | + containerId: '234', |
| 209 | + platform: 'testPlatform', |
| 210 | + startime: Date.now(), |
| 211 | + }; |
| 212 | + const mockReadDockerContainerData = { |
| 213 | + ...mockContainerData, |
| 214 | + memoryusage: 234, |
| 215 | + memorylimit: 234, |
| 216 | + memorypercent: 32, |
| 217 | + cpupercent: 45, |
| 218 | + networkreceived: 345, |
| 219 | + networksent: 345, |
| 220 | + processcount: 343, |
| 221 | + restartcount: 12, |
| 222 | + time: Date.now(), |
| 223 | + }; |
| 224 | + |
| 225 | + jest.mock('../../chronos_npm_package/controllers/dockerHelper', () => ({ |
| 226 | + getDockerContainer: jest.fn(() => Promise.resolve(mockContainerData)), |
| 227 | + readDockerContainer: jest.fn(() => Promise.resolve(mockReadDockerContainerData)), |
| 228 | + })); |
| 229 | + |
| 230 | + await mongo.docker({ microservice: microservice, interval: 1000, mode: 'testMode' }); |
| 231 | + expect(getDockerContainer).toHaveBeenCalledWith(microservice); |
| 232 | + jest.advanceTimersByTime(1000); |
| 233 | + expect(readDockerContainer).toHaveBeenCalledWith(mockContainerData); |
| 234 | + const savedContainerInfo = await ContainerInfo.findOne({ containername: microservice }); |
| 235 | + expect(savedContainerInfo).toBeDefined(); |
| 236 | + expect(savedContainerInfo).toMatchObject(mockReadDockerContainerData); |
| 237 | + }); |
| 238 | +}); |
| 239 | + |
0 commit comments