forked from elgohr/go-localstack
-
Notifications
You must be signed in to change notification settings - Fork 0
/
localstack_test.go
156 lines (140 loc) · 3.85 KB
/
localstack_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
package localstack_test
import (
"fmt"
"github.com/stretchr/testify/require"
"net"
"os"
"strings"
"testing"
"github.com/elgohr/go-localstack"
)
func TestLocalStack(t *testing.T) {
for _, s := range []struct {
name string
input []localstack.InstanceOption
expect func(t *testing.T, l *localstack.Instance)
}{
{
name: "with version before breaking change",
input: []localstack.InstanceOption{localstack.WithVersion("0.11.4")},
expect: havingIndividualEndpoints,
},
{
name: "with nil",
input: nil,
expect: havingOneEndpoint,
},
{
name: "with empty",
input: []localstack.InstanceOption{},
expect: havingOneEndpoint,
},
{
name: "with breaking change version",
input: []localstack.InstanceOption{localstack.WithVersion("0.11.5")},
expect: havingOneEndpoint,
},
{
name: "with version after breaking change",
input: []localstack.InstanceOption{localstack.WithVersion("latest")},
expect: havingOneEndpoint,
},
} {
t.Run(s.name, func(t *testing.T) {
l, err := localstack.NewInstance(s.input...)
require.NoError(t, err)
require.NoError(t, l.Start())
defer l.Stop()
s.expect(t, l)
})
}
}
func TestInstanceStartedTwiceWithoutLeaking(t *testing.T) {
l, err := localstack.NewInstance()
require.NoError(t, err)
defer l.Stop()
require.NoError(t, l.Start())
firstInstance := l.Endpoint(localstack.S3)
require.NoError(t, l.Start())
_, err = net.Dial("tcp", firstInstance)
require.Error(t, err, "should be teared down")
}
func TestInstanceWithVersions(t *testing.T) {
for _, s := range []struct {
version string
expect func(t require.TestingT, err error, msgAndArgs ...interface{})
}{
{version: "0.11.5", expect: require.NoError},
{version: "0.11.3", expect: require.NoError},
{version: "latest", expect: require.NoError},
{version: "bad.version.34", expect: require.Error},
} {
t.Run(s.version, func(t *testing.T) {
_, err := localstack.NewInstance(localstack.WithVersion(s.version))
s.expect(t, err)
})
}
}
func TestInstanceWithBadDockerEnvironment(t *testing.T) {
urlIfSet := os.Getenv("DOCKER_URL")
defer os.Setenv("DOCKER_URL", urlIfSet)
os.Setenv("DOCKER_URL", "what-is-this-thing:///var/run/not-a-valid-docker.sock")
_, err := localstack.NewInstance()
require.Error(t, err)
}
func TestInstanceStopWithoutStarted(t *testing.T) {
l, err := localstack.NewInstance()
require.NoError(t, err)
require.NoError(t, l.Stop())
}
func TestInstanceEndpointWithoutStarted(t *testing.T) {
l, err := localstack.NewInstance()
require.NoError(t, err)
require.Empty(t, l.Endpoint(localstack.S3))
}
func havingOneEndpoint(t *testing.T, l *localstack.Instance) {
endpoints := map[string]struct{}{}
for _, service := range services {
endpoints[l.Endpoint(service)] = struct{}{}
}
require.Equal(t, 1, len(endpoints), endpoints)
}
func havingIndividualEndpoints(t *testing.T, l *localstack.Instance) {
endpoints := map[string]struct{}{}
for _, service := range services {
endpoint := l.Endpoint(service)
checkAddress(t, endpoint)
_, exists := endpoints[endpoint]
require.False(t, exists, fmt.Sprintf("%s duplicated in %v", endpoint, endpoints))
endpoints[endpoint] = struct{}{}
}
require.Equal(t, len(services), len(endpoints))
}
func checkAddress(t *testing.T, val string) {
require.True(t, strings.HasPrefix(val, "localhost:"), val)
require.NotEmpty(t, val[10:])
}
var services = []localstack.Service{
localstack.CloudFormation,
localstack.CloudWatch,
localstack.CloudWatchLogs,
localstack.CloudWatchEvents,
localstack.DynamoDB,
localstack.DynamoDBStreams,
localstack.EC2,
localstack.ES,
localstack.Firehose,
localstack.IAM,
localstack.Kinesis,
localstack.Lambda,
localstack.Redshift,
localstack.Route53,
localstack.S3,
localstack.SecretsManager,
localstack.SES,
localstack.SNS,
localstack.SQS,
localstack.SSM,
localstack.STS,
localstack.StepFunctions,
}