-
Notifications
You must be signed in to change notification settings - Fork 270
/
RegistryDeploymentManager.java
220 lines (175 loc) · 9.17 KB
/
RegistryDeploymentManager.java
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
package io.apicurio.deployment;
import io.fabric8.kubernetes.api.model.Namespace;
import io.fabric8.kubernetes.api.model.PodList;
import io.fabric8.kubernetes.client.KubernetesClient;
import io.fabric8.kubernetes.client.KubernetesClientBuilder;
import io.fabric8.kubernetes.client.dsl.LogWatch;
import io.fabric8.kubernetes.client.dsl.Resource;
import io.fabric8.openshift.api.model.Route;
import io.fabric8.openshift.client.DefaultOpenShiftClient;
import io.fabric8.openshift.client.OpenShiftClient;
import org.junit.platform.engine.TestExecutionResult;
import org.junit.platform.launcher.TestExecutionListener;
import org.junit.platform.launcher.TestIdentifier;
import org.junit.platform.launcher.TestPlan;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.testcontainers.shaded.org.apache.commons.io.IOUtils;
import java.io.IOException;
import java.io.InputStream;
import java.nio.charset.StandardCharsets;
import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.TimeUnit;
import static io.apicurio.deployment.Constants.REGISTRY_IMAGE;
import static io.apicurio.deployment.KubernetesTestResources.*;
public class RegistryDeploymentManager implements TestExecutionListener {
private static final Logger LOGGER = LoggerFactory.getLogger(RegistryDeploymentManager.class);
static KubernetesClient kubernetesClient;
static List<LogWatch> logWatch;
static String testLogsIdentifier;
@Override
public void executionStarted(TestIdentifier testIdentifier) {
TestExecutionListener.super.executionStarted(testIdentifier);
if (Boolean.parseBoolean(System.getProperty("cluster.tests"))) {
logWatch = streamPodLogs(testLogsIdentifier);
}
}
@Override
public void executionFinished(TestIdentifier testIdentifier, TestExecutionResult testExecutionResult) {
TestExecutionListener.super.executionFinished(testIdentifier, testExecutionResult);
if (logWatch != null && !logWatch.isEmpty()) {
logWatch.forEach(LogWatch::close);
}
}
@Override
public void testPlanExecutionStarted(TestPlan testPlan) {
if (Boolean.parseBoolean(System.getProperty("cluster.tests"))) {
kubernetesClient = new KubernetesClientBuilder().build();
try {
handleInfraDeployment();
} catch (Exception e) {
LOGGER.error("Error starting registry deployment", e);
}
LOGGER.info("Test suite started ##################################################");
}
}
@Override
public void testPlanExecutionFinished(TestPlan testPlan) {
LOGGER.info("Test suite ended ##################################################");
try {
// Finally, once the testsuite is done, cleanup all the resources in the cluster
if (kubernetesClient != null
&& !(Boolean.parseBoolean(System.getProperty("preserveNamespace")))) {
LOGGER.info("Closing test resources ##################################################");
if (logWatch != null && !logWatch.isEmpty()) {
logWatch.forEach(LogWatch::close);
}
final Resource<Namespace> namespaceResource = kubernetesClient.namespaces()
.withName(TEST_NAMESPACE);
namespaceResource.delete();
}
} catch (Exception e) {
LOGGER.error("Exception closing test resources", e);
} finally {
if (kubernetesClient != null) {
kubernetesClient.close();
}
}
}
private void handleInfraDeployment() throws Exception {
// First, create the namespace used for the test.
kubernetesClient.load(getClass().getResourceAsStream(E2E_NAMESPACE_RESOURCE)).serverSideApply();
// Based on the configuration, deploy the appropriate variant
if (Boolean.parseBoolean(System.getProperty("deployInMemory"))) {
LOGGER.info(
"Deploying In Memory Registry Variant with image: {} ##################################################",
System.getProperty("registry-in-memory-image"));
InMemoryDeploymentManager.deployInMemoryApp(System.getProperty("registry-in-memory-image"));
testLogsIdentifier = "apicurio-registry-memory";
} else if (Boolean.parseBoolean(System.getProperty("deploySql"))) {
LOGGER.info(
"Deploying SQL Registry Variant with image: {} ##################################################",
System.getProperty("registry-sql-image"));
SqlDeploymentManager.deploySqlApp(System.getProperty("registry-sql-image"));
testLogsIdentifier = "apicurio-registry-sql";
} else if (Boolean.parseBoolean(System.getProperty("deployKafka"))) {
LOGGER.info(
"Deploying Kafka SQL Registry Variant with image: {} ##################################################",
System.getProperty("registry-kafkasql-image"));
KafkaSqlDeploymentManager.deployKafkaApp(System.getProperty("registry-kafkasql-image"));
testLogsIdentifier = "apicurio-registry-kafka";
}
}
static void prepareTestsInfra(String externalResources, String registryResources, boolean startKeycloak,
String registryImage) throws IOException {
if (startKeycloak) {
LOGGER.info("Deploying Keycloak resources ##################################################");
deployResource(KEYCLOAK_RESOURCES);
}
if (externalResources != null) {
LOGGER.info(
"Deploying external dependencies for Registry ##################################################");
deployResource(externalResources);
}
final InputStream resourceAsStream = RegistryDeploymentManager.class
.getResourceAsStream(registryResources);
assert resourceAsStream != null;
String registryLoadedResources = IOUtils.toString(resourceAsStream, StandardCharsets.UTF_8.name());
if (registryImage != null) {
registryLoadedResources = registryLoadedResources.replace(REGISTRY_IMAGE, registryImage);
}
try {
// Deploy all the resources associated to the registry variant
kubernetesClient
.load(IOUtils.toInputStream(registryLoadedResources, StandardCharsets.UTF_8.name()))
.serverSideApply();
} catch (Exception ex) {
LOGGER.debug("Error creating registry resources:", ex);
}
// Wait for all the pods of the variant to be ready
kubernetesClient.pods().inNamespace(TEST_NAMESPACE).waitUntilReady(360, TimeUnit.SECONDS);
setupTestNetworking();
}
private static void setupTestNetworking() {
// For openshift, a route to the application is created we use it to set up the networking needs.
if (Boolean.parseBoolean(System.getProperty("openshift.resources"))) {
OpenShiftClient openShiftClient = new DefaultOpenShiftClient();
try {
final Route registryRoute = openShiftClient.routes()
.load(RegistryDeploymentManager.class.getResourceAsStream(REGISTRY_OPENSHIFT_ROUTE))
.serverSideApply();
System.setProperty("quarkus.http.test-host", registryRoute.getSpec().getHost());
System.setProperty("quarkus.http.test-port", "80");
} catch (Exception ex) {
LOGGER.warn("The registry route already exists: ", ex);
}
} else {
// If we're running the cluster tests but no external endpoint has been provided, set the value of
// the load balancer.
if (System.getProperty("quarkus.http.test-host").equals("localhost")
&& !System.getProperty("os.name").contains("Mac OS")) {
System.setProperty("quarkus.http.test-host",
kubernetesClient.services().inNamespace(TEST_NAMESPACE).withName(APPLICATION_SERVICE)
.get().getSpec().getClusterIP());
}
}
}
private static void deployResource(String resource) {
// Deploy all the resources associated to the external requirements
kubernetesClient.load(RegistryDeploymentManager.class.getResourceAsStream(resource))
.serverSideApply();
// Wait for all the external resources pods to be ready
kubernetesClient.pods().inNamespace(TEST_NAMESPACE).waitUntilReady(360, TimeUnit.SECONDS);
}
private static List<LogWatch> streamPodLogs(String container) {
List<LogWatch> logWatchList = new ArrayList<>();
PodList podList = kubernetesClient.pods().inNamespace(TEST_NAMESPACE).withLabel("app", container)
.list();
podList.getItems()
.forEach(p -> logWatchList.add(kubernetesClient.pods().inNamespace(TEST_NAMESPACE)
.withName(p.getMetadata().getName()).inContainer(container).tailingLines(10)
.watchLog(System.out)));
return logWatchList;
}
}