Skip to content

Commit 7e2fcaa

Browse files
committed
add test to cover alternate encodings
1 parent d45c897 commit 7e2fcaa

File tree

3 files changed

+54
-4
lines changed

3 files changed

+54
-4
lines changed

instrumentation/resources/library/src/main/java/io/opentelemetry/instrumentation/resources/CgroupV1ContainerIdExtractor.java

+10-3
Original file line numberDiff line numberDiff line change
@@ -20,14 +20,21 @@ final class CgroupV1ContainerIdExtractor {
2020
Logger.getLogger(CgroupV1ContainerIdExtractor.class.getName());
2121
static final Path V1_CGROUP_PATH = Paths.get("/proc/self/cgroup");
2222
private final ContainerResource.Filesystem filesystem;
23+
private final Path inputFilePath;
2324

2425
CgroupV1ContainerIdExtractor() {
25-
this(ContainerResource.FILESYSTEM_INSTANCE);
26+
this(ContainerResource.FILESYSTEM_INSTANCE, V1_CGROUP_PATH);
2627
}
2728

2829
// Exists for testing
2930
CgroupV1ContainerIdExtractor(ContainerResource.Filesystem filesystem) {
31+
this(filesystem, V1_CGROUP_PATH);
32+
}
33+
34+
// Exists for testing
35+
CgroupV1ContainerIdExtractor(ContainerResource.Filesystem filesystem, Path inputFilePath) {
3036
this.filesystem = filesystem;
37+
this.inputFilePath = inputFilePath;
3138
}
3239

3340
/**
@@ -38,10 +45,10 @@ final class CgroupV1ContainerIdExtractor {
3845
* @return containerId
3946
*/
4047
Optional<String> extractContainerId() {
41-
if (!filesystem.isReadable(V1_CGROUP_PATH)) {
48+
if (!filesystem.isReadable(inputFilePath)) {
4249
return Optional.empty();
4350
}
44-
try (Stream<String> lines = filesystem.lines(V1_CGROUP_PATH)) {
51+
try (Stream<String> lines = filesystem.lines(inputFilePath)) {
4552
return lines
4653
.filter(line -> !line.isEmpty())
4754
.map(CgroupV1ContainerIdExtractor::getIdFromLine)

instrumentation/resources/library/src/main/java/io/opentelemetry/instrumentation/resources/ContainerResource.java

+5-1
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,11 @@ boolean isReadable(Path path) {
7878

7979
@MustBeClosed
8080
Stream<String> lines(Path path) throws IOException {
81-
return Files.lines(path, Charset.defaultCharset());
81+
return Files.lines(path, getDefaultCharset());
82+
}
83+
84+
Charset getDefaultCharset() {
85+
return Charset.defaultCharset();
8286
}
8387

8488
List<String> lineList(Path path) throws IOException {

instrumentation/resources/library/src/test/java/io/opentelemetry/instrumentation/resources/ContainerResourceTest.java

+39
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,10 @@
1111
import static org.mockito.Mockito.when;
1212

1313
import io.opentelemetry.sdk.resources.Resource;
14+
import java.io.File;
15+
import java.io.FileOutputStream;
16+
import java.nio.charset.Charset;
17+
import java.nio.charset.StandardCharsets;
1418
import java.util.Optional;
1519
import org.junit.jupiter.api.Test;
1620
import org.junit.jupiter.api.extension.ExtendWith;
@@ -50,4 +54,39 @@ void bothVersionsFail() {
5054
Resource resource = containerResource.buildResource();
5155
assertThat(resource).isSameAs(Resource.empty());
5256
}
57+
58+
@Test
59+
void testAlternateEncoding() throws Exception {
60+
String containerId = "ac679f8a8319c8cf7d38e1adf263bc08d231f2ff81abda3915f6e8ba4d64156a";
61+
String line = "13:name=systemd:/podruntime/docker/kubepods/" + containerId + ".aaaa";
62+
Charset ibmCharset = Charset.forName("IBM-273");
63+
byte[] utf8 = line.getBytes(StandardCharsets.UTF_8);
64+
byte[] ibm = line.getBytes(ibmCharset);
65+
assertThat(ibm).isNotEqualTo(utf8);
66+
67+
String ibmAsString = new String(ibm, ibmCharset);
68+
// Different bytes, different encoding, same semantic string value
69+
assertThat(line).isEqualTo(ibmAsString);
70+
71+
// Make temp file that contains the IBM encoding
72+
File tempFile = File.createTempFile("tmp", "mountinfo");
73+
tempFile.deleteOnExit();
74+
try (FileOutputStream out = new FileOutputStream(tempFile)) {
75+
out.write(ibm);
76+
}
77+
ContainerResource.Filesystem fs =
78+
new ContainerResource.Filesystem() {
79+
@Override
80+
Charset getDefaultCharset() {
81+
// Override to pretend our default encoding is ibm273
82+
return ibmCharset;
83+
}
84+
};
85+
CgroupV1ContainerIdExtractor extractor =
86+
new CgroupV1ContainerIdExtractor(fs, tempFile.toPath());
87+
ContainerResource testClass = new ContainerResource(extractor, null);
88+
89+
Resource resource = testClass.buildResource();
90+
assertThat(resource.getAttribute(CONTAINER_ID)).isEqualTo(containerId);
91+
}
5392
}

0 commit comments

Comments
 (0)