Elffuss BraidHub

kikocis/EDC public

main · 15.9 KiBRenderedRawPermalink
1# Integration testing
2
3## Decision
4
5Extend the existing `EdcExtension` JUnit facility (that can currently run a single EDC runtime and supports stubbing and extension of runtime services) as follows:
6
7- Load more than one EDC runtime.
8- Load EDC runtimes in separate Class Loaders.
9- Run each EDC runtime Class Loader with its effective runtime class path, based on its module's Gradle configuration.
10
11## Rationale
12
13The need to provide an integration test harness that supports multiple runtimes emerges from multiple needs:
14
15- Stabilizing [samples](/samples) that run multiple connectors, which have been breaking frequently.
16- Testing system behavior when multiple connectors interact, e.g. the contract negotiation process.
17- Testing system behavior upon component failure.
18- Providing a test facility for factoring out application components to separate runtimes (e.g. [DPF](https://github.com/eclipse-edc/Connector/issues/463)).
19
20Key drivers for the choice are:
21
22- Fast and efficient run in CI.
23- Fast "inner loop" (i.e. ability to quickly rerun tests after changing code) and debuggability for developers.
24- Use of existing frameworks, stability and portability.
25
26We have performed technical spikes testing multiple approaches (detailed further below), including various combinations of:
27
28- JUnit
29- [Docker compose](https://docs.docker.com/compose/)
30- Starting custom Class Loaders for separate threads for the Provider and Connector with separate class paths. Using distinct Class Loaders (and threads) for each service effectively provides full runtime isolation within a single JVM
31- [Testcontainers](https://www.testcontainers.org/) with custom containers was also evaluated, but support for bidirectional communication to host is complex, and we didn't manage to get it running.
32
33Spinning additional Class Loaders for runtimes with JUnit provides very fast inner loop. Using the Gradle Classpath is [DRY](https://en.wikipedia.org/wiki/Don%27t_repeat_yourself) and ensures the runtime under test exactly matches the standalone one.
34
35In contrast, approaches based on Docker have a slow inner loop and require rebuild between runs.
36
37The approach used is not limited to the Dataspace Connector, it can be used to run any Java module if required in the future.
38
39## Spikes
40
41We have performed technical spikes on [sample 04.0-file-transfer](/samples/04.0-file-transfer/README.md), that runs two EDC connectors, a Consumer and a Provider. [Spike code is in a forked repository](https://github.com/agera-edc/DataSpaceConnector/pull/3/files). The test requires three components:
42
43- Consumer EDC connector
44- Provider EDC connector
45- HTTP client code to interact with the Consumer provider API
46
47We have written code for the following three options:
48
49- **Docker-compose** which works but provides an inconvenient inner loop.
50- **Class Loader with Gradle Classpath**, which uses Gradle to determine the effective class path for each runtime, and is very efficient. It is therefore the base for the recommendation above.
51- **Class Loader with Shadow JAR**, a variant of the above which uses the Shadow JAR, and provides a less efficient inner loop.
52
53### Docker-compose
54
55In this setup, we run both connectors in Docker containers. Once they are up, we run HTTP client code in a JUnit test running on the host.
56
57```yaml
58# docker-compose.yaml
59services:
60 sample04-connector-provider:
61 build:
62 context: .
63 target: sample04-connector-provider
64 ports:
65 - "8181:8181"
66 volumes:
67 - /tmp/provider:/tmp/provider
68 - /tmp/consumer:/tmp/consumer
69
70 sample04-connector-consumer:
71 build:
72 context: .
73 target: sample04-connector-consumer
74 ports:
75 - "9191:9191"
76 volumes:
77 - /tmp/provider:/tmp/provider
78 - /tmp/consumer:/tmp/consumer
79
80```
81
82```shell
83$ (cd samples/04.0-file-transfer && docker-compose up -d)
84
85[...]
86Successfully built 26b639e8f852
87Successfully tagged 040-file-transfer_sample04-connector-consumer:latest
88
89[+] Running 3/3
90 ⠿ Network 040-file-transfer_default Created 0.0s
91 ⠿ Container sample04-connector-provider Started 0.7s
92 ⠿ Container sample04-connector-consumer Started 0.6s
93```
94
95```shell
96RUN_INTEGRATION_TEST=true EDC_PROVIDER_CONNECTOR_HOST=http://sample04-connector-provider:8181 time ./gradlew cleanTest :samples:04.0-file-transfer:integration-tests:test --tests org.eclipse.dataspaceconnector.samples.FileTransferSystemTest
97```
98
99This setup is stable and straightforward and also resembles a real deployment scenario in which provider and consumer connectors are running as a separate independent java process, but the inner loop is not very efficient for local development as to reload every new code change connector jars needs to rebuild and restart docker process to use latest jars. Debugging a remote process is possible but adds complexity.
100
101For faster local development/debugging one efficient approach could be to run connectors code in debug mode within IDE and necessary IDE run configurations can be provided as xml config files. These run configurations also can be committed in the repository to reduce local development environment setup effort.
102
103### Class Loader with Gradle Classpath
104
105The integration tests module only contains the classpath of the Provider module:
106
107```kotlin
108 implementation(project(":samples:04.0-file-transfer:provider"))
109```
110
111The JUnit integration test runs the Provider using the preexisting `EdcExtension`, and the Consumer using a newly developed extension:
112
113```java
114// EDC Consumer runtime
115@RegisterExtension
116static EdcRuntimeExtension consumer = new EdcRuntimeExtension(
117 ":samples:04.0-file-transfer:consumer", // Gradle module of the runtime to be started
118 "consumer", // prefix for console log output
119 Map.of( // settings
120 "web.http.port", String.valueOf(CONSUMER_CONNECTOR_PORT),
121 "edc.api.control.auth.apikey.value", API_KEY_CONTROL_AUTH,
122 "ids.webhook.address", CONSUMER_CONNECTOR_HOST));
123
124// EDC Provider runtime
125@RegisterExtension
126static EdcRuntimeExtension provider = new EdcRuntimeExtension(
127 ":samples:04.0-file-transfer:provider",
128 "provider",
129 Map.of(...));
130```
131
132The extension determines the class path by running a custom Gradle task:
133
134```java
135// EdcRuntimeExtension.java
136Runtime.getRuntime().exec("/gradlew -q " + moduleName + ":printClasspath");
137
138// ... process classpath (see below) ...
139
140// run a thread with a custom class loader
141var classLoader = URLClassLoader.newInstance(classPathEntries,
142 ClassLoader.getSystemClassLoader());
143
144var mainClassName = BaseRuntime.class.getCanonicalName();
145var mainClass = classLoader.loadClass(mainClassName);
146var mainMethod = mainClass.getMethod("main", String[].class);
147
148runtimeThread = new Thread(() ->
149 {
150 Thread.currentThread().setContextClassLoader(classLoader);
151 mainMethod.invoke(null, new Object[]{new String[0]});
152 })
153```
154
155```kotlin
156 // build.gradle.kts (under allprojects)
157 tasks.register("printClasspath") {
158 doLast {
159 println("${sourceSets["main"].runtimeClasspath.asPath}");
160 }
161 }
162```
163
164The Gradle classpath is composed of JARs within the Connector project (e.g. `/path/to/EclipseDataSpaceConnector/extensions/api/control/build/libs/control-0.0.1-SNAPSHOT.jar`) and JARs from the Gradle cache (e.g. `/home/user/.gradle/caches/modules-2/files-2.1/org.eclipse.jetty/jetty-util/11.0.6/292fa5d7b2cef3483da8a7fa9dd608bfc9896564/jetty-util-11.0.6.jar`). The Extension code replaces JAR entries of Connector modules with their compiled classes and resource directories (`build/classes/java/main/` and `build/resources/main/`), and loads the connector within a separate Class Loader.
165
166Setting up the Test task to pass standard output:
167
168```kotlin
169// samples/04.0-file-transfer/integration-tests/build.gradle.kts
170tasks.getByName<Test>("test") {
171 testLogging {
172 showStandardStreams = true
173 }
174}
175```
176
177The test runs in seconds.
178
179```shell
180$ time ./gradlew cleanTest :samples:04.0-file-transfer:integration-tests:test --tests org.eclipse.dataspaceconnector.samples.ClassLoaderWithGradleClasspathTest
181
182> Configure project :
183> No version was specified, setting default 0.0.1-SNAPSHOT
184> If you want to change this, supply the -Pversion=X.Y.Z parameter
185
186
187> Task :samples:04.0-file-transfer:integration-tests:test
188
189ClassLoaderWithGradleClasspathTest STANDARD_OUT
190 INFO 2022-02-03T17:19:42.201218 Configuration file does not exist: dataspaceconnector-configuration.properties. Ignoring.
191 INFO 2022-02-03T17:19:42.21943 Initialized FS Configuration
192 INFO 2022-02-03T17:19:42.228993 Secrets vault not configured. Defaulting to null vault.
193 INFO 2022-02-03T17:19:42.229459 Initialized Null Vault
194 INFO 2022-02-03T17:19:42.469222 Initialized Core Services
195 INFO 2022-02-03T17:19:42.470778 Initialized In-Memory Asset Index
196 INFO 2022-02-03T17:19:42.472156 Initialized In-Memory Contract Definition Store
197 INFO 2022-02-03T17:19:42.480915 Initialized Core Contract Service
198 INFO 2022-02-03T17:19:42.481961 Initialized In-Memory Transfer Process Store
199 INFO 2022-02-03T17:19:42.483762 Initialized org.eclipse.dataspaceconnector.transfer.core.CommandExtension
200 INFO 2022-02-03T17:19:42.489996 Initialized Core Transfer
201 INFO 2022-02-03T17:19:42.490808 Initialized Mock IAM
202[...]
203 INFO 2022-02-03T17:19:42.673829 HTTP listening on 9191
204 INFO 2022-02-03T17:19:42.76275 Started Jetty Service
205 INFO 2022-02-03T17:19:43.228771 Registered Web API context at: /api/*
206 INFO 2022-02-03T17:19:43.229002 Started Jersey Web Service
207 INFO 2022-02-03T17:19:43.229442 Started IDS Multipart API
208 INFO 2022-02-03T17:19:43.22957 Started org.eclipse.dataspaceconnector.extensions.api.FileTransferExtension
209 INFO 2022-02-03T17:19:43.229659 Started IDS Multipart Dispatcher API
210 INFO 2022-02-03T17:19:43.229751 Started IDS Transform Extension
211 INFO 2022-02-03T17:19:43.229837 Started EDC Control API
212 INFO 2022-02-03T17:19:43.229915 Started API Endpoint
213 INFO 2022-02-03T17:19:43.230525 edc-ce5e1eb0-62ea-43d0-858a-e48118e97d85 ready
214
215ClassLoaderWithGradleClasspathTest > transferFile_success() STANDARD_OUT
216 INFO 2022-02-03T17:19:43.666544 Configuration file does not exist: dataspaceconnector-configuration.properties. Ignoring.
217 INFO 2022-02-03T17:19:43.666879 Initialized FS Configuration
218 INFO 2022-02-03T17:19:43.667783 Secrets vault not configured. Defaulting to null vault.
219 INFO 2022-02-03T17:19:43.667928 Initialized Null Vault
220 INFO 2022-02-03T17:19:43.677249 Initialized Core Services
221 INFO 2022-02-03T17:19:43.677423 Initialized In-Memory Asset Index
222 INFO 2022-02-03T17:19:43.677543 Initialized In-Memory Contract Definition Store
223 INFO 2022-02-03T17:19:43.677745 Initialized Core Contract Service
224 INFO 2022-02-03T17:19:43.677868 Initialized In-Memory Transfer Process Store
225 INFO 2022-02-03T17:19:43.677988 Initialized org.eclipse.dataspaceconnector.transfer.core.CommandExtension
226 INFO 2022-02-03T17:19:43.678098 Initialized Core Transfer
227 INFO 2022-02-03T17:19:43.678255 Initialized Mock IAM
228[...]
229 INFO 2022-02-03T17:19:43.685601 Started In-Memory Contract Negotiation Store
230 INFO 2022-02-03T17:19:43.686003 HTTP listening on 8181
231 INFO 2022-02-03T17:19:43.689505 Started Jetty Service
232
233ClassLoaderWithGradleClasspathTest > transferFile_success() STANDARD_OUT
234 INFO 2022-02-03T17:19:43.736455 Registered Web API context at: /api/*
235 INFO 2022-02-03T17:19:43.736607 Started Jersey Web Service
236 INFO 2022-02-03T17:19:43.736657 Started IDS Multipart API
237 INFO 2022-02-03T17:19:43.736695 Started org.eclipse.dataspaceconnector.extensions.api.FileTransferExtension
238 INFO 2022-02-03T17:19:43.736754 Started IDS Multipart Dispatcher API
239 INFO 2022-02-03T17:19:43.73684 Started IDS Transform Extension
240 DEBUG 2022-02-03T17:19:48.434665 [Consumer] ContractNegotiation initiated. aa9a36be-af41-4da4-b198-711389690558 is now in state INITIAL.
241 DEBUG 2022-02-03T17:19:52.85985 [Provider] ContractNegotiation initiated. f8b7bb63-cdb3-4c17-8969-aaa0b868d5b3 is now in state REQUESTED.
242 DEBUG 2022-02-03T17:19:52.864953 [Provider] Contract offer received. Will be approved.
243 DEBUG 2022-02-03T17:19:52.865395 [Provider] ContractNegotiation f8b7bb63-cdb3-4c17-8969-aaa0b868d5b3 is now in state CONFIRMING.
244 DEBUG 2022-02-03T17:19:52.876959 Response received from connector. Status 200
245 DEBUG 2022-02-03T17:19:52.889059 [Consumer] ContractNegotiation aa9a36be-af41-4da4-b198-711389690558 is now in state REQUESTED.
246 DEBUG 2022-02-03T17:19:53.743579 [Consumer] Contract agreement received. Validation successful.
247 DEBUG 2022-02-03T17:19:53.744268 [Consumer] ContractNegotiation aa9a36be-af41-4da4-b198-711389690558 is now in state CONFIRMED.
248 DEBUG 2022-02-03T17:19:53.748143 Response received from connector. Status 200
249 DEBUG 2022-02-03T17:19:53.754331 [Provider] ContractNegotiation f8b7bb63-cdb3-4c17-8969-aaa0b868d5b3 is now in state CONFIRMED.
250 INFO 2022-02-03T17:19:53.831271 Received request for file test-document against provider http://localhost:8181/api/v1/ids/data
251 DEBUG 2022-02-03T17:19:57.647989 Response received from connector. Status 200
252 INFO 2022-02-03T17:19:57.650468 Object received: org.eclipse.dataspaceconnector.ids.api.multipart.dispatcher.message.MultipartRequestInProcessResponse@4eeb3196
253 INFO 2022-02-03T17:19:58.687893 Copying data from File to File
254 INFO 2022-02-03T17:19:58.690052 Successfully copied file to /tmp/consumer/test-document.txt
255 DEBUG 2022-02-03T17:20:02.640684 Process 60e34667-c51b-4a38-bb08-ddd4d705c234 is now COMPLETED
256[...]
257 INFO 2022-02-03T17:20:02.68389 Connector shutdown complete
258
259BUILD SUCCESSFUL in 33s
260240 actionable tasks: 2 executed, 238 up-to-date
2611.89user 0.11system 0:34.37elapsed 5%CPU
262```
263
264Running in an IDE (IntelliJ IDEA), the debugger can be used in both Provider and Consumer code. When a class source is modified and the test is rerun, the change is immediately reflected.
265
266We could have run the equivalent setup inverting the Provider and Consumer in the two extensions we used. In this infrastructure, only one of the connectors can use the `EdcExtension` facilities to add or mock services. This exploratory spike code was built as a quick-and-dirty PoC. When adapting this spike code for merging, we should improve this setup, by extending the `EdcExtension` to provide both classpath isolation and test runtime configurability.
267
268Note that this setup requires the modules to have previously been built (`./gradlew build`).
269
270### Class Loader with Shadow JAR
271
272The setup is very similar to the above, but the Shadow JAR of the connector is run:
273
274```java
275 // EDC Consumer runtime
276 @RegisterExtension
277 @Order(1)
278 static JarRuntimeExtension otherConnector = new JarRuntimeExtension(
279 "../consumer/build/libs/consumer.jar",
280 Map.of(
281 "web.http.port", "9191",
282 "edc.api.control.auth.apikey.value", API_KEY_CONTROL_AUTH,
283 "ids.webhook.address", "http://localhost:9191"));
284
285
286 // EDC Provider runtime
287 @RegisterExtension
288 @Order(2)
289 static EdcExtension edc = new EdcExtension();
290```
291
292```java
293// JarRuntimeExtension
294var classLoader = URLClassLoader.newInstance(new URL[]{jarFile.toURI().toURL()},
295 ClassLoader.getSystemClassLoader());
296Thread.currentThread().setContextClassLoader(classLoader);
297
298var mainClass = classLoader.loadClass(mainClassName);
299var mainMethod = mainClass.getMethod("main", String[].class);
300mainMethod.invoke(null, new Object[]{new String[0]});
301```
302
303The setup is slightly simpler than the previous one, but the inner loop is less efficient: the Gradle `shadowJar` task must be rerun between test runs.