Skip to content

Commit c9507df

Browse files
committed
fix: executeCliCmd
1 parent 0a1e245 commit c9507df

File tree

3 files changed

+41
-6
lines changed

3 files changed

+41
-6
lines changed

docs/modules/valkey.md

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,13 @@ Choose an image from the [container registry](https://hub.docker.com/r/valkey/va
2525
### With password
2626

2727
<!--codeinclude-->
28-
[](../../packages/modules/valkey/src/valkey-container.test.ts) inside_block:valkeyWithCredentials
28+
[](../../packages/modules/valkey/src/valkey-container.test.ts) inside_block:valkeyWithPassword
29+
<!--/codeinclude-->
30+
31+
### With username and password
32+
33+
<!--codeinclude-->
34+
[](../../packages/modules/valkey/src/valkey-container.test.ts) inside_block:valkeyWithUsernameAndPassword
2935
<!--/codeinclude-->
3036

3137
### With persistent data

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

Lines changed: 21 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -89,8 +89,27 @@ describe("ValkeyContainer", { timeout: 240_000 }, () => {
8989
// }
9090
});
9191

92-
it("should start with credentials and login", async () => {
93-
// valkeyWithCredentials {
92+
it("should start with username and password", async () => {
93+
// valkeyWithUsernameAndPassword {
94+
const username = "testUser";
95+
const password = "testPassword";
96+
97+
await using container = await new ValkeyContainer(IMAGE).withUsername(username).withPassword(password).start();
98+
99+
expect(container.getConnectionUrl()).toEqual(`redis://${username}:${password}@${container.getHost()}:${container.getPort()}`);
100+
// }
101+
102+
const client = createClient({ url: container.getConnectionUrl() });
103+
await client.connect();
104+
105+
await client.set("key", "val");
106+
expect(await client.get("key")).toBe("val");
107+
108+
client.destroy();
109+
});
110+
111+
it("should start with password only", async () => {
112+
// valkeyWithPassword {
94113
const password = "testPassword";
95114

96115
await using container = await new ValkeyContainer(IMAGE).withPassword(password).start();

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

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,7 @@ export class ValkeyContainer extends GenericContainer {
7171
]);
7272
}
7373

74-
return new StartedValkeyContainer(await super.start(), this.password);
74+
return new StartedValkeyContainer(await super.start(), this.password, this.username);
7575
}
7676

7777
private async importInitialData(container: StartedTestContainer) {
@@ -85,7 +85,8 @@ export class ValkeyContainer extends GenericContainer {
8585
export class StartedValkeyContainer extends AbstractStartedContainer {
8686
constructor(
8787
startedTestContainer: StartedTestContainer,
88-
private readonly password?: string
88+
private readonly password?: string,
89+
private readonly username?: string,
8990
) {
9091
super(startedTestContainer);
9192
}
@@ -98,18 +99,27 @@ export class StartedValkeyContainer extends AbstractStartedContainer {
9899
return this.password ? this.password.toString() : "";
99100
}
100101

102+
public getUsername(): string {
103+
return this.username ? this.username.toString() : "";
104+
}
105+
101106
public getConnectionUrl(): string {
102107
const url = new URL("", "redis://");
103108
url.hostname = this.getHost();
104109
url.port = this.getPort().toString();
105110
url.password = this.getPassword();
111+
url.username = this.getUsername();
106112
return url.toString();
107113
}
108114

109115
public async executeCliCmd(cmd: string, additionalFlags: string[] = []): Promise<string> {
116+
const authCommand = this.password ? [
117+
`--pass ${this.password}`,
118+
...(this.username ? [`--user ${this.username}`] : [])
119+
] : [];
110120
const result = await this.startedTestContainer.exec([
111121
"redis-cli",
112-
...(this.password != "" ? [`-a ${this.password}`] : []),
122+
...authCommand,
113123
`${cmd}`,
114124
...additionalFlags,
115125
]);

0 commit comments

Comments
 (0)