|
| 1 | +import { test, expect } from "@playwright/test"; |
| 2 | +import * as fs from "fs"; |
| 3 | +import * as path from "path"; |
| 4 | + |
| 5 | +const dashboardDir = path.join(__dirname, "../grafana/dashboards"); |
| 6 | + |
| 7 | +// Allow specified panels and sections to be empty, e.g. because |
| 8 | +// metrics aren't available or are otherwise expected to be empty. |
| 9 | +const allowEmptyPanels = new Set(["5xx error rate"]); |
| 10 | +const allowEmptySections = new Set(["sled-agent"]); |
| 11 | + |
| 12 | +const dashboards = fs |
| 13 | + .readdirSync(dashboardDir) |
| 14 | + .filter((f) => f.endsWith(".json")) |
| 15 | + .map((f) => { |
| 16 | + const content = JSON.parse( |
| 17 | + fs.readFileSync(path.join(dashboardDir, f), "utf-8"), |
| 18 | + ); |
| 19 | + let section = ""; |
| 20 | + const panels = (content.panels || []) |
| 21 | + .map((p: { id: number; title: string; type: string }) => { |
| 22 | + if (p.type === "row") { |
| 23 | + section = p.title; |
| 24 | + return null; |
| 25 | + } |
| 26 | + return { id: p.id, title: p.title, section }; |
| 27 | + }) |
| 28 | + .filter(Boolean) as { id: number; title: string; section: string }[]; |
| 29 | + return { |
| 30 | + file: f, |
| 31 | + uid: content.uid as string, |
| 32 | + title: content.title as string, |
| 33 | + panels, |
| 34 | + }; |
| 35 | + }); |
| 36 | + |
| 37 | +for (const dashboard of dashboards) { |
| 38 | + test(`${dashboard.title}: all ${dashboard.panels.length} panels render data`, async ({ |
| 39 | + page, |
| 40 | + }) => { |
| 41 | + await page.goto(`/d/${dashboard.uid}`); |
| 42 | + await page.waitForLoadState("networkidle"); |
| 43 | + |
| 44 | + // Scroll to the bottom to force Grafana to render all panels. |
| 45 | + for (let attempt = 0; attempt < 20; attempt++) { |
| 46 | + await page.evaluate(() => window.scrollBy(0, 1000)); |
| 47 | + await page.waitForTimeout(300); |
| 48 | + const currentCount = await page.locator("[data-viz-panel-key]").count(); |
| 49 | + if (currentCount === dashboard.panels.length) { |
| 50 | + break; |
| 51 | + } |
| 52 | + } |
| 53 | + |
| 54 | + // Wait for panels to finish loading. |
| 55 | + await page.waitForLoadState("networkidle"); |
| 56 | + await expect(page.locator('[aria-label="Panel loading bar"]')).toHaveCount( |
| 57 | + 0, |
| 58 | + { timeout: 30_000 }, |
| 59 | + ); |
| 60 | + |
| 61 | + // Assert the expected number of panels are present. |
| 62 | + const panelLocators = page.locator("[data-viz-panel-key]"); |
| 63 | + await expect(panelLocators).toHaveCount(dashboard.panels.length, { |
| 64 | + timeout: 5_000, |
| 65 | + }); |
| 66 | + |
| 67 | + // Assert each panel has rendered content. For panels rendered |
| 68 | + // with <canvas>, the canvas shouldn't be empty. For tables, |
| 69 | + // there should be >0 rows. |
| 70 | + for (let i = 0; i < dashboard.panels.length; i++) { |
| 71 | + const panel = panelLocators.nth(i); |
| 72 | + const key = await panel.getAttribute("data-viz-panel-key"); |
| 73 | + const meta = dashboard.panels.find((p) => `panel-${p.id}` === key); |
| 74 | + |
| 75 | + if ( |
| 76 | + meta && |
| 77 | + (allowEmptyPanels.has(meta.title) || |
| 78 | + allowEmptySections.has(meta.section)) |
| 79 | + ) { |
| 80 | + continue; |
| 81 | + } |
| 82 | + |
| 83 | + const hasCanvas = (await panel.locator("canvas").count()) > 0; |
| 84 | + if (hasCanvas) { |
| 85 | + const isBlank = await panel |
| 86 | + .locator("canvas") |
| 87 | + .first() |
| 88 | + .evaluate((canvas: HTMLCanvasElement) => { |
| 89 | + const ctx = canvas.getContext("2d"); |
| 90 | + if (!ctx) { |
| 91 | + return true; |
| 92 | + } |
| 93 | + const data = ctx.getImageData( |
| 94 | + 0, |
| 95 | + 0, |
| 96 | + canvas.width, |
| 97 | + canvas.height, |
| 98 | + ).data; |
| 99 | + return data.every((v) => v === 0); |
| 100 | + }); |
| 101 | + expect(isBlank, `panel ${key} canvas should not be blank`).toBe(false); |
| 102 | + } else { |
| 103 | + const content = panel.locator( |
| 104 | + ".unwrapped-log-line, .rdg-row:not(.rdg-header-row)", |
| 105 | + ); |
| 106 | + await expect( |
| 107 | + content.first(), |
| 108 | + `panel ${key} should have log lines or table rows`, |
| 109 | + ).toBeAttached({ timeout: 10_000 }); |
| 110 | + } |
| 111 | + } |
| 112 | + }); |
| 113 | +} |
0 commit comments