|
| 1 | +// Copyright The OpenTelemetry Authors |
| 2 | +// |
| 3 | +// Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +// you may not use this file except in compliance with the License. |
| 5 | +// You may obtain a copy of the License at |
| 6 | +// |
| 7 | +// http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +// |
| 9 | +// Unless required by applicable law or agreed to in writing, software |
| 10 | +// distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +// See the License for the specific language governing permissions and |
| 13 | +// limitations under the License. |
| 14 | + |
| 15 | +package resource |
| 16 | + |
| 17 | +import ( |
| 18 | + "errors" |
| 19 | + "io" |
| 20 | + "os" |
| 21 | + "strings" |
| 22 | + "testing" |
| 23 | + |
| 24 | + "github.com/stretchr/testify/assert" |
| 25 | +) |
| 26 | + |
| 27 | +func setDefaultContainerProviders() { |
| 28 | + setContainerProviders( |
| 29 | + getContainerIDFromCGroup, |
| 30 | + ) |
| 31 | +} |
| 32 | + |
| 33 | +func setContainerProviders( |
| 34 | + idProvider containerIDProvider, |
| 35 | +) { |
| 36 | + containerID = idProvider |
| 37 | +} |
| 38 | + |
| 39 | +func TestGetContainerIDFromLine(t *testing.T) { |
| 40 | + testCases := []struct { |
| 41 | + name string |
| 42 | + line string |
| 43 | + expectedContainerID string |
| 44 | + }{ |
| 45 | + { |
| 46 | + name: "with suffix", |
| 47 | + line: "13:name=systemd:/podruntime/docker/kubepods/ac679f8a8319c8cf7d38e1adf263bc08d23.aaaa", |
| 48 | + expectedContainerID: "ac679f8a8319c8cf7d38e1adf263bc08d23", |
| 49 | + }, |
| 50 | + { |
| 51 | + name: "with prefix and suffix", |
| 52 | + line: "13:name=systemd:/podruntime/docker/kubepods/crio-dc679f8a8319c8cf7d38e1adf263bc08d23.stuff", |
| 53 | + expectedContainerID: "dc679f8a8319c8cf7d38e1adf263bc08d23", |
| 54 | + }, |
| 55 | + { |
| 56 | + name: "no prefix and suffix", |
| 57 | + line: "13:name=systemd:/pod/d86d75589bf6cc254f3e2cc29debdf85dde404998aa128997a819ff991827356", |
| 58 | + expectedContainerID: "d86d75589bf6cc254f3e2cc29debdf85dde404998aa128997a819ff991827356", |
| 59 | + }, |
| 60 | + { |
| 61 | + name: "with space", |
| 62 | + line: " 13:name=systemd:/pod/d86d75589bf6cc254f3e2cc29debdf85dde404998aa128997a819ff991827356 ", |
| 63 | + expectedContainerID: "d86d75589bf6cc254f3e2cc29debdf85dde404998aa128997a819ff991827356", |
| 64 | + }, |
| 65 | + { |
| 66 | + name: "invalid hex string", |
| 67 | + line: "13:name=systemd:/podruntime/docker/kubepods/ac679f8a8319c8cf7d38e1adf263bc08d23zzzz", |
| 68 | + }, |
| 69 | + { |
| 70 | + name: "no container id - 1", |
| 71 | + line: "pids: /", |
| 72 | + }, |
| 73 | + { |
| 74 | + name: "no container id - 2", |
| 75 | + line: "pids: ", |
| 76 | + }, |
| 77 | + } |
| 78 | + |
| 79 | + for _, tc := range testCases { |
| 80 | + t.Run(tc.name, func(t *testing.T) { |
| 81 | + containerID := getContainerIDFromLine(tc.line) |
| 82 | + assert.Equal(t, tc.expectedContainerID, containerID) |
| 83 | + }) |
| 84 | + } |
| 85 | +} |
| 86 | + |
| 87 | +func TestGetContainerIDFromReader(t *testing.T) { |
| 88 | + testCases := []struct { |
| 89 | + name string |
| 90 | + reader io.Reader |
| 91 | + expectedContainerID string |
| 92 | + }{ |
| 93 | + { |
| 94 | + name: "multiple lines", |
| 95 | + reader: strings.NewReader(`// |
| 96 | +1:name=systemd:/podruntime/docker/kubepods/docker-dc579f8a8319c8cf7d38e1adf263bc08d23 |
| 97 | +1:name=systemd:/podruntime/docker/kubepods/docker-dc579f8a8319c8cf7d38e1adf263bc08d24 |
| 98 | +`), |
| 99 | + expectedContainerID: "dc579f8a8319c8cf7d38e1adf263bc08d23", |
| 100 | + }, |
| 101 | + { |
| 102 | + name: "no container id", |
| 103 | + reader: strings.NewReader(`// |
| 104 | +1:name=systemd:/podruntime/docker |
| 105 | +`), |
| 106 | + expectedContainerID: "", |
| 107 | + }, |
| 108 | + } |
| 109 | + |
| 110 | + for _, tc := range testCases { |
| 111 | + t.Run(tc.name, func(t *testing.T) { |
| 112 | + containerID := getContainerIDFromReader(tc.reader) |
| 113 | + assert.Equal(t, tc.expectedContainerID, containerID) |
| 114 | + }) |
| 115 | + } |
| 116 | +} |
| 117 | + |
| 118 | +func TestGetContainerIDFromCGroup(t *testing.T) { |
| 119 | + t.Cleanup(func() { |
| 120 | + osStat = defaultOSStat |
| 121 | + osOpen = defaultOSOpen |
| 122 | + }) |
| 123 | + |
| 124 | + testCases := []struct { |
| 125 | + name string |
| 126 | + cgroupFileNotExist bool |
| 127 | + openFileError error |
| 128 | + content string |
| 129 | + expectedContainerID string |
| 130 | + expectedError bool |
| 131 | + }{ |
| 132 | + { |
| 133 | + name: "the cgroup file does not exist", |
| 134 | + cgroupFileNotExist: true, |
| 135 | + }, |
| 136 | + { |
| 137 | + name: "error when opening cgroup file", |
| 138 | + openFileError: errors.New("test"), |
| 139 | + expectedError: true, |
| 140 | + }, |
| 141 | + { |
| 142 | + name: "cgroup file", |
| 143 | + content: "1:name=systemd:/podruntime/docker/kubepods/docker-dc579f8a8319c8cf7d38e1adf263bc08d23", |
| 144 | + expectedContainerID: "dc579f8a8319c8cf7d38e1adf263bc08d23", |
| 145 | + }, |
| 146 | + } |
| 147 | + |
| 148 | + for _, tc := range testCases { |
| 149 | + t.Run(tc.name, func(t *testing.T) { |
| 150 | + osStat = func(name string) (os.FileInfo, error) { |
| 151 | + if tc.cgroupFileNotExist { |
| 152 | + return nil, os.ErrNotExist |
| 153 | + } |
| 154 | + return nil, nil |
| 155 | + } |
| 156 | + |
| 157 | + osOpen = func(name string) (io.ReadCloser, error) { |
| 158 | + if tc.openFileError != nil { |
| 159 | + return nil, tc.openFileError |
| 160 | + } |
| 161 | + return io.NopCloser(strings.NewReader(tc.content)), nil |
| 162 | + } |
| 163 | + |
| 164 | + containerID, err := getContainerIDFromCGroup() |
| 165 | + assert.Equal(t, tc.expectedError, err != nil) |
| 166 | + assert.Equal(t, tc.expectedContainerID, containerID) |
| 167 | + }) |
| 168 | + } |
| 169 | +} |
0 commit comments