|
19 | 19 | package co.elastic.apm.agent.impl.metadata; |
20 | 20 |
|
21 | 21 | import co.elastic.apm.agent.util.CustomEnvVariables; |
| 22 | +import com.fasterxml.jackson.databind.JsonNode; |
22 | 23 | import org.junit.jupiter.api.Test; |
| 24 | +import org.junit.jupiter.params.ParameterizedTest; |
| 25 | +import org.junit.jupiter.params.provider.Arguments; |
| 26 | +import org.junit.jupiter.params.provider.MethodSource; |
| 27 | +import specs.TestJsonSpec; |
23 | 28 |
|
24 | 29 | import javax.annotation.Nullable; |
25 | 30 |
|
| 31 | +import java.util.ArrayList; |
26 | 32 | import java.util.HashMap; |
| 33 | +import java.util.Iterator; |
| 34 | +import java.util.List; |
27 | 35 | import java.util.Map; |
| 36 | +import java.util.stream.Stream; |
28 | 37 |
|
29 | 38 | import static org.assertj.core.api.Assertions.assertThat; |
30 | 39 |
|
31 | 40 | public class ContainerInfoTest extends CustomEnvVariables { |
32 | 41 |
|
| 42 | + @ParameterizedTest(name = "{0}") |
| 43 | + @MethodSource("getCommonPatterns") |
| 44 | + void testCommonPatterns(String testName, String groupLine, @Nullable String containerId, @Nullable String podId) { |
| 45 | + SystemInfo systemInfo = createSystemInfo().parseContainerId(groupLine); |
| 46 | + assertThat(systemInfo).isNotNull(); |
| 47 | + SystemInfo.Container containerInfo = systemInfo.getContainerInfo(); |
| 48 | + if (containerId == null) { |
| 49 | + assertThat(containerInfo).isNull(); |
| 50 | + } else { |
| 51 | + assertThat(containerInfo).describedAs("missing container info from '%s'", groupLine).isNotNull(); |
| 52 | + assertThat(containerInfo.getId()).isEqualTo(containerId); |
| 53 | + } |
| 54 | + SystemInfo.Kubernetes kubernetesInfo = systemInfo.getKubernetesInfo(); |
| 55 | + if (podId == null) { |
| 56 | + assertThat(kubernetesInfo).isNull(); |
| 57 | + } else { |
| 58 | + assertThat(kubernetesInfo).describedAs("missing kubernetes info from '%s'", groupLine).isNotNull(); |
| 59 | + assertThat(kubernetesInfo.getPod()).isNotNull(); |
| 60 | + assertThat(kubernetesInfo.getPod().getUid()).isEqualTo(podId); |
| 61 | + } |
| 62 | + } |
| 63 | + |
| 64 | + static Stream<Arguments> getCommonPatterns() { |
| 65 | + JsonNode json = TestJsonSpec.getJson("cgroup_parsing.json"); |
| 66 | + |
| 67 | + assertThat(json.isObject()) |
| 68 | + .describedAs("unexpected JSON spec format") |
| 69 | + .isTrue(); |
| 70 | + |
| 71 | + List<Arguments> args = new ArrayList<>(); |
| 72 | + Iterator<Map.Entry<String, JsonNode>> iterator = json.fields(); |
| 73 | + while(iterator.hasNext()){ |
| 74 | + Map.Entry<String, JsonNode> entry = iterator.next(); |
| 75 | + args.add(Arguments.of( |
| 76 | + entry.getKey(), |
| 77 | + entry.getValue().get("groupLine").asText(), |
| 78 | + entry.getValue().get("containerId").asText(null), |
| 79 | + entry.getValue().get("podId").asText(null) |
| 80 | + )); |
| 81 | + } |
| 82 | + |
| 83 | + return args.stream(); |
| 84 | + } |
| 85 | + |
33 | 86 | @Test |
34 | 87 | void testContainerIdParsing() { |
35 | 88 | String validId = "3741401135a8d27237e2fb9c0fb2ecd93922c0d1dd708345451e479613f8d4ae"; |
|
0 commit comments