Skip to content

Commit 3e6fa91

Browse files
committed
Fix Redis stack container to accept input env vars
Same as redis image with input command See issue #1150
1 parent 7e87f99 commit 3e6fa91

File tree

2 files changed

+47
-1
lines changed

2 files changed

+47
-1
lines changed

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

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -136,6 +136,22 @@ describe("RedisContainer", { timeout: 240_000 }, () => {
136136
client.destroy();
137137
});
138138

139+
it("should start redis-stack with custom env", async () => {
140+
const container = new RedisContainer(REDISSTACK_IMAGE).withEnvironment({ REDIS_ARGS: "--loglevel verbose" });
141+
const startedContainer = await container.start();
142+
143+
// @ts-expect-error - accessing private property for testing
144+
expect(container.createOpts.Env).toEqual(["REDIS_ARGS=--loglevel verbose"]);
145+
146+
const client = createClient({ url: startedContainer.getConnectionUrl() });
147+
await client.connect();
148+
149+
await client.set("key", "val");
150+
expect(await client.get("key")).toBe("val");
151+
152+
client.destroy();
153+
});
154+
139155
it("should start redis with custom command and keeping default args", async () => {
140156
const sourcePath = fs.mkdtempSync("redis-");
141157

@@ -162,4 +178,25 @@ describe("RedisContainer", { timeout: 240_000 }, () => {
162178
client.destroy();
163179
fs.rmSync(sourcePath, { force: true, recursive: true });
164180
});
181+
182+
it("should start redis-stack with custom env and keeping default args", async () => {
183+
const sourcePath = fs.mkdtempSync("redis-");
184+
185+
const container = new RedisContainer(REDISSTACK_IMAGE)
186+
.withEnvironment({ REDIS_ARGS: "--loglevel verbose" })
187+
.withPersistence(sourcePath);
188+
const startedContainer = await container.start();
189+
190+
// @ts-expect-error - accessing private property for testing
191+
expect(container.createOpts.Env).toEqual(["REDIS_ARGS=--loglevel verbose --save 1 1 --appendonly yes"]);
192+
193+
const client = createClient({ url: startedContainer.getConnectionUrl() });
194+
await client.connect();
195+
196+
await client.set("key", "val");
197+
expect(await client.get("key")).toBe("val");
198+
199+
client.destroy();
200+
fs.rmSync(sourcePath, { force: true, recursive: true });
201+
});
165202
});

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

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,8 +44,17 @@ export class RedisContainer extends GenericContainer {
4444
...(this.persistenceVolume ? ["--save 1 1 ", "--appendonly yes"] : []),
4545
];
4646
if (this.imageName.image.includes("redis-stack")) {
47+
const existingRedisArgs =
48+
(this.createOpts.Env || []).find((e) => e.startsWith("REDIS_ARGS="))?.split("=", 2)[1] || "";
49+
50+
// merge with filter to remove empty items
51+
const mergedRedisArgs = [existingRedisArgs, ...redisArgs].filter(Boolean).join(" ");
52+
53+
// remove existing REDIS_ARGS to avoid duplicates
54+
this.createOpts.Env = (this.createOpts.Env || []).filter((e) => !e.startsWith("REDIS_ARGS="));
55+
4756
this.withEnvironment({
48-
REDIS_ARGS: redisArgs.join(" "),
57+
REDIS_ARGS: mergedRedisArgs,
4958
}).withEntrypoint(["/entrypoint.sh"]);
5059
} else {
5160
const existingCmd = this.createOpts.Cmd || [];

0 commit comments

Comments
 (0)