|
| 1 | +package container |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "fmt" |
| 6 | + "io" |
| 7 | + "net/http" |
| 8 | + "net/http/httptest" |
| 9 | + "strings" |
| 10 | + "testing" |
| 11 | + "time" |
| 12 | + |
| 13 | + "github.com/localstack/lstk/internal/config" |
| 14 | + "github.com/localstack/lstk/internal/output" |
| 15 | + "github.com/localstack/lstk/internal/runtime" |
| 16 | + "github.com/stretchr/testify/assert" |
| 17 | + "github.com/stretchr/testify/require" |
| 18 | + "go.uber.org/mock/gomock" |
| 19 | +) |
| 20 | + |
| 21 | +func TestStatus_NotRunning(t *testing.T) { |
| 22 | + ctrl := gomock.NewController(t) |
| 23 | + mockRT := runtime.NewMockRuntime(ctrl) |
| 24 | + mockRT.EXPECT().IsRunning(gomock.Any(), "localstack-aws").Return(false, nil) |
| 25 | + |
| 26 | + containers := []config.ContainerConfig{{Type: config.EmulatorAWS}} |
| 27 | + sink := output.NewPlainSink(io.Discard) |
| 28 | + |
| 29 | + err := Status(context.Background(), mockRT, containers, "", sink) |
| 30 | + |
| 31 | + require.Error(t, err) |
| 32 | + assert.True(t, output.IsSilent(err)) |
| 33 | + assert.Contains(t, err.Error(), "is not running") |
| 34 | +} |
| 35 | + |
| 36 | +func TestStatus_IsRunningError(t *testing.T) { |
| 37 | + ctrl := gomock.NewController(t) |
| 38 | + mockRT := runtime.NewMockRuntime(ctrl) |
| 39 | + mockRT.EXPECT().IsRunning(gomock.Any(), "localstack-aws").Return(false, fmt.Errorf("docker unavailable")) |
| 40 | + |
| 41 | + containers := []config.ContainerConfig{{Type: config.EmulatorAWS}} |
| 42 | + sink := output.NewPlainSink(io.Discard) |
| 43 | + |
| 44 | + err := Status(context.Background(), mockRT, containers, "", sink) |
| 45 | + |
| 46 | + require.Error(t, err) |
| 47 | + assert.Contains(t, err.Error(), "docker unavailable") |
| 48 | +} |
| 49 | + |
| 50 | +func TestStatus_RunningWithResources(t *testing.T) { |
| 51 | + server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { |
| 52 | + switch r.URL.Path { |
| 53 | + case "/_localstack/health": |
| 54 | + w.Header().Set("Content-Type", "application/json") |
| 55 | + _, _ = fmt.Fprintln(w, `{"version": "4.14.1"}`) |
| 56 | + case "/_localstack/resources": |
| 57 | + w.Header().Set("Content-Type", "application/x-ndjson") |
| 58 | + _, _ = fmt.Fprintln(w, `{"AWS::S3::Bucket": [{"region_name": "us-east-1", "account_id": "000000000000", "id": "my-bucket"}]}`) |
| 59 | + _, _ = fmt.Fprintln(w, `{"AWS::Lambda::Function": [{"region_name": "us-east-1", "account_id": "000000000000", "id": "my-func"}]}`) |
| 60 | + } |
| 61 | + })) |
| 62 | + defer server.Close() |
| 63 | + |
| 64 | + host := strings.TrimPrefix(server.URL, "http://") |
| 65 | + |
| 66 | + ctrl := gomock.NewController(t) |
| 67 | + mockRT := runtime.NewMockRuntime(ctrl) |
| 68 | + mockRT.EXPECT().IsRunning(gomock.Any(), "localstack-aws").Return(true, nil) |
| 69 | + mockRT.EXPECT().ContainerStartedAt(gomock.Any(), "localstack-aws").Return(time.Now().Add(-5*time.Minute), nil) |
| 70 | + |
| 71 | + containers := []config.ContainerConfig{{Type: config.EmulatorAWS}} |
| 72 | + var buf strings.Builder |
| 73 | + sink := output.NewPlainSink(&buf) |
| 74 | + |
| 75 | + err := Status(context.Background(), mockRT, containers, host, sink) |
| 76 | + |
| 77 | + require.NoError(t, err) |
| 78 | + out := buf.String() |
| 79 | + assert.Contains(t, out, "is running") |
| 80 | + assert.Contains(t, out, "4.14.1") |
| 81 | + assert.Contains(t, out, "S3") |
| 82 | + assert.Contains(t, out, "my-bucket") |
| 83 | + assert.Contains(t, out, "Lambda") |
| 84 | + assert.Contains(t, out, "my-func") |
| 85 | + assert.Contains(t, out, "2 resources") |
| 86 | + assert.Contains(t, out, "2 services") |
| 87 | +} |
| 88 | + |
| 89 | +func TestStatus_RunningNoResources(t *testing.T) { |
| 90 | + server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { |
| 91 | + switch r.URL.Path { |
| 92 | + case "/_localstack/health": |
| 93 | + w.Header().Set("Content-Type", "application/json") |
| 94 | + _, _ = fmt.Fprintln(w, `{"version": "4.14.1"}`) |
| 95 | + case "/_localstack/resources": |
| 96 | + w.Header().Set("Content-Type", "application/x-ndjson") |
| 97 | + } |
| 98 | + })) |
| 99 | + defer server.Close() |
| 100 | + |
| 101 | + host := strings.TrimPrefix(server.URL, "http://") |
| 102 | + |
| 103 | + ctrl := gomock.NewController(t) |
| 104 | + mockRT := runtime.NewMockRuntime(ctrl) |
| 105 | + mockRT.EXPECT().IsRunning(gomock.Any(), "localstack-aws").Return(true, nil) |
| 106 | + mockRT.EXPECT().ContainerStartedAt(gomock.Any(), "localstack-aws").Return(time.Time{}, fmt.Errorf("not found")) |
| 107 | + |
| 108 | + containers := []config.ContainerConfig{{Type: config.EmulatorAWS}} |
| 109 | + var buf strings.Builder |
| 110 | + sink := output.NewPlainSink(&buf) |
| 111 | + |
| 112 | + err := Status(context.Background(), mockRT, containers, host, sink) |
| 113 | + |
| 114 | + require.NoError(t, err) |
| 115 | + out := buf.String() |
| 116 | + assert.Contains(t, out, "is running") |
| 117 | + assert.Contains(t, out, "No resources deployed") |
| 118 | +} |
| 119 | + |
| 120 | +func TestStatus_MultipleContainers_StopsAtFirstNotRunning(t *testing.T) { |
| 121 | + ctrl := gomock.NewController(t) |
| 122 | + mockRT := runtime.NewMockRuntime(ctrl) |
| 123 | + mockRT.EXPECT().IsRunning(gomock.Any(), "localstack-aws").Return(false, nil) |
| 124 | + |
| 125 | + containers := []config.ContainerConfig{ |
| 126 | + {Type: config.EmulatorAWS}, |
| 127 | + {Type: config.EmulatorSnowflake}, |
| 128 | + } |
| 129 | + sink := output.NewPlainSink(io.Discard) |
| 130 | + |
| 131 | + err := Status(context.Background(), mockRT, containers, "", sink) |
| 132 | + |
| 133 | + require.Error(t, err) |
| 134 | + assert.True(t, output.IsSilent(err)) |
| 135 | +} |
0 commit comments