From 3933a3881bdbcff817eca649de5c3204fd10b439 Mon Sep 17 00:00:00 2001 From: Adria Navarro Date: Fri, 12 May 2023 18:34:56 +0200 Subject: [PATCH] Test dynamodb connection --- packages/server/src/db/dynamoClient.ts | 2 +- packages/server/src/integrations/dynamodb.ts | 9 +++ .../integrations/validators/dynamodb.spec.ts | 62 +++++++++++++++++++ 3 files changed, 72 insertions(+), 1 deletion(-) create mode 100644 qa-core/src/integrations/validators/dynamodb.spec.ts diff --git a/packages/server/src/db/dynamoClient.ts b/packages/server/src/db/dynamoClient.ts index cb045b7d6f..597be2b21e 100644 --- a/packages/server/src/db/dynamoClient.ts +++ b/packages/server/src/db/dynamoClient.ts @@ -140,7 +140,7 @@ export function init(endpoint: string) { docClient = new AWS.DynamoDB.DocumentClient(docClientParams) } -if (!env.isProd()) { +if (!env.isProd() && !env.isJest()) { env._set("AWS_ACCESS_KEY_ID", "KEY_ID") env._set("AWS_SECRET_ACCESS_KEY", "SECRET_KEY") init("http://localhost:8333") diff --git a/packages/server/src/integrations/dynamodb.ts b/packages/server/src/integrations/dynamodb.ts index d748622130..ca004ee11b 100644 --- a/packages/server/src/integrations/dynamodb.ts +++ b/packages/server/src/integrations/dynamodb.ts @@ -149,6 +149,15 @@ class DynamoDBIntegration implements IntegrationBase { this.client = new AWS.DynamoDB.DocumentClient(this.config) } + async testConnection() { + try { + const scanRes = await new AWS.DynamoDB(this.config).listTables().promise() + return !!scanRes.$response + } catch (e: any) { + return { error: e.message as string } + } + } + async create(query: { table: string json: Omit diff --git a/qa-core/src/integrations/validators/dynamodb.spec.ts b/qa-core/src/integrations/validators/dynamodb.spec.ts new file mode 100644 index 0000000000..53377ff06b --- /dev/null +++ b/qa-core/src/integrations/validators/dynamodb.spec.ts @@ -0,0 +1,62 @@ +import { GenericContainer } from "testcontainers" +import { env } from "@budibase/backend-core" + +import dynamodb from "../../../../packages/server/src/integrations/dynamodb" +import { generator } from "../../shared" + +jest.unmock("aws-sdk") + +describe("datasource validators", () => { + describe("dynamodb", () => { + let connectionSettings: { + user: string + password: string + url: string + } + + beforeAll(async () => { + const user = "root" + const password = generator.hash() + const container = await new GenericContainer("amazon/dynamodb-local") + .withExposedPorts(8000) + .start() + + connectionSettings = { + user, + password, + url: `http://${container.getContainerIpAddress()}:${container.getMappedPort( + 8000 + )}`, + } + env._set("AWS_ACCESS_KEY_ID", "mocked_key") + env._set("AWS_SECRET_ACCESS_KEY", "mocked_secret") + }) + + it("test valid connection string", async () => { + const integration = new dynamodb.integration({ + endpoint: connectionSettings.url, + region: "", + accessKeyId: "", + secretAccessKey: "", + }) + + const result = await integration.testConnection() + expect(result).toBe(true) + }) + + it("test wrong endpoint", async () => { + const integration = new dynamodb.integration({ + endpoint: "http://wrong.url:2880", + region: "", + accessKeyId: "", + secretAccessKey: "", + }) + + const result = await integration.testConnection() + expect(result).toEqual({ + error: + "Inaccessible host: `wrong.url' at port `undefined'. This service may not be available in the `eu-west-1' region.", + }) + }) + }) +})