-
-
Notifications
You must be signed in to change notification settings - Fork 251
Add ability to set command flags to Gcloud emulators #926
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
Merged
Merged
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
1dc91a0
feat(gloud) add ability to set cmd flags
digital88 b9340fa
refactored approach to flags
digital88 94cefb3
fixed after review
digital88 d71d67b
Refactor flag management to base class
cristianrgreco 0535f75
Merge branch 'main' into pr/digital88/926
cristianrgreco 7b70104
Naming conventions
cristianrgreco File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,35 @@ | ||
| import { GenericContainer, Wait } from "testcontainers"; | ||
| import { EmulatorFlagsManager } from "./emulator-flags-manager"; | ||
|
|
||
| export class AbstractGcloudEmulator extends GenericContainer { | ||
| private readonly flagsManager: EmulatorFlagsManager; | ||
|
|
||
| constructor( | ||
| image: string, | ||
| port: number, | ||
| private readonly cmd: string | ||
| ) { | ||
| super(image); | ||
| this.flagsManager = new EmulatorFlagsManager(); | ||
| this.withExposedPorts(port) | ||
| .withFlag("host-port", `0.0.0.0:${port}`) | ||
| .withWaitStrategy(Wait.forLogMessage(/.*running.*/)) | ||
| .withStartupTimeout(120_000); | ||
| } | ||
|
|
||
| /** | ||
| * Adds flag as argument to emulator start command. | ||
| * Adding same flag name twice replaces existing flag value. | ||
| * @param name flag name. Must be set to non-empty string. May optionally contain -- prefix. | ||
| * @param value flag value. May be empty string. | ||
| * @returns this instance for chaining. | ||
| */ | ||
| public withFlag(name: string, value: string) { | ||
| this.flagsManager.withFlag(name, value); | ||
| return this; | ||
| } | ||
|
|
||
| public override async beforeContainerCreated(): Promise<void> { | ||
| this.withCommand(["/bin/sh", "-c", `${this.cmd} ${this.flagsManager.expandFlags()}`]); | ||
| } | ||
| } |
13 changes: 5 additions & 8 deletions
13
packages/modules/gcloud/src/datastore-emulator-container.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
62 changes: 62 additions & 0 deletions
62
packages/modules/gcloud/src/emulator-flags-manager.test.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,62 @@ | ||
| import { EmulatorFlagsManager } from "./emulator-flags-manager"; | ||
|
|
||
| describe("EmulatorFlagsManager", () => { | ||
| it("should add flag without --", async () => { | ||
| const flagsManager = new EmulatorFlagsManager().withFlag("database-mode", "firestore-native"); | ||
|
|
||
| const flags = flagsManager.expandFlags(); | ||
|
|
||
| expect(flags.trim()).toEqual("--database-mode=firestore-native"); | ||
| }); | ||
|
|
||
| it("should add flag with --", async () => { | ||
| const flagsManager = new EmulatorFlagsManager().withFlag("--database-mode", "firestore-native"); | ||
|
|
||
| const flags = flagsManager.expandFlags(); | ||
|
|
||
| expect(flags.trim()).toEqual("--database-mode=firestore-native"); | ||
| }); | ||
|
|
||
| it("should add many flags", async () => { | ||
| const flagsManager = new EmulatorFlagsManager() | ||
| .withFlag("database-mode", "firestore-native") | ||
| .withFlag("--host-port", "0.0.0.0:8080"); | ||
|
|
||
| const flags = flagsManager.expandFlags(); | ||
|
|
||
| expect(flags.trim()).toEqual("--database-mode=firestore-native --host-port=0.0.0.0:8080"); | ||
| }); | ||
|
|
||
| it("should overwrite same flag if added more than once", async () => { | ||
| const flagsManager = new EmulatorFlagsManager() | ||
| .withFlag("database-mode", "firestore-native") | ||
| .withFlag("--database-mode", "datastore-mode"); | ||
|
|
||
| const flags = flagsManager.expandFlags(); | ||
|
|
||
| expect(flags.trim()).toEqual("--database-mode=datastore-mode"); | ||
| }); | ||
|
|
||
| it("should add flag with no value", async () => { | ||
| const flagsManager = new EmulatorFlagsManager().withFlag("database-mode", "").withFlag("--host-port", ""); | ||
|
|
||
| const flags = flagsManager.expandFlags(); | ||
|
|
||
| expect(flags.trim()).toEqual("--database-mode --host-port"); | ||
| }); | ||
|
|
||
| it("should throw if flag name not set", async () => { | ||
| expect(() => new EmulatorFlagsManager().withFlag("", "firestore-native")).toThrowError(); | ||
| }); | ||
|
|
||
| it("should clear all flags added", async () => { | ||
| const flagsManager = new EmulatorFlagsManager() | ||
| .withFlag("database-mode", "firestore-native") | ||
| .withFlag("host-port", "0.0.0.0:8080"); | ||
|
|
||
| flagsManager.clearFlags(); | ||
| const flags = flagsManager.expandFlags(); | ||
|
|
||
| expect(flags.trim()).toEqual(""); | ||
| }); | ||
| }); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,36 @@ | ||
| export class EmulatorFlagsManager { | ||
| private flags: { [name: string]: string } = {}; | ||
|
|
||
| /** | ||
| * Adds flag as argument to emulator start command. | ||
| * Adding same flag name twice replaces existing flag value. | ||
| * @param name flag name. Must be set to non-empty string. May optionally contain -- prefix. | ||
| * @param value flag value. May be empty string. | ||
| * @returns this instance for chaining. | ||
| */ | ||
| public withFlag(name: string, value: string): this { | ||
| if (!name) throw new Error("Flag name must be set."); | ||
| if (name.startsWith("--")) this.flags[name] = value; | ||
| else this.flags[`--${name}`] = value; | ||
| return this; | ||
| } | ||
|
|
||
| private flagToString(name: string, value: string): string { | ||
| return `${name}${value ? "=" + value : ""}`; | ||
| } | ||
|
|
||
| /** | ||
| * | ||
| * @returns string with all flag names and values, concatenated in same order they were added. | ||
| */ | ||
| public expandFlags(): string { | ||
| return `${Object.keys(this.flags).reduce((p, c) => p + " " + this.flagToString(c, this.flags[c]), "")}`; | ||
| } | ||
|
|
||
| /** | ||
| * Clears all added flags. | ||
| */ | ||
| public clearFlags() { | ||
| this.flags = {}; | ||
| } | ||
| } | ||
12 changes: 4 additions & 8 deletions
12
packages/modules/gcloud/src/firestore-emulator-container.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.