Skip to content
This repository was archived by the owner on Dec 2, 2020. It is now read-only.

Commit 3fad6dd

Browse files
committed
Make Lambda client mock-able
1 parent 1ce6f40 commit 3fad6dd

File tree

9 files changed

+2458
-36
lines changed

9 files changed

+2458
-36
lines changed

Makefile

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,15 +22,18 @@ $(LOCAL_BINARY): $(SOURCES) GITCOMMIT_SHA
2222

2323
.PHONY: test
2424
test:
25-
env -i PATH=$$PATH GOPATH=$$GOPATH GOROOT=$$GOROOT go test -timeout=120s -v -cover ./...
25+
go test -v -timeout 30s -short -cover $(shell go list ./img2lambda/... | grep -v /vendor/)
2626

2727
.PHONY: generate
2828
generate: $(SOURCES)
29-
PATH=$(LOCAL_PATH) go generate ./...
29+
PATH=$(LOCAL_PATH) go generate -x $(shell go list ./img2lambda/... | grep -v '/vendor/')
3030

3131
.PHONY: install-deps
3232
install-deps:
3333
curl https://raw.githubusercontent.com/golang/dep/master/install.sh | sh
34+
go get golang.org/x/tools/cmd/cover
35+
go get github.com/golang/mock/mockgen
36+
go get golang.org/x/tools/cmd/goimports
3437

3538
.PHONY: windows-build
3639
windows-build: $(WINDOWS_BINARY)

img2lambda/cli/main.go

Lines changed: 15 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -8,20 +8,13 @@ import (
88

99
"github.com/awslabs/aws-lambda-container-image-converter/img2lambda/extract"
1010
"github.com/awslabs/aws-lambda-container-image-converter/img2lambda/publish"
11+
"github.com/awslabs/aws-lambda-container-image-converter/img2lambda/types"
1112
"github.com/awslabs/aws-lambda-container-image-converter/img2lambda/version"
1213
"github.com/urfave/cli"
1314
)
1415

15-
type cmdOptions struct {
16-
image string // Name of the container image
17-
region string // AWS region
18-
outputDir string // Output directory for the Lambda layers
19-
dryRun bool // Dry-run (will not register with Lambda)
20-
layerNamespace string // Prefix for published Lambda layers
21-
}
22-
23-
func createApp() (*cli.App, *cmdOptions) {
24-
opts := cmdOptions{}
16+
func createApp() (*cli.App, *types.CmdOptions) {
17+
opts := types.CmdOptions{}
2518

2619
app := cli.NewApp()
2720
app.EnableBashCompletion = true
@@ -36,30 +29,30 @@ func createApp() (*cli.App, *cmdOptions) {
3629
cli.StringFlag{
3730
Name: "image, i",
3831
Usage: "Name of the source container image. For example, 'my-docker-image:latest'. The Docker daemon must be pulled locally already.",
39-
Destination: &opts.image,
32+
Destination: &opts.Image,
4033
},
4134
cli.StringFlag{
4235
Name: "region, r",
4336
Usage: "AWS region",
4437
Value: "us-east-1",
45-
Destination: &opts.region,
38+
Destination: &opts.Region,
4639
},
4740
cli.StringFlag{
4841
Name: "output-directory, o",
4942
Usage: "Destination directory for command output",
5043
Value: "./output",
51-
Destination: &opts.outputDir,
44+
Destination: &opts.OutputDir,
5245
},
5346
cli.StringFlag{
5447
Name: "layer-namespace, n",
5548
Usage: "Prefix for the layers published to Lambda",
5649
Value: "img2lambda",
57-
Destination: &opts.layerNamespace,
50+
Destination: &opts.LayerNamespace,
5851
},
5952
cli.BoolFlag{
6053
Name: "dry-run, d",
6154
Usage: "Conduct a dry-run: Repackage the image, but only write the Lambda layers to local disk (do not publish to Lambda)",
62-
Destination: &opts.dryRun,
55+
Destination: &opts.DryRun,
6356
},
6457
}
6558

@@ -69,15 +62,15 @@ func createApp() (*cli.App, *cmdOptions) {
6962
return app, &opts
7063
}
7164

72-
func validateCliOptions(opts *cmdOptions, context *cli.Context) {
73-
if opts.image == "" {
74-
fmt.Println("ERROR: Image name is required\n")
65+
func validateCliOptions(opts *types.CmdOptions, context *cli.Context) {
66+
if opts.Image == "" {
67+
fmt.Print("ERROR: Image name is required\n\n")
7568
cli.ShowAppHelpAndExit(context, 1)
7669
}
7770
}
7871

79-
func repackImageAction(opts *cmdOptions) error {
80-
layers, err := extract.RepackImage("docker-daemon:"+opts.image, opts.outputDir)
72+
func repackImageAction(opts *types.CmdOptions) error {
73+
layers, err := extract.RepackImage("docker-daemon:"+opts.Image, opts.OutputDir)
8174
if err != nil {
8275
return err
8376
}
@@ -86,8 +79,8 @@ func repackImageAction(opts *cmdOptions) error {
8679
return errors.New("No compatible layers found in the image (likely nothing found in /opt)")
8780
}
8881

89-
if !opts.dryRun {
90-
err := publish.PublishLambdaLayers(opts.image, layers, opts.region, opts.layerNamespace, opts.outputDir)
82+
if !opts.DryRun {
83+
err := publish.PublishLambdaLayers(types.ConvertToPublishOptions(opts), layers)
9184
if err != nil {
9285
return err
9386
}

img2lambda/clients/clients.go

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
package clients
2+
3+
import (
4+
"github.com/aws/aws-sdk-go/aws"
5+
"github.com/aws/aws-sdk-go/aws/request"
6+
"github.com/aws/aws-sdk-go/aws/session"
7+
"github.com/aws/aws-sdk-go/service/lambda"
8+
"github.com/awslabs/aws-lambda-container-image-converter/img2lambda/version"
9+
)
10+
11+
var userAgentHandler = request.NamedHandler{
12+
Name: "img2lambda.UserAgentHandler",
13+
Fn: request.MakeAddToUserAgentHandler("aws-lambda-container-image-converter", version.Version),
14+
}
15+
16+
func NewLambdaClient(region string) *lambda.Lambda {
17+
sess := session.Must(session.NewSession())
18+
19+
sess.Handlers.Build.PushBackNamed(userAgentHandler)
20+
21+
client := lambda.New(sess, &aws.Config{Region: aws.String(region)})
22+
23+
return client
24+
}
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
package testing
2+
3+
//go:generate mockgen.sh github.com/aws/aws-sdk-go/service/lambda/lambdaiface LambdaAPI mocks/lambda_mocks.go
4+
//go:generate mockgen.sh github.com/containers/image/types ImageReference,ImageSource mocks/image_mocks.go

0 commit comments

Comments
 (0)