Skip to content

Commit

Permalink
chore: use HTTP calls to invoke the lambda from the tests (#1794)
Browse files Browse the repository at this point in the history
  • Loading branch information
mdelapenya authored Oct 25, 2023
1 parent d43fae3 commit b90800d
Showing 1 changed file with 60 additions and 9 deletions.
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
}

0 comments on commit b90800d

Please sign in to comment.