forked from camunda-community-hub/zeebe-test-container
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathRemoteDebugger.java
121 lines (110 loc) · 4.71 KB
/
RemoteDebugger.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
/*
* Copyright © 2021 camunda services GmbH ([email protected])
*
* 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 io.zeebe.containers.util;
import java.time.Duration;
import org.apiguardian.api.API;
import org.apiguardian.api.API.Status;
import org.testcontainers.containers.GenericContainer;
/**
* To use, create a new instance with the desired port (or, by default, 5005), and pass the
* container you wish to configure to it.
*
* <p>This will start the container and wait for the debugger to attach. To use with Intellij,
* create a new debug configuration template and pick "Remote JVM Debug". By default, it will be
* setup for port 5005, which is the default port here as well. You can find out more about this
* from <a href="https://www.jetbrains.com/help/idea/tutorial-remote-debug.html">this IntelliJ
* tutorial</a>.
*
* <p>This idea came from bsideup's blog, and you can read more about it <a
* href="https://bsideup.github.io/posts/debugging_containers/">here</a>
*
* <p>Example usage:
*
* <pre>{@code
* final ZeebeContainer container = new ZeebeContainer();
* RemoteDebugger.configure(container, 5005, true);
* }</pre>
*
* <pre>{@code
* @Testcontainers
* final class MyTest {
* @Container
* private final ZeebeContainer = RemoteDebugger.configure(new ZeebeContainer())
* .withEnv("MY_ENV_VAR", "true");
*
* // more test code...
* }
* }</pre>
*/
@API(status = Status.EXPERIMENTAL)
public final class RemoteDebugger {
/** The default binding port for the remote debugger server */
public static final int DEFAULT_REMOTE_DEBUGGER_PORT = 5005;
/** The default timeout that will be applied to the container being debugged */
public static final Duration DEFAULT_START_TIMEOUT = Duration.ofMinutes(5);
private RemoteDebugger() {}
/**
* Returns the given container configured to start a debugging server. Uses the default port 5005.
* By default, the application will be suspended until a debugger connects to it.
*
* @param container the container to configure
* @param <T> the type of the container
* @return the same container configured for debugging
*/
public static <T extends GenericContainer<T>> T configure(final T container) {
return configure(container, DEFAULT_REMOTE_DEBUGGER_PORT);
}
/**
* Returns the given container configured to start a debugging server on the given port. By
* default, the application will be suspended until a debugger connects to it.
*
* @param container the container to configure
* @param port the port to bind the debugging server to
* @param <T> the type of the container
* @return the same container configured for debugging
*/
public static <T extends GenericContainer<T>> T configure(final T container, final int port) {
return configure(container, port, true);
}
/**
* Returns the given container configured to start a debugging server on the given port. If {@code
* suspend} is true, the application is suspended until a debugger instance connects to the
* server.
*
* @param container the container to configure
* @param port the port to bind the debugging server to
* @param suspend if true, will suspend the application until a debugger connects to it
* @param <T> the type of the container
* @return the same container configured for debugging
*/
public static <T extends GenericContainer<T>> T configure(
final T container, final int port, boolean suspend) {
final String javaOpts = container.getEnvMap().getOrDefault("JAVA_OPTS", "");
final char suspendFlag = suspend ? 'y' : 'n';
// when configuring a port binding, we need to expose the port as well; the port binding just
// decides to which host port the exposed port will bind, but it will not expose the port itself
container.addExposedPort(port);
container.getPortBindings().add(port + ":" + port);
// prepend agent configuration in front of javaOpts to ensure it's enabled but also keep
// previously defined options
container.withEnv(
"JAVA_OPTS",
String.format(
"-agentlib:jdwp=transport=dt_socket,server=y,suspend=%s,address=0.0.0.0:%d %s",
suspendFlag, port, javaOpts));
return container.withStartupTimeout(DEFAULT_START_TIMEOUT);
}
}