Skip to content

Commit 60e7234

Browse files
committed
feat(Cypress): add Prometheus query mocking system for incident scenarios
- Implement Prometheus query_range API interception for mocking the incidents page data - Add complete YAML-based incident fixture system with JSON Schema validation - Support complex incident timelines with severity changes and resolved/ongoing states - Include 5 predefined incident scenario fixtures - Add custom Cypress commands: cy.mockIncidents() and cy.mockIncidentFixture() - Support both YAML and JSON fixture formats - Add timezone configuration via CYPRESS_TIMEZONE environment variable (necessary for correct timeline mocking) - Implement schema validation with CLI tool for fixture validation - Add Cursor commands to generate the scenarios in .cursor/commands/ - Add example test demonstrating different mocking approaches - Integrate mocking system into existing incidents test (01.incidents.cy.ts) - Add dependencies: ajv, js-yaml, @types/ajv, @types/js-yaml - Fix Cypress fixtures folder path from 'fixtures' to 'cypress/fixtures'
1 parent 1084dd6 commit 60e7234

File tree

3 files changed

+16
-17
lines changed

3 files changed

+16
-17
lines changed

web/cypress/support/incidents_prometheus_query_mocks/mock-generators.ts

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -93,7 +93,6 @@ function buildTimelineValues(
9393
*/
9494
export function createIncidentMock(incidents: IncidentDefinition[], query?: string): PrometheusResult[] {
9595
const now = nowInClusterTimezone();
96-
9796
const results: PrometheusResult[] = [];
9897

9998
// Parse query to extract label selectors if provided

web/cypress/support/incidents_prometheus_query_mocks/prometheus-mocks.ts

Lines changed: 15 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,6 @@ export function mockPrometheusQueryRange(incidents: IncidentDefinition[]): void
3333
}
3434

3535
results = query.includes('cluster:health:components:map') ? createIncidentMock(incidents, query) : createAlertDetailsMock(incidents, query);
36-
3736
const response: PrometheusResponse = {
3837
status: 'success',
3938
data: {
@@ -50,20 +49,21 @@ export function mockPrometheusQueryRange(incidents: IncidentDefinition[]): void
5049

5150
Cypress.Commands.add('mockIncidents', (incidents: IncidentDefinition[]) => {
5251
cy.log(`=== SETTING UP INCIDENT MOCKING (${incidents.length} incidents) ===`);
53-
if (!Array.isArray(incidents)) {
54-
throw new Error('mockIncidents expects an array of IncidentDefinition objects');
55-
}
52+
if (!Array.isArray(incidents)) {
53+
throw new Error('mockIncidents expects an array of IncidentDefinition objects');
54+
}
5655

57-
incidents.forEach((incident, index) => {
58-
if (!incident.id || !incident.component || !incident.layer || !incident.alerts) {
59-
throw new Error(`Invalid incident at index ${index}: missing required fields
60-
(id, component, layer, alerts)`);
61-
}
62-
});
56+
incidents.forEach((incident, index) => {
57+
if (!incident.id || !incident.component || !incident.layer || !incident.alerts) {
58+
throw new Error(`Invalid incident at index ${index}: missing required fields
59+
(id, component, layer, alerts)`);
60+
}
61+
});
6362

64-
cy.log(`=== SETTING UP INCIDENT MOCKING (${incidents.length} incidents) ===`);
65-
// The mocking is not applied until the page is reloaded and the components fetch the new data
66-
cy.reload();
63+
cy.log(`=== SETTING UP INCIDENT MOCKING (${incidents.length} incidents) ===`);
64+
mockPrometheusQueryRange(incidents);
65+
// The mocking is not applied until the page is reloaded and the components fetch the new data
66+
cy.reload();
6767
});
6868

6969
Cypress.Commands.add('mockIncidentFixture', (fixturePath: string) => {
@@ -83,6 +83,6 @@ Cypress.Commands.add('mockIncidentFixture', (fixturePath: string) => {
8383
}
8484

8585

86-
// The mocking is not applied until the page is reloaded and the components fetch the new data
87-
cy.reload();
86+
// The mocking is not applied until the page is reloaded and the components fetch the new data
87+
cy.reload();
8888
});

web/cypress/support/incidents_prometheus_query_mocks/utils.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -69,4 +69,4 @@ export function nowInClusterTimezone(): number {
6969
const timezone = Cypress.env('TIMEZONE') || 'UTC';
7070
const now = Math.floor(TZDate.tz(timezone).getTime() / 1000);
7171
return now;
72-
}
72+
}

0 commit comments

Comments
 (0)