Skip to content

Commit 97eb1b5

Browse files
committed
User defined variables incorrectly rendered
It fixes an issue with variable names on Grafana 11.3, where these variables were rendered as undefined. Github issue #278 CMK-20263
1 parent 29f79f3 commit 97eb1b5

File tree

4 files changed

+54
-2
lines changed

4 files changed

+54
-2
lines changed

src/ui/components.tsx

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -83,9 +83,10 @@ export const CheckMkGenericAsyncSelect = function <Value extends string | (strin
8383
const data = await autocompleter(inputValue);
8484
if (showVariables !== false) {
8585
for (const variable of getTemplateSrv().getVariables()) {
86+
const variableId = variable?.id || variable.name;
8687
data.splice(0, 0, {
87-
value: `$${variable.id}` as Value,
88-
label: `$${variable.id}`,
88+
value: `$${variableId}` as Value,
89+
label: `$${variableId}`,
8990
isGrafanaVariable: true,
9091
});
9192
}

tests/e2e/constants.ts

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@ export const GRAFANA_SELECTORS = {
4040
HOST_LABEL_FILTER_FIELD_ID: 'input_host_label',
4141
REMOVE_FILTER: (f: string) => `button[data-test-id="cmk-oac-minus-button-${f}"]`,
4242

43+
AGGREGATION_FIELD_ID: 'input_Aggregation',
4344
HOST_NAME_FILTER_FIELD_ID: 'input_Hostname',
4445
SERVICE_FILTER_FIELD_ID: 'input_Service',
4546
HOSTNAME_REGEX_FILTER_FIELD: 'input[data-test-id="host_name_regex-filter-input"]',
@@ -63,6 +64,13 @@ export const GRAFANA_SELECTORS = {
6364
APPLY_CHANGES_AND_SAVE_BUTTON: 'button[data-testid="data-testid Save dashboard button"]',
6465
SAVE_DASHBOARD_TITLE: 'input[aria-label="Save dashboard title field"]',
6566
SAVE_BUTTON: 'button[data-testid="data-testid Save dashboard drawer button"]',
67+
68+
SETTINGS_BUTTON: 'button[data-testid="data-testid Dashboard settings"]',
69+
VARIABLES_TAB: 'a[data-testid="data-testid Tab Variables"]',
70+
ADD_VARIABLE_BUTTON: 'button[data-testid="data-testid Call to action button Add variable"]',
71+
VARIABLE_NAME_INPUT: 'input[data-testid="data-testid Variable editor Form Name field"]',
72+
BACK_TO_DASHBOARD_BUTTON: 'button[data-testid="data-testid Back to dashboard button"]',
73+
ADD_VISUALIZATION_BUTTON: 'button[data-testid="data-testid Create new panel button"]',
6674
},
6775
};
6876

tests/e2e/models/DashboardPage.ts

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -146,5 +146,33 @@ export class DashboardPage {
146146
async refresGraph() {
147147
await this.page.locator(GRAFANA_SELECTORS.DASHBOARD.REFRESH_GRAPH_BUTTON).click();
148148
}
149+
150+
async goToNewDashboardSettings() {
151+
await this.page.goto(current_config.grafanaUrl + 'dashboard/new');
152+
await this.page.locator(GRAFANA_SELECTORS.DASHBOARD.SETTINGS_BUTTON).click();
153+
}
154+
155+
async addNewVariable(variableName: string) {
156+
await this.page.locator(GRAFANA_SELECTORS.DASHBOARD.VARIABLES_TAB).click();
157+
await this.page.locator(GRAFANA_SELECTORS.DASHBOARD.ADD_VARIABLE_BUTTON).click();
158+
await this.page.locator(GRAFANA_SELECTORS.DASHBOARD.VARIABLE_NAME_INPUT).fill(variableName);
159+
}
160+
161+
async goBackToDashboard() {
162+
await this.page.locator(GRAFANA_SELECTORS.DASHBOARD.BACK_TO_DASHBOARD_BUTTON).click();
163+
}
164+
165+
async addVisualization() {
166+
await this.page.locator(GRAFANA_SELECTORS.DASHBOARD.ADD_VISUALIZATION_BUTTON).click();
167+
}
168+
169+
async assertAggregationVariableExists(variableName: string) {
170+
const fieldSelector = `input[id="${GRAFANA_SELECTORS.DASHBOARD.AGGREGATION_FIELD_ID}"]`;
171+
await this.page.locator(fieldSelector).fill(variableName);
172+
await this.expectSpinners(false);
173+
await this.page.keyboard.press('Enter');
174+
await this.expectSpinners(false);
175+
await expect(this.page.getByText(variableName).first()).toBeVisible();
176+
}
149177
}
150178
export default DashboardPage;

tests/e2e/tests/e2e.test.ts

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -270,3 +270,18 @@ test.describe('E2E tests', () => {
270270
});
271271
});
272272
});
273+
274+
test.describe('General tests', () => {
275+
test.slow();
276+
test('Variables get rendered', async ({ page }) => {
277+
const customVariableName = 'MyVariable';
278+
const dashboardPage = new DashboardPage(page);
279+
await dashboardPage.goToNewDashboardSettings();
280+
await dashboardPage.addNewVariable(customVariableName);
281+
await dashboardPage.saveDashboard();
282+
await dashboardPage.goBackToDashboard();
283+
await dashboardPage.addVisualization();
284+
await dashboardPage.selectDatasource(CMK_EDITION.CEE);
285+
await dashboardPage.assertAggregationVariableExists(customVariableName);
286+
});
287+
});

0 commit comments

Comments
 (0)