|
| 1 | +/* |
| 2 | + * Teleport |
| 3 | + * Copyright (C) 2025 Gravitational, Inc. |
| 4 | + * |
| 5 | + * This program is free software: you can redistribute it and/or modify |
| 6 | + * it under the terms of the GNU Affero General Public License as published by |
| 7 | + * the Free Software Foundation, either version 3 of the License, or |
| 8 | + * (at your option) any later version. |
| 9 | + * |
| 10 | + * This program is distributed in the hope that it will be useful, |
| 11 | + * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 12 | + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 13 | + * GNU Affero General Public License for more details. |
| 14 | + * |
| 15 | + * You should have received a copy of the GNU Affero General Public License |
| 16 | + * along with this program. If not, see <http://www.gnu.org/licenses/>. |
| 17 | + */ |
| 18 | + |
| 19 | +package container_test |
| 20 | + |
| 21 | +import ( |
| 22 | + "os" |
| 23 | + "path/filepath" |
| 24 | + "testing" |
| 25 | + |
| 26 | + "github.com/stretchr/testify/assert" |
| 27 | + "github.com/stretchr/testify/require" |
| 28 | + |
| 29 | + "github.com/gravitational/teleport/lib/tbot/workloadidentity/workloadattest/container" |
| 30 | + "github.com/gravitational/teleport/lib/utils" |
| 31 | +) |
| 32 | + |
| 33 | +func TestLookupPID(t *testing.T) { |
| 34 | + tests := map[string]struct { |
| 35 | + parser container.Parser |
| 36 | + expected *container.Info |
| 37 | + error string |
| 38 | + }{ |
| 39 | + "k8s-real-docker-desktop": { |
| 40 | + parser: container.KubernetesParser, |
| 41 | + expected: &container.Info{ |
| 42 | + PodID: "941f292f-a62d-48ab-b9a8-eec84d87b928", |
| 43 | + ID: "3f79e718744418736d0f6b9958e08d44e969c6577068c33de1cc400d35aacec8", |
| 44 | + Rootfulness: container.RootfulnessUnknown, |
| 45 | + }, |
| 46 | + }, |
| 47 | + "k8s-real-orbstack": { |
| 48 | + parser: container.KubernetesParser, |
| 49 | + expected: &container.Info{ |
| 50 | + PodID: "36827f77-691f-45aa-a470-0989cf3749c4", |
| 51 | + ID: "64dd9bf5199ff782835247cb072e4842dc3d0135ef02f6498cb6bb6f37a320d2", |
| 52 | + Rootfulness: container.RootfulnessUnknown, |
| 53 | + }, |
| 54 | + }, |
| 55 | + "k8s-real-k3s-ubuntu-v1.28.6+k3s2": { |
| 56 | + parser: container.KubernetesParser, |
| 57 | + expected: &container.Info{ |
| 58 | + PodID: "fecd2321-17b5-49b9-9f75-8c5be777fbfb", |
| 59 | + ID: "397529d07efebd566f15dbc7e8af9f3ef586033f5e753adfa96b2bf730102c64", |
| 60 | + Rootfulness: container.RootfulnessUnknown, |
| 61 | + }, |
| 62 | + }, |
| 63 | + "k8s-real-gcp-v1.29.5-gke.1091002": { |
| 64 | + parser: container.KubernetesParser, |
| 65 | + expected: &container.Info{ |
| 66 | + PodID: "61c266b0-6f75-4490-8d92-3c9ae4d02787", |
| 67 | + ID: "9da25af0b548c8c60aa60f77f299ba727bf72d58248bd7528eb5390ffcce555a", |
| 68 | + Rootfulness: container.RootfulnessUnknown, |
| 69 | + }, |
| 70 | + }, |
| 71 | + "podman-real-4.3.1-rootful-systemd-pod": { |
| 72 | + parser: container.PodmanParser, |
| 73 | + expected: &container.Info{ |
| 74 | + PodID: "88c57f699ea2c137d7f19b7a6aaa5828072cf12207b56d7155f02d4ecade4510", |
| 75 | + ID: "4f6f96595778a052ebbd8e783156e347143cd79f81348d0995a0ffd5718c3393", |
| 76 | + Rootfulness: container.Rootful, |
| 77 | + }, |
| 78 | + }, |
| 79 | + "podman-real-4.3.1-rootful-systemd-container": { |
| 80 | + parser: container.PodmanParser, |
| 81 | + expected: &container.Info{ |
| 82 | + PodID: "", |
| 83 | + ID: "12519ca1a57b8f58bc2a44f4e33e37eaf07c55a8d468ffb3db33f29d8d869186", |
| 84 | + Rootfulness: container.Rootful, |
| 85 | + }, |
| 86 | + }, |
| 87 | + "podman-real-4.3.1-rootless-systemd-pod": { |
| 88 | + parser: container.PodmanParser, |
| 89 | + expected: &container.Info{ |
| 90 | + PodID: "5ffc3df0af9a6dd0f92668fc949734aad2ad41a5670b7218196d377d55ca32c5", |
| 91 | + ID: "d54768c18894b931db6f6876f6be2178d8a8b34fc3485659fda78fe86af3e08b", |
| 92 | + Rootfulness: container.Rootless, |
| 93 | + }, |
| 94 | + }, |
| 95 | + "podman-real-4.3.1-rootless-systemd-container": { |
| 96 | + parser: container.PodmanParser, |
| 97 | + expected: &container.Info{ |
| 98 | + PodID: "", |
| 99 | + ID: "f89494c4c00e68029e176eb60c5be675f9b076b9ca63190678b27a2ef0d09d13", |
| 100 | + Rootfulness: container.Rootless, |
| 101 | + }, |
| 102 | + }, |
| 103 | + "podman-real-4.3.1-rootful-cgroupfs-container": { |
| 104 | + parser: container.PodmanParser, |
| 105 | + expected: &container.Info{ |
| 106 | + PodID: "", |
| 107 | + ID: "1861a57278895fe0165c953c04e6c1082bcd73428776f5209616061d0022e881", |
| 108 | + Rootfulness: container.Rootful, |
| 109 | + }, |
| 110 | + }, |
| 111 | + "podman-real-4.3.1-rootless-cgroupfs-systemd-enabled-container": { |
| 112 | + parser: container.PodmanParser, |
| 113 | + error: "--cgroup-manager cgroupfs", |
| 114 | + }, |
| 115 | + } |
| 116 | + for name, tc := range tests { |
| 117 | + t.Run(name, func(t *testing.T) { |
| 118 | + tempDir := t.TempDir() |
| 119 | + require.NoError(t, os.MkdirAll(filepath.Join(tempDir, "proc", "1234"), 0755)) |
| 120 | + require.NoError(t, utils.CopyFile( |
| 121 | + filepath.Join("testdata", "mountfile", name), |
| 122 | + filepath.Join(tempDir, "proc", "1234", "mountinfo"), |
| 123 | + 0755), |
| 124 | + ) |
| 125 | + |
| 126 | + info, err := container.LookupPID(tempDir, 1234, tc.parser) |
| 127 | + if tc.error != "" { |
| 128 | + require.ErrorContains(t, err, tc.error) |
| 129 | + } else { |
| 130 | + require.NoError(t, err) |
| 131 | + assert.Equal(t, tc.expected, info) |
| 132 | + } |
| 133 | + }) |
| 134 | + } |
| 135 | +} |
0 commit comments