Skip to content

Commit 964b04d

Browse files
apmmachineapmmachineSylvainJuge
authored
test: synchronizing json spec (#2805)
* test: synchronizing json specs * test with shared spec JSON Co-authored-by: apmmachine <[email protected]> Co-authored-by: Sylvain Juge <[email protected]>
1 parent 37d631f commit 964b04d

File tree

2 files changed

+75
-0
lines changed

2 files changed

+75
-0
lines changed

apm-agent-core/src/test/java/co/elastic/apm/agent/impl/metadata/ContainerInfoTest.java

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,17 +19,70 @@
1919
package co.elastic.apm.agent.impl.metadata;
2020

2121
import co.elastic.apm.agent.util.CustomEnvVariables;
22+
import com.fasterxml.jackson.databind.JsonNode;
2223
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;
2328

2429
import javax.annotation.Nullable;
2530

31+
import java.util.ArrayList;
2632
import java.util.HashMap;
33+
import java.util.Iterator;
34+
import java.util.List;
2735
import java.util.Map;
36+
import java.util.stream.Stream;
2837

2938
import static org.assertj.core.api.Assertions.assertThat;
3039

3140
public class ContainerInfoTest extends CustomEnvVariables {
3241

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+
3386
@Test
3487
void testContainerIdParsing() {
3588
String validId = "3741401135a8d27237e2fb9c0fb2ecd93922c0d1dd708345451e479613f8d4ae";
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
{
2+
"testUnderscores": {
3+
"groupLine": "1:name=systemd:/kubepods.slice/kubepods-burstable.slice/kubepods-burstable-pod90d81341_92de_11e7_8cf2_507b9d4141fa.slice/crio-2227daf62df6694645fee5df53c1f91271546a9560e8600a525690ae252b7f63.scope",
4+
"containerId": "2227daf62df6694645fee5df53c1f91271546a9560e8600a525690ae252b7f63",
5+
"podId": "90d81341-92de-11e7-8cf2-507b9d4141fa"
6+
},
7+
"testOpenshiftForm": {
8+
"groupLine": "9:freezer:/kubepods.slice/kubepods-pod22949dce_fd8b_11ea_8ede_98f2b32c645c.slice/docker-b15a5bdedd2e7645c3be271364324321b908314e4c77857bbfd32a041148c07f.scope",
9+
"containerId": "b15a5bdedd2e7645c3be271364324321b908314e4c77857bbfd32a041148c07f",
10+
"podId": "22949dce-fd8b-11ea-8ede-98f2b32c645c"
11+
},
12+
"testUbuntuCGroup": {
13+
"groupLine": "1:name=systemd:/user.slice/user-1000.slice/[email protected]/apps.slice/apps-org.gnome.Terminal.slice/vte-spawn-75bc72bd-6642-4cf5-b62c-0674e11bfc84.scope",
14+
"containerId": null,
15+
"podId": null
16+
},
17+
"testAwsEcsCGroup": {
18+
"groupLine": "1:name=systemd:/ecs/03752a671e744971a862edcee6195646/03752a671e744971a862edcee6195646-4015103728",
19+
"containerId": "03752a671e744971a862edcee6195646-4015103728",
20+
"podId": null
21+
}
22+
}

0 commit comments

Comments
 (0)