-
-
Notifications
You must be signed in to change notification settings - Fork 4
/
ensemble_test.go
205 lines (179 loc) · 5.32 KB
/
ensemble_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
package ensemble_test
import (
"context"
"testing"
"github.com/stretchr/testify/require"
"github.com/testcontainers/testcontainers-go"
kafkaTC "github.com/testcontainers/testcontainers-go/modules/kafka"
"github.com/testcontainers/testcontainers-go/network"
"github.com/testcontainers/testcontainers-go/wait"
"microcks.io/testcontainers-go/ensemble"
"microcks.io/testcontainers-go/ensemble/async/connection/kafka"
"microcks.io/testcontainers-go/internal/test"
)
func TestMockingFunctionalityAtStartup(t *testing.T) {
ctx := context.Background()
// Ensemble containers.
ec, err := ensemble.RunContainers(ctx,
ensemble.WithMainArtifact("../testdata/apipastries-openapi.yaml"),
ensemble.WithSecondaryArtifact("../testdata/apipastries-postman-collection.json"),
)
require.NoError(t, err)
// Cleanup containers.
t.Cleanup(func() {
if err := ec.Terminate(ctx); err != nil {
t.Fatalf("failed to terminate container: %s", err)
}
})
// Tests & assertions.
test.ConfigRetrieval(t, ctx, ec.GetMicrocksContainer())
test.MockEndpoints(t, ctx, ec.GetMicrocksContainer())
test.MicrocksMockingFunctionality(t, ctx, ec.GetMicrocksContainer())
}
func TestPostmanContractTestingFunctionality(t *testing.T) {
ctx := context.Background()
// Ensemble containers.
ec, err := ensemble.RunContainers(
ctx,
ensemble.WithMainArtifact("../testdata/apipastries-openapi.yaml"),
ensemble.WithSecondaryArtifact("../testdata/apipastries-postman-collection.json"),
ensemble.WithPostman(),
)
require.NoError(t, err)
networkName := ec.GetNetwork().Name
// Demo pastry bad implementation.
badImpl, err := testcontainers.GenericContainer(ctx, testcontainers.GenericContainerRequest{
ContainerRequest: testcontainers.ContainerRequest{
Image: "quay.io/microcks/contract-testing-demo:02",
Networks: []string{networkName},
NetworkAliases: map[string][]string{
networkName: {"bad-impl"},
},
WaitingFor: wait.ForLog("Example app listening on port 3002"),
},
Started: true,
})
require.NoError(t, err)
// Demo pastry good implementation.
goodImpl, err := testcontainers.GenericContainer(ctx, testcontainers.GenericContainerRequest{
ContainerRequest: testcontainers.ContainerRequest{
Image: "quay.io/microcks/contract-testing-demo:03",
Networks: []string{networkName},
NetworkAliases: map[string][]string{
networkName: {"good-impl"},
},
WaitingFor: wait.ForLog("Example app listening on port 3003"),
},
Started: true,
})
require.NoError(t, err)
// Cleanup containers.
t.Cleanup(func() {
if err := ec.GetMicrocksContainer().Terminate(ctx); err != nil {
t.Fatalf("failed to terminate container: %s", err)
}
if err := badImpl.Terminate(ctx); err != nil {
t.Fatalf("failed to terminate container: %s", err)
}
if err := goodImpl.Terminate(ctx); err != nil {
t.Fatalf("failed to terminate container: %s", err)
}
})
// Tests & assertions.
test.ConfigRetrieval(t, ctx, ec.GetMicrocksContainer())
test.MicrocksContractTestingFunctionality(
t,
ctx,
ec.GetMicrocksContainer(),
badImpl,
goodImpl,
)
}
func TestAsyncFeatureSetup(t *testing.T) {
ctx := context.Background()
// Ensemble containers.
ec, err := ensemble.RunContainers(
ctx,
ensemble.WithAsyncFeature(),
ensemble.WithHostAccessPorts([]int{8080}),
)
require.NoError(t, err)
// Cleanup containers.
t.Cleanup(func() {
if err := ec.Terminate(ctx); err != nil {
t.Fatalf("failed to terminate container: %s", err)
}
})
// Tests & assertions.
test.ConfigRetrieval(t, ctx, ec.GetMicrocksContainer())
}
func TestAsyncFeatureMockingFunctionality(t *testing.T) {
ctx := context.Background()
// Ensemble containers.
ec, err := ensemble.RunContainers(
ctx,
ensemble.WithAsyncFeature(),
ensemble.WithMainArtifact("../testdata/pastry-orders-asyncapi.yaml"),
)
require.NoError(t, err)
// Cleanup containers.
t.Cleanup(func() {
if err := ec.Terminate(ctx); err != nil {
t.Fatalf("failed to terminate container: %s", err)
}
})
// Tests & assertions.
test.ConfigRetrieval(t, ctx, ec.GetMicrocksContainer())
test.MicrocksAsyncMockingFunctionality(t, ctx, ec.GetAsyncMinionContainer())
}
func TestAsyncKafkaMockingFunctionality(t *testing.T) {
ctx := context.Background()
// Common network.
net, err := network.New(ctx, network.WithCheckDuplicate())
if err != nil {
require.NoError(t, err)
return
}
// Kafka container.
kc, err := kafkaTC.Run(ctx,
"confluentinc/confluent-local:7.5.0",
network.WithNetwork([]string{"kafka"}, net),
)
if err != nil {
require.NoError(t, err)
return
}
brokers, err := kc.Brokers(ctx)
if err != nil {
require.NoError(t, err)
return
}
// Ensemble containers.
ec, err := ensemble.RunContainers(
ctx,
ensemble.WithAsyncFeature(),
ensemble.WithMainArtifact("../testdata/pastry-orders-asyncapi.yaml"),
ensemble.WithKafkaConnection(kafka.Connection{
BootstrapServers: brokers[0],
}),
ensemble.WithNetwork(net),
)
require.NoError(t, err)
// Cleanup containers.
t.Cleanup(func() {
if err := ec.Terminate(ctx); err != nil {
t.Fatalf("failed to terminate container: %s", err)
}
if err := kc.Terminate(ctx); err != nil {
t.Fatalf("failed to terminate Kafka container: %s", err)
}
})
// Tests & assertions.
test.ConfigRetrieval(t, ctx, ec.GetMicrocksContainer())
test.MicrocksAsyncKafkaMockingFunctionality(
t,
ctx,
kc,
ec.GetAsyncMinionContainer(),
)
}