-
Notifications
You must be signed in to change notification settings - Fork 156
tests(parameters): integration tests for SSMProvider
#1257
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
dreamorosi
merged 8 commits into
main
from
1240-maintenance-integration-tests-for-ssmprovider
Feb 17, 2023
Merged
Changes from all commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
107a06c
chore: update cdkAspect to include SSM
dreamorosi e1318c8
tests: SSMProvider class usage
dreamorosi 7f5f934
chore: removed unused import
dreamorosi 4440039
tests: completed tests
dreamorosi e15dc7b
tests: fixed typos
dreamorosi 97a59ba
chore: added missing effect in cdkAspect
dreamorosi bd25df0
chore: reduced scope of ssmSecureString custom resource
dreamorosi da22d85
refactor: options setting logic
dreamorosi 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
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,5 +1,4 @@ | ||
export * from './SSMProvider'; | ||
export * from './getParameter'; | ||
export * from './getParameters'; | ||
export * from './getParametersByName'; | ||
export * from '../types/SSMProvider'; | ||
export * from './getParametersByName'; |
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
183 changes: 183 additions & 0 deletions
183
packages/parameters/tests/e2e/ssmProvider.class.test.functionCode.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,183 @@ | ||
import { Context } from 'aws-lambda'; | ||
import { | ||
SSMProvider, | ||
} from '../../src/ssm'; | ||
import { | ||
SSMGetOptionsInterface, | ||
SSMGetMultipleOptionsInterface, | ||
SSMGetParametersByNameOptionsInterface | ||
} from '../../src/types'; | ||
import { TinyLogger } from '../helpers/tinyLogger'; | ||
import { middleware } from '../helpers/sdkMiddlewareRequestCounter'; | ||
import { SSMClient } from '@aws-sdk/client-ssm'; | ||
|
||
// We use a custom logger to log pure JSON objects to stdout | ||
const logger = new TinyLogger(); | ||
|
||
const defaultProvider = new SSMProvider(); | ||
// Provider test 8, 9 | ||
const customClient = new SSMClient({}); | ||
customClient.middlewareStack.use(middleware); | ||
const providerWithMiddleware = new SSMProvider({ | ||
awsSdkV3Client: customClient | ||
}); | ||
|
||
const paramA = process.env.PARAM_A ?? 'my-param'; | ||
const paramB = process.env.PARAM_B ?? 'my-param'; | ||
const paramEncryptedA = process.env.PARAM_ENCRYPTED_A ?? 'my-encrypted-param'; | ||
const paramEncryptedB = process.env.PARAM_ENCRYPTED_B ?? 'my-encrypted-param'; | ||
|
||
// Use provider specified, or default to main one & return it with cache cleared | ||
const resolveProvider = (provider?: SSMProvider): SSMProvider => { | ||
const resolvedProvider = provider ? provider : defaultProvider; | ||
resolvedProvider.clearCache(); | ||
|
||
return resolvedProvider; | ||
}; | ||
|
||
// Helper function to call get() and log the result | ||
const _call_get = async ( | ||
paramName: string, | ||
testName: string, | ||
options?: SSMGetOptionsInterface, | ||
provider?: SSMProvider | ||
): Promise<void> => { | ||
try { | ||
const currentProvider = resolveProvider(provider); | ||
|
||
const parameterValue = await currentProvider.get(paramName, options); | ||
logger.log({ | ||
test: testName, | ||
value: parameterValue | ||
}); | ||
} catch (err) { | ||
logger.log({ | ||
test: testName, | ||
error: err.message | ||
}); | ||
} | ||
}; | ||
|
||
// Helper function to call getMultiple() and log the result | ||
const _call_get_multiple = async ( | ||
paramPath: string, | ||
testName: string, | ||
options?: SSMGetMultipleOptionsInterface, | ||
provider?: SSMProvider | ||
): Promise<void> => { | ||
try { | ||
const currentProvider = resolveProvider(provider); | ||
|
||
const parameterValues = await currentProvider.getMultiple( | ||
paramPath, | ||
options | ||
); | ||
logger.log({ | ||
test: testName, | ||
value: parameterValues | ||
}); | ||
} catch (err) { | ||
logger.log({ | ||
test: testName, | ||
error: err.message | ||
}); | ||
} | ||
}; | ||
|
||
// Helper function to call getParametersByName() and log the result | ||
const _call_get_parameters_by_name = async ( | ||
params: Record<string, SSMGetParametersByNameOptionsInterface>, | ||
testName: string, | ||
options?: SSMGetParametersByNameOptionsInterface, | ||
provider?: SSMProvider | ||
): Promise<void> => { | ||
try { | ||
const currentProvider = resolveProvider(provider); | ||
|
||
const parameterValues = await currentProvider.getParametersByName(params, options); | ||
logger.log({ | ||
test: testName, | ||
value: parameterValues | ||
}); | ||
} catch (err) { | ||
logger.log({ | ||
test: testName, | ||
error: err.message | ||
}); | ||
} | ||
}; | ||
|
||
export const handler = async (_event: unknown, _context: Context): Promise<void> => { | ||
// Test 1 - get a single parameter by name with default options | ||
await _call_get(paramA, 'get'); | ||
|
||
// Test 2 - get a single parameter by name with decrypt | ||
await _call_get(paramEncryptedA, 'get-decrypt', { decrypt: true }); | ||
|
||
// Test 3 - get multiple parameters by path with default options | ||
// Get path (/param/get) | ||
const parameterPath = paramA.substring(0, paramA.lastIndexOf('/')); | ||
await _call_get_multiple(parameterPath, 'get-multiple'); | ||
|
||
// Test 4 - get multiple parameters by path recursively (aka. get all parameters under a path recursively) | ||
// Get parameters root (i.e. from /param/get/a & /param/get/b to /param) | ||
const parameterRoot = paramA.substring( | ||
0, | ||
paramA.substring(1, paramA.length).indexOf('/') + 1 | ||
); | ||
await _call_get_multiple(parameterRoot, 'get-multiple-recursive', { recursive: true }); | ||
|
||
// Test 5 - get multiple parameters by path with decrypt | ||
// Get parameters path (i.e. from /param/get/a & /param/get/b to /param/get) | ||
const parameterPathDecrypt = paramEncryptedA.substring(0, paramEncryptedA.lastIndexOf('/')); | ||
await _call_get_multiple(parameterPathDecrypt, 'get-multiple-decrypt', { decrypt: true }); | ||
|
||
// Test 6 - get multiple parameters by name with default options | ||
await _call_get_parameters_by_name({ | ||
[paramA]: {}, | ||
[paramB]: {}, | ||
}, 'get-multiple-by-name'); | ||
|
||
// Test 7 - get multiple parameters by name, some of them encrypted and some not | ||
await _call_get_parameters_by_name({ | ||
[paramA]: {}, | ||
[paramEncryptedA]: { decrypt: true }, | ||
[paramEncryptedB]: { decrypt: true }, | ||
}, 'get-multiple-by-name-mixed-decrypt'); | ||
|
||
// Test 8 | ||
// get parameter twice with middleware, which counts the number of requests, we check later if we only called SSM API once | ||
try { | ||
providerWithMiddleware.clearCache(); | ||
middleware.counter = 0; | ||
await providerWithMiddleware.get(paramA); | ||
await providerWithMiddleware.get(paramA); | ||
logger.log({ | ||
test: 'get-cached', | ||
value: middleware.counter // should be 1 | ||
}); | ||
} catch (err) { | ||
logger.log({ | ||
test: 'get-cached', | ||
error: err.message | ||
}); | ||
} | ||
|
||
// Test 9 | ||
// get parameter twice, but force fetch 2nd time, we count number of SDK requests and check that we made two API calls | ||
try { | ||
providerWithMiddleware.clearCache(); | ||
middleware.counter = 0; | ||
await providerWithMiddleware.get(paramA); | ||
await providerWithMiddleware.get(paramA, { forceFetch: true }); | ||
logger.log({ | ||
test: 'get-forced', | ||
value: middleware.counter // should be 2 | ||
}); | ||
} catch (err) { | ||
logger.log({ | ||
test: 'get-forced', | ||
error: err.message | ||
}); | ||
} | ||
}; |
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.
Uh oh!
There was an error while loading. Please reload this page.