Elastic/Kibana插件测试最佳实践指南
kibana Your window into the Elastic Stack 项目地址: https://gitcode.com/gh_mirrors/ki/kibana
前言
在Elastic/Kibana生态系统中,插件开发是扩展平台功能的重要方式。本文将深入探讨Kibana插件的测试策略与实践,帮助开发者构建健壮可靠的插件系统。
测试金字塔策略
Kibana插件测试遵循经典的测试金字塔模型,分为三个层次:
- 单元测试:小而快,专注于单一功能点,大量使用测试替身
- 集成测试:验证系统间交互,如HTTP API调用、Elasticsearch交互等
- 端到端测试:通过浏览器验证用户可见行为
核心集成测试实践
核心测试替身使用
Kibana提供了丰富的核心测试替身,位于src/core/server/mocks
和src/core/public/mocks
路径下。这些测试替身虽然不返回真实数据,但完全模拟了核心API的接口。
典型使用示例
import { elasticsearchServiceMock } from 'src/core/server/mocks';
test('ES客户端测试', async () => {
// 1. 创建测试ES客户端
const esClient = elasticsearchServiceMock.createScopedClusterClient();
// 2. 设置测试返回值
esClient.callAsCurrentUser.mockResolvedValue(测试ES响应);
// 3. 调用被测函数
const result = await 被测函数(esClient);
// 4. 验证调用参数
expect(esClient.callAsCurrentUser).toHaveBeenCalledWith(预期参数);
// 5. 验证返回结果
expect(result).toEqual(预期结果);
});
HTTP路由测试策略
HTTP API是Kibana的重要公共接口,测试策略应根据API的公开程度进行调整:
单元测试实践
适用于测试路由控制器和插件模型的内部逻辑。
优点:
- 执行速度快
- 调试简单
缺点:
- 不测试真实依赖
- 不验证跨插件集成
示例:文本格式化测试
describe('TextFormatter', () => {
const sanitizer = sanitizerMock.createSetup();
it('正确格式化文本', async () => {
sanitizer.sanitize.mockResolvedValue('处理后的文本');
expect(await TextFormatter.format('原始文本', sanitizer))
.toBe('预期格式');
});
it('处理非法文本', async () => {
sanitizer.sanitize.mockRejectedValue(new MisformedTextError());
await expect(TextFormatter.format('非法文本', sanitizer))
.rejects.toThrow(MisformedTextError);
});
});
集成测试实践
功能测试运行器(FTR)
适用于需要完整Elastic Stack环境的测试场景。
测试重点:
- 认证/非认证访问
- 请求参数验证
- 核心业务逻辑
- 跨插件依赖
示例测试场景:
describe('文本存储API', () => {
it('验证文本长度限制', async () => {
const response = await supertest
.post('/myPlugin/formatter/text')
.send({ text: '超长文本'.repeat(100) })
.expect(400);
expect(response.body.message).to.contain('最大长度限制');
});
it('存储并检索文本', async () => {
// 存储
const { body } = await supertest
.post('/myPlugin/formatter/text')
.send({ text: '测试文本' })
.expect(200);
// 检索
const response = await supertest
.get(`/myPlugin/formatter/text/${body.id}`)
.expect(200);
expect(response.text).toBe('测试文本');
});
});
TestUtils测试
适用于不需要完整Elasticsearch环境的测试场景。
示例:
describe('格式化API', () => {
let root: ReturnType<typeof kbnTestServer.createRoot>;
beforeAll(async () => {
root = kbnTestServer.createRoot();
await root.start();
});
it('处理非法HTML标记', async () => {
await kbnTestServer.request
.get(root, '/myPlugin/formatter')
.query({ text: '<script>' })
.expect(400, '不能包含未转义的HTML标记');
});
});
应用程序测试要点
Kibana作为单页应用(SPA),应用程序测试需要特别注意:
-
清理资源:
- 取消订阅和轮询
- 重置核心API状态
- 关闭连接(如WebSocket)
-
不可靠的卸载:
- 不要依赖卸载逻辑来保存关键状态
- 浏览器可能直接关闭而不执行清理
应用挂载示例
class Plugin {
setup(core) {
core.application.register({
async mount(params) {
const [{ renderApp }, deps] = await Promise.all([
import('./application'),
core.getStartServices()
]);
return renderApp(params, ...deps);
}
});
}
}
结语
有效的测试策略是构建高质量Kibana插件的基础。通过合理运用单元测试、集成测试和端到端测试,开发者可以确保插件的可靠性和稳定性。记住测试金字塔原则,在保证覆盖面的同时,保持测试套件的执行效率。
kibana Your window into the Elastic Stack 项目地址: https://gitcode.com/gh_mirrors/ki/kibana
创作声明:本文部分内容由AI辅助生成(AIGC),仅供参考