From 896bc61934a79b46829cda6c6ce7efba69b1f5e2 Mon Sep 17 00:00:00 2001 From: Adria Navarro Date: Wed, 17 May 2023 09:38:37 +0200 Subject: [PATCH 1/3] Check elastic connection --- .../server/src/integrations/elasticsearch.ts | 17 +++++++++++++++-- 1 file changed, 15 insertions(+), 2 deletions(-) diff --git a/packages/server/src/integrations/elasticsearch.ts b/packages/server/src/integrations/elasticsearch.ts index 621e81f367..af52799c51 100644 --- a/packages/server/src/integrations/elasticsearch.ts +++ b/packages/server/src/integrations/elasticsearch.ts @@ -4,6 +4,7 @@ import { QueryType, IntegrationBase, DatasourceFeature, + ConnectionInfo, } from "@budibase/types" import { Client, ClientOptions } from "@elastic/elasticsearch" @@ -21,7 +22,7 @@ const SCHEMA: Integration = { "Elasticsearch is a search engine based on the Lucene library. It provides a distributed, multitenant-capable full-text search engine with an HTTP web interface and schema-free JSON documents.", friendlyName: "ElasticSearch", type: "Non-relational", - features: [], + features: [DatasourceFeature.CONNECTION_CHECKING], datasource: { url: { type: DatasourceFieldType.STRING, @@ -97,7 +98,7 @@ const SCHEMA: Integration = { class ElasticSearchIntegration implements IntegrationBase { private config: ElasticsearchConfig - private client: any + private client constructor(config: ElasticsearchConfig) { this.config = config @@ -116,6 +117,18 @@ class ElasticSearchIntegration implements IntegrationBase { this.client = new Client(clientConfig) } + async testConnection(): Promise { + try { + await this.client.info() + return { connected: true } + } catch (e: any) { + return { + connected: false, + error: e.message as string, + } + } + } + async create(query: { index: string; json: object }) { const { index, json } = query From 6b2e6f7bcbae67301476db23739f1f83530ca0b0 Mon Sep 17 00:00:00 2001 From: Adria Navarro Date: Wed, 17 May 2023 09:38:59 +0200 Subject: [PATCH 2/3] Test happy path --- .../integrations/validators/elastic.spec.ts | 23 +++++++++++++++++++ 1 file changed, 23 insertions(+) create mode 100644 qa-core/src/integrations/validators/elastic.spec.ts diff --git a/qa-core/src/integrations/validators/elastic.spec.ts b/qa-core/src/integrations/validators/elastic.spec.ts new file mode 100644 index 0000000000..61e4ed00f5 --- /dev/null +++ b/qa-core/src/integrations/validators/elastic.spec.ts @@ -0,0 +1,23 @@ +import { ElasticsearchContainer } from "testcontainers" +import elastic from "../../../../packages/server/src/integrations/elasticsearch" + +jest.unmock("@elastic/elasticsearch") + +describe("datasource validators", () => { + describe("elastic search", () => { + let url: string + + beforeAll(async () => { + const container = await new ElasticsearchContainer().start() + url = container.getHttpUrl() + }) + + it("test valid connection string", async () => { + const integration = new elastic.integration({ + url, + }) + const result = await integration.testConnection() + expect(result).toEqual({ connected: true }) + }) + }) +}) From 860a1ee27178ca26d4e52122e9f1a47688b0d6d2 Mon Sep 17 00:00:00 2001 From: Adria Navarro Date: Wed, 17 May 2023 09:44:55 +0200 Subject: [PATCH 3/3] Test invalid config --- qa-core/src/integrations/validators/elastic.spec.ts | 11 +++++++++++ 1 file changed, 11 insertions(+) diff --git a/qa-core/src/integrations/validators/elastic.spec.ts b/qa-core/src/integrations/validators/elastic.spec.ts index 61e4ed00f5..39fd732744 100644 --- a/qa-core/src/integrations/validators/elastic.spec.ts +++ b/qa-core/src/integrations/validators/elastic.spec.ts @@ -19,5 +19,16 @@ describe("datasource validators", () => { const result = await integration.testConnection() expect(result).toEqual({ connected: true }) }) + + it("test wrong connection string", async () => { + const integration = new elastic.integration({ + url: `http://localhost:5656`, + }) + const result = await integration.testConnection() + expect(result).toEqual({ + connected: false, + error: "connect ECONNREFUSED 127.0.0.1:5656", + }) + }) }) })