Skip to content

Commit

Permalink
Functional tests
Browse files Browse the repository at this point in the history
  • Loading branch information
ymao1 committed Nov 17, 2020
1 parent 33c41da commit b35af3c
Show file tree
Hide file tree
Showing 5 changed files with 94 additions and 1 deletion.
43 changes: 42 additions & 1 deletion x-pack/plugins/alerts/server/saved_objects/migrations.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -261,8 +261,48 @@ describe('7.10.0 migrates with failure', () => {
});
});

describe('7.11.0', () => {
beforeEach(() => {
jest.resetAllMocks();
encryptedSavedObjectsSetup.createMigration.mockImplementation(
(shouldMigrateWhenPredicate, migration) => migration
);
});

test('add updatedAt field to alert - set to SavedObject updated_at attribute', () => {
const migration711 = getMigrations(encryptedSavedObjectsSetup)['7.11.0'];
const alert = getMockData({}, true);
expect(migration711(alert, { log })).toEqual({
...alert,
attributes: {
...alert.attributes,
updatedAt: alert.updated_at,
},
});
});

test('add updatedAt field to alert - set to createdAt when SavedObject updated_at is not defined', () => {
const migration711 = getMigrations(encryptedSavedObjectsSetup)['7.11.0'];
const alert = getMockData({});
expect(migration711(alert, { log })).toEqual({
...alert,
attributes: {
...alert.attributes,
updatedAt: alert.attributes.createdAt,
},
});
});
});

function getUpdatedAt(): string {
const updatedAt = new Date();
updatedAt.setHours(updatedAt.getHours() + 2);
return updatedAt.toISOString();
}

function getMockData(
overwrites: Record<string, unknown> = {}
overwrites: Record<string, unknown> = {},
withSavedObjectUpdatedAt: boolean = false
): SavedObjectUnsanitizedDoc<Partial<RawAlert>> {
return {
attributes: {
Expand Down Expand Up @@ -295,6 +335,7 @@ function getMockData(
],
...overwrites,
},
updated_at: withSavedObjectUpdatedAt ? getUpdatedAt() : undefined,
id: uuid.v4(),
type: 'alert',
};
Expand Down
20 changes: 20 additions & 0 deletions x-pack/plugins/alerts/server/saved_objects/migrations.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,8 +37,15 @@ export function getMigrations(
)
);

const migrationAlertUpdatedAtDate = encryptedSavedObjects.createMigration<RawAlert, RawAlert>(
// migrate all documents in 7.11 in order to add the "updatedAt" field
(doc): doc is SavedObjectUnsanitizedDoc<RawAlert> => true,
pipeMigrations(setAlertUpdatedAtDate)
);

return {
'7.10.0': executeMigrationWithErrorHandling(migrationWhenRBACWasIntroduced, '7.10.0'),
'7.11.0': executeMigrationWithErrorHandling(migrationAlertUpdatedAtDate, '7.11.0'),
};
}

Expand All @@ -59,6 +66,19 @@ function executeMigrationWithErrorHandling(
};
}

const setAlertUpdatedAtDate = (
doc: SavedObjectUnsanitizedDoc<RawAlert>
): SavedObjectUnsanitizedDoc<RawAlert> => {
const updatedAt = doc.updated_at || doc.attributes.createdAt;
return {
...doc,
attributes: {
...doc.attributes,
updatedAt,
},
};
};

