Skip to content

Commit

Permalink
[ResponseOps][Alerting] Unable to upgrade to 8.4.0 when I have snooze…
Browse files Browse the repository at this point in the history
…d rules (#139427)

* Adding migration

* Removing timer

* Adding functional test

* Adding another test
  • Loading branch information
doakalexi committed Aug 25, 2022
1 parent dacaab8 commit 8991e04
Show file tree
Hide file tree
Showing 5 changed files with 106 additions and 0 deletions.
27 changes: 27 additions & 0 deletions x-pack/plugins/alerting/server/saved_objects/migrations.test.ts
Expand Up @@ -2397,6 +2397,33 @@ describe('successful migrations', () => {
});
});

describe('8.4.1', () => {
test('removes isSnoozedUntil', () => {
const migration841 = getMigrations(encryptedSavedObjectsSetup, {}, isPreconfigured)[
'8.4.1'
];
const mutedAlert = getMockData(
{
isSnoozedUntil: '1970-01-02T00:00:00.000Z',
},
true
);
expect(mutedAlert.attributes.isSnoozedUntil).toBeTruthy();
const migratedAlert841 = migration841(mutedAlert, migrationContext);

expect(migratedAlert841.attributes.isSnoozedUntil).toBeFalsy();
});

test('works as expected if isSnoozedUntil is not populated', () => {
const migration841 = getMigrations(encryptedSavedObjectsSetup, {}, isPreconfigured)[
'8.4.1'
];
const mutedAlert = getMockData({}, true);
expect(mutedAlert.attributes.isSnoozedUntil).toBeFalsy();
expect(() => migration841(mutedAlert, migrationContext)).not.toThrowError();
});
});

describe('Metrics Inventory Threshold rule', () => {
test('Migrates incorrect action group spelling', () => {
const migration800 = getMigrations(encryptedSavedObjectsSetup, {}, isPreconfigured)[
Expand Down
18 changes: 18 additions & 0 deletions x-pack/plugins/alerting/server/saved_objects/migrations.ts
Expand Up @@ -170,6 +170,12 @@ export function getMigrations(
pipeMigrations(addSearchType, removeInternalTags, convertSnoozes)
);

const migrationRules841 = createEsoMigration(
encryptedSavedObjects,
(doc: SavedObjectUnsanitizedDoc<RawRule>): doc is SavedObjectUnsanitizedDoc<RawRule> => true,
pipeMigrations(removeIsSnoozedUntil)
);

const migrationRules850 = createEsoMigration(
encryptedSavedObjects,
(doc): doc is SavedObjectUnsanitizedDoc<RawRule> => isEsQueryRuleType(doc),
Expand All @@ -189,6 +195,7 @@ export function getMigrations(
'8.0.1': executeMigrationWithErrorHandling(migrationRules801, '8.0.1'),
'8.2.0': executeMigrationWithErrorHandling(migrationRules820, '8.2.0'),
'8.3.0': executeMigrationWithErrorHandling(migrationRules830, '8.3.0'),
'8.4.1': executeMigrationWithErrorHandling(migrationRules841, '8.4.1'),
'8.5.0': executeMigrationWithErrorHandling(migrationRules850, '8.5.0'),
},
getSearchSourceMigrations(encryptedSavedObjects, searchSourceMigrations)
Expand Down Expand Up @@ -1062,3 +1069,14 @@ function getSearchSourceMigrations(
}
return filteredMigrations;
}

function removeIsSnoozedUntil(
doc: SavedObjectUnsanitizedDoc<RawRule>
): SavedObjectUnsanitizedDoc<RawRule> {
return {
...doc,
attributes: {
...(omit(doc.attributes, ['isSnoozedUntil']) as RawRule),
},
};
}
Expand Up @@ -444,6 +444,26 @@ export default function createGetTests({ getService }: FtrProviderContext) {
expect(response.body._source?.alert?.tags).to.eql(['test-tag-1', 'foo-tag']);
});

it('8.4.1 removes IsSnoozedUntil', async () => {
const searchResult = await es.search<RawRule>(
{
index: '.kibana',
body: {
query: {
term: {
_id: 'alert:4d973df0-23df-11ed-8ae4-e988ad0f6fa7',
},
},
},
},
{ meta: true }
);

expect(searchResult.statusCode).to.equal(200);
const hit = searchResult.body.hits.hits[0];
expect((hit!._source!.alert! as RawRule).isSnoozedUntil).to.be(undefined);
});

it('8.5.0 removes runtime and field params from older ES Query rules', async () => {
const response = await es.get<{
alert: {
Expand Down
38 changes: 38 additions & 0 deletions x-pack/test/functional/es_archives/alerts/data.json
Expand Up @@ -1194,3 +1194,41 @@
}
}
}

{
"type": "doc",
"value": {
"id": "alert:4d973df0-23df-11ed-8ae4-e988ad0f6fa7",
"index": ".kibana_1",
"source": {
"alert": {
"alertTypeId": "example.always-firing",
"apiKey": null,
"apiKeyOwner": null,
"consumer": "alerts",
"createdAt": "2022-08-24T19:02:30.889Z",
"createdBy": "elastic",
"enabled": false,
"muteAll": false,
"mutedInstanceIds": [],
"name": "remove snoozed",
"params": {},
"schedule": {
"interval": "1m"
},
"scheduledTaskId": null,
"tags": [],
"throttle": null,
"updatedBy": "elastic",
"isSnoozedUntil": "2022-08-24T19:05:49.817Z"
},
"migrationVersion": {
"alert": "8.3.0"
},
"references": [
],
"type": "alert",
"updated_at": "2022-08-24T19:05:50.159Z"
}
}
}
3 changes: 3 additions & 0 deletions x-pack/test/functional/es_archives/alerts/mappings.json
Expand Up @@ -172,6 +172,9 @@
},
"updatedBy": {
"type": "keyword"
},
"isSnoozedUntil": {
"type": "date"
}
}
},
Expand Down

0 comments on commit 8991e04

Please sign in to comment.