@@ -25,15 +25,65 @@ public function setUp(): void
2525 $ this ->detector = new Container ($ root ->url ());
2626 }
2727
28- public function test_valid_v1 (): void
28+ /**
29+ * cgroup (v1) should take precedence over mountinfo (v2)
30+ * @dataProvider cgroupMountinfoProvider
31+ */
32+ public function test_with_cgroup_and_mountinfo (string $ cgroup , string $ mountinfo , string $ expected ): void
2933 {
30- $ valid = 'a8493b8a4f6f23b65c5db50be86619ca4da078da040aa3d5ccff26fe50de205d ' ;
31- $ this ->cgroup ->setContent ($ valid );
34+ $ cgroup && $ this ->cgroup ->setContent ($ cgroup );
35+ $ mountinfo && $ this ->mountinfo ->setContent ($ mountinfo );
36+ $ resource = $ this ->detector ->getResource ();
37+
38+ $ this ->assertSame ($ expected , $ resource ->getAttributes ()->get (ResourceAttributes::CONTAINER_ID ));
39+ }
40+
41+ public static function cgroupMountinfoProvider (): array
42+ {
43+ return [
44+ 'k8s ' => [
45+ file_get_contents (__DIR__ . '/fixtures/v1.cgroup.k8s.txt ' ),
46+ file_get_contents (__DIR__ . '/fixtures/v2.mountinfo.k8s.txt ' ),
47+ '78ea929aa43e7b71f7c36583d82038d92a76800bf5da9b8850e8bd7b514bc075 ' ,
48+ ],
49+ 'docker with invalid cgroup ' => [
50+ 'no-container-ids-here ' ,
51+ file_get_contents (__DIR__ . '/fixtures/v2.mountinfo.docker.txt ' ),
52+ 'a8493b8a4f6f23b65c5db50be86619ca4da078da040aa3d5ccff26fe50de205d ' ,
53+ ],
54+ 'podman ' => [
55+ 'no-container-ids-here ' ,
56+ file_get_contents (__DIR__ . '/fixtures/v2.mountinfo.podman.txt ' ),
57+ '2a33efc76e519c137fe6093179653788bed6162d4a15e5131c8e835c968afbe6 ' ,
58+ ],
59+ ];
60+ }
61+
62+ /**
63+ * @dataProvider cgroupProvider
64+ */
65+ public function test_valid_v1 (string $ data , string $ expected ): void
66+ {
67+ $ this ->cgroup ->setContent ($ data );
3268 $ resource = $ this ->detector ->getResource ();
3369
3470 $ this ->assertSame (ResourceAttributes::SCHEMA_URL , $ resource ->getSchemaUrl ());
3571 $ this ->assertIsString ($ resource ->getAttributes ()->get (ResourceAttributes::CONTAINER_ID ));
36- $ this ->assertSame ($ valid , $ resource ->getAttributes ()->get (ResourceAttributes::CONTAINER_ID ));
72+ $ this ->assertSame ($ expected , $ resource ->getAttributes ()->get (ResourceAttributes::CONTAINER_ID ));
73+ }
74+
75+ public static function cgroupProvider (): array
76+ {
77+ return [
78+ 'docker ' => [
79+ file_get_contents (__DIR__ . '/fixtures/v1.cgroup.docker.txt ' ),
80+ '7be92808767a667f35c8505cbf40d14e931ef6db5b0210329cf193b15ba9d605 ' ,
81+ ],
82+ 'k8s ' => [
83+ file_get_contents (__DIR__ . '/fixtures/v1.cgroup.k8s.txt ' ),
84+ '78ea929aa43e7b71f7c36583d82038d92a76800bf5da9b8850e8bd7b514bc075 ' ,
85+ ],
86+ ];
3787 }
3888
3989 public function test_invalid_v1 (): void
@@ -44,23 +94,32 @@ public function test_invalid_v1(): void
4494 $ this ->assertEmpty ($ resource ->getAttributes ());
4595 }
4696
47- public function test_valid_v2 (): void
97+ /**
98+ * @dataProvider mountinfoProvider
99+ */
100+ public function test_valid_v2 (string $ data , string $ expected ): void
48101 {
49- $ expected = 'a8493b8a4f6f23b65c5db50be86619ca4da078da040aa3d5ccff26fe50de205d ' ;
50- $ data = <<< EOS
51- 1366 1365 0:30 / /sys/fs/cgroup ro,nosuid,nodev,noexec,relatime - cgroup2 cgroup rw
52- 1408 1362 0:107 / /dev/mqueue rw,nosuid,nodev,noexec,relatime - mqueue mqueue rw
53- 1579 1362 0:112 / /dev/shm rw,nosuid,nodev,noexec,relatime - tmpfs shm rw,size=65536k,inode64
54- 1581 1359 259:2 /var/lib/docker/containers/a8493b8a4f6f23b65c5db50be86619ca4da078da040aa3d5ccff26fe50de205d/hostname /etc/hostname rw,relatime - ext4 /dev/nvme0n1p2 rw,errors=remount-ro
55- 1583 1359 259:3 /brett/docker/otel/opentelemetry-php /usr/src/myapp rw,relatime - ext4 /dev/nvme0n1p3 rw
56- EOS ;
57102 $ this ->mountinfo ->withContent ($ data );
58103 $ resource = $ this ->detector ->getResource ();
59104
60105 $ this ->assertCount (1 , $ resource ->getAttributes ());
61106 $ this ->assertSame ($ expected , $ resource ->getAttributes ()->get (ResourceAttributes::CONTAINER_ID ));
62107 }
63108
109+ public static function mountinfoProvider (): array
110+ {
111+ return [
112+ 'docker ' => [
113+ file_get_contents (__DIR__ . '/fixtures/v2.mountinfo.docker.txt ' ),
114+ 'a8493b8a4f6f23b65c5db50be86619ca4da078da040aa3d5ccff26fe50de205d ' ,
115+ ],
116+ 'podman ' => [
117+ file_get_contents (__DIR__ . '/fixtures/v2.mountinfo.podman.txt ' ),
118+ '2a33efc76e519c137fe6093179653788bed6162d4a15e5131c8e835c968afbe6 ' ,
119+ ],
120+ ];
121+ }
122+
64123 public function test_invalid_v2 (): void
65124 {
66125 $ data = <<< EOS
0 commit comments