Skip to content

Commit 6e703e7

Browse files
author
Alex Ciminian
committed
Improve ContainerInfo coverage to enable check on new module
1 parent 8513b0f commit 6e703e7

3 files changed

Lines changed: 54 additions & 8 deletions

File tree

utils/container-utils/container-utils.gradle

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,5 @@
11
apply from: "$rootDir/gradle/java.gradle"
22

3-
// TODO: add more tests
4-
excludedClassesCoverage += [
5-
'datadog.common.exec*'
6-
]
7-
83
dependencies {
94
compile deps.slf4j
105

utils/container-utils/src/main/java/datadog/common/container/ContainerInfo.java

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,11 @@ public static boolean isRunningInContainer() {
7070
}
7171

7272
public static ContainerInfo fromDefaultProcFile() throws IOException, ParseException {
73-
final String content = new String(Files.readAllBytes(CGROUP_DEFAULT_PROCFILE));
73+
return fromProcFile(CGROUP_DEFAULT_PROCFILE);
74+
}
75+
76+
static ContainerInfo fromProcFile(Path path) throws IOException, ParseException {
77+
final String content = new String(Files.readAllBytes(path));
7478
if (content.isEmpty()) {
7579
log.debug("Proc file is empty");
7680
return new ContainerInfo();

utils/container-utils/src/test/groovy/ContainerInfoTest.groovy

Lines changed: 49 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,19 @@
11
package datadog.trace.core
22

3+
import datadog.common.container.ContainerInfo
34
import datadog.trace.util.test.DDSpecification
45
import spock.lang.Unroll
56

7+
import java.nio.file.Path
8+
import java.nio.file.Paths
9+
import java.text.ParseException
10+
611
class ContainerInfoTest extends DDSpecification {
712

813
@Unroll
914
def "CGroupInfo is parsed from individual lines"() {
1015
when:
11-
datadog.common.container.ContainerInfo.CGroupInfo cGroupInfo = datadog.common.container.ContainerInfo.parseLine(line)
16+
ContainerInfo.CGroupInfo cGroupInfo = ContainerInfo.parseLine(line)
1217

1318
then:
1419
cGroupInfo.getId() == id
@@ -83,7 +88,7 @@ class ContainerInfoTest extends DDSpecification {
8388
@Unroll
8489
def "Container info parsed from file content"() {
8590
when:
86-
datadog.common.container.ContainerInfo containerInfo = datadog.common.container.ContainerInfo.parse(content)
91+
ContainerInfo containerInfo = ContainerInfo.parse(content)
8792

8893
then:
8994
containerInfo.getContainerId() == containerId
@@ -144,4 +149,46 @@ class ContainerInfoTest extends DDSpecification {
144149
2:memory:/ecs/55091c13-b8cf-4801-b527-f4601742204d/432624d2150b349fe35ba397284dea788c2bf66b885d14dfc1569b01890ca7da
145150
1:name=systemd:/ecs/55091c13-b8cf-4801-b527-f4601742204d/432624d2150b349fe35ba397284dea788c2bf66b885d14dfc1569b01890ca7da"""
146151
}
152+
153+
def "ContainerInfo from empty file is empty"() {
154+
when:
155+
File f = File.createTempFile("container-info-test-", "-empty-file")
156+
f.deleteOnExit()
157+
Path p = Paths.get(f.path)
158+
ContainerInfo containerInfo = ContainerInfo.fromProcFile(p)
159+
160+
161+
then:
162+
containerInfo.getContainerId() == null
163+
containerInfo.getPodId() == null
164+
containerInfo.getCGroups().size() == 0
165+
}
166+
167+
def "ContainerInfo throws java.text.ParseException when given malformed procfile"() {
168+
when:
169+
File f = File.createTempFile("container-info-test-", "-malformed-file")
170+
f.deleteOnExit()
171+
f.write("This is not valid")
172+
Path p = Paths.get(f.path)
173+
ContainerInfo.fromProcFile(p)
174+
f.deleteOnExit()
175+
176+
then:
177+
thrown(ParseException)
178+
}
179+
180+
def "ContainerInfo tolerates missing container id and pod id in procfile"() {
181+
when:
182+
File f = File.createTempFile("container-info-test-", "-missing-container-id")
183+
f.deleteOnExit()
184+
f.write("1:cpuset:fake-path")
185+
Path p = Paths.get(f.path)
186+
ContainerInfo containerInfo = ContainerInfo.fromProcFile(p)
187+
f.deleteOnExit()
188+
189+
then:
190+
containerInfo.getContainerId() == null
191+
containerInfo.getPodId() == null
192+
containerInfo.getCGroups().size() == 1
193+
}
147194
}

0 commit comments

Comments
 (0)