|
| 1 | +package main |
| 2 | + |
| 3 | +import ( |
| 4 | + "encoding/json" |
| 5 | + "io" |
| 6 | + "net/http" |
| 7 | + "net/http/httptest" |
| 8 | + "testing" |
| 9 | + |
| 10 | + "github.com/aws/aws-lambda-runtime-interface-emulator/internal/lsapi" |
| 11 | + "github.com/stretchr/testify/assert" |
| 12 | + "github.com/stretchr/testify/require" |
| 13 | +) |
| 14 | + |
| 15 | +// --- JSON contract tests --- |
| 16 | + |
| 17 | +// TestInvokeRequestContract verifies that InvokeRequest correctly maps the JSON field names |
| 18 | +// that LocalStack sends to the RIE's /invoke endpoint (defined in |
| 19 | +// localstack-pro/localstack-core/localstack/services/lambda_/invocation/execution_environment.py). |
| 20 | +// |
| 21 | +// WARNING: The LocalStack↔RIE API contract is currently unversioned. Any change to these |
| 22 | +// field names is a silent breaking change that requires a coordinated update of both |
| 23 | +// localstack-pro and lambda-runtime-init with no safe rollback path. |
| 24 | +func TestInvokeRequestContract(t *testing.T) { |
| 25 | + raw := `{ |
| 26 | + "invoke-id": "abc-123", |
| 27 | + "invoked-function-arn": "arn:aws:lambda:us-east-1:000000000000:function:my-fn", |
| 28 | + "payload": "{\"key\":\"value\"}", |
| 29 | + "trace-id": "Root=1-abc;Parent=def;Sampled=1" |
| 30 | + }` |
| 31 | + |
| 32 | + var req lsapi.InvokeRequest |
| 33 | + require.NoError(t, json.Unmarshal([]byte(raw), &req)) |
| 34 | + |
| 35 | + assert.Equal(t, "abc-123", req.InvokeId) |
| 36 | + assert.Equal(t, "arn:aws:lambda:us-east-1:000000000000:function:my-fn", req.InvokedFunctionArn) |
| 37 | + assert.Equal(t, `{"key":"value"}`, req.Payload) |
| 38 | + assert.Equal(t, "Root=1-abc;Parent=def;Sampled=1", req.TraceId) |
| 39 | +} |
| 40 | + |
| 41 | +// TestLogResponseContract verifies that LogResponse uses the "logs" JSON key expected by |
| 42 | +// LocalStack's invocation_logs handler (executor_endpoint.py). |
| 43 | +func TestLogResponseContract(t *testing.T) { |
| 44 | + raw := `{"logs":"START RequestId: abc\nEND RequestId: abc\n"}` |
| 45 | + |
| 46 | + var lr lsapi.LogResponse |
| 47 | + require.NoError(t, json.Unmarshal([]byte(raw), &lr)) |
| 48 | + |
| 49 | + assert.Equal(t, "START RequestId: abc\nEND RequestId: abc\n", lr.Logs) |
| 50 | +} |
| 51 | + |
| 52 | +// --- LocalStackAdapter.SendStatus tests --- |
| 53 | + |
| 54 | +func TestSendStatus_ReadySendsToCorrectPath(t *testing.T) { |
| 55 | + var capturedReq *http.Request |
| 56 | + srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { |
| 57 | + capturedReq = r |
| 58 | + w.WriteHeader(http.StatusAccepted) |
| 59 | + })) |
| 60 | + defer srv.Close() |
| 61 | + |
| 62 | + adapter := &LocalStackAdapter{UpstreamEndpoint: srv.URL, RuntimeId: "runtime-abc"} |
| 63 | + require.NoError(t, adapter.SendStatus(Ready, []byte{})) |
| 64 | + |
| 65 | + assert.Equal(t, http.MethodPost, capturedReq.Method) |
| 66 | + assert.Equal(t, "/status/runtime-abc/ready", capturedReq.URL.Path) |
| 67 | +} |
| 68 | + |
| 69 | +func TestSendStatus_ErrorSendsToCorrectPath(t *testing.T) { |
| 70 | + var capturedReq *http.Request |
| 71 | + srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { |
| 72 | + capturedReq = r |
| 73 | + w.WriteHeader(http.StatusAccepted) |
| 74 | + })) |
| 75 | + defer srv.Close() |
| 76 | + |
| 77 | + adapter := &LocalStackAdapter{UpstreamEndpoint: srv.URL, RuntimeId: "runtime-abc"} |
| 78 | + require.NoError(t, adapter.SendStatus(Error, []byte(`{"errorMessage":"init failed"}`))) |
| 79 | + |
| 80 | + assert.Equal(t, http.MethodPost, capturedReq.Method) |
| 81 | + assert.Equal(t, "/status/runtime-abc/error", capturedReq.URL.Path) |
| 82 | +} |
| 83 | + |
| 84 | +// --- LocalStackAdapter.SendLogs tests --- |
| 85 | + |
| 86 | +func TestSendLogs_SendsJSONWithLogsKey(t *testing.T) { |
| 87 | + var capturedPath string |
| 88 | + var capturedBody lsapi.LogResponse |
| 89 | + srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { |
| 90 | + capturedPath = r.URL.Path |
| 91 | + body, _ := io.ReadAll(r.Body) |
| 92 | + _ = json.Unmarshal(body, &capturedBody) |
| 93 | + w.WriteHeader(http.StatusAccepted) |
| 94 | + })) |
| 95 | + defer srv.Close() |
| 96 | + |
| 97 | + adapter := &LocalStackAdapter{UpstreamEndpoint: srv.URL} |
| 98 | + logs := lsapi.LogResponse{Logs: "START RequestId: invoke-1\nEND RequestId: invoke-1\n"} |
| 99 | + require.NoError(t, adapter.SendLogs("invoke-1", logs)) |
| 100 | + |
| 101 | + assert.Equal(t, "/invocations/invoke-1/logs", capturedPath) |
| 102 | + assert.Equal(t, logs.Logs, capturedBody.Logs) |
| 103 | +} |
| 104 | + |
| 105 | +// --- LocalStackAdapter.SendResult routing tests --- |
| 106 | + |
| 107 | +func TestSendResult_SuccessGoesToResponseEndpoint(t *testing.T) { |
| 108 | + var capturedPath string |
| 109 | + srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { |
| 110 | + capturedPath = r.URL.Path |
| 111 | + w.WriteHeader(http.StatusAccepted) |
| 112 | + })) |
| 113 | + defer srv.Close() |
| 114 | + |
| 115 | + adapter := &LocalStackAdapter{UpstreamEndpoint: srv.URL} |
| 116 | + require.NoError(t, adapter.SendResult("invoke-1", []byte(`{"result":"ok"}`), false)) |
| 117 | + |
| 118 | + assert.Equal(t, "/invocations/invoke-1/response", capturedPath) |
| 119 | +} |
| 120 | + |
| 121 | +func TestSendResult_ErrorBodyGoesToErrorEndpoint(t *testing.T) { |
| 122 | + var capturedPath string |
| 123 | + srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { |
| 124 | + capturedPath = r.URL.Path |
| 125 | + w.WriteHeader(http.StatusAccepted) |
| 126 | + })) |
| 127 | + defer srv.Close() |
| 128 | + |
| 129 | + // Body contains "errorType" — LocalStack distinguishes function errors this way |
| 130 | + adapter := &LocalStackAdapter{UpstreamEndpoint: srv.URL} |
| 131 | + errBody := []byte(`{"errorMessage":"something went wrong","errorType":"RuntimeError"}`) |
| 132 | + require.NoError(t, adapter.SendResult("invoke-1", errBody, false)) |
| 133 | + |
| 134 | + assert.Equal(t, "/invocations/invoke-1/error", capturedPath) |
| 135 | +} |
| 136 | + |
| 137 | +func TestSendResult_ExplicitErrorFlagGoesToErrorEndpoint(t *testing.T) { |
| 138 | + var capturedPath string |
| 139 | + srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { |
| 140 | + capturedPath = r.URL.Path |
| 141 | + w.WriteHeader(http.StatusAccepted) |
| 142 | + })) |
| 143 | + defer srv.Close() |
| 144 | + |
| 145 | + // isError=true covers cases like timeout where the RIE itself constructs the error body |
| 146 | + adapter := &LocalStackAdapter{UpstreamEndpoint: srv.URL} |
| 147 | + require.NoError(t, adapter.SendResult("invoke-1", []byte(`{"errorMessage":"Task timed out"}`), true)) |
| 148 | + |
| 149 | + assert.Equal(t, "/invocations/invoke-1/error", capturedPath) |
| 150 | +} |
0 commit comments