跳到主要内容

Grafana 功能开关

介绍

Grafana 使用一种称为 功能开关 的机制,可以在运行时开启或关闭代码。插件可以选择性地根据功能开关的状态来改变其行为;如果它们这样做了,您可以在端到端测试中对其进行覆盖。本指南介绍了 @grafana/plugin-e2e 中使功能开关更容易工作的特性。

将功能开关配置传递给 Grafana

配置可在整个 Grafana 栈中使用的功能开关最简单的方法是指定启动 Grafana 实例时可用的环境变量。有关操作说明,请参阅 我们的文档

在端到端测试中覆盖前端功能开关

@grafana/plugin-e2e 工具允许您覆盖 Grafana 配置使用的前端功能开关。您可以通过在 Playwright 配置文件中指定自定义选项 featureToggles 来实现。

// playwright.config.ts
import { defineConfig, devices } from '@playwright/test';
import { PluginOptions } from '@grafana/plugin-e2e';

export default defineConfig<PluginOptions>({
testDir: './tests',
reporter: 'html',
use: {
baseURL: 'https://:3000',
featureToggles: {
experimentalPluginQueryEditor: false,
experimentalPluginConfigEditor: true,
},
},
...
}
注意

以这种方式定义的功能开关会传播到 window.grafanaBootData.settings.featureToggles。这意味着它们只会影响 Grafana 的前端。如果您需要功能开关对整个栈产生影响,请参阅 上一节

在特定测试文件中覆盖功能开关

要在特定测试文件中覆盖测试的功能开关,请使用如下代码。

import { test, expect } from '@grafana/plugin-e2e';

test.use({
featureToggles: {
experimentalPluginQueryEditor: true,
},
});

在测试中检查功能是否启用

使用 isFeatureToggleEnabled fixture 来确定某个功能开关是否已启用。在底层,isFeatureToggleEnabled 检查给定功能是否在 window.grafanaBootData.settings.featureToggles 对象中定义并启用。

import { test, expect } from '@grafana/plugin-e2e';
import * as semver from 'semver';

test('valid credentials should return a 200 status code', async ({
createDataSourceConfigPage,
page,
isFeatureToggleEnabled,
}) => {
const configPage = await createDataSourceConfigPage({ type: 'grafana-snowflake-datasource' });
await configPage.getByGrafanaSelector('Data source connection URL').fill('https://:9090');
const isSecureSocksDSProxyEnabled = await isFeatureToggleEnabled('secureSocksDSProxyEnabled');
if (isSecureSocksDSProxyEnabled) {
page.getByLabel('Enabled').check();
}
await expect(configPage.saveAndTest()).toBeOK();
});