|
5 | 5 | "context" |
6 | 6 | "errors" |
7 | 7 | "io" |
| 8 | + "net" |
| 9 | + "strconv" |
8 | 10 | "testing" |
9 | 11 |
|
10 | 12 | "github.com/localstack/lstk/internal/log" |
@@ -53,6 +55,89 @@ func TestEmitPostStartPointers_WithoutWebApp(t *testing.T) { |
53 | 55 | assert.Contains(t, got, "> Tip:") |
54 | 56 | } |
55 | 57 |
|
| 58 | +func TestSelectContainersToStart_AttachesWhenExternalContainerOnConfiguredPort(t *testing.T) { |
| 59 | + ctrl := gomock.NewController(t) |
| 60 | + mockRT := runtime.NewMockRuntime(ctrl) |
| 61 | + |
| 62 | + c := runtime.ContainerConfig{ |
| 63 | + Image: "localstack/localstack-pro:3.5.0", |
| 64 | + Name: "localstack-aws-3.5.0", |
| 65 | + Tag: "3.5.0", |
| 66 | + Port: "4566", |
| 67 | + ContainerPort: "4566/tcp", |
| 68 | + } |
| 69 | + |
| 70 | + mockRT.EXPECT().IsRunning(gomock.Any(), c.Name).Return(false, nil) |
| 71 | + mockRT.EXPECT().FindRunningByImage(gomock.Any(), []string{"localstack/localstack-pro", "localstack/localstack"}, "4566/tcp"). |
| 72 | + Return(&runtime.RunningContainer{Name: "external-container", Image: "localstack/localstack-pro:3.5.0", BoundPort: "4566"}, nil) |
| 73 | + |
| 74 | + var out bytes.Buffer |
| 75 | + sink := output.NewPlainSink(&out) |
| 76 | + |
| 77 | + result, err := selectContainersToStart(context.Background(), mockRT, sink, nil, []runtime.ContainerConfig{c}, "", "") |
| 78 | + |
| 79 | + require.NoError(t, err) |
| 80 | + assert.Empty(t, result, "container should be skipped (already running)") |
| 81 | + assert.Contains(t, out.String(), "already running") |
| 82 | + assert.NotContains(t, out.String(), "config specifies") |
| 83 | +} |
| 84 | + |
| 85 | +func TestSelectContainersToStart_AttachesWhenExternalContainerVersionDiffers(t *testing.T) { |
| 86 | + ctrl := gomock.NewController(t) |
| 87 | + mockRT := runtime.NewMockRuntime(ctrl) |
| 88 | + |
| 89 | + c := runtime.ContainerConfig{ |
| 90 | + Image: "localstack/localstack-pro:3.4.0", |
| 91 | + Name: "localstack-aws-3.4.0", |
| 92 | + Tag: "3.4.0", |
| 93 | + Port: "4566", |
| 94 | + ContainerPort: "4566/tcp", |
| 95 | + } |
| 96 | + |
| 97 | + mockRT.EXPECT().IsRunning(gomock.Any(), c.Name).Return(false, nil) |
| 98 | + mockRT.EXPECT().FindRunningByImage(gomock.Any(), []string{"localstack/localstack-pro", "localstack/localstack"}, "4566/tcp"). |
| 99 | + Return(&runtime.RunningContainer{Name: "external-container", Image: "localstack/localstack-pro:3.5.0", BoundPort: "4566"}, nil) |
| 100 | + |
| 101 | + var out bytes.Buffer |
| 102 | + sink := output.NewPlainSink(&out) |
| 103 | + |
| 104 | + result, err := selectContainersToStart(context.Background(), mockRT, sink, nil, []runtime.ContainerConfig{c}, "", "") |
| 105 | + |
| 106 | + require.NoError(t, err) |
| 107 | + assert.Empty(t, result, "container should be skipped (already running)") |
| 108 | + assert.Contains(t, out.String(), "already running") |
| 109 | +} |
| 110 | + |
| 111 | +func TestSelectContainersToStart_QueuesContainerWhenNoneRunningOnPort(t *testing.T) { |
| 112 | + ctrl := gomock.NewController(t) |
| 113 | + mockRT := runtime.NewMockRuntime(ctrl) |
| 114 | + |
| 115 | + // Use a free port by binding one and immediately releasing it. |
| 116 | + ln, err := net.Listen("tcp", "127.0.0.1:0") |
| 117 | + require.NoError(t, err) |
| 118 | + freePort := ln.Addr().(*net.TCPAddr).Port |
| 119 | + require.NoError(t, ln.Close()) |
| 120 | + |
| 121 | + c := runtime.ContainerConfig{ |
| 122 | + Image: "localstack/localstack-pro:3.5.0", |
| 123 | + Name: "localstack-aws-3.5.0", |
| 124 | + Tag: "3.5.0", |
| 125 | + Port: strconv.Itoa(freePort), |
| 126 | + ContainerPort: "4566/tcp", |
| 127 | + } |
| 128 | + |
| 129 | + mockRT.EXPECT().IsRunning(gomock.Any(), c.Name).Return(false, nil) |
| 130 | + mockRT.EXPECT().FindRunningByImage(gomock.Any(), []string{"localstack/localstack-pro", "localstack/localstack"}, "4566/tcp"). |
| 131 | + Return(nil, nil) |
| 132 | + |
| 133 | + sink := output.NewPlainSink(io.Discard) |
| 134 | + |
| 135 | + result, err := selectContainersToStart(context.Background(), mockRT, sink, nil, []runtime.ContainerConfig{c}, "", "") |
| 136 | + |
| 137 | + require.NoError(t, err) |
| 138 | + assert.Equal(t, []runtime.ContainerConfig{c}, result, "container should be queued for start") |
| 139 | +} |
| 140 | + |
56 | 141 | func TestServicePortRange_ReturnsExpectedPorts(t *testing.T) { |
57 | 142 | ports := servicePortRange() |
58 | 143 |
|
|
0 commit comments