-
-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathmicrocks-container.test.ts
More file actions
259 lines (210 loc) · 11.1 KB
/
microcks-container.test.ts
File metadata and controls
259 lines (210 loc) · 11.1 KB
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
/*
* 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.
*/
import * as path from "path";
import { GenericContainer, Network, Wait } from "testcontainers";
import { MicrocksContainer, TestRequest, TestRunnerType, OAuth2GrantType, StartedMicrocksContainer } from "./microcks-container";
import KeycloakContainer from 'keycloak-testcontainer';
describe("MicrocksContainer", () => {
const resourcesDir = path.resolve(__dirname, "..", "test-resources");
// start and mock {
it("should start, load artifacts and expose mock", async () => {
var logs = "";
// Start container and load artifacts.
const container = await new MicrocksContainer()
.withDebugLogLevel()
.withLogConsumer(stream => {
stream.on("data", line => logs += line + "\n");
stream.on("end", () => console.log("Stream closed"));
})
.withSnapshots([path.resolve(resourcesDir, "microcks-repository.json")])
.withMainArtifacts([path.resolve(resourcesDir, "apipastries-openapi.yaml")])
.withMainRemoteArtifacts(["https://raw.githubusercontent.com/microcks/microcks/master/samples/APIPastry-openapi.yaml"])
.withSecondaryArtifacts([path.resolve(resourcesDir, "apipastries-postman-collection.json")])
.start();
// Check that debug logs are present in container logs.
expect(logs).toContain("DEBUG 1");
// Test mock endpoints relative methods.
let baseWsUrl = container.getSoapMockEndpoint("Pastries Service", "1.0");
expect(container.getHttpEndpoint() + "/soap/Pastries Service/1.0").toBe(baseWsUrl);
let baseApiUrl = container.getRestMockEndpoint("API Pastries", "0.0.1");
expect(container.getHttpEndpoint() + "/rest/API Pastries/0.0.1").toBe(baseApiUrl);
let baseGraphUrl = container.getGraphQLMockEndpoint("Pastries Graph", "1");
expect(container.getHttpEndpoint() + "/graphql/Pastries Graph/1").toBe(baseGraphUrl);
let baseGrpcUrl = container.getGrpcMockEndpoint();
expect("grpc://" + container.getHost() + ":" + container.getMappedPort(9090)).toBe(baseGrpcUrl);
let baseWsPath = container.getSoapMockEndpointPath("Pastries Service", "1.0");
expect("/soap/Pastries Service/1.0").toBe(baseWsPath);
let baseApiPath = container.getRestMockEndpointPath("API Pastries", "0.0.1");
expect("/rest/API Pastries/0.0.1").toBe(baseApiPath);
let baseGraphPath = container.getGraphQLMockEndpointPath("Pastries Graph", "1");
expect("/graphql/Pastries Graph/1").toBe(baseGraphPath);
let validWsUrl = container.getValidatingSoapMockEndpoint("Pastries Service", "1.0");
expect(container.getHttpEndpoint() + "/soap/Pastries Service/1.0?validate=true").toBe(validWsUrl);
let validApiUrl = container.getValidatingRestMockEndpoint("API Pastries", "0.0.1");
expect(container.getHttpEndpoint() + "/rest-valid/API Pastries/0.0.1").toBe(validApiUrl);
let validWsPath = container.getValidatingSoapMockEndpointPath("Pastries Service", "1.0");
expect("/soap/Pastries Service/1.0?validate=true").toBe(validWsPath);
let validApiPath = container.getValidatingRestMockEndpointPath("API Pastries", "0.0.1");
expect("/rest-valid/API Pastries/0.0.1").toBe(validApiPath);
// Check available services loaded including snapshot.
var services = await fetch(container.getHttpEndpoint() + "/api/services");
expect(services.status).toBe(200);
var servicesJson = await services.json();
expect(servicesJson.length).toBe(6);
var names = servicesJson.map((service: { name: any; }) => service.name);
expect(names).toContain("API Pastries");
expect(names).toContain("API Pastry - 2.0");
expect(names).toContain("HelloService Mock");
expect(names).toContain("Movie Graph API");
expect(names).toContain("Petstore API");
expect(names).toContain("io.github.microcks.grpc.hello.v1.HelloService");
// Get base Url for API Pastries / 0.0.1
var pastriesUrl = container.getRestMockEndpoint("API Pastries", "0.0.1");
// Check that mock from main/primary artifact has been loaded.
var response = await fetch(pastriesUrl + "/pastries/Millefeuille");
var responseJson = await response.json();
expect(response.status).toBe(200);
expect(responseJson.name).toBe("Millefeuille");
// Check it has been called once.
await testInvocationsCheckingFunctionality(container, "API Pastries", "0.0.1", 1);
// Check that mock from secondary artifact has been loaded too.
response = await fetch(pastriesUrl + "/pastries/Eclair Chocolat");
responseJson = await response.json();
expect(response.status).toBe(200);
expect(responseJson.name).toBe("Eclair Chocolat");
// Check it has been called twice.
await testInvocationsCheckingFunctionality(container, "API Pastries", "0.0.1", 2);
// Check that mock from from main/primary remote artifact has been loaded.
pastriesUrl = container.getRestMockEndpoint("API Pastry - 2.0", "2.0.0");
response = await fetch(pastriesUrl + "/pastry/Millefeuille");
responseJson = await response.json();
expect(response.status).toBe(200);
expect(responseJson.name).toBe("Millefeuille");
// Check it has been called once.
await testInvocationsCheckingFunctionality(container, "API Pastry - 2.0", "2.0.0", 1);
// Now stop the container.
await container.stop();
});
// }
async function testInvocationsCheckingFunctionality(container: StartedMicrocksContainer,
serviceName: string, serviceVersion: string, expectedCount: number) {
expect(await container.verify(serviceName, serviceVersion)).toBe(true);
expect(await container.getServiceInvocationsCount(serviceName, serviceVersion)).toBe(expectedCount)
}
// start and contract test {
it("should start, load artifacts and contract test mock", async () => {
const network = await new Network().start();
// Start microcks container and other containers.
const container = await new MicrocksContainer().withNetwork(network).start();
const badImpl = await new GenericContainer("quay.io/microcks/contract-testing-demo:01")
.withNetwork(network)
.withNetworkAliases("bad-impl")
.withWaitStrategy(Wait.forLogMessage("Example app listening on port 3001", 1))
.start();
const goodImpl = await new GenericContainer("quay.io/microcks/contract-testing-demo:02")
.withNetwork(network)
.withNetworkAliases("good-impl")
.withWaitStrategy(Wait.forLogMessage("Example app listening on port 3002", 1))
.start();
await container.importAsMainArtifact(path.resolve(resourcesDir, "apipastries-openapi.yaml"));
var testRequest: TestRequest = {
serviceId: "API Pastries:0.0.1",
runnerType: TestRunnerType.OPEN_API_SCHEMA,
testEndpoint: "http://bad-impl:3001",
timeout: 2000
}
var testResult = await container.testEndpoint(testRequest);
expect(testResult.success).toBe(false);
expect(testResult.testedEndpoint).toBe("http://bad-impl:3001");
expect(testResult.testCaseResults.length).toBe(3);
expect(testResult.testCaseResults[0].testStepResults[0].message).toContain("string found, number expected");
expect(testResult.testCaseResults[0].testStepResults[0].message).toContain("required property 'status' not found");
// Retrieve messages for the failing test case.
const messages = await container.getMessagesForTestCase(testResult, "GET /pastries");
expect(messages.length).toBe(3);
messages.forEach(message => {
expect(message.request).not.toBeNull();
expect(message.response).not.toBeNull();
expect(message.request.content).not.toBeNull();
// Check these are the correct requests.
expect(message.request.queryParameters).not.toBeNull();
expect(message.request.queryParameters.length).toBe(1);
expect(message.request.queryParameters[0].name).toBe('size');
});
testRequest = {
serviceId: "API Pastries:0.0.1",
runnerType: TestRunnerType.OPEN_API_SCHEMA,
testEndpoint: "http://good-impl:3002",
timeout: 3000
}
testResult = await container.testEndpoint(testRequest);
expect(testResult.success).toBe(true);
expect(testResult.testedEndpoint).toBe("http://good-impl:3002");
expect(testResult.testCaseResults.length).toBe(3);
expect(testResult.testCaseResults[0].testStepResults[0].message).toBe("");
// Now stop the containers and the network.
await container.stop();
await badImpl.stop();
await goodImpl.stop();
await network.stop();
});
// }
// start and contract test with OAuth2 context{
it("should start, load artifacts and contract test mock with OAuth2 context", async () => {
const network = await new Network().start();
// Start microcks container and other containers.
const container = await new MicrocksContainer().withNetwork(network).start();
const keycloak = await new KeycloakContainer({ tag: '26.0' })
.withNetwork(network)
.withNetworkAliases("keycloak")
.withRealmImport(path.resolve(resourcesDir, "realm"))
.start();
const goodImpl = await new GenericContainer("quay.io/microcks/contract-testing-demo:02")
.withNetwork(network)
.withNetworkAliases("good-impl")
.withWaitStrategy(Wait.forLogMessage("Example app listening on port 3002", 1))
.start();
await container.importAsMainArtifact(path.resolve(resourcesDir, "apipastries-openapi.yaml"));
var testRequest: TestRequest = {
serviceId: "API Pastries:0.0.1",
runnerType: TestRunnerType.OPEN_API_SCHEMA,
testEndpoint: "http://good-impl:3002",
timeout: 3000,
oAuth2Context: {
clientId: "myrealm-serviceaccount",
clientSecret: "ab54d329-e435-41ae-a900-ec6b3fe15c54",
tokenUri: "http://keycloak:8080/realms/myrealm/protocol/openid-connect/token",
grantType: OAuth2GrantType.CLIENT_CREDENTIALS
}
}
var testResult = await container.testEndpoint(testRequest);
expect(testResult.success).toBe(true);
expect(testResult.testedEndpoint).toBe("http://good-impl:3002");
expect(testResult.testCaseResults.length).toBe(3);
expect(testResult.testCaseResults[0].testStepResults[0].message).toBe("");
// Ensure test has used a valid OAuth2 client.
expect(testResult.authorizedClient).toBeDefined();
expect(testResult.authorizedClient?.principalName).toBe("myrealm-serviceaccount");
expect(testResult.authorizedClient?.tokenUri).toBe("http://keycloak:8080/realms/myrealm/protocol/openid-connect/token");
expect(testResult.authorizedClient?.scopes).toBe("openid profile email");
// Now stop the containers and the network.
await container.stop();
await keycloak.stop();
await goodImpl.stop();
await network.stop();
});
// }
});