-
Notifications
You must be signed in to change notification settings - Fork 1.2k
test: improve coverage for apphosting yaml configs #10355
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
Open
joehan
wants to merge
1
commit into
main
Choose a base branch
from
test/apphosting-yaml
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+110
−46
Open
Changes from all commits
Commits
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 |
|---|---|---|
| @@ -1,52 +1,116 @@ | ||
| import { expect } from "chai"; | ||
| import { AppHostingYamlConfig } from "./yaml"; | ||
|
|
||
| describe("merge", () => { | ||
| it("merges incoming apphosting yaml config with precendence", () => { | ||
| const apphostingYaml = AppHostingYamlConfig.empty(); | ||
| apphostingYaml.env = { | ||
| ENV_1: { value: "env_1" }, | ||
| ENV_2: { value: "env_2" }, | ||
| SECRET: { secret: "secret_1" }, | ||
| }; | ||
|
|
||
| const incomingAppHostingYaml = AppHostingYamlConfig.empty(); | ||
| incomingAppHostingYaml.env = { | ||
| ENV_1: { value: "incoming_env_1" }, | ||
| ENV_3: { value: "incoming_env_3" }, | ||
| SECRET_2: { value: "incoming_secret_2" }, | ||
| }; | ||
|
|
||
| apphostingYaml.merge(incomingAppHostingYaml); | ||
| expect(apphostingYaml.env).to.deep.equal({ | ||
| ENV_1: { value: "incoming_env_1" }, | ||
| ENV_2: { value: "env_2" }, | ||
| ENV_3: { value: "incoming_env_3" }, | ||
| SECRET: { secret: "secret_1" }, | ||
| SECRET_2: { value: "incoming_secret_2" }, | ||
| import * as sinon from "sinon"; | ||
| import { AppHostingYamlConfig, toEnvMap, toEnvList } from "./yaml"; | ||
| import * as configModule from "./config"; | ||
| import * as utils from "../utils"; | ||
| import * as fsutils from "../fsutils"; | ||
| import { FirebaseError } from "../error"; | ||
|
|
||
| describe("apphosting/yaml", () => { | ||
| let fileExistsStub: sinon.SinonStub; | ||
| let readFileStub: sinon.SinonStub; | ||
| let storeStub: sinon.SinonStub; | ||
|
|
||
| beforeEach(() => { | ||
| fileExistsStub = sinon.stub(fsutils, "fileExistsSync"); | ||
| readFileStub = sinon.stub(utils, "readFileFromDirectory"); | ||
| storeStub = sinon.stub(configModule, "store"); | ||
| }); | ||
|
|
||
| afterEach(() => { | ||
| sinon.restore(); | ||
| }); | ||
|
|
||
| describe("loadFromFile", () => { | ||
| it("should successfully load configuration with env vars", async () => { | ||
| fileExistsStub.returns(true); | ||
| readFileStub.resolves({ source: "env:\n - variable: FOO\n value: bar" }); | ||
|
|
||
| const res = await AppHostingYamlConfig.loadFromFile("apphosting.yaml"); | ||
|
|
||
| expect(res.filename).to.equal("apphosting.yaml"); | ||
| expect(res.env).to.deep.equal({ FOO: { value: "bar" } }); | ||
| }); | ||
|
|
||
| it("should throw if file does not exist", async () => { | ||
| fileExistsStub.returns(false); | ||
|
|
||
| await expect(AppHostingYamlConfig.loadFromFile("missing.yaml")).to.be.rejectedWith( | ||
| FirebaseError, | ||
| /Cannot load missing.yaml from given path/, | ||
| ); | ||
| }); | ||
|
|
||
| it("should return empty env if file contains no env", async () => { | ||
| fileExistsStub.returns(true); | ||
| readFileStub.resolves({ source: "runConfig:\n cpu: 2" }); | ||
|
|
||
| const res = await AppHostingYamlConfig.loadFromFile("apphosting.yaml"); | ||
|
|
||
| expect(res.env).to.deep.equal({}); | ||
| }); | ||
| }); | ||
|
|
||
| describe("empty", () => { | ||
| it("should create an empty config", () => { | ||
| const res = AppHostingYamlConfig.empty(); | ||
| expect(res.env).to.deep.equal({}); | ||
| }); | ||
| }); | ||
|
|
||
| it("conditionally allows secrets to become plaintext", () => { | ||
| const apphostingYaml = AppHostingYamlConfig.empty(); | ||
| apphostingYaml.env = { | ||
| API_KEY: { secret: "api_key" }, | ||
| }; | ||
|
|
||
| const incomingYaml = AppHostingYamlConfig.empty(); | ||
| incomingYaml.env = { | ||
| API_KEY: { value: "plaintext" }, | ||
| }; | ||
|
|
||
| expect(() => | ||
| apphostingYaml.merge(incomingYaml, /* alllowSecretsToBecomePlaintext */ false), | ||
| ).to.throw("Cannot convert secret to plaintext in apphosting yaml"); | ||
|
|
||
| expect(() => | ||
| apphostingYaml.merge(incomingYaml, /* alllowSecretsToBecomePlaintext */ true), | ||
| ).to.not.throw(); | ||
| expect(apphostingYaml.env).to.deep.equal({ | ||
| API_KEY: { value: "plaintext" }, | ||
| describe("merge", () => { | ||
| it("should override variables from incoming config", () => { | ||
| const base = AppHostingYamlConfig.empty(); | ||
| base.env = { FOO: { value: "1" }, BAR: { secret: "sec" } }; | ||
|
|
||
| const other = AppHostingYamlConfig.empty(); | ||
| other.env = { FOO: { value: "2" }, BAZ: { value: "3" } }; | ||
|
|
||
| base.merge(other, true); | ||
|
|
||
| expect(base.env).to.deep.equal({ | ||
| FOO: { value: "2" }, | ||
| BAR: { secret: "sec" }, | ||
| BAZ: { value: "3" }, | ||
| }); | ||
| }); | ||
|
|
||
| it("should throw when a secret turns into plaintext and allowSecretsToBecomePlaintext is false", () => { | ||
| const base = AppHostingYamlConfig.empty(); | ||
| base.env = { DB_PASS: { secret: "my-secret" } }; | ||
|
|
||
| const other = AppHostingYamlConfig.empty(); | ||
| other.env = { DB_PASS: { value: "plaintext" } }; | ||
|
|
||
| expect(() => base.merge(other, false)).to.throw(/Cannot convert secret to plaintext/); | ||
| }); | ||
| }); | ||
|
|
||
| describe("utilities", () => { | ||
| it("toEnvMap", () => { | ||
| const list = [{ variable: "FOO", value: "bar" }]; | ||
| const map = toEnvMap(list); | ||
| expect(map).to.deep.equal({ FOO: { value: "bar" } }); | ||
| }); | ||
|
|
||
| it("toEnvList", () => { | ||
| const map = { FOO: { value: "bar" } }; | ||
| const list = toEnvList(map); | ||
| expect(list).to.deep.equal([{ variable: "FOO", value: "bar" }]); | ||
| }); | ||
| }); | ||
|
|
||
| describe("upsertFile", () => { | ||
| it("should parse, merge, and store successfully", async () => { | ||
| fileExistsStub.returns(true); | ||
| readFileStub.resolves({ source: "env:\n - variable: FOO\n value: bar" }); | ||
|
|
||
| const conf = AppHostingYamlConfig.empty(); | ||
| conf.env = { BAZ: { value: "qux" } }; | ||
|
|
||
| await conf.upsertFile("apphosting.yaml"); | ||
|
|
||
| expect(storeStub).to.have.been.calledOnce; | ||
| }); | ||
| }); | ||
| }); |
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
Oops, something went wrong.
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The use of
anyas an escape hatch is discouraged by the repository style guide (Line 38). You can avoid it by casting toPartial<Env>, which allows the deletion of the requiredvariableproperty while maintaining better type safety thanany.References