|
| 1 | +import { createClient } from "redis"; |
| 2 | +import { RedisContainer, StartedRedisContainer } from "./redis-container"; |
| 3 | +import * as os from "os"; |
| 4 | +import * as path from "path"; |
| 5 | +import * as fs from "fs"; |
| 6 | + |
| 7 | +describe("RedisContainer", () => { |
| 8 | + jest.setTimeout(240_000); |
| 9 | + |
| 10 | + // startContainer { |
| 11 | + it("should connect and execute set-get", async () => { |
| 12 | + const container = await new RedisContainer().start(); |
| 13 | + |
| 14 | + const client = await connectTo(container); |
| 15 | + |
| 16 | + await client.set("key", "val"); |
| 17 | + expect(await client.get("key")).toBe("val"); |
| 18 | + |
| 19 | + await client.disconnect(); |
| 20 | + await container.stop(); |
| 21 | + }); |
| 22 | + // } |
| 23 | + |
| 24 | + it("should connect with password and execute set-get", async () => { |
| 25 | + const container = await new RedisContainer().withPassword("test").start(); |
| 26 | + |
| 27 | + const client = await connectTo(container); |
| 28 | + |
| 29 | + await client.set("key", "val"); |
| 30 | + expect(await client.get("key")).toBe("val"); |
| 31 | + |
| 32 | + await client.disconnect(); |
| 33 | + await container.stop(); |
| 34 | + }); |
| 35 | + |
| 36 | + // persistentData { |
| 37 | + it("should reconnect with volume and persistence data", async () => { |
| 38 | + const sourcePath = fs.mkdtempSync(path.join(os.tmpdir(), "redis-")); |
| 39 | + const container = await new RedisContainer().withPassword("test").withPersistence(sourcePath).start(); |
| 40 | + let client = await connectTo(container); |
| 41 | + |
| 42 | + await client.set("key", "val"); |
| 43 | + await client.disconnect(); |
| 44 | + await container.restart(); |
| 45 | + client = await connectTo(container); |
| 46 | + expect(await client.get("key")).toBe("val"); |
| 47 | + |
| 48 | + await client.disconnect(); |
| 49 | + await container.stop(); |
| 50 | + try { |
| 51 | + fs.rmSync(sourcePath, { force: true, recursive: true }); |
| 52 | + } catch (e) { |
| 53 | + //Ignore clean up, when have no access on fs. |
| 54 | + console.log(e); |
| 55 | + } |
| 56 | + }); |
| 57 | + // } |
| 58 | + |
| 59 | + // initial data import { |
| 60 | + it("should load initial data and can read it", async () => { |
| 61 | + const container = await new RedisContainer() |
| 62 | + .withPassword("test") |
| 63 | + .withInitialData(path.join(__dirname, "initData.redis")) |
| 64 | + .start(); |
| 65 | + const client = await connectTo(container); |
| 66 | + const user = { |
| 67 | + first_name: "David", |
| 68 | + last_name: "Bloom", |
| 69 | + dob: "03-MAR-1981", |
| 70 | + }; |
| 71 | + expect(await client.get("user:002")).toBe(JSON.stringify(user)); |
| 72 | + |
| 73 | + await client.disconnect(); |
| 74 | + await container.stop(); |
| 75 | + }); |
| 76 | + // } |
| 77 | + |
| 78 | + // startWithCredentials { |
| 79 | + it("should start with credentials and login", async () => { |
| 80 | + const password = "testPassword"; |
| 81 | + |
| 82 | + // Test authentication |
| 83 | + const container = await new RedisContainer().withPassword(password).start(); |
| 84 | + expect(container.getConnectionUrl()).toEqual(`redis://:${password}@${container.getHost()}:${container.getPort()}`); |
| 85 | + |
| 86 | + const client = await connectTo(container); |
| 87 | + |
| 88 | + await client.set("key", "val"); |
| 89 | + expect(await client.get("key")).toBe("val"); |
| 90 | + |
| 91 | + await client.disconnect(); |
| 92 | + await container.stop(); |
| 93 | + }); |
| 94 | + // } |
| 95 | + |
| 96 | + // executeCommand { |
| 97 | + it("should execute container cmd and return the result", async () => { |
| 98 | + const container = await new RedisContainer().start(); |
| 99 | + |
| 100 | + const queryResult = await container.executeCliCmd("info", ["clients"]); |
| 101 | + expect(queryResult).toEqual(expect.stringContaining("connected_clients:1")); |
| 102 | + |
| 103 | + await container.stop(); |
| 104 | + }); |
| 105 | + // } |
| 106 | + |
| 107 | + // simpleConnect { |
| 108 | + async function connectTo(container: StartedRedisContainer) { |
| 109 | + const client = createClient({ |
| 110 | + url: container.getConnectionUrl(), |
| 111 | + }); |
| 112 | + await client.connect(); |
| 113 | + expect(client.isOpen).toBeTruthy(); |
| 114 | + return client; |
| 115 | + } |
| 116 | + // } |
| 117 | +}); |
0 commit comments