const consumersToChange: Map<string, string> = new Map(
Object.entries({
alerting: 'alerts',
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,7 @@ export default function createAlertTests({ getService }: FtrProviderContext) {
});
expect(Date.parse(response.body.createdAt)).to.be.greaterThan(0);
expect(Date.parse(response.body.updatedAt)).to.be.greaterThan(0);
expect(Date.parse(response.body.updatedAt)).to.eql(Date.parse(response.body.createdAt));

expect(typeof response.body.scheduledTaskId).to.be('string');
const { _source: taskRecord } = await getScheduledTask(response.body.scheduledTaskId);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -63,13 +63,15 @@ export default function executionStatusAlertTests({ getService }: FtrProviderCon
);
expect(response.status).to.eql(200);
const alertId = response.body.id;
const alertUpdatedAt = response.body.updatedAt;
dates.push(response.body.executionStatus.lastExecutionDate);
objectRemover.add(Spaces.space1.id, alertId, 'alert', 'alerts');

const executionStatus = await waitForStatus(alertId, new Set(['ok']));
dates.push(executionStatus.lastExecutionDate);
dates.push(Date.now());
ensureDatetimesAreOrdered(dates);
ensureAlertUpdatedAtHasNotChanged(alertId, alertUpdatedAt);

// Ensure AAD isn't broken
await checkAAD({
Expand Down Expand Up @@ -97,13 +99,15 @@ export default function executionStatusAlertTests({ getService }: FtrProviderCon
);
expect(response.status).to.eql(200);
const alertId = response.body.id;
const alertUpdatedAt = response.body.updatedAt;
dates.push(response.body.executionStatus.lastExecutionDate);
objectRemover.add(Spaces.space1.id, alertId, 'alert', 'alerts');

const executionStatus = await waitForStatus(alertId, new Set(['active']));
dates.push(executionStatus.lastExecutionDate);
dates.push(Date.now());
ensureDatetimesAreOrdered(dates);
ensureAlertUpdatedAtHasNotChanged(alertId, alertUpdatedAt);

// Ensure AAD isn't broken
await checkAAD({
Expand All @@ -128,13 +132,15 @@ export default function executionStatusAlertTests({ getService }: FtrProviderCon
);
expect(response.status).to.eql(200);
const alertId = response.body.id;
const alertUpdatedAt = response.body.updatedAt;
dates.push(response.body.executionStatus.lastExecutionDate);
objectRemover.add(Spaces.space1.id, alertId, 'alert', 'alerts');

const executionStatus = await waitForStatus(alertId, new Set(['error']));
dates.push(executionStatus.lastExecutionDate);
dates.push(Date.now());
ensureDatetimesAreOrdered(dates);
ensureAlertUpdatedAtHasNotChanged(alertId, alertUpdatedAt);

// Ensure AAD isn't broken
await checkAAD({
Expand Down Expand Up @@ -162,12 +168,14 @@ export default function executionStatusAlertTests({ getService }: FtrProviderCon
);
expect(response.status).to.eql(200);
const alertId = response.body.id;
const alertUpdatedAt = response.body.updatedAt;
objectRemover.add(Spaces.space1.id, alertId, 'alert', 'alerts');

const executionStatus = await waitForStatus(alertId, new Set(['error']));
expect(executionStatus.error).to.be.ok();
expect(executionStatus.error.reason).to.be('execute');
expect(executionStatus.error.message).to.be('this alert is intended to fail');
ensureAlertUpdatedAtHasNotChanged(alertId, alertUpdatedAt);
});

it('should eventually have error reason "unknown" when appropriate', async () => {
Expand All @@ -183,6 +191,7 @@ export default function executionStatusAlertTests({ getService }: FtrProviderCon
);
expect(response.status).to.eql(200);
const alertId = response.body.id;
const alertUpdatedAt = response.body.updatedAt;
objectRemover.add(Spaces.space1.id, alertId, 'alert', 'alerts');

let executionStatus = await waitForStatus(alertId, new Set(['ok']));
Expand All @@ -201,6 +210,7 @@ export default function executionStatusAlertTests({ getService }: FtrProviderCon
executionStatus = await waitForStatus(alertId, new Set(['error']));
expect(executionStatus.error).to.be.ok();
expect(executionStatus.error.reason).to.be('unknown');
ensureAlertUpdatedAtHasNotChanged(alertId, alertUpdatedAt);

const message = 'params invalid: [param1]: expected value of type [string] but got [number]';
expect(executionStatus.error.message).to.be(message);
Expand Down Expand Up @@ -306,6 +316,18 @@ export default function executionStatusAlertTests({ getService }: FtrProviderCon
await delay(WaitForStatusIncrement);
return await waitForStatus(id, statuses, waitMillis - WaitForStatusIncrement);
}

async function ensureAlertUpdatedAtHasNotChanged(alertId: string, originalUpdatedAt: string) {
const response = await supertest.get(
`${getUrlPrefix(Spaces.space1.id)}/api/alerts/alert/${alertId}`
);
const { updatedAt, executionStatus } = response.body;
expect(Date.parse(updatedAt)).to.be.greaterThan(0);
expect(Date.parse(updatedAt)).to.eql(Date.parse(originalUpdatedAt));
expect(Date.parse(executionStatus.lastExecutionDate)).to.be.greaterThan(
Date.parse(originalUpdatedAt)
);
}
}

function expectErrorExecutionStatus(executionStatus: Record<string, any>, startDate: number) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -82,5 +82,14 @@ export default function createGetTests({ getService }: FtrProviderContext) {
},
]);
});

it('7.11.0 migrates alerts to contain `updatedAt` field', async () => {
const response = await supertest.get(
`${getUrlPrefix(``)}/api/alerts/alert/74f3e6d7-b7bb-477d-ac28-92ee22728e6e`
);

expect(response.status).to.eql(200);
expect(response.body.updatedAt).to.eql('2020-06-17T15:35:39.839Z');
});
});
}

0 comments on commit b35af3c

Please sign in to comment.