Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(nats): added js and support arg without value #873

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions docs/modules/nats.md
Original file line number Diff line number Diff line change
Expand Up @@ -21,3 +21,7 @@ npm install @testcontainers/nats --save-dev
<!--codeinclude-->
[Set credentials:](../../packages/modules/nats/src/nats-container.test.ts) inside_block:credentials
<!--/codeinclude-->

<!--codeinclude-->
[Enable JetStream:](../../packages/modules/nats/src/nats-container.test.ts) inside_block:JetStream
<!--/codeinclude-->
37 changes: 37 additions & 0 deletions packages/modules/nats/src/nats-container.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,43 @@
});
// }

// JetStream {
it("should start with JetStream ", async () => {
// enable JS
const container = await new NatsContainer().withJS().start();
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is JS a well known acronym for Jetstream for Nats users?


const nc = await connect(container.getConnectionOptions());

// just take manager for check js
await nc.jetstream().jetstreamManager()

Check failure on line 78 in packages/modules/nats/src/nats-container.test.ts

View workflow job for this annotation

GitHub Actions / lint

Insert `;`

// close the connection
await nc.close();
// check if the close was OK
const err = await nc.closed();
expect(err).toBe(undefined);

await container.stop();
});

it("should fail without JetStream ", async () => {
const container = await new NatsContainer().start();

const nc = await connect(container.getConnectionOptions());

// just take manager for check js
await expect(nc.jetstream().jetstreamManager()).rejects.toThrowError('503')

Check failure on line 95 in packages/modules/nats/src/nats-container.test.ts

View workflow job for this annotation

GitHub Actions / lint

Replace `'503')` with `"503");`

// close the connection
await nc.close();
// check if the close was OK
const err = await nc.closed();
expect(err).toBe(undefined);

await container.stop();
});
// }

it("should immediately end when started with version argument ", async () => {
// for the complete list of available arguments see:
// See Command Line Options section inside [NATS docker image documentation](https://hub.docker.com/_/nats)
Expand Down
59 changes: 37 additions & 22 deletions packages/modules/nats/src/nats-container.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,44 +7,48 @@
const USER_ARGUMENT_KEY = "--user";
const PASS_ARGUMENT_KEY = "--pass";

function buildCmdsFromArgs(args: { [p: string]: string }): string[] {
const result: string[] = [];
result.push("nats-server");

for (const argsKey in args) {
result.push(argsKey);
result.push(args[argsKey]);
}
return result;
}

export class NatsContainer extends GenericContainer {
private args: { [name: string]: string } = {};
private args = new Set<string>()

Check failure on line 11 in packages/modules/nats/src/nats-container.ts

View workflow job for this annotation

GitHub Actions / lint

Insert `;`
private values = new Map<string, string>()

Check failure on line 12 in packages/modules/nats/src/nats-container.ts

View workflow job for this annotation

GitHub Actions / lint

Insert `;`

constructor(image = "nats:2.8.4-alpine") {
super(image);

this.args[USER_ARGUMENT_KEY] = "test";
this.args[PASS_ARGUMENT_KEY] = "test";
this.withUsername('test')

Check failure on line 16 in packages/modules/nats/src/nats-container.ts

View workflow job for this annotation

GitHub Actions / lint

Replace `'test')` with `"test");`
this.withPass('test')

Check failure on line 17 in packages/modules/nats/src/nats-container.ts

View workflow job for this annotation

GitHub Actions / lint

Replace `'test')` with `"test");`

this.withExposedPorts(CLIENT_PORT, ROUTING_PORT_FOR_CLUSTERING, HTTP_MANAGEMENT_PORT)
.withWaitStrategy(Wait.forLogMessage(/.*Server is ready.*/))
.withStartupTimeout(120_000);
}

public withUsername(user: string): this {
this.args[USER_ARGUMENT_KEY] = user;
this.withArg(USER_ARGUMENT_KEY, user)

Check failure on line 25 in packages/modules/nats/src/nats-container.ts

View workflow job for this annotation

GitHub Actions / lint

Insert `;`
return this;
}

/**
* Enable JetStream
* @returns {this}
*/
public withJS(): this {
this.withArg('-js')

Check failure on line 34 in packages/modules/nats/src/nats-container.ts

View workflow job for this annotation

GitHub Actions / lint

Replace `'-js')` with `"-js");`
return this;
}

public withPass(pass: string): this {
this.args[PASS_ARGUMENT_KEY] = pass;
this.withArg(PASS_ARGUMENT_KEY, pass)

Check failure on line 39 in packages/modules/nats/src/nats-container.ts

View workflow job for this annotation

GitHub Actions / lint

Insert `;`
return this;
}

public withArg(name: string, value: string) {
public withArg(name: string, value: string): this

Check failure on line 43 in packages/modules/nats/src/nats-container.ts

View workflow job for this annotation

GitHub Actions / lint

Insert `;`
public withArg(name: string): this
public withArg(...args: [string, string] | [string]): this {
let [name, value] = args
name = NatsContainer.ensureDashInFrontOfArgumentName(name);
this.args[name] = value;
this.args.add(name)
if (args.length === 2) {
this.values.set(name, value!)

Check warning on line 50 in packages/modules/nats/src/nats-container.ts

View workflow job for this annotation

GitHub Actions / lint

Forbidden non-null assertion
}
return this;
}

Expand All @@ -61,16 +65,27 @@
}

public override async start(): Promise<StartedNatsContainer> {
this.withCommand(buildCmdsFromArgs(this.args));
this.withCommand(this.getNormalizedCommand());
return new StartedNatsContainer(await super.start(), this.getUser(), this.getPass());
}

private getUser(): string {
return this.args[USER_ARGUMENT_KEY];
return this.values.get(USER_ARGUMENT_KEY)!;

Check warning on line 73 in packages/modules/nats/src/nats-container.ts

View workflow job for this annotation

GitHub Actions / lint

Forbidden non-null assertion
}

private getPass(): string {
return this.args[PASS_ARGUMENT_KEY];
return this.values.get(PASS_ARGUMENT_KEY)!;

Check warning on line 77 in packages/modules/nats/src/nats-container.ts

View workflow job for this annotation

GitHub Actions / lint

Forbidden non-null assertion
}

private getNormalizedCommand(): string[] {
const result: string[] = ['nats-server']
for (const arg of this.args) {
result.push(arg)
if (this.values.has(arg)) {
result.push(this.values.get(arg)!)

Check warning on line 85 in packages/modules/nats/src/nats-container.ts

View workflow job for this annotation

GitHub Actions / lint

Forbidden non-null assertion
}
}
return result
}
}

Expand Down
Loading