-
-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathmicrocks.go
494 lines (411 loc) · 17 KB
/
microcks.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
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
/*
* Copyright The Microcks Authors.
*
* 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 microcks
import (
"bytes"
"context"
"fmt"
"io"
"mime/multipart"
"net/http"
"os"
"path/filepath"
"strconv"
"strings"
"time"
"github.com/testcontainers/testcontainers-go"
"github.com/testcontainers/testcontainers-go/wait"
client "microcks.io/go-client"
)
const (
DefaultImage = "quay.io/microcks/microcks-uber:latest"
// DefaultHttpPort represents the default Microcks HTTP port.
DefaultHttpPort = "8080/tcp"
// DefaultGrpcPort represents the default Microcks GRPC port.
DefaultGrpcPort = "9090/tcp"
// DefaultNetworkAlias represents the default network alias of the the MicrocksContainer.
DefaultNetworkAlias = "microcks"
)
// MicrocksContainer represents the Microcks container type used in the module.
type MicrocksContainer struct {
testcontainers.Container
}
// Deprecated: use Run instead
// RunContainer creates an instance of the MicrocksContainer type.
func RunContainer(ctx context.Context, opts ...testcontainers.ContainerCustomizer) (*MicrocksContainer, error) {
return Run(ctx, DefaultImage, opts...)
}
// Run creates an instance of the MicrocksContainer type.
func Run(ctx context.Context, image string, opts ...testcontainers.ContainerCustomizer) (*MicrocksContainer, error) {
req := testcontainers.ContainerRequest{
Image: image,
ExposedPorts: []string{DefaultHttpPort, DefaultGrpcPort},
WaitingFor: wait.ForLog("Started MicrocksApplication"),
}
genericContainerReq := testcontainers.GenericContainerRequest{
ContainerRequest: req,
Started: true,
}
for _, opt := range opts {
opt.Customize(&genericContainerReq)
}
container, err := testcontainers.GenericContainer(ctx, genericContainerReq)
if err != nil {
return nil, err
}
return &MicrocksContainer{Container: container}, nil
}
// WithMainArtifact provides paths to artifacts that will be imported as main or main
// ones within the Microcks container.
// Once it will be started and healthy.
func WithMainArtifact(artifactFilePath string) testcontainers.CustomizeRequestOption {
return WithArtifact(artifactFilePath, true)
}
// WithSecondaryArtifact provides paths to artifacts that will be imported as main or main
// ones within the Microcks container.
// Once it will be started and healthy.
func WithSecondaryArtifact(artifactFilePath string) testcontainers.CustomizeRequestOption {
return WithArtifact(artifactFilePath, false)
}
// WithArtifact provides paths to artifacts that will be imported within the Microcks container.
// Once it will be started and healthy.
func WithArtifact(artifactFilePath string, main bool) testcontainers.CustomizeRequestOption {
return func(req *testcontainers.GenericContainerRequest) error {
hooks := testcontainers.ContainerLifecycleHooks{
PostReadies: []testcontainers.ContainerHook{
importArtifactHook(artifactFilePath, main),
},
}
req.LifecycleHooks = append(req.LifecycleHooks, hooks)
return nil
}
}
// WithNetwork allows to add a custom network.
// Deprecated: Use network.WithNetwork from testcontainers instead.
func WithNetwork(networkName string) testcontainers.CustomizeRequestOption {
return func(req *testcontainers.GenericContainerRequest) error {
req.Networks = append(req.Networks, networkName)
return nil
}
}
// WithNetworkAlias allows to add a custom network alias for a specific network.
// Deprecated: Use network.WithNetwork from testcontainers instead.
func WithNetworkAlias(networkName, networkAlias string) testcontainers.CustomizeRequestOption {
return func(req *testcontainers.GenericContainerRequest) error {
if req.NetworkAliases == nil {
req.NetworkAliases = make(map[string][]string)
}
req.NetworkAliases[networkName] = []string{networkAlias}
return nil
}
}
// WithEnv allows to add an environment variable.
func WithEnv(key, value string) testcontainers.CustomizeRequestOption {
return func(req *testcontainers.GenericContainerRequest) error {
if req.Env == nil {
req.Env = make(map[string]string)
}
req.Env[key] = value
return nil
}
}
// WithHostAccessPorts allows to set the host access ports.
func WithHostAccessPorts(hostAccessPorts []int) testcontainers.CustomizeRequestOption {
return func(req *testcontainers.GenericContainerRequest) error {
req.HostAccessPorts = hostAccessPorts
return nil
}
}
// WithSecret allows to add a new secret.
func WithSecret(s client.Secret) testcontainers.CustomizeRequestOption {
return func(req *testcontainers.GenericContainerRequest) error {
hooks := testcontainers.ContainerLifecycleHooks{
PostReadies: []testcontainers.ContainerHook{
createSecretHook(s),
},
}
req.LifecycleHooks = append(req.LifecycleHooks, hooks)
return nil
}
}
// HttpEndpoint allows retrieving the Http endpoint where Microcks can be accessed.
// (you'd have to append '/api' to access APIs)
func (container *MicrocksContainer) HttpEndpoint(ctx context.Context) (string, error) {
ip, err := container.Host(ctx)
if err != nil {
return "", err
}
port, err := container.MappedPort(ctx, DefaultHttpPort)
if err != nil {
return "", err
}
return fmt.Sprintf("http://%s:%s", ip, port.Port()), nil
}
// SoapMockEndpoint get the exposed mock endpoint for a SOAP Service.
func (container *MicrocksContainer) SoapMockEndpoint(ctx context.Context, service string, version string) (string, error) {
endpoint, err := container.HttpEndpoint(ctx)
if err != nil {
return "", err
}
return fmt.Sprintf("%s/soap/%s/%s", endpoint, service, version), nil
}
// RestMockEndpoints get the exposed mock endpoint for a REST Service.
func (container *MicrocksContainer) RestMockEndpoint(ctx context.Context, service string, version string) (string, error) {
endpoint, err := container.HttpEndpoint(ctx)
if err != nil {
return "", err
}
return fmt.Sprintf("%s/rest/%s/%s", endpoint, service, version), nil
}
// GraphQLMockEndpoint get the exposed mock endpoints for a GraphQL Service.
func (container *MicrocksContainer) GraphQLMockEndpoint(ctx context.Context, service string, version string) (string, error) {
endpoint, err := container.HttpEndpoint(ctx)
if err != nil {
return "", err
}
return fmt.Sprintf("%s/graphql/%s/%s", endpoint, service, version), nil
}
// GrpcMockEndpoint get the exposed mock endpoint for a GRPC Service.
func (container *MicrocksContainer) GrpcMockEndpoint(ctx context.Context) (string, error) {
ip, err := container.Host(ctx)
if err != nil {
return "", err
}
port, err := container.MappedPort(ctx, DefaultGrpcPort)
if err != nil {
return "", err
}
return fmt.Sprintf("grpc://%s:%s", ip, port.Port()), nil
}
// ImportAsMainArtifact imports an artifact as a primary or main one within the Microcks container.
func (container *MicrocksContainer) ImportAsMainArtifact(ctx context.Context, artifactFilePath string) (int, error) {
return container.importArtifact(ctx, artifactFilePath, true)
}
// ImportAsSecondaryArtifact imports an artifact as a secondary one within the Microcks container.
func (container *MicrocksContainer) ImportAsSecondaryArtifact(ctx context.Context, artifactFilePath string) (int, error) {
return container.importArtifact(ctx, artifactFilePath, false)
}
// TestEndpoint launches a conformance test on an endpoint.
func (container *MicrocksContainer) TestEndpoint(ctx context.Context, testRequest *client.TestRequest) (*client.TestResult, error) {
// Retrieve API endpoint.
httpEndpoint, err := container.HttpEndpoint(ctx)
if err != nil {
return nil, fmt.Errorf("error retrieving Microcks API endpoint: %w", err)
}
// Create Microcks client.
c, err := client.NewClientWithResponses(httpEndpoint + "/api")
if err != nil {
return nil, fmt.Errorf("error creating Microcks client: %w", err)
}
testResult, err := c.CreateTestWithResponse(ctx, *testRequest)
if err != nil {
return nil, fmt.Errorf("error creating test with response: %w", err)
}
if testResult.HTTPResponse.StatusCode == 201 {
// Retrieve Id and start polling for final result.
var testResultId string = testResult.JSON201.Id
// Wait an initial delay to avoid inefficient poll.
time.Sleep(100 * time.Millisecond)
// Compute future time that is the end of waiting time frame + extra 1000 to avoid race condition.
future := nowInMilliseconds() + int64(testRequest.Timeout) + 1000
for nowInMilliseconds() < future {
testResultResponse, err := c.GetTestResultWithResponse(ctx, testResultId)
if err != nil {
return nil, fmt.Errorf("error getting test result with response: %w", err)
}
//fmt.Println(string(testResultResponse.Body[:]))
// If still in progress, then wait again.
if testResultResponse.JSON200.InProgress {
time.Sleep(200 * time.Millisecond)
} else {
break
}
}
// Return the final result.
response, err := c.GetTestResultWithResponse(ctx, testResultId)
return response.JSON200, err
}
return nil, fmt.Errorf("couldn't launch on new test on Microcks. Please check Microcks container logs")
}
// TestEndpointAsync launches a conformance test on an endpoint and will provide result via a channel.
func (container *MicrocksContainer) TestEndpointAsync(ctx context.Context, testRequest *client.TestRequest, testResult chan *client.TestResult) error {
result, err := container.TestEndpoint(ctx, testRequest)
if err != nil {
return fmt.Errorf("error executing TestEndpoint: %w", err)
}
testResult <- result // result to channel testResult.
return nil
}
// MessagesForTestCase retrieves messages exchanged during a test on an endpoint.
func (container *MicrocksContainer) MessagesForTestCase(ctx context.Context, testResult *client.TestResult, operationName string) (*[]client.RequestResponsePair, error) {
// Retrieve API endpoint.
httpEndpoint, err := container.HttpEndpoint(ctx)
if err != nil {
return nil, fmt.Errorf("error retrieving Microcks API endpoint: %w", err)
}
// Create Microcks client.
c, err := client.NewClientWithResponses(httpEndpoint + "/api")
if err != nil {
return nil, fmt.Errorf("error creating Microcks client: %w", err)
}
// Build the test case identifier and call api.
operation := encodeOperationName(operationName)
testCaseId := fmt.Sprintf("%s-%s-%s", testResult.Id, strconv.Itoa(int(testResult.TestNumber)), operation)
response, err := c.GetMessagesByTestCaseWithResponse(ctx, testResult.Id, testCaseId)
return response.JSON200, err
}
// EventMessagesForTestCase retrieves event messages received during a test on an endpoint.
func (container *MicrocksContainer) EventMessagesForTestCase(ctx context.Context, testResult *client.TestResult, operationName string) (*[]client.UnidirectionalEvent, error) {
// Retrieve API endpoint.
httpEndpoint, err := container.HttpEndpoint(ctx)
if err != nil {
return nil, fmt.Errorf("error retrieving Microcks API endpoint: %w", err)
}
// Create Microcks client.
c, err := client.NewClientWithResponses(httpEndpoint + "/api")
if err != nil {
return nil, fmt.Errorf("error creating Microcks client: %w", err)
}
// Build the test case identifier and call api.
operation := encodeOperationName(operationName)
testCaseId := fmt.Sprintf("%s-%s-%s", testResult.Id, strconv.Itoa(int(testResult.TestNumber)), operation)
response, err := c.GetEventsByTestCaseWithResponse(ctx, testResult.Id, testCaseId)
return response.JSON200, err
}
// Verify checks that given Service has been invoked at least one time, for the current invocations' date.
func (container *MicrocksContainer) Verify(ctx context.Context, serviceName string, serviceVersion string) (bool, error) {
return container.VerifyAtDate(ctx, serviceName, serviceVersion, time.Now())
}
// VerifyAtDate checks that given Service has been invoked at least one time, for the given invocations' date.
func (container *MicrocksContainer) VerifyAtDate(ctx context.Context, serviceName string, serviceVersion string, date time.Time) (bool, error) {
invocationsCount, err := container.ServiceInvocationsCount(ctx, serviceName, serviceVersion)
if err != nil {
return false, fmt.Errorf("cannot retrieve invocations stats: %w", err)
}
return invocationsCount > 0, nil
}
// ServiceInvocationsCount gets the invocations' count for a given service, identified by its name and version, for the current date.
func (container *MicrocksContainer) ServiceInvocationsCount(ctx context.Context, serviceName string, serviceVersion string) (int, error) {
// Retrieve API endpoint.
return container.ServiceInvocationsCountAtDate(ctx, serviceName, serviceVersion, time.Now())
}
// ServiceInvocationsCountAtDate gets the invocations' count for a given service, identified by its name and version, for the given invocations' date.
func (container *MicrocksContainer) ServiceInvocationsCountAtDate(ctx context.Context, serviceName string, serviceVersion string, date time.Time) (int, error) {
// Retrieve API endpoint.
httpEndpoint, err := container.HttpEndpoint(ctx)
if err != nil {
return 0, fmt.Errorf("error retrieving Microcks API endpoint: %w", err)
}
// Create Microcks client.
c, err := client.NewClientWithResponses(httpEndpoint + "/api")
if err != nil {
return 0, fmt.Errorf("error creating Microcks client: %w", err)
}
// Build the day.
day := formatDay(date)
// To avoid race condition issue while Microcks server is processing metrics asynchronously.
// The wait time is lower on other language bindings (100ms). Don't know why...
time.Sleep(250 * time.Millisecond)
stats, err := c.GetInvocationStatsByServiceWithResponse(ctx, serviceName, serviceVersion, &client.GetInvocationStatsByServiceParams{
Day: &day,
})
invocationStats := stats.JSON200
if invocationStats != nil {
return int(stats.JSON200.DailyCount), err
}
return 0, err
}
func importArtifactHook(artifactFilePath string, mainArtifact bool) testcontainers.ContainerHook {
return func(ctx context.Context, container testcontainers.Container) error {
microcksContainer := &MicrocksContainer{Container: container}
_, err := microcksContainer.importArtifact(ctx, artifactFilePath, mainArtifact)
return err
}
}
func (container *MicrocksContainer) importArtifact(ctx context.Context, artifactFilePath string, mainArtifact bool) (int, error) {
// Retrieve API endpoint.
httpEndpoint, err := container.HttpEndpoint(ctx)
if err != nil {
return http.StatusInternalServerError, fmt.Errorf("error retrieving Microcks API endpoint: %w", err)
}
// Create Microcks client.
c, err := client.NewClientWithResponses(httpEndpoint + "/api")
if err != nil {
return http.StatusInternalServerError, fmt.Errorf("error creating Microcks client: %w", err)
}
// Ensure file exists on fs.
file, err := os.Open(artifactFilePath)
if err != nil {
return http.StatusInternalServerError, fmt.Errorf("error opening artifact file: %w", err)
}
defer file.Close()
// Create a multipart request body, reading the file.
body := &bytes.Buffer{}
writer := multipart.NewWriter(body)
part, err := writer.CreateFormFile("file", filepath.Base(artifactFilePath))
if err != nil {
return http.StatusInternalServerError, fmt.Errorf("error creating multipart form: %w", err)
}
_, err = io.Copy(part, file)
if err != nil {
return http.StatusInternalServerError, fmt.Errorf("error copying file to multipart form: %w", err)
}
// Add the mainArtifact flag to request.
_ = writer.WriteField("mainArtifact", strconv.FormatBool(mainArtifact))
err = writer.Close()
if err != nil {
return http.StatusInternalServerError, fmt.Errorf("error closing multipart form: %w", err)
}
response, err := c.UploadArtifactWithBody(ctx, nil, writer.FormDataContentType(), body)
if err != nil {
return 0, err
}
return response.StatusCode, err
}
func createSecretHook(s client.Secret) testcontainers.ContainerHook {
return func(ctx context.Context, container testcontainers.Container) error {
microcksContainer := &MicrocksContainer{Container: container}
_, err := microcksContainer.createSecret(ctx, s)
return err
}
}
func (container *MicrocksContainer) createSecret(ctx context.Context, s client.Secret) (int, error) {
// Retrieve API endpoint.
httpEndpoint, err := container.HttpEndpoint(ctx)
if err != nil {
return http.StatusInternalServerError, fmt.Errorf("error retrieving Microcks API endpoint: %w", err)
}
// Create Microcks client.
c, err := client.NewClientWithResponses(httpEndpoint + "/api")
if err != nil {
return http.StatusInternalServerError, fmt.Errorf("error creating Microcks client: %w", err)
}
// Create secret.
response, err := c.CreateSecret(ctx, s, nil)
if err != nil {
return 0, err
}
return response.StatusCode, err
}
func nowInMilliseconds() int64 {
return time.Now().UnixNano() / int64(time.Millisecond)
}
func encodeOperationName(operationName string) string {
return strings.ReplaceAll(operationName, "/", "!")
}
func formatDay(date time.Time) string {
return date.Format("20060102")
}