Full codebase including API server (32 modules + routes), dashboard frontend, DashCA certificate distribution, installer script, and deployment skills.
99 lines
3.4 KiB
JavaScript
99 lines
3.4 KiB
JavaScript
/**
|
|
* Monitoring Route Tests
|
|
*
|
|
* Tests resource monitoring endpoints and legacy container stats endpoints.
|
|
* Note: GET /api/stats/containers requires a live Docker connection, so in the
|
|
* test environment it will return 500 (Docker unavailable). We assert both
|
|
* the happy path (200) and the expected failure (500) to keep the test green
|
|
* regardless of whether Docker is running.
|
|
*/
|
|
|
|
const request = require('supertest');
|
|
const fs = require('fs');
|
|
const path = require('path');
|
|
const os = require('os');
|
|
|
|
const testServicesFile = path.join(os.tmpdir(), `monitoring-services-${Date.now()}.json`);
|
|
const testConfigFile = path.join(os.tmpdir(), `monitoring-config-${Date.now()}.json`);
|
|
|
|
process.env.SERVICES_FILE = testServicesFile;
|
|
process.env.CONFIG_FILE = testConfigFile;
|
|
process.env.ENABLE_HEALTH_CHECKER = 'false';
|
|
process.env.NODE_ENV = 'test';
|
|
|
|
fs.writeFileSync(testServicesFile, '[]', 'utf8');
|
|
fs.writeFileSync(testConfigFile, '{}', 'utf8');
|
|
|
|
const app = require('../server');
|
|
|
|
describe('Monitoring Routes', () => {
|
|
afterAll(() => {
|
|
try { fs.unlinkSync(testServicesFile); } catch (e) { /* ignore */ }
|
|
try { fs.unlinkSync(testConfigFile); } catch (e) { /* ignore */ }
|
|
});
|
|
|
|
describe('GET /api/monitoring/stats', () => {
|
|
test('should return 200 with stats data', async () => {
|
|
const res = await request(app).get('/api/monitoring/stats');
|
|
|
|
expect(res.statusCode).toBe(200);
|
|
expect(res.body.success).toBe(true);
|
|
expect(res.body).toHaveProperty('stats');
|
|
});
|
|
});
|
|
|
|
describe('GET /api/monitoring/stats/:containerId', () => {
|
|
test('should return 404 for non-existent container', async () => {
|
|
const res = await request(app).get('/api/monitoring/stats/nonexistent-container');
|
|
|
|
expect(res.statusCode).toBe(404);
|
|
});
|
|
});
|
|
|
|
describe('GET /api/monitoring/history/:containerId', () => {
|
|
test('should return 200 with history array for any container ID', async () => {
|
|
const res = await request(app).get('/api/monitoring/history/some-container');
|
|
|
|
expect(res.statusCode).toBe(200);
|
|
expect(res.body.success).toBe(true);
|
|
expect(res.body).toHaveProperty('history');
|
|
expect(res.body).toHaveProperty('hours');
|
|
});
|
|
|
|
test('should accept hours query parameter', async () => {
|
|
const res = await request(app)
|
|
.get('/api/monitoring/history/some-container')
|
|
.query({ hours: 6 });
|
|
|
|
expect(res.statusCode).toBe(200);
|
|
expect(res.body.hours).toBe(6);
|
|
});
|
|
});
|
|
|
|
describe('GET /api/monitoring/alerts/:containerId', () => {
|
|
test('should return 200 with alert config (empty for unknown container)', async () => {
|
|
const res = await request(app).get('/api/monitoring/alerts/unknown-container');
|
|
|
|
expect(res.statusCode).toBe(200);
|
|
expect(res.body.success).toBe(true);
|
|
expect(res.body).toHaveProperty('config');
|
|
});
|
|
});
|
|
|
|
describe('GET /api/stats/containers', () => {
|
|
test('should return 200 with containers array or 500 if Docker unavailable', async () => {
|
|
const res = await request(app).get('/api/stats/containers');
|
|
|
|
// In test environment Docker may not be available
|
|
expect([200, 500]).toContain(res.statusCode);
|
|
|
|
if (res.statusCode === 200) {
|
|
expect(res.body.success).toBe(true);
|
|
expect(res.body).toHaveProperty('stats');
|
|
expect(Array.isArray(res.body.stats)).toBe(true);
|
|
expect(res.body).toHaveProperty('timestamp');
|
|
}
|
|
});
|
|
});
|
|
});
|