Skip to content

Commit 018c496

Browse files
authored
Add support for redis-stack containers (#1005)
1 parent 98ecce6 commit 018c496

File tree

3 files changed

+42
-4
lines changed

3 files changed

+42
-4
lines changed

docs/modules/redis.md

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,21 +11,37 @@ npm install @testcontainers/redis --save-dev
1111
## Examples
1212

1313
<!--codeinclude-->
14+
1415
[Start container:](../../packages/modules/redis/src/redis-container.test.ts) inside_block:startContainer
16+
1517
<!--/codeinclude-->
1618

1719
<!--codeinclude-->
20+
1821
[Connect redis client to container:](../../packages/modules/redis/src/redis-container.test.ts) inside_block:simpleConnect
22+
1923
<!--/codeinclude-->
2024

2125
<!--codeinclude-->
26+
2227
[Start container with password authentication:](../../packages/modules/redis/src/redis-container.test.ts) inside_block:startWithCredentials
28+
2329
<!--/codeinclude-->
2430

2531
<!--codeinclude-->
32+
2633
[Define volume for persistent/predefined data:](../../packages/modules/redis/src/redis-container.test.ts) inside_block:persistentData
34+
2735
<!--/codeinclude-->
2836

2937
<!--codeinclude-->
38+
39+
[Start container with redis/redis-stack-server image:](../../packages/modules/redis/src/redis-container.test.ts) inside_block:startWithRedisStack
40+
41+
<!--/codeinclude-->
42+
43+
<!--codeinclude-->
44+
3045
[Execute a command inside the container:](../../packages/modules/redis/src/redis-container.test.ts) inside_block:executeCommand
46+
3147
<!--/codeinclude-->

packages/modules/redis/src/redis-container.test.ts

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -102,6 +102,22 @@ describe("RedisContainer", { timeout: 240_000 }, () => {
102102
});
103103
// }
104104

105+
// startWithRedisStack {
106+
it("should start with redis-stack-server and json module", async () => {
107+
const container = await new RedisContainer("redis/redis-stack-server:7.4.0-v4")
108+
.withPassword("testPassword")
109+
.start();
110+
const client = await connectTo(container);
111+
112+
await client.json.set("key", "$", { name: "test" });
113+
const result = await client.json.get("key");
114+
expect(result).toEqual({ name: "test" });
115+
116+
await client.disconnect();
117+
await container.stop();
118+
});
119+
// }
120+
105121
// simpleConnect {
106122
async function connectTo(container: StartedRedisContainer) {
107123
const client = createClient({

packages/modules/redis/src/redis-container.ts

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -39,11 +39,17 @@ export class RedisContainer extends GenericContainer {
3939
}
4040

4141
public override async start(): Promise<StartedRedisContainer> {
42-
this.withCommand([
43-
"redis-server",
44-
...(this.password ? [`--requirepass "${this.password}"`] : []),
42+
const redisArgs = [
43+
...(this.password ? [`--requirepass ${this.password}`] : []),
4544
...(this.persistenceVolume ? ["--save 1 1 ", "--appendonly yes"] : []),
46-
]);
45+
];
46+
if (this.imageName.image.includes("redis-stack")) {
47+
this.withEnvironment({
48+
REDIS_ARGS: redisArgs.join(" "),
49+
}).withEntrypoint(["/entrypoint.sh"]);
50+
} else {
51+
this.withCommand(["redis-server", ...redisArgs]);
52+
}
4753
if (this.persistenceVolume) {
4854
this.withBindMounts([{ mode: "rw", source: this.persistenceVolume, target: "/data" }]);
4955
}

0 commit comments

Comments
 (0)