Skip to content
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

chore: use HTTP calls to invoke the lambda from the tests #1794

Merged
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
69 changes: 60 additions & 9 deletions modules/localstack/examples_test.go
Original file line number Diff line number Diff line change
@@ -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"
Expand Down Expand Up @@ -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",
Expand All @@ -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
}
Loading