|
11 | 11 | import static org.mockito.Mockito.when;
|
12 | 12 |
|
13 | 13 | 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; |
14 | 18 | import java.util.Optional;
|
15 | 19 | import org.junit.jupiter.api.Test;
|
16 | 20 | import org.junit.jupiter.api.extension.ExtendWith;
|
@@ -50,4 +54,39 @@ void bothVersionsFail() {
|
50 | 54 | Resource resource = containerResource.buildResource();
|
51 | 55 | assertThat(resource).isSameAs(Resource.empty());
|
52 | 56 | }
|
| 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 | + } |
53 | 92 | }
|
0 commit comments