From f875d5f130cd1e630f519a3c4bd969220122c7fd Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Manuel=20de=20la=20Pe=C3=B1a?= Date: Wed, 25 Oct 2023 16:30:29 +0200 Subject: [PATCH] chore: use HTTP calls to invoke the lambda from the tests --- modules/localstack/examples_test.go | 69 +++++++++++++++++++++++++---- 1 file changed, 60 insertions(+), 9 deletions(-) diff --git a/modules/localstack/examples_test.go b/modules/localstack/examples_test.go index c9b53c1dcf..277e297405 100644 --- a/modules/localstack/examples_test.go +++ b/modules/localstack/examples_test.go @@ -1,10 +1,14 @@ package localstack_test import ( + "bytes" "context" + "encoding/json" "fmt" "io" + "net/http" "path/filepath" + "strings" "time" "github.com/testcontainers/testcontainers-go" @@ -162,8 +166,8 @@ func ExampleRunContainer_usingLambdas() { // the three commands below are doing the following: // 1. create a lambda function - // 2. wait for the lambda function to be active - // 3. invoke the lambda function with a payload, writing the result to the output.txt file + // 2. create the URL function configuration for the lambda function + // 3. wait for the lambda function to be active lambdaCommands := [][]string{ { "awslocal", "lambda", @@ -174,28 +178,75 @@ func ExampleRunContainer_usingLambdas() { "--handler", "index.handler", "--role", "arn:aws:iam::000000000000:role/lambda-role", }, + {"awslocal", "lambda", "create-function-url-config", "--function-name", lambdaName, "--auth-type", "NONE"}, {"awslocal", "lambda", "wait", "function-active-v2", "--function-name", lambdaName}, - {"awslocal", "lambda", "invoke", "--function-name", lambdaName, "--payload", `{"body": "{\"num1\": \"10\", \"num2\": \"10\"}" }`, "output.txt"}, } for _, cmd := range lambdaCommands { - _, _, err = container.Exec(ctx, cmd) + _, _, err := container.Exec(ctx, cmd) if err != nil { panic(err) } } - // the output.txt file lives in the WORKDIR of the localstack container - _, reader, err := container.Exec(ctx, []string{"cat", "output.txt"}, exec.Multiplexed()) + // 4. get the URL for the lambda function + cmd := []string{ + "awslocal", "lambda", "list-function-url-configs", "--function-name", lambdaName, + } + _, reader, err := container.Exec(ctx, cmd, exec.Multiplexed()) if err != nil { panic(err) } - content, err := io.ReadAll(reader) + buf := new(bytes.Buffer) + _, err = buf.ReadFrom(reader) if err != nil { panic(err) } - fmt.Println(string(content)) + + content := buf.Bytes() + + type FunctionURLConfig struct { + FunctionURLConfigs []struct { + FunctionURL string `json:"FunctionUrl"` + FunctionArn string `json:"FunctionArn"` + CreationTime string `json:"CreationTime"` + LastModifiedTime string `json:"LastModifiedTime"` + AuthType string `json:"AuthType"` + } `json:"FunctionUrlConfigs"` + } + + v := &FunctionURLConfig{} + err = json.Unmarshal(content, v) + if err != nil { + panic(err) + } + + httpClient := http.Client{ + Timeout: 5 * time.Second, + } + + functionURL := v.FunctionURLConfigs[0].FunctionURL + // replace the port with the one exposed by the container + + mappedPort, err := container.MappedPort(ctx, "4566/tcp") + if err != nil { + panic(err) + } + + functionURL = strings.ReplaceAll(functionURL, "4566", mappedPort.Port()) + + resp, err := httpClient.Post(functionURL, "application/json", bytes.NewBufferString(`{"num1": "10", "num2": "10"}`)) + if err != nil { + panic(err) + } + + jsonResponse, err := io.ReadAll(resp.Body) + if err != nil { + panic(err) + } + + fmt.Println(string(jsonResponse)) // Output: - // {"statusCode":200,"body":"The product of 10 and 10 is 100"} + // The product of 10 and 10 is 100 }