|
| 1 | +package snapshot_test |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "io" |
| 6 | + "net/http" |
| 7 | + "net/http/httptest" |
| 8 | + "strings" |
| 9 | + "testing" |
| 10 | + |
| 11 | + "github.com/localstack/lstk/internal/snapshot" |
| 12 | + "github.com/stretchr/testify/assert" |
| 13 | + "github.com/stretchr/testify/require" |
| 14 | +) |
| 15 | + |
| 16 | +func TestStateClient_ExportState_OK(t *testing.T) { |
| 17 | + srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { |
| 18 | + assert.Equal(t, "/_localstack/pods/state", r.URL.Path) |
| 19 | + assert.Equal(t, http.MethodGet, r.Method) |
| 20 | + w.WriteHeader(http.StatusOK) |
| 21 | + _, _ = w.Write([]byte("ZIP_DATA")) |
| 22 | + })) |
| 23 | + defer srv.Close() |
| 24 | + |
| 25 | + client := snapshot.NewStateClient(srv.URL) |
| 26 | + body, err := client.ExportState(context.Background()) |
| 27 | + require.NoError(t, err) |
| 28 | + defer func() { _ = body.Close() }() |
| 29 | + |
| 30 | + data, err := io.ReadAll(body) |
| 31 | + require.NoError(t, err) |
| 32 | + assert.Equal(t, "ZIP_DATA", string(data)) |
| 33 | +} |
| 34 | + |
| 35 | +func TestStateClient_ExportState_ServerError(t *testing.T) { |
| 36 | + srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { |
| 37 | + w.WriteHeader(http.StatusInternalServerError) |
| 38 | + })) |
| 39 | + defer srv.Close() |
| 40 | + |
| 41 | + client := snapshot.NewStateClient(srv.URL) |
| 42 | + _, err := client.ExportState(context.Background()) |
| 43 | + require.Error(t, err) |
| 44 | + assert.Contains(t, err.Error(), "500") |
| 45 | +} |
| 46 | + |
| 47 | +func TestStateClient_ExportState_NotFound(t *testing.T) { |
| 48 | + srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { |
| 49 | + w.WriteHeader(http.StatusNotFound) |
| 50 | + })) |
| 51 | + defer srv.Close() |
| 52 | + |
| 53 | + client := snapshot.NewStateClient(srv.URL) |
| 54 | + _, err := client.ExportState(context.Background()) |
| 55 | + require.Error(t, err) |
| 56 | + assert.Contains(t, err.Error(), "404") |
| 57 | +} |
| 58 | + |
| 59 | +func TestStateClient_ExportState_ConnectionRefused(t *testing.T) { |
| 60 | + // Bind then immediately close to get a port that refuses connections. |
| 61 | + srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {})) |
| 62 | + addr := srv.URL |
| 63 | + srv.Close() |
| 64 | + |
| 65 | + client := snapshot.NewStateClient(addr) |
| 66 | + _, err := client.ExportState(context.Background()) |
| 67 | + require.Error(t, err) |
| 68 | + assert.Contains(t, err.Error(), "connect to LocalStack") |
| 69 | +} |
| 70 | + |
| 71 | +func TestStateClient_ExportState_ContextCancelled(t *testing.T) { |
| 72 | + started := make(chan struct{}) |
| 73 | + srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { |
| 74 | + close(started) |
| 75 | + // block until the client cancels |
| 76 | + <-r.Context().Done() |
| 77 | + })) |
| 78 | + defer srv.Close() |
| 79 | + |
| 80 | + ctx, cancel := context.WithCancel(context.Background()) |
| 81 | + client := snapshot.NewStateClient(srv.URL) |
| 82 | + |
| 83 | + errCh := make(chan error, 1) |
| 84 | + go func() { |
| 85 | + _, err := client.ExportState(ctx) |
| 86 | + errCh <- err |
| 87 | + }() |
| 88 | + |
| 89 | + <-started |
| 90 | + cancel() |
| 91 | + |
| 92 | + err := <-errCh |
| 93 | + require.Error(t, err) |
| 94 | +} |
| 95 | + |
| 96 | +func TestStateClient_ExportState_LargeBody(t *testing.T) { |
| 97 | + const size = 1 << 20 // 1 MB |
| 98 | + payload := strings.Repeat("X", size) |
| 99 | + |
| 100 | + srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { |
| 101 | + w.WriteHeader(http.StatusOK) |
| 102 | + _, _ = w.Write([]byte(payload)) |
| 103 | + })) |
| 104 | + defer srv.Close() |
| 105 | + |
| 106 | + client := snapshot.NewStateClient(srv.URL) |
| 107 | + body, err := client.ExportState(context.Background()) |
| 108 | + require.NoError(t, err) |
| 109 | + defer func() { _ = body.Close() }() |
| 110 | + |
| 111 | + data, err := io.ReadAll(body) |
| 112 | + require.NoError(t, err) |
| 113 | + assert.Equal(t, size, len(data)) |
| 114 | +} |
0 commit comments