MCPcopy Index your code
hub / github.com/aws/aws-lambda-go / TestStartViaEmulator

Function TestStartViaEmulator

lambdaurl/http_handler_test.go:216–267  ·  view source on GitHub ↗
(t *testing.T)

Source from the content-addressed store, hash-verified

214}
215
216func TestStartViaEmulator(t *testing.T) {
217 addr1 := "localhost:" + strconv.Itoa(6001)
218 addr2 := "localhost:" + strconv.Itoa(7001)
219 rieInvokeAPI := "http://" + addr1 + "/2015-03-31/functions/function/invocations"
220 if _, err := exec.LookPath("aws-lambda-rie"); err != nil {
221 t.Skipf("%v - install from https://github.com/aws/aws-lambda-runtime-interface-emulator/", err)
222 }
223
224 // compile our handler, it'll always run to timeout ensuring the SIGTERM is triggered by aws-lambda-rie
225 testDir := t.TempDir()
226 handlerBuild := exec.Command("go", "build", "-o", path.Join(testDir, "lambdaurl.handler"), "./testdata/lambdaurl.go")
227 handlerBuild.Stderr = os.Stderr
228 handlerBuild.Stdout = os.Stderr
229 require.NoError(t, handlerBuild.Run())
230
231 // run the runtime interface emulator, capture the logs for assertion
232 cmd := exec.Command("aws-lambda-rie", "--runtime-interface-emulator-address", addr1, "--runtime-api-address", addr2, "lambdaurl.handler")
233 cmd.Env = []string{
234 "PATH=" + testDir,
235 "AWS_LAMBDA_FUNCTION_TIMEOUT=2",
236 }
237 cmd.Stderr = os.Stderr
238 stdout, err := cmd.StdoutPipe()
239 require.NoError(t, err)
240 var logs string
241 done := make(chan interface{}) // closed on completion of log flush
242 go func() {
243 logBytes, err := ioutil.ReadAll(stdout)
244 require.NoError(t, err)
245 logs = string(logBytes)
246 close(done)
247 }()
248 require.NoError(t, cmd.Start())
249 t.Cleanup(func() { _ = cmd.Process.Kill() })
250
251 // give a moment for the port to bind
252 time.Sleep(500 * time.Millisecond)
253
254 client := &http.Client{Timeout: 5 * time.Second} // http client timeout to prevent case from hanging on aws-lambda-rie
255 resp, err := client.Post(rieInvokeAPI, "application/json", strings.NewReader("{}"))
256 require.NoError(t, err)
257 defer resp.Body.Close()
258 body, err := ioutil.ReadAll(resp.Body)
259 assert.NoError(t, err)
260
261 expected := "{\"statusCode\":200,\"headers\":{\"Content-Type\":\"text/html; charset=utf-8\"}}\x00\x00\x00\x00\x00\x00\x00\x00<!DOCTYPE HTML>\n<html>\n<body>\nHello World!\n</body>\n</html>\n"
262 assert.Equal(t, expected, string(body))
263
264 require.NoError(t, cmd.Process.Kill()) // now ensure the logs are drained
265 <-done
266 t.Logf("stdout:\n%s", logs)
267}

Callers

nothing calls this directly

Calls 1

CloseMethod · 0.45

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…