|
1 | 1 | package datadog.trace.core |
2 | 2 |
|
| 3 | +import datadog.common.container.ContainerInfo |
3 | 4 | import datadog.trace.util.test.DDSpecification |
4 | 5 | import spock.lang.Unroll |
5 | 6 |
|
| 7 | +import java.nio.file.Path |
| 8 | +import java.nio.file.Paths |
| 9 | +import java.text.ParseException |
| 10 | + |
6 | 11 | class ContainerInfoTest extends DDSpecification { |
7 | 12 |
|
8 | 13 | @Unroll |
9 | 14 | def "CGroupInfo is parsed from individual lines"() { |
10 | 15 | when: |
11 | | - datadog.common.container.ContainerInfo.CGroupInfo cGroupInfo = datadog.common.container.ContainerInfo.parseLine(line) |
| 16 | + ContainerInfo.CGroupInfo cGroupInfo = ContainerInfo.parseLine(line) |
12 | 17 |
|
13 | 18 | then: |
14 | 19 | cGroupInfo.getId() == id |
@@ -83,7 +88,7 @@ class ContainerInfoTest extends DDSpecification { |
83 | 88 | @Unroll |
84 | 89 | def "Container info parsed from file content"() { |
85 | 90 | when: |
86 | | - datadog.common.container.ContainerInfo containerInfo = datadog.common.container.ContainerInfo.parse(content) |
| 91 | + ContainerInfo containerInfo = ContainerInfo.parse(content) |
87 | 92 |
|
88 | 93 | then: |
89 | 94 | containerInfo.getContainerId() == containerId |
@@ -144,4 +149,46 @@ class ContainerInfoTest extends DDSpecification { |
144 | 149 | 2:memory:/ecs/55091c13-b8cf-4801-b527-f4601742204d/432624d2150b349fe35ba397284dea788c2bf66b885d14dfc1569b01890ca7da |
145 | 150 | 1:name=systemd:/ecs/55091c13-b8cf-4801-b527-f4601742204d/432624d2150b349fe35ba397284dea788c2bf66b885d14dfc1569b01890ca7da""" |
146 | 151 | } |
| 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 | + } |
147 | 194 | } |
0 commit comments