-
Notifications
You must be signed in to change notification settings - Fork 6
Introduce fakes and contract tests for AWS Lambda integration #763
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
base: main
Are you sure you want to change the base?
Changes from all commits
291a404
9a6eca0
cd4e54a
96e6043
1f1784d
93b13dc
d07be83
7eac7cd
f744999
2ff108a
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -139,6 +139,27 @@ func (staticCreds *AWSStaticCreds) NewLambdaClient() (*lambda.Client, error) { | |
| return lambda.NewFromConfig(cfg), nil | ||
| } | ||
|
|
||
| // LambdaAPI is the interface for AWS Lambda operations used by this package. | ||
| // The real *lambda.Client satisfies this implicitly. | ||
| type LambdaAPI interface { | ||
| ListFunctions(ctx context.Context, params *lambda.ListFunctionsInput, optFns ...func(*lambda.Options)) (*lambda.ListFunctionsOutput, error) | ||
| GetFunctionConfiguration(ctx context.Context, params *lambda.GetFunctionConfigurationInput, optFns ...func(*lambda.Options)) (*lambda.GetFunctionConfigurationOutput, error) | ||
| } | ||
|
|
||
| // defaultNewLambdaClient creates a real Lambda client from credentials. | ||
| func defaultNewLambdaClient(creds *AWSStaticCreds) (LambdaAPI, error) { | ||
| return creds.NewLambdaClient() | ||
| } | ||
|
|
||
| // NewLambdaClientFunc is the factory used by GetLambdaPackageData to create a | ||
| // LambdaAPI client. Tests can replace this to inject a FakeLambdaClient. | ||
| var NewLambdaClientFunc = defaultNewLambdaClient | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Observation (non-blocking): Package-level mutable globals for test injection are pragmatic here but worth noting: if
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Concurrency note (non-blocking, echoing earlier review): Package-level mutable global is pragmatic here, but worth noting: if |
||
|
|
||
| // ResetLambdaClientFactory restores the default (real AWS) client factory. | ||
| func ResetLambdaClientFactory() { | ||
| NewLambdaClientFunc = defaultNewLambdaClient | ||
| } | ||
|
|
||
| // NewECSClient returns a new ECS API client | ||
| func (staticCreds *AWSStaticCreds) NewECSClient() (*ecs.Client, error) { | ||
| cfg, err := staticCreds.NewAWSConfigFromEnvOrFlags() | ||
|
|
@@ -149,7 +170,7 @@ func (staticCreds *AWSStaticCreds) NewECSClient() (*ecs.Client, error) { | |
| } | ||
|
|
||
| // getFilteredLambdaFuncs fetches a filtered set of lambda functions recursively (50 at a time) and returns a list of FunctionConfiguration | ||
| func getFilteredLambdaFuncs(client *lambda.Client, nextMarker *string, allFunctions *[]types.FunctionConfiguration, | ||
| func getFilteredLambdaFuncs(client LambdaAPI, nextMarker *string, allFunctions *[]types.FunctionConfiguration, | ||
| filter *filters.ResourceFilterOptions) (*[]types.FunctionConfiguration, error) { | ||
| params := &lambda.ListFunctionsInput{} | ||
| if nextMarker != nil { | ||
|
|
@@ -187,11 +208,16 @@ func getFilteredLambdaFuncs(client *lambda.Client, nextMarker *string, allFuncti | |
|
|
||
| // GetLambdaPackageData returns a digest and metadata of a Lambda function package | ||
| func (staticCreds *AWSStaticCreds) GetLambdaPackageData(filter *filters.ResourceFilterOptions) ([]*LambdaData, error) { | ||
| lambdaData := []*LambdaData{} | ||
| client, err := staticCreds.NewLambdaClient() | ||
| client, err := NewLambdaClientFunc(staticCreds) | ||
| if err != nil { | ||
| return lambdaData, err | ||
| return []*LambdaData{}, err | ||
| } | ||
| return getLambdaPackageDataFromClient(client, filter) | ||
| } | ||
|
|
||
| // getLambdaPackageDataFromClient fetches Lambda function data using the provided LambdaAPI client. | ||
| func getLambdaPackageDataFromClient(client LambdaAPI, filter *filters.ResourceFilterOptions) ([]*LambdaData, error) { | ||
| lambdaData := []*LambdaData{} | ||
|
|
||
| filteredFunctions, err := getFilteredLambdaFuncs(client, nil, &[]types.FunctionConfiguration{}, filter) | ||
| if err != nil { | ||
|
|
@@ -247,7 +273,7 @@ func (staticCreds *AWSStaticCreds) GetLambdaPackageData(filter *filters.Resource | |
| } | ||
|
|
||
| // getAndProcessOneLambdaFunc get a lambda function by its name and return a LambdaData object from it | ||
| func getAndProcessOneLambdaFunc(client *lambda.Client, functionName string) (*LambdaData, error) { | ||
| func getAndProcessOneLambdaFunc(client LambdaAPI, functionName string) (*LambdaData, error) { | ||
| params := &lambda.GetFunctionConfigurationInput{ | ||
| FunctionName: aws.String(functionName), | ||
| } | ||
|
|
||
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.
Good practice:
TearDownTestproperly resets the factory after every test. This prevents test pollution even if a test panics (testify's suite runner handles deferred cleanup). Well done.