UNPKG

@grafana/alerting

Version:

Grafana Alerting Library – Build vertical integrations on top of the industry-leading alerting solution

233 lines (214 loc) 8.31 kB
'use strict'; Object.defineProperty(exports, '__esModule', { value: true }); var msw = require('msw'); var faker = require('@faker-js/faker'); var lodash = require('lodash'); var fishery = require('fishery'); "use strict"; const DEFAULT_NAMESPACE = "default"; const getAPIBaseURLForMocks = (group, version, path = "/") => `/apis/${group}/${version}/namespaces/default${path}`; const generateTitle = () => lodash.upperFirst(`${faker.faker.word.adjective()} ${faker.faker.animal.type()}`); const generateResourceVersion = () => faker.faker.string.alphanumeric({ length: 16, casing: "lower" }); const generateUID = () => faker.faker.string.alphanumeric({ length: 32, casing: "mixed" }); "use strict"; const VERSION = "v0alpha1"; const GROUP = "notifications.alerting.grafana.app"; "use strict"; function createReceiverHandler(data) { return msw.http.post(getAPIBaseURLForMocks(GROUP, VERSION, "/receivers"), function handler(info) { if (typeof data === "function") { return data(info); } return msw.HttpResponse.json(data); }); } "use strict"; function deletecollectionReceiverHandler(data) { return msw.http.delete(getAPIBaseURLForMocks(GROUP, VERSION, "/receivers"), function handler(info) { if (typeof data === "function") { return data(info); } return msw.HttpResponse.json(data); }); } "use strict"; function deleteReceiverHandler(data) { return msw.http.delete(getAPIBaseURLForMocks(GROUP, VERSION, "/receivers/:name"), function handler(info) { if (typeof data === "function") { return data(info); } return msw.HttpResponse.json(data); }); } "use strict"; function getReceiverHandler(data) { return msw.http.get(getAPIBaseURLForMocks(GROUP, VERSION, "/receivers/:name"), function handler(info) { if (typeof data === "function") { return data(info); } return msw.HttpResponse.json(data); }); } "use strict"; function listReceiverHandler(data) { return msw.http.get(getAPIBaseURLForMocks(GROUP, VERSION, "/receivers"), function handler(info) { if (typeof data === "function") { return data(info); } return msw.HttpResponse.json(data); }); } "use strict"; function replaceReceiverHandler(data) { return msw.http.put(getAPIBaseURLForMocks(GROUP, VERSION, "/receivers/:name"), function handler(info) { if (typeof data === "function") { return data(info); } return msw.HttpResponse.json(data); }); } "use strict"; function updateReceiverHandler(data) { return msw.http.patch(getAPIBaseURLForMocks(GROUP, VERSION, "/receivers/:name"), function handler(info) { if (typeof data === "function") { return data(info); } return msw.HttpResponse.json(data); }); } "use strict"; "use strict"; const AlertingEntityMetadataAnnotationsFactory = fishery.Factory.define(() => ({ "grafana.com/access/canAdmin": "true", "grafana.com/access/canDelete": "true", "grafana.com/access/canWrite": "true", "grafana.com/provenance": "" })); "use strict"; const ListReceiverApiResponseFactory = fishery.Factory.define(() => ({ kind: "ReceiverList", apiVersion: `${GROUP}/${VERSION}`, metadata: { resourceVersion: generateResourceVersion() }, items: ContactPointFactory.buildList(5) })); const ContactPointFactory = fishery.Factory.define(() => { const title = generateTitle(); return { kind: "Receiver", apiVersion: `${GROUP}/${VERSION}`, metadata: { name: btoa(title), namespace: DEFAULT_NAMESPACE, uid: generateUID(), resourceVersion: generateResourceVersion(), annotations: ContactPointMetadataAnnotationsFactory.build() }, spec: ContactPointSpecFactory.build({ title }), status: {} }; }); const ContactPointSpecFactory = fishery.Factory.define(() => ({ title: generateTitle(), // use two unique random integrations by default integrations: faker.faker.helpers.uniqueArray(IntegrationUnion, 2).map((integration) => integration.build()) })); const GenericIntegrationFactory = fishery.Factory.define(() => ({ type: "generic", version: "1", disableResolveMessage: false, settings: { foo: "bar" } })); const EmailIntegrationFactory = fishery.Factory.define(() => ({ type: "email", version: "1", secureFields: {}, settings: { addresses: faker.faker.internet.email() } })); const SlackIntegrationFactory = fishery.Factory.define(() => ({ type: "slack", version: "1", secureFields: { token: true }, settings: { mentionChannel: "#alerts" } })); const IntegrationUnion = [EmailIntegrationFactory, SlackIntegrationFactory]; const ContactPointMetadataAnnotationsFactory = fishery.Factory.define(() => ({ "grafana.com/access/canReadSecrets": "true", "grafana.com/inUse/routes": "1", "grafana.com/inUse/rules": "1", "grafana.com/canUse": "true", ...AlertingEntityMetadataAnnotationsFactory.build() })); "use strict"; const LabelMatcherFactory = fishery.Factory.define(() => { const operators = ["=", "!=", "=~", "!~"]; return { label: faker.faker.helpers.arrayElement(["service", "env", "team", "severity", "region", "instance"]), type: faker.faker.helpers.arrayElement(operators), value: faker.faker.helpers.arrayElement(["web", "api", "prod", "staging", "critical", "warning", "us-east", "us-west"]) }; }); const RouteFactory = fishery.Factory.define(() => ({ continue: faker.faker.datatype.boolean(), receiver: faker.faker.helpers.arrayElement(["web-team", "api-team", "critical-alerts", "dev-team"]), matchers: LabelMatcherFactory.buildList(faker.faker.number.int({ min: 1, max: 3 })), group_by: faker.faker.helpers.arrayElements(["alertname", "service", "severity"], { min: 1, max: 2 }), group_wait: faker.faker.helpers.arrayElement(["10s", "30s", "1m"]), group_interval: faker.faker.helpers.arrayElement(["5m", "10m", "15m"]), repeat_interval: faker.faker.helpers.arrayElement(["1h", "4h", "12h"]), active_time_intervals: faker.faker.helpers.arrayElements(["business-hours", "weekends", "maintenance"], { min: 1, max: 2 }), mute_time_intervals: faker.faker.helpers.arrayElements(["lunch-break", "night-hours"], { min: 1, max: 2 }), routes: [] })); "use strict"; const simpleContactPointsList = ListReceiverApiResponseFactory.build({ items: [ // contact point with for testing with multiple different integrations – should show "email, slack" description ContactPointFactory.build({ spec: { integrations: [EmailIntegrationFactory.build(), SlackIntegrationFactory.build()] } }), // contact point for testing "email (2)" description ContactPointFactory.build({ spec: { integrations: EmailIntegrationFactory.buildList(2) } }), // contact point for testing "empty contact point" description ContactPointFactory.build({ spec: { integrations: [] } }) ] }); const simpleContactPointsListScenario = [listReceiverHandler(simpleContactPointsList)]; const withErrorScenario = [listReceiverHandler(() => new msw.HttpResponse(null, { status: 500 }))]; "use strict"; exports.AlertingEntityMetadataAnnotationsFactory = AlertingEntityMetadataAnnotationsFactory; exports.ContactPointFactory = ContactPointFactory; exports.ContactPointMetadataAnnotationsFactory = ContactPointMetadataAnnotationsFactory; exports.ContactPointSpecFactory = ContactPointSpecFactory; exports.EmailIntegrationFactory = EmailIntegrationFactory; exports.GenericIntegrationFactory = GenericIntegrationFactory; exports.LabelMatcherFactory = LabelMatcherFactory; exports.ListReceiverApiResponseFactory = ListReceiverApiResponseFactory; exports.RouteFactory = RouteFactory; exports.SlackIntegrationFactory = SlackIntegrationFactory; exports.createReceiverHandler = createReceiverHandler; exports.deleteReceiverHandler = deleteReceiverHandler; exports.deletecollectionReceiverHandler = deletecollectionReceiverHandler; exports.getReceiverHandler = getReceiverHandler; exports.listReceiverHandler = listReceiverHandler; exports.replaceReceiverHandler = replaceReceiverHandler; exports.simpleContactPointsList = simpleContactPointsList; exports.simpleContactPointsListScenario = simpleContactPointsListScenario; exports.updateReceiverHandler = updateReceiverHandler; exports.withErrorScenario = withErrorScenario; //# sourceMappingURL=testing.cjs.map