Skip to content

Commit b90800d

Browse files
authored
chore: use HTTP calls to invoke the lambda from the tests (#1794)
1 parent d43fae3 commit b90800d

File tree

1 file changed

+60
-9
lines changed

1 file changed

+60
-9
lines changed

modules/localstack/examples_test.go

Lines changed: 60 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,14 @@
11
package localstack_test
22

33
import (
4+
"bytes"
45
"context"
6+
"encoding/json"
57
"fmt"
68
"io"
9+
"net/http"
710
"path/filepath"
11+
"strings"
812
"time"
913

1014
"github.com/testcontainers/testcontainers-go"
@@ -162,8 +166,8 @@ func ExampleRunContainer_usingLambdas() {
162166

163167
// the three commands below are doing the following:
164168
// 1. create a lambda function
165-
// 2. wait for the lambda function to be active
166-
// 3. invoke the lambda function with a payload, writing the result to the output.txt file
169+
// 2. create the URL function configuration for the lambda function
170+
// 3. wait for the lambda function to be active
167171
lambdaCommands := [][]string{
168172
{
169173
"awslocal", "lambda",
@@ -174,28 +178,75 @@ func ExampleRunContainer_usingLambdas() {
174178
"--handler", "index.handler",
175179
"--role", "arn:aws:iam::000000000000:role/lambda-role",
176180
},
181+
{"awslocal", "lambda", "create-function-url-config", "--function-name", lambdaName, "--auth-type", "NONE"},
177182
{"awslocal", "lambda", "wait", "function-active-v2", "--function-name", lambdaName},
178-
{"awslocal", "lambda", "invoke", "--function-name", lambdaName, "--payload", `{"body": "{\"num1\": \"10\", \"num2\": \"10\"}" }`, "output.txt"},
179183
}
180184
for _, cmd := range lambdaCommands {
181-
_, _, err = container.Exec(ctx, cmd)
185+
_, _, err := container.Exec(ctx, cmd)
182186
if err != nil {
183187
panic(err)
184188
}
185189
}
186190

187-
// the output.txt file lives in the WORKDIR of the localstack container
188-
_, reader, err := container.Exec(ctx, []string{"cat", "output.txt"}, exec.Multiplexed())
191+
// 4. get the URL for the lambda function
192+
cmd := []string{
193+
"awslocal", "lambda", "list-function-url-configs", "--function-name", lambdaName,
194+
}
195+
_, reader, err := container.Exec(ctx, cmd, exec.Multiplexed())
189196
if err != nil {
190197
panic(err)
191198
}
192199

193-
content, err := io.ReadAll(reader)
200+
buf := new(bytes.Buffer)
201+
_, err = buf.ReadFrom(reader)
194202
if err != nil {
195203
panic(err)
196204
}
197-
fmt.Println(string(content))
205+
206+
content := buf.Bytes()
207+
208+
type FunctionURLConfig struct {
209+
FunctionURLConfigs []struct {
210+
FunctionURL string `json:"FunctionUrl"`
211+
FunctionArn string `json:"FunctionArn"`
212+
CreationTime string `json:"CreationTime"`
213+
LastModifiedTime string `json:"LastModifiedTime"`
214+
AuthType string `json:"AuthType"`
215+
} `json:"FunctionUrlConfigs"`
216+
}
217+
218+
v := &FunctionURLConfig{}
219+
err = json.Unmarshal(content, v)
220+
if err != nil {
221+
panic(err)
222+
}
223+
224+
httpClient := http.Client{
225+
Timeout: 5 * time.Second,
226+
}
227+
228+
functionURL := v.FunctionURLConfigs[0].FunctionURL
229+
// replace the port with the one exposed by the container
230+
231+
mappedPort, err := container.MappedPort(ctx, "4566/tcp")
232+
if err != nil {
233+
panic(err)
234+
}
235+
236+
functionURL = strings.ReplaceAll(functionURL, "4566", mappedPort.Port())
237+
238+
resp, err := httpClient.Post(functionURL, "application/json", bytes.NewBufferString(`{"num1": "10", "num2": "10"}`))
239+
if err != nil {
240+
panic(err)
241+
}
242+
243+
jsonResponse, err := io.ReadAll(resp.Body)
244+
if err != nil {
245+
panic(err)
246+
}
247+
248+
fmt.Println(string(jsonResponse))
198249

199250
// Output:
200-
// {"statusCode":200,"body":"The product of 10 and 10 is 100"}
251+
// The product of 10 and 10 is 100
201252
}

0 commit comments

Comments
 (0)