@@ -38,7 +38,8 @@ abstract class SmokeTest extends Specification {
38
38
protected static final OkHttpClient CLIENT = OkHttpUtils . client()
39
39
40
40
@Shared
41
- private Network network = Network . newNetwork()
41
+ private Backend backend = Backend . getInstance()
42
+
42
43
@Shared
43
44
protected String agentPath = System . getProperty(" io.opentelemetry.smoketest.agent.shadowJar.path" )
44
45
@@ -60,31 +61,8 @@ abstract class SmokeTest extends Specification {
60
61
protected void customizeContainer (GenericContainer container ) {
61
62
}
62
63
63
- @Shared
64
- private GenericContainer backend
65
-
66
- @Shared
67
- private GenericContainer collector
68
-
69
64
def setupSpec () {
70
- backend = new GenericContainer<> (" ghcr.io/open-telemetry/java-test-containers:smoke-fake-backend-20201128.1734635" )
71
- .withExposedPorts(8080 )
72
- .waitingFor(Wait . forHttp(" /health" ). forPort(8080 ))
73
- .withNetwork(network)
74
- .withNetworkAliases(" backend" )
75
- .withImagePullPolicy(PullPolicy . alwaysPull())
76
- .withLogConsumer(new Slf4jLogConsumer (LoggerFactory . getLogger(" smoke.tests.backend" )))
77
- backend. start()
78
-
79
- collector = new GenericContainer<> (" otel/opentelemetry-collector-dev:latest" )
80
- .dependsOn(backend)
81
- .withNetwork(network)
82
- .withNetworkAliases(" collector" )
83
- .withLogConsumer(new Slf4jLogConsumer (LoggerFactory . getLogger(" smoke.tests.collector" )))
84
- .withImagePullPolicy(PullPolicy . alwaysPull())
85
- .withCopyFileToContainer(MountableFile . forClasspathResource(" /otel.yaml" ), " /etc/otel.yaml" )
86
- .withCommand(" --config /etc/otel.yaml" )
87
- collector. start()
65
+ backend. setup()
88
66
}
89
67
90
68
def startTarget (int jdk , String serverVersion = null ) {
@@ -95,7 +73,7 @@ abstract class SmokeTest extends Specification {
95
73
def output = new ToStringConsumer ()
96
74
target = new GenericContainer<> (getTargetImage(jdk, serverVersion))
97
75
.withExposedPorts(8080 )
98
- .withNetwork(network)
76
+ .withNetwork(backend . network)
99
77
.withLogConsumer(output)
100
78
.withLogConsumer(new Slf4jLogConsumer (LoggerFactory . getLogger(" smoke.tests.target" )))
101
79
.withCopyFileToContainer(MountableFile . forHostPath(agentPath), " /opentelemetry-javaagent-all.jar" )
@@ -133,9 +111,7 @@ abstract class SmokeTest extends Specification {
133
111
}
134
112
135
113
def cleanupSpec () {
136
- backend. stop()
137
- collector. stop()
138
- network. close()
114
+ backend. cleanup()
139
115
}
140
116
141
117
protected static Stream<AnyValue > findResourceAttribute (Collection<ExportTraceServiceRequest > traces ,
@@ -243,4 +219,59 @@ abstract class SmokeTest extends Specification {
243
219
}
244
220
return encoding
245
221
}
222
+
223
+ static class Backend {
224
+ private static final INSTANCE = new Backend ()
225
+
226
+ private final Network network = Network . newNetwork()
227
+ private GenericContainer backend
228
+ private GenericContainer collector
229
+
230
+ boolean started = false
231
+
232
+ static Backend getInstance () {
233
+ return INSTANCE
234
+ }
235
+
236
+ def setup () {
237
+ // we start backend & collector once for all tests
238
+ if (started) {
239
+ return
240
+ }
241
+ started = true
242
+ Runtime . addShutdownHook { stop() }
243
+
244
+ backend = new GenericContainer<> (" ghcr.io/open-telemetry/java-test-containers:smoke-fake-backend-20201128.1734635" )
245
+ .withExposedPorts(8080 )
246
+ .waitingFor(Wait . forHttp(" /health" ). forPort(8080 ))
247
+ .withNetwork(network)
248
+ .withNetworkAliases(" backend" )
249
+ .withImagePullPolicy(PullPolicy . alwaysPull())
250
+ .withLogConsumer(new Slf4jLogConsumer (LoggerFactory . getLogger(" smoke.tests.backend" )))
251
+ backend. start()
252
+
253
+ collector = new GenericContainer<> (" otel/opentelemetry-collector-dev:latest" )
254
+ .dependsOn(backend)
255
+ .withNetwork(network)
256
+ .withNetworkAliases(" collector" )
257
+ .withLogConsumer(new Slf4jLogConsumer (LoggerFactory . getLogger(" smoke.tests.collector" )))
258
+ .withImagePullPolicy(PullPolicy . alwaysPull())
259
+ .withCopyFileToContainer(MountableFile . forClasspathResource(" /otel.yaml" ), " /etc/otel.yaml" )
260
+ .withCommand(" --config /etc/otel.yaml" )
261
+ collector. start()
262
+ }
263
+
264
+ int getMappedPort (int originalPort ) {
265
+ return backend. getMappedPort(originalPort)
266
+ }
267
+
268
+ def cleanup () {
269
+ }
270
+
271
+ def stop () {
272
+ backend?. stop()
273
+ collector?. stop()
274
+ network?. close()
275
+ }
276
+ }
246
277
}
0 commit comments