| 1 |
// Copyright The OpenTelemetry Authors |
| 2 |
// SPDX-License-Identifier: Apache-2.0 |
| 3 |
|
| 4 |
package resource |
| 5 |
|
| 6 |
import ( |
| 7 |
"errors" |
| 8 |
"io" |
| 9 |
"os" |
| 10 |
"strings" |
| 11 |
"testing" |
| 12 |
|
| 13 |
"github.com/stretchr/testify/assert" |
| 14 |
) |
| 15 |
|
| 16 |
func setDefaultContainerProviders() { |
| 17 |
setContainerProviders( |
| 18 |
getContainerIDFromCGroup, |
| 19 |
) |
| 20 |
} |
| 21 |
|
| 22 |
func setContainerProviders( |
| 23 |
idProvider containerIDProvider, |
| 24 |
) { |
| 25 |
containerID = idProvider |
| 26 |
} |
| 27 |
|
| 28 |
func TestGetContainerIDFromLine(t *testing.T) { |
| 29 |
testCases := []struct { |
| 30 |
name string |
| 31 |
line string |
| 32 |
expectedContainerID string |
| 33 |
}{ |
| 34 |
{ |
| 35 |
name: "with suffix", |
| 36 |
line: "13:name=systemd:/podruntime/docker/kubepods/ac679f8a8319c8cf7d38e1adf263bc08d23.aaaa", |
| 37 |
expectedContainerID: "ac679f8a8319c8cf7d38e1adf263bc08d23", |
| 38 |
}, |
| 39 |
{ |
| 40 |
name: "with prefix and suffix", |
| 41 |
line: "13:name=systemd:/podruntime/docker/kubepods/crio-dc679f8a8319c8cf7d38e1adf263bc08d23.stuff", |
| 42 |
expectedContainerID: "dc679f8a8319c8cf7d38e1adf263bc08d23", |
| 43 |
}, |
| 44 |
{ |
| 45 |
name: "no prefix and suffix", |
| 46 |
line: "13:name=systemd:/pod/d86d75589bf6cc254f3e2cc29debdf85dde404998aa128997a819ff991827356", |
| 47 |
expectedContainerID: "d86d75589bf6cc254f3e2cc29debdf85dde404998aa128997a819ff991827356", |
| 48 |
}, |
| 49 |
{ |
| 50 |
name: "with space", |
| 51 |
line: " 13:name=systemd:/pod/d86d75589bf6cc254f3e2cc29debdf85dde404998aa128997a819ff991827356 ", |
| 52 |
expectedContainerID: "d86d75589bf6cc254f3e2cc29debdf85dde404998aa128997a819ff991827356", |
| 53 |
}, |
| 54 |
{ |
| 55 |
name: "with colon", |
| 56 |
line: " 13:name=systemd:/kuberuntime/containerd/kubepods-pod872d2066_00ef_48ea_a7d8_51b18b72d739:cri-containerd:e857a4bf05a69080a759574949d7a0e69572e27647800fa7faff6a05a8332aa1", |
| 57 |
expectedContainerID: "e857a4bf05a69080a759574949d7a0e69572e27647800fa7faff6a05a8332aa1", |
| 58 |
}, |
| 59 |
{ |
| 60 |
name: "invalid hex string", |
| 61 |
line: "13:name=systemd:/podruntime/docker/kubepods/ac679f8a8319c8cf7d38e1adf263bc08d23zzzz", |
| 62 |
}, |
| 63 |
{ |
| 64 |
name: "no container id - 1", |
| 65 |
line: "pids: /", |
| 66 |
}, |
| 67 |
{ |
| 68 |
name: "no container id - 2", |
| 69 |
line: "pids: ", |
| 70 |
}, |
| 71 |
} |
| 72 |
|
| 73 |
for _, tc := range testCases { |
| 74 |
t.Run(tc.name, func(t *testing.T) { |
| 75 |
containerID := getContainerIDFromLine(tc.line) |
| 76 |
assert.Equal(t, tc.expectedContainerID, containerID) |
| 77 |
}) |
| 78 |
} |
| 79 |
} |
| 80 |
|
| 81 |
func TestGetContainerIDFromReader(t *testing.T) { |
| 82 |
testCases := []struct { |
| 83 |
name string |
| 84 |
reader io.Reader |
| 85 |
expectedContainerID string |
| 86 |
}{ |
| 87 |
{ |
| 88 |
name: "multiple lines", |
| 89 |
reader: strings.NewReader(`// |
| 90 |
1:name=systemd:/podruntime/docker/kubepods/docker-dc579f8a8319c8cf7d38e1adf263bc08d23 |
| 91 |
1:name=systemd:/podruntime/docker/kubepods/docker-dc579f8a8319c8cf7d38e1adf263bc08d24 |
| 92 |
`), |
| 93 |
expectedContainerID: "dc579f8a8319c8cf7d38e1adf263bc08d23", |
| 94 |
}, |
| 95 |
{ |
| 96 |
name: "no container id", |
| 97 |
reader: strings.NewReader(`// |
| 98 |
1:name=systemd:/podruntime/docker |
| 99 |
`), |
| 100 |
expectedContainerID: "", |
| 101 |
}, |
| 102 |
} |
| 103 |
|
| 104 |
for _, tc := range testCases { |
| 105 |
t.Run(tc.name, func(t *testing.T) { |
| 106 |
containerID := getContainerIDFromReader(tc.reader) |
| 107 |
assert.Equal(t, tc.expectedContainerID, containerID) |
| 108 |
}) |
| 109 |
} |
| 110 |
} |
| 111 |
|
| 112 |
func TestGetContainerIDFromCGroup(t *testing.T) { |
| 113 |
t.Cleanup(func() { |
| 114 |
osStat = defaultOSStat |
| 115 |
osOpen = defaultOSOpen |
| 116 |
}) |
| 117 |
|
| 118 |
testCases := []struct { |
| 119 |
name string |
| 120 |
cgroupFileNotExist bool |
| 121 |
openFileError error |
| 122 |
content string |
| 123 |
expectedContainerID string |
| 124 |
expectedError bool |
| 125 |
}{ |
| 126 |
{ |
| 127 |
name: "the cgroup file does not exist", |
| 128 |
cgroupFileNotExist: true, |
| 129 |
}, |
| 130 |
{ |
| 131 |
name: "error when opening cgroup file", |
| 132 |
openFileError: errors.New("test"), |
| 133 |
expectedError: true, |
| 134 |
}, |
| 135 |
{ |
| 136 |
name: "cgroup file", |
| 137 |
content: "1:name=systemd:/podruntime/docker/kubepods/docker-dc579f8a8319c8cf7d38e1adf263bc08d23", |
| 138 |
expectedContainerID: "dc579f8a8319c8cf7d38e1adf263bc08d23", |
| 139 |
}, |
| 140 |
} |
| 141 |
|
| 142 |
for _, tc := range testCases { |
| 143 |
t.Run(tc.name, func(t *testing.T) { |
| 144 |
osStat = func(string) (os.FileInfo, error) { |
| 145 |
if tc.cgroupFileNotExist { |
| 146 |
return nil, os.ErrNotExist |
| 147 |
} |
| 148 |
return nil, nil |
| 149 |
} |
| 150 |
|
| 151 |
osOpen = func(string) (io.ReadCloser, error) { |
| 152 |
if tc.openFileError != nil { |
| 153 |
return nil, tc.openFileError |
| 154 |
} |
| 155 |
return io.NopCloser(strings.NewReader(tc.content)), nil |
| 156 |
} |
| 157 |
|
| 158 |
containerID, err := getContainerIDFromCGroup() |
| 159 |
assert.Equal(t, tc.expectedError, err != nil) |
| 160 |
assert.Equal(t, tc.expectedContainerID, containerID) |
| 161 |
}) |
| 162 |
} |
| 163 |
} |