-
Notifications
You must be signed in to change notification settings - Fork 23
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[Step Function] 3.3. Add class StepFunctionConfigLoader
- Loading branch information
Showing
4 changed files
with
118 additions
and
4 deletions.
There are no files selected for viewing
This file contains 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 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,24 @@ | ||
import { ConfigLoader } from "../env"; | ||
|
||
export interface Configuration { | ||
// When set, it will be added to the state machine's log group name. | ||
env?: string; | ||
} | ||
|
||
const envEnvVar = "DD_ENV"; | ||
|
||
export class StepFunctionConfigLoader extends ConfigLoader<Configuration> { | ||
readonly defaultConfiguration: Configuration = {}; | ||
|
||
public getConfigFromEnvVars(): Configuration { | ||
const config: Configuration = { | ||
...this.defaultConfiguration, | ||
}; | ||
|
||
if (envEnvVar in process.env) { | ||
config.env = process.env[envEnvVar]; | ||
} | ||
|
||
return config; | ||
} | ||
} |
This file contains 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 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,86 @@ | ||
import { InputEvent } from "../../src/types"; | ||
import { StepFunctionConfigLoader } from "../../src/step_function/env"; | ||
|
||
const loader = new StepFunctionConfigLoader(); | ||
describe("getConfig", () => { | ||
const originalEnv = process.env; | ||
|
||
beforeEach(async () => { | ||
jest.resetModules(); | ||
process.env = { ...originalEnv }; | ||
}); | ||
|
||
afterEach(() => { | ||
jest.resetAllMocks(); | ||
process.env = originalEnv; | ||
}); | ||
|
||
// TODO: Change "env" to a field which is in defaultConfig once there is one | ||
describe("1. CloudFormation Macro params are set", () => { | ||
it("uses CloudFormation Macro params over environment variables and default configuration", () => { | ||
const event: InputEvent = { | ||
params: { | ||
env: "macroEnv", | ||
}, | ||
fragment: { | ||
Mappings: {}, | ||
}, | ||
} as any; | ||
|
||
process.env.DD_ENV = "envVarEnv"; | ||
|
||
const config = loader.getConfig(event); | ||
expect(config.env).toBe("macroEnv"); | ||
}); | ||
}); | ||
|
||
describe("2. CloudFormation Mappings are set", () => { | ||
it("uses CloudFormation Mappings params over environment variables and default configuration", () => { | ||
const event: InputEvent = { | ||
params: {}, | ||
fragment: { | ||
Mappings: { | ||
Datadog: { | ||
Parameters: { | ||
env: "mappingEnv", | ||
}, | ||
}, | ||
}, | ||
}, | ||
} as any; | ||
|
||
process.env.DD_ENV = "envVarEnv"; | ||
|
||
const config = loader.getConfig(event); | ||
expect(config.env).toBe("mappingEnv"); | ||
}); | ||
}); | ||
|
||
describe("3. Neither CloudFormation Macro params nor CloudFormation Mappings is set", () => { | ||
it("uses environment variables over default configuration", () => { | ||
const event: InputEvent = { | ||
params: {}, | ||
fragment: { | ||
Mappings: {}, | ||
}, | ||
} as any; | ||
|
||
process.env.DD_ENV = "envVarEnv"; | ||
|
||
const config = loader.getConfig(event); | ||
expect(config.env).toBe("envVarEnv"); | ||
}); | ||
|
||
it("uses default configuration if no other params are set", () => { | ||
const event: InputEvent = { | ||
params: {}, | ||
fragment: { | ||
Mappings: {}, | ||
}, | ||
} as any; | ||
|
||
const config = loader.getConfig(event); | ||
expect(config.env).toBe(undefined); | ||
}); | ||
}); | ||
}); |