跳到主要内容

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://127.0.0.1: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://127.0.0.1:9090');
const isSecureSocksDSProxyEnabled = await isFeatureToggleEnabled('secureSocksDSProxyEnabled');
if (isSecureSocksDSProxyEnabled) {
page.getByLabel('Enabled').check();
}
await expect(configPage.saveAndTest()).toBeOK();
});