Merge pull request #10583 from Budibase/budi-6932/verify_redis

Implement redis connection verification
This commit is contained in:
Michael Drury 2023-05-15 17:22:32 +01:00 committed by GitHub
commit 18d2c33839
2 changed files with 83 additions and 1 deletions

View File

@ -93,7 +93,7 @@ const SCHEMA: Integration = {
class RedisIntegration { class RedisIntegration {
private readonly config: RedisConfig private readonly config: RedisConfig
private client: any private client
constructor(config: RedisConfig) { constructor(config: RedisConfig) {
this.config = config this.config = config
@ -106,6 +106,17 @@ class RedisIntegration {
}) })
} }
async testConnection() {
try {
await this.client.ping()
return true
} catch (e: any) {
return { error: e.message as string }
} finally {
await this.disconnect()
}
}
async disconnect() { async disconnect() {
return this.client.quit() return this.client.quit()
} }

View File

@ -0,0 +1,71 @@
import redis from "../../../../packages/server/src/integrations/redis"
import { GenericContainer } from "testcontainers"
import { generator } from "../../shared"
describe("datasource validators", () => {
describe("redis", () => {
describe("unsecured", () => {
let host: string
let port: number
beforeAll(async () => {
const container = await new GenericContainer("redis")
.withExposedPorts(6379)
.start()
host = container.getContainerIpAddress()
port = container.getMappedPort(6379)
})
it("test valid connection", async () => {
const integration = new redis.integration({
host,
port,
username: "",
})
const result = await integration.testConnection()
expect(result).toBe(true)
})
it("test invalid connection even with wrong user/password", async () => {
const integration = new redis.integration({
host,
port,
username: generator.name(),
password: generator.hash(),
})
const result = await integration.testConnection()
expect(result).toEqual({
error:
"WRONGPASS invalid username-password pair or user is disabled.",
})
})
})
describe("secured", () => {
let host: string
let port: number
beforeAll(async () => {
const container = await new GenericContainer("redis")
.withExposedPorts(6379)
.withCmd(["redis-server", "--requirepass", "P@ssW0rd!"])
.start()
host = container.getContainerIpAddress()
port = container.getMappedPort(6379)
})
it("test valid connection", async () => {
const integration = new redis.integration({
host,
port,
username: "",
password: "P@ssW0rd!",
})
const result = await integration.testConnection()
expect(result).toBe(true)
})
})
})
})