Skip to content

Commit 5b5060a

Browse files
authored
fixes with tests on windows (#1671)
1 parent ab41de5 commit 5b5060a

File tree

14 files changed

+95
-38
lines changed

14 files changed

+95
-38
lines changed

CHANGELOG.asciidoc

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,7 @@ Even when matching on the main class name or on system properties,
6464
* Update Byte-buddy to `1.11.0` - {pull}1769[#1769]
6565
* Support for user.domain {pull}1756[#1756]
6666
* JAX-RS supports javax.ws.rs.PATCH
67+
* Enabling build and unit tests on Windows - {pull}1671[#1671]
6768
6869
[float]
6970
===== Bug fixes
@@ -75,6 +76,7 @@ Even when matching on the main class name or on system properties,
7576
* Apply consistent proxy class exclusion heuristic - {pull}1738[#1738]
7677
* Fix micrometer serialization error - {pull}1741[#1741]
7778
* Optimize & avoid `ensureInstrumented` deadlock by skipping stack-frame computation for Java7+ bytecode - {pull}1758[#1758]
79+
* Fix instrumentation plugins loading on Windows - {pull}1671[#1671]
7880
7981
[float]
8082
===== Refactors

apm-agent-attach-cli/src/test/java/co/elastic/apm/attach/ElasticApmAttacherTest.java

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,9 @@ void testCreateTempPropertiesWithExternalConfig() throws IOException {
7878

7979
File externalConfigFile = File.createTempFile("external-config", ".tmp");
8080
toClean.add(externalConfigFile);
81-
externalConfig.store(new FileOutputStream(externalConfigFile), null);
81+
try(FileOutputStream fos = new FileOutputStream(externalConfigFile)) {
82+
externalConfig.store(fos, null);
83+
}
8284

8385
Map<String, String> config = Map.of(
8486
"foo", "bär",
@@ -98,7 +100,9 @@ void testCreateTempPropertiesWithExternalConfig() throws IOException {
98100

99101
private Properties readProperties(File propertyFile) throws IOException {
100102
Properties properties = new Properties();
101-
properties.load(new FileReader(propertyFile));
103+
try (FileReader fileReader = new FileReader(propertyFile)) {
104+
properties.load(fileReader);
105+
}
102106
return properties;
103107
}
104108

apm-agent-core/src/main/java/co/elastic/apm/agent/util/PackageScanner.java

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
*/
2525
package co.elastic.apm.agent.util;
2626

27+
import java.io.File;
2728
import java.io.IOException;
2829
import java.net.URI;
2930
import java.net.URISyntaxException;
@@ -76,13 +77,21 @@ public static List<String> getClassNames(final String basePackage, ClassLoader c
7677
return classNames;
7778
}
7879

80+
/**
81+
* Lists all classes in the provided path, as part of the provided base package
82+
* @param basePackage the package to prepend to all class files found in the relative path
83+
* @param basePath the base lookup path. <b>NOTE: this is a file system path, as opposed to the Java class resource
84+
* path, localized to the file system. Specifically, we need to use the proper {@link File#separatorChar}</b>.
85+
* @return a list of fully qualified class names from the scanned package
86+
* @throws IOException error in file tree scanning
87+
*/
7988
private static List<String> listClassNames(final String basePackage, final Path basePath) throws IOException {
8089
final List<String> classNames = new ArrayList<>();
8190
Files.walkFileTree(basePath, new SimpleFileVisitor<Path>() {
8291
@Override
8392
public FileVisitResult visitFile(Path file, BasicFileAttributes attrs) {
8493
if (file.toString().endsWith(".class")) {
85-
classNames.add(basePackage + "." + basePath.relativize(file).toString().replace('/', '.').replace(".class", ""));
94+
classNames.add(basePackage + "." + basePath.relativize(file).toString().replace(File.separatorChar, '.').replace(".class", ""));
8695
}
8796
return FileVisitResult.CONTINUE;
8897
}

apm-agent-core/src/test/java/co/elastic/apm/agent/logging/Log4j2ConfigurationFactoryTest.java

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@
3131
import org.apache.logging.log4j.core.appender.RollingFileAppender;
3232
import org.apache.logging.log4j.core.config.Configuration;
3333
import org.apache.logging.log4j.core.layout.PatternLayout;
34+
import org.junit.jupiter.api.AfterEach;
3435
import org.junit.jupiter.api.Test;
3536
import org.junit.jupiter.api.io.TempDir;
3637
import org.stagemonitor.configuration.source.AbstractConfigurationSource;
@@ -45,13 +46,22 @@
4546

4647
class Log4j2ConfigurationFactoryTest {
4748

49+
private Appender appender;
50+
51+
@AfterEach
52+
public void stopRollingAppender() {
53+
if (appender != null) {
54+
appender.stop();
55+
}
56+
}
57+
4858
@Test
4959
void testLogFileJson(@TempDir Path tempDir) {
5060
String logFile = tempDir.resolve("agent.json").toString();
5161
Configuration configuration = getLogConfig(Map.of("log_file", logFile, "log_format_file", "json"));
5262

5363
assertThat(configuration.getAppenders().values()).hasSize(1);
54-
Appender appender = configuration.getAppenders().values().iterator().next();
64+
appender = configuration.getAppenders().values().iterator().next();
5565

5666
assertThat(appender).isInstanceOf(RollingFileAppender.class);
5767
assertThat(((RollingFileAppender) appender).getFileName()).isEqualTo(logFile);
@@ -64,7 +74,7 @@ void testLogFilePlainText(@TempDir Path tempDir) {
6474
Configuration configuration = getLogConfig(Map.of("log_file", logFile));
6575

6676
assertThat(configuration.getAppenders().values()).hasSize(1);
67-
Appender appender = configuration.getAppenders().values().iterator().next();
77+
appender = configuration.getAppenders().values().iterator().next();
6878

6979
assertThat(appender).isInstanceOf(RollingFileAppender.class);
7080
assertThat(((RollingFileAppender) appender).getFileName()).isEqualTo(logFile);

apm-agent-plugins/apm-log-shader-plugin/apm-log-shader-plugin-common/src/test/java/co/elastic/apm/agent/log/shader/UtilsTest.java

Lines changed: 24 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -31,11 +31,15 @@
3131

3232
import javax.annotation.Nullable;
3333

34+
import java.io.File;
35+
3436
import static org.assertj.core.api.Assertions.assertThat;
3537
import static org.mockito.Mockito.when;
3638

3739
public class UtilsTest extends AbstractInstrumentationTest {
3840

41+
private static final String fileSeparator = File.separator;
42+
3943
@Nullable
4044
private final String logEcsFormattingDestinationDir = config.getConfig(LoggingConfiguration.class).getLogEcsFormattingDestinationDir();
4145

@@ -45,36 +49,36 @@ private String computeShadeLogFilePathWithConfiguredDir(String logFilePath) {
4549

4650
@Test
4751
void testShadePathComputation() {
48-
assertThat(computeShadeLogFilePathWithConfiguredDir("/test/absolute/path/app.log")).isEqualTo("/test/absolute/path/app.ecs.json");
49-
assertThat(computeShadeLogFilePathWithConfiguredDir("test/relative/path/app.log")).isEqualTo("test/relative/path/app.ecs.json");
50-
assertThat(computeShadeLogFilePathWithConfiguredDir("/app.log")).isEqualTo("/app.ecs.json");
51-
assertThat(computeShadeLogFilePathWithConfiguredDir("app.log")).isEqualTo("app.ecs.json");
52+
assertThat(computeShadeLogFilePathWithConfiguredDir("/test/absolute/path/app.log")).isEqualTo(replaceFileSeparator("/test/absolute/path/app.ecs.json"));
53+
assertThat(computeShadeLogFilePathWithConfiguredDir("test/relative/path/app.log")).isEqualTo(replaceFileSeparator("test/relative/path/app.ecs.json"));
54+
assertThat(computeShadeLogFilePathWithConfiguredDir("/app.log")).isEqualTo(replaceFileSeparator("/app.ecs.json"));
55+
assertThat(computeShadeLogFilePathWithConfiguredDir("app.log")).isEqualTo(replaceFileSeparator("app.ecs.json"));
5256
}
5357

5458
@Test
5559
void testReplace() {
5660
when(config.getConfig(LoggingConfiguration.class).getLogEcsReformatting()).thenReturn(LogEcsReformatting.REPLACE);
57-
assertThat(computeShadeLogFilePathWithConfiguredDir("/test/absolute/path/app.log")).isEqualTo("/test/absolute/path/app.ecs.json");
58-
assertThat(computeShadeLogFilePathWithConfiguredDir("/test/absolute/path/app")).isEqualTo("/test/absolute/path/app.ecs.json");
59-
assertThat(computeShadeLogFilePathWithConfiguredDir("/test/absolute/path/app.log.1")).isEqualTo("/test/absolute/path/app.log.ecs.json");
61+
assertThat(computeShadeLogFilePathWithConfiguredDir("/test/absolute/path/app.log")).isEqualTo(replaceFileSeparator("/test/absolute/path/app.ecs.json"));
62+
assertThat(computeShadeLogFilePathWithConfiguredDir("/test/absolute/path/app")).isEqualTo(replaceFileSeparator("/test/absolute/path/app.ecs.json"));
63+
assertThat(computeShadeLogFilePathWithConfiguredDir("/test/absolute/path/app.log.1")).isEqualTo(replaceFileSeparator("/test/absolute/path/app.log.ecs.json"));
6064
}
6165

6266
@Test
6367
void testAlternativeShadeLogsDestination_AbsolutePath() {
6468
String shadeDir = "/some/alt/location";
65-
assertThat(Utils.computeShadeLogFilePath("/test/absolute/path/app.log", shadeDir)).isEqualTo("/some/alt/location/app.ecs.json");
66-
assertThat(Utils.computeShadeLogFilePath("test/relative/path/app.log", shadeDir)).isEqualTo("/some/alt/location/app.ecs.json");
67-
assertThat(Utils.computeShadeLogFilePath("/app.log", shadeDir)).isEqualTo("/some/alt/location/app.ecs.json");
68-
assertThat(Utils.computeShadeLogFilePath("app.log", shadeDir)).isEqualTo("/some/alt/location/app.ecs.json");
69+
assertThat(Utils.computeShadeLogFilePath("/test/absolute/path/app.log", shadeDir)).isEqualTo(replaceFileSeparator("/some/alt/location/app.ecs.json"));
70+
assertThat(Utils.computeShadeLogFilePath("test/relative/path/app.log", shadeDir)).isEqualTo(replaceFileSeparator("/some/alt/location/app.ecs.json"));
71+
assertThat(Utils.computeShadeLogFilePath("/app.log", shadeDir)).isEqualTo(replaceFileSeparator("/some/alt/location/app.ecs.json"));
72+
assertThat(Utils.computeShadeLogFilePath("app.log", shadeDir)).isEqualTo(replaceFileSeparator("/some/alt/location/app.ecs.json"));
6973
}
7074

7175
@Test
7276
void testAlternativeShadeLogsDestination_RelativePath() {
7377
String shadeDir = "some/alt/location";
74-
assertThat(Utils.computeShadeLogFilePath("/test/absolute/path/app.log", shadeDir)).isEqualTo("/test/absolute/path/some/alt/location/app.ecs.json");
75-
assertThat(Utils.computeShadeLogFilePath("test/relative/path/app.log", shadeDir)).isEqualTo("test/relative/path/some/alt/location/app.ecs.json");
76-
assertThat(Utils.computeShadeLogFilePath("/app.log", shadeDir)).isEqualTo("/some/alt/location/app.ecs.json");
77-
assertThat(Utils.computeShadeLogFilePath("app.log", shadeDir)).isEqualTo("some/alt/location/app.ecs.json");
78+
assertThat(Utils.computeShadeLogFilePath("/test/absolute/path/app.log", shadeDir)).isEqualTo(replaceFileSeparator("/test/absolute/path/some/alt/location/app.ecs.json"));
79+
assertThat(Utils.computeShadeLogFilePath("test/relative/path/app.log", shadeDir)).isEqualTo(replaceFileSeparator("test/relative/path/some/alt/location/app.ecs.json"));
80+
assertThat(Utils.computeShadeLogFilePath("/app.log", shadeDir)).isEqualTo(replaceFileSeparator("/some/alt/location/app.ecs.json"));
81+
assertThat(Utils.computeShadeLogFilePath("app.log", shadeDir)).isEqualTo(replaceFileSeparator("some/alt/location/app.ecs.json"));
7882
}
7983

8084
@Test
@@ -83,4 +87,9 @@ void testFileExtensionReplacement() {
8387
assertThat(Utils.replaceFileExtensionToEcsJson("app")).isEqualTo("app.ecs.json");
8488
assertThat(Utils.replaceFileExtensionToEcsJson("app.some.log")).isEqualTo("app.some.ecs.json");
8589
}
90+
91+
private String replaceFileSeparator(String input) {
92+
return input.replace("/", fileSeparator);
93+
}
94+
8695
}

apm-agent-plugins/apm-log-shipper-plugin/src/test/java/co/elastic/apm/agent/log/shipper/ApmServerLogShipperTest.java

Lines changed: 18 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -32,14 +32,15 @@
3232
import co.elastic.apm.agent.report.serialize.DslJsonSerializer;
3333
import com.fasterxml.jackson.databind.JsonNode;
3434
import com.fasterxml.jackson.databind.ObjectMapper;
35+
import com.github.tomakehurst.wiremock.WireMockServer;
3536
import com.github.tomakehurst.wiremock.core.WireMockConfiguration;
36-
import com.github.tomakehurst.wiremock.junit.WireMockRule;
3737
import org.assertj.core.util.Lists;
3838
import org.awaitility.Awaitility;
39-
import org.junit.After;
40-
import org.junit.Before;
41-
import org.junit.Rule;
42-
import org.junit.Test;
39+
import org.junit.jupiter.api.AfterEach;
40+
import org.junit.jupiter.api.BeforeEach;
41+
import org.junit.jupiter.api.Test;
42+
import org.junit.jupiter.api.condition.DisabledOnOs;
43+
import org.junit.jupiter.api.condition.OS;
4344
import org.stagemonitor.configuration.ConfigurationRegistry;
4445

4546
import java.io.BufferedReader;
@@ -64,21 +65,22 @@
6465
import static com.github.tomakehurst.wiremock.client.WireMock.urlEqualTo;
6566
import static org.assertj.core.api.Assertions.assertThat;
6667

67-
public class ApmServerLogShipperTest {
68+
@DisabledOnOs(OS.WINDOWS)
69+
class ApmServerLogShipperTest {
6870

69-
@Rule
70-
public WireMockRule mockApmServer = new WireMockRule(WireMockConfiguration.wireMockConfig().dynamicPort());
71+
public WireMockServer mockApmServer = new WireMockServer(WireMockConfiguration.wireMockConfig().dynamicPort());
7172
private TailableFile tailableFile;
7273
private ApmServerLogShipper logShipper;
7374
private File logFile;
7475
private final ByteBuffer buffer = ByteBuffer.allocate(1024);
7576
private ApmServerClient apmServerClient;
7677

77-
@Before
78-
public void setUp() throws Exception {
78+
@BeforeEach
79+
void setUp() throws Exception {
7980
ConfigurationRegistry config = SpyConfiguration.createSpyConfig();
8081
mockApmServer.stubFor(post("/intake/v2/logs").willReturn(ok()));
8182
mockApmServer.stubFor(get("/").willReturn(ok()));
83+
mockApmServer.start();
8284

8385
apmServerClient = new ApmServerClient(config.getConfig(ReporterConfiguration.class));
8486
startClientWithValidUrls();
@@ -93,15 +95,17 @@ private void startClientWithValidUrls() throws MalformedURLException {
9395
apmServerClient.start(List.of(new URL("http", "localhost", mockApmServer.port(), "/")));
9496
}
9597

96-
@After
97-
public void tearDown() {
98+
@AfterEach
99+
void tearDown() {
100+
mockApmServer.stop();
101+
98102
if (!logFile.delete()) {
99103
logFile.deleteOnExit();
100104
}
101105
}
102106

103107
@Test
104-
public void testSendLogs() throws Exception {
108+
void testSendLogs() throws Exception {
105109
Files.write(logFile.toPath(), List.of("foo"));
106110
assertThat(tailableFile.tail(buffer, logShipper, 100)).isEqualTo(1);
107111
logShipper.endRequest();
@@ -115,7 +119,7 @@ public void testSendLogs() throws Exception {
115119
}
116120

117121
@Test
118-
public void testSendLogsAfterServerUrlsSet() throws Exception {
122+
void testSendLogsAfterServerUrlsSet() throws Exception {
119123
apmServerClient.start(Lists.emptyList());
120124
Files.write(logFile.toPath(), List.of("foo"));
121125
assertThat(logShipper.getErrorCount()).isEqualTo(0);

apm-agent-plugins/apm-log-shipper-plugin/src/test/java/co/elastic/apm/agent/log/shipper/TailableFileTest.java

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,8 @@
2727
import org.junit.jupiter.api.AfterEach;
2828
import org.junit.jupiter.api.BeforeEach;
2929
import org.junit.jupiter.api.Test;
30+
import org.junit.jupiter.api.condition.DisabledOnOs;
31+
import org.junit.jupiter.api.condition.OS;
3032

3133
import java.io.File;
3234
import java.io.IOException;
@@ -42,7 +44,7 @@
4244
import static org.assertj.core.api.Assertions.assertThat;
4345
import static org.assertj.core.api.Assertions.assertThatThrownBy;
4446

45-
47+
@DisabledOnOs(OS.WINDOWS)
4648
class TailableFileTest {
4749

4850
private final ByteBuffer buffy = ByteBuffer.allocate(1024);

apm-agent-plugins/apm-micrometer-plugin/src/test/java/co/elastic/apm/agent/micrometer/MicrometerMetricsReporterTest.java

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,7 @@
5151
import org.junit.jupiter.api.Test;
5252

5353
import javax.annotation.Nonnull;
54+
import java.nio.charset.StandardCharsets;
5455
import java.time.Duration;
5556
import java.util.Arrays;
5657
import java.util.List;
@@ -144,6 +145,7 @@ void testNonAsciiMetricNameDisabledMetrics() {
144145
meterRegistry.counter("网络").increment(42);
145146

146147
JsonNode metricSet = getSingleMetricSet();
148+
System.out.println("JsonNode metric = " + metricSet.toPrettyString());
147149
assertThat(metricSet.get("metricset").get("samples").get("网络").get("value").doubleValue()).isEqualTo(42);
148150
}
149151

@@ -465,7 +467,7 @@ private List<JsonNode> getMetricSets() {
465467
metricsReporter.run();
466468
List<JsonNode> metricSets = reporter.getBytes()
467469
.stream()
468-
.map(String::new)
470+
.map(k -> new String(k, StandardCharsets.UTF_8))
469471
.flatMap(s -> Arrays.stream(s.split("\n")))
470472
.map(this::deserialize)
471473
.collect(Collectors.toList());

apm-agent-plugins/apm-profiling-plugin/src/test/java/co/elastic/apm/agent/profiler/CallTreeSpanifyTest.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,9 @@
3535
import org.junit.jupiter.api.AfterEach;
3636
import org.junit.jupiter.api.BeforeEach;
3737
import org.junit.jupiter.api.Test;
38+
import org.junit.jupiter.api.condition.DisabledIfSystemProperty;
39+
import org.junit.jupiter.api.condition.DisabledOnOs;
40+
import org.junit.jupiter.api.condition.OS;
3841
import org.stagemonitor.configuration.ConfigurationRegistry;
3942

4043
import java.io.IOException;
@@ -68,6 +71,7 @@ void tearDown() throws IOException {
6871
}
6972

7073
@Test
74+
@DisabledOnOs(OS.WINDOWS)
7175
void testSpanification() throws Exception {
7276
CallTree.Root callTree = CallTreeTest.getCallTree(tracer, new String[]{
7377
" dd ",

apm-agent-plugins/apm-profiling-plugin/src/test/java/co/elastic/apm/agent/profiler/CallTreeTest.java

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,8 @@
4242
import org.junit.jupiter.api.BeforeEach;
4343
import org.junit.jupiter.api.Disabled;
4444
import org.junit.jupiter.api.Test;
45+
import org.junit.jupiter.api.condition.DisabledOnOs;
46+
import org.junit.jupiter.api.condition.OS;
4547
import org.stagemonitor.configuration.ConfigurationRegistry;
4648

4749
import javax.annotation.Nonnull;
@@ -61,6 +63,7 @@
6163
import static org.assertj.core.api.Assertions.assertThat;
6264
import static org.mockito.Mockito.when;
6365

66+
@DisabledOnOs(OS.WINDOWS)
6467
class CallTreeTest {
6568

6669
private MockReporter reporter;

0 commit comments

Comments
 (0)