forked from khulnasoft-lab/go-localstack
-
Notifications
You must be signed in to change notification settings - Fork 0
/
localstack_internal_test.go
221 lines (211 loc) · 7.7 KB
/
localstack_internal_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
// Copyright 2021 - Lars Gohr
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package localstack
import (
"context"
"errors"
"github.com/docker/docker/api/types"
"github.com/docker/docker/api/types/container"
"github.com/docker/go-connections/nat"
"github.com/elgohr/go-localstack/internal/internalfakes"
"github.com/sirupsen/logrus"
"github.com/stretchr/testify/require"
"io"
"os"
"strings"
"testing"
"time"
)
func TestInstance_Start_Fails(t *testing.T) {
for _, tt := range [...]struct {
when string
given func(f *internalfakes.FakeDockerClient) *Instance
then func(t *testing.T, err error, f *internalfakes.FakeDockerClient)
}{
{
when: "can't restart localstack when already running",
given: func(f *internalfakes.FakeDockerClient) *Instance {
f.ContainerStopReturns(errors.New("can't stop"))
return &Instance{
cli: f,
log: logrus.StandardLogger(),
containerId: "running",
}
},
then: func(t *testing.T, err error, f *internalfakes.FakeDockerClient) {
require.EqualError(t, err, "localstack: can't stop an already running instance: can't stop")
require.Equal(t, 0, f.ImageBuildCallCount())
require.Equal(t, 0, f.ContainerCreateCallCount())
require.Equal(t, 0, f.ContainerStartCallCount())
require.Equal(t, 0, f.ContainerInspectCallCount())
},
},
{
when: "can't build image",
given: func(f *internalfakes.FakeDockerClient) *Instance {
f.ImageBuildReturns(types.ImageBuildResponse{}, errors.New("can't build"))
return &Instance{
cli: f,
log: logrus.StandardLogger(),
}
},
then: func(t *testing.T, err error, f *internalfakes.FakeDockerClient) {
require.EqualError(t, err, "localstack: could not build image: can't build")
require.Equal(t, 1, f.ImageBuildCallCount())
require.Equal(t, 0, f.ContainerCreateCallCount())
require.Equal(t, 0, f.ContainerStartCallCount())
require.Equal(t, 0, f.ContainerInspectCallCount())
},
},
{
when: "can't close after building image",
given: func(f *internalfakes.FakeDockerClient) *Instance {
f.ImageBuildReturns(types.ImageBuildResponse{Body: ErrCloser(strings.NewReader(""), errors.New("can't close"))}, nil)
f.ContainerCreateReturns(container.CreateResponse{}, errors.New("can't create"))
return &Instance{
cli: f,
log: logrus.StandardLogger(),
}
},
then: func(t *testing.T, err error, f *internalfakes.FakeDockerClient) {
require.EqualError(t, err, "localstack: could not create container: can't create")
},
},
{
when: "can't create container",
given: func(f *internalfakes.FakeDockerClient) *Instance {
f.ImageBuildReturns(types.ImageBuildResponse{Body: io.NopCloser(strings.NewReader(""))}, nil)
f.ContainerCreateReturns(container.CreateResponse{}, errors.New("can't create"))
return &Instance{
cli: f,
log: logrus.StandardLogger(),
}
},
then: func(t *testing.T, err error, f *internalfakes.FakeDockerClient) {
require.EqualError(t, err, "localstack: could not create container: can't create")
require.Equal(t, 1, f.ImageBuildCallCount())
require.Equal(t, 1, f.ContainerCreateCallCount())
ctx, config, hostConfig, networkingConfig, platform, containerName := f.ContainerCreateArgsForCall(0)
require.NotNil(t, ctx)
require.Equal(t, &container.Config{
Image: "go-localstack",
Env: []string{},
Tty: true,
AttachStdout: true,
AttachStderr: true,
}, config)
pm := nat.PortMap{}
for service := range AvailableServices {
pm[nat.Port(service.Port)] = []nat.PortBinding{{HostIP: "0.0.0.0", HostPort: ""}}
}
require.Equal(t, &container.HostConfig{
PortBindings: pm,
AutoRemove: true,
}, hostConfig)
require.Nil(t, networkingConfig)
require.Nil(t, platform)
require.Empty(t, containerName)
require.Equal(t, 0, f.ContainerStartCallCount())
require.Equal(t, 0, f.ContainerInspectCallCount())
},
},
{
when: "can't start container",
given: func(f *internalfakes.FakeDockerClient) *Instance {
f.ImageBuildReturns(types.ImageBuildResponse{Body: io.NopCloser(strings.NewReader(""))}, nil)
f.ContainerStartReturns(errors.New("can't start"))
return &Instance{
cli: f,
log: logrus.StandardLogger(),
}
},
then: func(t *testing.T, err error, f *internalfakes.FakeDockerClient) {
require.EqualError(t, err, "localstack: could not start container: can't start")
require.Equal(t, 1, f.ImageBuildCallCount())
require.Equal(t, 1, f.ContainerCreateCallCount())
require.Equal(t, 1, f.ContainerStartCallCount())
require.Equal(t, 0, f.ContainerInspectCallCount())
},
},
{
when: "container inspect doesn't contain ports",
given: func(f *internalfakes.FakeDockerClient) *Instance {
f.ImageBuildReturns(types.ImageBuildResponse{Body: io.NopCloser(strings.NewReader(""))}, nil)
f.ContainerInspectReturns(types.ContainerJSON{NetworkSettings: &types.NetworkSettings{
NetworkSettingsBase: types.NetworkSettingsBase{
Ports: map[nat.Port][]nat.PortBinding{},
},
}}, nil)
return &Instance{
cli: f,
fixedPort: true,
log: logrus.StandardLogger(),
}
},
then: func(t *testing.T, err error, f *internalfakes.FakeDockerClient) {
require.EqualError(t, err, "localstack: could not get port from container")
require.Equal(t, 1, f.ImageBuildCallCount())
require.Equal(t, 1, f.ContainerCreateCallCount())
require.Equal(t, 1, f.ContainerStartCallCount())
require.Equal(t, 6, f.ContainerInspectCallCount())
},
},
} {
t.Run(tt.when, func(t *testing.T) {
f := &internalfakes.FakeDockerClient{}
f.ContainerLogsReturns(io.NopCloser(strings.NewReader("")), nil)
tt.then(t, tt.given(f).Start(), f)
})
}
}
func TestInstance_StartWithContext_Fails_Stop_AfterTest(t *testing.T) {
f := &internalfakes.FakeDockerClient{}
ctx, cancel := context.WithCancel(context.Background())
cancel()
f.ContainerStopReturns(errors.New("can't stop"))
i := &Instance{cli: f, log: logrus.StandardLogger(), containerId: "something"}
require.EqualError(t, i.StartWithContext(ctx), "localstack: can't stop an already running instance: can't stop")
}
func TestInstance_Stop_Fails(t *testing.T) {
f := &internalfakes.FakeDockerClient{}
f.ContainerStopReturns(errors.New("can't stop"))
i := &Instance{cli: f, log: logrus.StandardLogger(), containerId: "something"}
require.EqualError(t, i.Stop(), "can't stop")
}
func TestInstance_checkAvailable_Session_Fails(t *testing.T) {
ctx, cancel := context.WithTimeout(context.Background(), time.Second)
defer cancel()
require.NoError(t, os.Setenv("AWS_STS_REGIONAL_ENDPOINTS", "FAILURE"))
defer func() {
require.NoError(t, os.Unsetenv("AWS_STS_REGIONAL_ENDPOINTS"))
}()
i := &Instance{log: logrus.StandardLogger()}
require.Error(t, i.checkAvailable(ctx))
}
func TestInstance_waitToBeAvailable_Context_Expired(t *testing.T) {
ctx, cancel := context.WithCancel(context.Background())
cancel()
i := &Instance{log: logrus.StandardLogger()}
require.Error(t, i.waitToBeAvailable(ctx))
}
func ErrCloser(r io.Reader, err error) io.ReadCloser {
return errCloser{Reader: r, Error: err}
}
type errCloser struct {
io.Reader
Error error
}
func (e errCloser) Close() error {
return e.Error
}