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 (elastic#139427) (elastic#139486)

* Adding migration

* Removing timer

* Adding functional test

* Adding another test

(cherry picked from commit 8991e04)

# Conflicts:
#	x-pack/plugins/alerting/server/saved_objects/migrations.ts
#	x-pack/test/alerting_api_integration/spaces_only/tests/alerting/migrations.ts
#	x-pack/test/functional/es_archives/alerts/data.json
  • Loading branch information
doakalexi authored Aug 25, 2022
1 parent 3c025a6 commit 711ff3a
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
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
Expand Up @@ -169,6 +169,12 @@ export function getMigrations(
pipeMigrations(addSearchType, removeInternalTags, convertSnoozes)
);

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

return mergeSavedObjectMigrationMaps(
{
'7.10.0': executeMigrationWithErrorHandling(migrationWhenRBACWasIntroduced, '7.10.0'),
Expand All @@ -182,6 +188,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'),
},
getSearchSourceMigrations(encryptedSavedObjects, searchSourceMigrations)
);
Expand Down Expand Up @@ -1013,3 +1020,14 @@ function getSearchSourceMigrations(
}
return filteredMigrations;
}

function removeIsSnoozedUntil(
doc: SavedObjectUnsanitizedDoc<RawRule>
): SavedObjectUnsanitizedDoc<RawRule> {
return {
...doc,
attributes: {
...(omit(doc.attributes, ['isSnoozedUntil']) as RawRule),
},
};
}
Original file line number Diff line number Diff line change
Expand Up @@ -443,5 +443,25 @@ export default function createGetTests({ getService }: FtrProviderContext) {
expect(response.statusCode).to.equal(200);
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);
});
});
}
38 changes: 38 additions & 0 deletions x-pack/test/functional/es_archives/alerts/data.json
Original file line number Diff line number Diff line change
Expand Up @@ -1041,3 +1041,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
Original file line number Diff line number Diff line change
Expand Up @@ -172,6 +172,9 @@
},
"updatedBy": {
"type": "keyword"
},
"isSnoozedUntil": {
"type": "date"
}
}
},
Expand Down

0 comments on commit 711ff3a

Please sign in to comment.