-
Notifications
You must be signed in to change notification settings - Fork 87
Expand file tree
/
Copy pathJenkinsOtelPluginNoConfigurationTest.java
More file actions
108 lines (95 loc) · 4.19 KB
/
JenkinsOtelPluginNoConfigurationTest.java
File metadata and controls
108 lines (95 loc) · 4.19 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
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
package io.jenkins.plugins.opentelemetry;
import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.hasSize;
import static org.hamcrest.Matchers.nullValue;
import hudson.model.Result;
import io.jenkins.plugins.opentelemetry.init.StepExecutionInstrumentationInitializer;
import io.opentelemetry.sdk.common.CompletableResultCode;
import io.opentelemetry.sdk.testing.exporter.InMemorySpanExporterProvider;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.TimeUnit;
import java.util.logging.Level;
import org.hamcrest.Matchers;
import org.jenkinsci.plugins.workflow.cps.CpsFlowDefinition;
import org.jenkinsci.plugins.workflow.job.WorkflowJob;
import org.jenkinsci.plugins.workflow.steps.SynchronousNonBlockingStepExecution;
import org.junit.Rule;
import org.junit.Test;
import org.jvnet.hudson.test.BuildWatcher;
import org.jvnet.hudson.test.JenkinsRule;
import org.jvnet.hudson.test.LogRecorder;
public class JenkinsOtelPluginNoConfigurationTest {
@Rule
public JenkinsRule j = new JenkinsRule();
@Rule
public BuildWatcher buildWatcher = new BuildWatcher();
/**
* As the JVM and classes are loaded only once for the whole test, {@link SynchronousNonBlockingStepExecution#getExecutorService()} augments only once. The current boolean keeps track of the augmentation status.
*/
private static boolean augmented = false;
/**
* Test that the StepExecutionInstrumentationInitializer does nothing when configuration is not set.
* This test is similar to {@link JenkinsOtelPluginIntegrationTest#testSpanContextPropagationSynchronousNonBlockingTestStep()}
*/
@Test
public void test_noOp_when_not_configured() throws Exception {
String pipelineScript =
"""
node() {
stage('ze-stage1') {
echo message: 'hello'
spanContextPropagationSynchronousNonBlockingTestStep()
}
}""";
j.createOnlineSlave();
final String jobName = "test-SpanContextPropagationSynchronousTestStep";
WorkflowJob pipeline = j.createProject(WorkflowJob.class, jobName);
pipeline.setDefinition(new CpsFlowDefinition(pipelineScript, true));
try (LogRecorder recorder = new LogRecorder()
.quiet()
.record(StepExecutionInstrumentationInitializer.class, Level.FINE)
.capture(10)) {
j.assertBuildStatus(Result.SUCCESS, pipeline.scheduleBuild2(0));
checkAugmentation(recorder);
}
CompletableResultCode result = JenkinsControllerOpenTelemetry.get()
.getOpenTelemetrySdk()
.getSdkTracerProvider()
.forceFlush();
result.join(1, TimeUnit.SECONDS);
// without specific configuration, no spans should be exported
assertThat(InMemorySpanExporterProvider.LAST_CREATED_INSTANCE, nullValue());
}
/**
* Make sure a standard pipeline with synchronous non-blocking steps works with {@link StepExecutionInstrumentationInitializer#augment(ExecutorService)}
*/
@Test
public void test_standard_pipeline() throws Exception {
j.createOnlineSlave();
WorkflowJob pipeline = j.createProject(WorkflowJob.class);
pipeline.setDefinition(new CpsFlowDefinition(
"""
node {
writeFile(file: 'file', text: 'Hello, World!')
archiveArtifacts('file')
}
""",
true));
try (LogRecorder recorder = new LogRecorder()
.quiet()
.record(StepExecutionInstrumentationInitializer.class, Level.FINE)
.capture(10)) {
var build = j.assertBuildStatus(Result.SUCCESS, pipeline.scheduleBuild2(0));
assertThat(build.getArtifacts(), hasSize(1));
checkAugmentation(recorder);
}
}
private void checkAugmentation(LogRecorder recorder) {
if (!augmented) {
assertThat(
recorder.getMessages(),
Matchers.hasItem("Instrumenting " + SynchronousNonBlockingStepExecution.class.getName() + "..."));
augmented = true;
}
}
}