|
| 1 | +import { expect } from '@playwright/test'; |
| 2 | +import yaml from 'js-yaml'; |
| 3 | + |
| 4 | +import BasePage from './base-page'; |
| 5 | + |
| 6 | +type AlertmanagerConfig = { |
| 7 | + global?: Record<string, any>; |
| 8 | + receivers?: AlertmanagerReceiver[]; |
| 9 | + route?: any; |
| 10 | + inhibit_rules?: any[]; |
| 11 | +}; |
| 12 | + |
| 13 | +type AlertmanagerReceiver = { |
| 14 | + name: string; |
| 15 | + [key: string]: any; |
| 16 | +}; |
| 17 | + |
| 18 | +export class AlertmanagerPage extends BasePage { |
| 19 | + private readonly createReceiverButton = this.page.getByTestId('create-receiver'); |
| 20 | + private readonly receiverNameInput = this.page.getByTestId('receiver-name'); |
| 21 | + private readonly receiverTypeDropdown = this.page.getByTestId('receiver-type'); |
| 22 | + private readonly saveChangesButton = this.page.getByTestId('save-changes'); |
| 23 | + private readonly advancedConfigButton = this.page.getByTestId('advanced-configuration'); |
| 24 | + |
| 25 | + async navigateToAlertmanager(): Promise<void> { |
| 26 | + await this.goTo('/settings/cluster/alertmanagerconfig'); |
| 27 | + await this.createReceiverButton.waitFor({ state: 'visible' }); |
| 28 | + } |
| 29 | + |
| 30 | + async navigateToYAMLPage(): Promise<void> { |
| 31 | + await this.goTo('/settings/cluster/alertmanageryaml'); |
| 32 | + // Wait for editor toolbar to load (indicates editor is ready) |
| 33 | + await this.page.getByRole('button', { name: 'Copy code to clipboard' }).waitFor(); |
| 34 | + } |
| 35 | + |
| 36 | + async navigateToEditReceiver(receiverName: string): Promise<void> { |
| 37 | + await this.goTo(`/settings/cluster/alertmanagerconfig/receivers/${receiverName}/edit`); |
| 38 | + await this.saveChangesButton.waitFor({ state: 'visible' }); |
| 39 | + } |
| 40 | + |
| 41 | + async createReceiver(receiverName: string, receiverTypeConfig: string): Promise<void> { |
| 42 | + await this.robustClick(this.createReceiverButton); |
| 43 | + await this.receiverNameInput.fill(receiverName); |
| 44 | + |
| 45 | + // Open receiver type dropdown and select |
| 46 | + await this.robustClick(this.receiverTypeDropdown); |
| 47 | + const typeOption = this.page.getByTestId(`receiver-type-${receiverTypeConfig}`); |
| 48 | + await this.robustClick(typeOption); |
| 49 | + } |
| 50 | + |
| 51 | + async save(): Promise<void> { |
| 52 | + await expect(this.saveChangesButton).toBeEnabled(); |
| 53 | + await this.robustClick(this.saveChangesButton); |
| 54 | + // Wait for the save to complete and redirect back to the receiver list |
| 55 | + await this.createReceiverButton.waitFor({ state: 'visible', timeout: 30_000 }); |
| 56 | + } |
| 57 | + |
| 58 | + async showAdvancedConfiguration(): Promise<void> { |
| 59 | + const button = this.advancedConfigButton.locator('button'); |
| 60 | + await this.robustClick(button); |
| 61 | + } |
| 62 | + |
| 63 | + async getYAMLContent(): Promise<string> { |
| 64 | + // Get content from Monaco editor |
| 65 | + const content = await this.page.evaluate(() => { |
| 66 | + const monacoEditor = (window as any).monaco?.editor?.getModels()?.[0]; |
| 67 | + return monacoEditor?.getValue() || ''; |
| 68 | + }); |
| 69 | + |
| 70 | + return content; |
| 71 | + } |
| 72 | + |
| 73 | + async setYAMLContent(content: string): Promise<void> { |
| 74 | + await this.page.evaluate((text) => { |
| 75 | + const monacoEditor = (window as any).monaco?.editor?.getModels()?.[0]; |
| 76 | + monacoEditor?.setValue(text); |
| 77 | + }, content); |
| 78 | + } |
| 79 | + |
| 80 | + async validateReceiverInList(receiverName: string): Promise<void> { |
| 81 | + // Navigate to list page and wait for the receiver to appear. |
| 82 | + // The alertmanager config propagation can take a few seconds after the secret |
| 83 | + // is patched, so retry navigation until the receiver row is visible. |
| 84 | + await expect(async () => { |
| 85 | + await this.navigateToAlertmanager(); |
| 86 | + await expect(this.page.getByRole('row', { name: new RegExp(receiverName) })).toBeVisible({ |
| 87 | + timeout: 5_000, |
| 88 | + }); |
| 89 | + }).toPass({ intervals: [2_000, 3_000, 5_000], timeout: 30_000 }); |
| 90 | + |
| 91 | + // Check that integration type cell is visible |
| 92 | + const integrationTypeCell = this.page.getByTestId( |
| 93 | + `data-view-cell-${receiverName}-integration-types`, |
| 94 | + ); |
| 95 | + await expect(integrationTypeCell).toBeVisible(); |
| 96 | + |
| 97 | + // Check that routing labels cell is visible |
| 98 | + const routingLabelsCell = this.page.getByTestId( |
| 99 | + `data-view-cell-${receiverName}-routing-labels`, |
| 100 | + ); |
| 101 | + await expect(routingLabelsCell).toBeVisible(); |
| 102 | + } |
| 103 | +} |
| 104 | + |
| 105 | +export function getGlobalsAndReceiverConfig( |
| 106 | + receiverName: string, |
| 107 | + configName: string, |
| 108 | + yamlContent: string, |
| 109 | +): { |
| 110 | + globals: any; |
| 111 | + receiverConfig: any; |
| 112 | +} { |
| 113 | + const parsed = yaml.load(yamlContent); |
| 114 | + const config: AlertmanagerConfig = |
| 115 | + typeof parsed === 'object' && parsed !== null ? (parsed as AlertmanagerConfig) : ({} as AlertmanagerConfig); |
| 116 | + const receiver: AlertmanagerReceiver | undefined = config.receivers?.find( |
| 117 | + (r) => r.name === receiverName, |
| 118 | + ); |
| 119 | + |
| 120 | + return { |
| 121 | + globals: config.global || {}, |
| 122 | + receiverConfig: receiver?.[configName]?.[0] || {}, |
| 123 | + }; |
| 124 | +} |
0 commit comments