-
Notifications
You must be signed in to change notification settings - Fork 333
Expand file tree
/
Copy pathClassInjectingLoadClassDisabledForkedTest.groovy
More file actions
64 lines (51 loc) · 2.09 KB
/
ClassInjectingLoadClassDisabledForkedTest.groovy
File metadata and controls
64 lines (51 loc) · 2.09 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
package locator
import datadog.trace.agent.test.InstrumentationSpecification
import datadog.trace.config.inversion.ConfigHelper
import net.bytebuddy.agent.builder.AgentBuilder
import net.bytebuddy.description.type.TypeDescription
import net.bytebuddy.dynamic.DynamicType
import net.bytebuddy.utility.JavaModule
import spock.lang.Shared
import java.lang.instrument.ClassFileTransformer
/**
* This test checks that we don't fall back to loadClass when it is disabled.
*/
class ClassInjectingLoadClassDisabledForkedTest extends InstrumentationSpecification {
static volatile ClassFileTransformer extraTransformer = null
@Override
protected void configurePreAgent() {
super.configurePreAgent()
// Opt out of strict config validation - test module loads test instrumentations with fake names
ConfigHelper.get().setConfigInversionStrict(ConfigHelper.StrictnessPolicy.TEST)
injectSysConfig("dd.resolver.use.loadclass", "false")
// Since this method is not at all configurePreAgent, but more like
// configurePreAgentAndOhByTheWayBeforeEveryTest we need to not install
// the extra transformer multiple times
if (!extraTransformer) {
AgentBuilder builder = new AgentBuilder.Default()
builder = ClassInjectingTransformer.instrument(builder)
extraTransformer = builder.installOn(INSTRUMENTATION)
}
}
@Override
protected void cleanupAfterAgent() {
if (extraTransformer) {
INSTRUMENTATION.removeTransformer(extraTransformer)
extraTransformer = null
}
super.cleanupAfterAgent()
}
@Override
void onTransformation(TypeDescription typeDescription, ClassLoader classLoader, JavaModule module, boolean loaded, DynamicType dynamicType) {
transformed += typeDescription.name
}
@Shared
def transformed = []
def "should not find classes injected via defineClass"() {
setup:
def instrumented = new ClassInjectingTestInstrumentation.ToBeInstrumented("test")
expect:
!transformed.contains('locator.ClassInjectingTestInstrumentation$ToBeInstrumented')
instrumented.message == "test:${ClassInjectingTransformer.NAME}"
}
}