-
Notifications
You must be signed in to change notification settings - Fork 127
Expand file tree
/
Copy pathutils.go
More file actions
266 lines (237 loc) · 7.09 KB
/
utils.go
File metadata and controls
266 lines (237 loc) · 7.09 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
// Copyright (c) 2023-2026, Nubificus LTD
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package urunce2etesting
import (
"bufio"
"fmt"
"log"
"os"
"path/filepath"
"strconv"
"strings"
"time"
probing "github.com/prometheus-community/pro-bing"
)
const (
maxPullRetries = 5
pullRetryDelay = 2 * time.Second
)
func getTestImages(cases []containerTestArgs) []string {
unique := make(map[string]struct{})
for _, tc := range cases {
unique[tc.Image] = struct{}{}
}
images := make([]string, 0, len(unique))
for img := range unique {
images = append(images, img)
}
return images
}
func pullAllImages(testFunc string, images []string) error {
for _, image := range images {
log.Printf("Pulling image: %s", image)
if err := pullImageWithRetry(testFunc, image); err != nil {
return fmt.Errorf("failed to pull %s: %w", image, err)
}
}
return nil
}
func removeAllImages(testFunc string, images []string) {
for _, image := range images {
log.Printf("Removing image: %s", image)
if err := removeImageForTest(testFunc, image); err != nil {
log.Printf("Warning: failed to remove %s: %v", image, err)
}
}
}
func pullImageWithRetry(testFunc string, image string) error {
var err error
for i := 0; i < maxPullRetries; i++ {
err = pullImageForTest(testFunc, image)
if err == nil {
return nil
}
fmt.Printf("Attempt %d/%d failed to pull %s: %v. Retrying in %v...\n", i+1, maxPullRetries, image, err, pullRetryDelay)
time.Sleep(pullRetryDelay)
}
return fmt.Errorf("failed to pull %s after %d attempts: %w", image, maxPullRetries, err)
}
func pullImageForTest(testFunc string, image string) error {
switch testFunc {
case testCrictl:
cmd := crictlName + " pull " + image
output, err := commonCmdExec(cmd)
if err != nil {
return fmt.Errorf("%s -- %v", output, err)
}
return nil
case testNerdctl:
return commonPull(nerdctlCmd, image)
case testDocker:
return commonPull(dockerName, image)
default:
return commonPull(ctrCmd, image)
}
}
func removeImageForTest(testFunc string, image string) error {
switch testFunc {
case testCrictl:
cmd := crictlName + " rmi " + image
output, err := commonCmdExec(cmd)
if err != nil {
return fmt.Errorf("%s -- %v", output, err)
}
return nil
case testNerdctl:
return commonRmImage(nerdctlCmd, image)
case testDocker:
return commonRmImage(dockerName, image)
default:
return commonRmImage(ctrCmd, image)
}
}
func pingUnikernel(ipAddress string) error {
pinger, err := probing.NewPinger(ipAddress)
if err != nil {
return fmt.Errorf("failed to create Pinger: %v", err)
}
pinger.Count = 3
pinger.Timeout = 5 * time.Second
err = pinger.Run()
if err != nil {
return fmt.Errorf("failed to ping %s: %v", ipAddress, err)
}
if pinger.PacketsRecv != pinger.PacketsSent {
return fmt.Errorf("packets received (%d) not equal to packets sent (%d)", pinger.PacketsRecv, pinger.PacketsSent)
}
if pinger.PacketsSent == 0 {
return fmt.Errorf("no packets were sent")
}
return nil
}
func compareNS(cntr string, defNS string, specPath string) error {
if specPath == "" {
if cntr == defNS {
return fmt.Errorf("Unikernel's namespace is the default")
}
} else {
nsLink, err := os.Readlink(specPath)
if err != nil {
return err
}
if cntr != nsLink {
return fmt.Errorf("Unikernel's namespace differs from spec's namespace")
}
}
return nil
}
func getProcNS(proc string) (map[string]string, error) {
procPath := filepath.Join("/proc", proc, "ns")
ns := make(map[string]string)
cgroupPath := filepath.Join(procPath, "cgroup")
var err error
ns["cgroup"], err = os.Readlink(cgroupPath)
if err != nil {
return nil, err
}
ipcPath := filepath.Join(procPath, "ipc")
ns["ipc"], err = os.Readlink(ipcPath)
if err != nil {
return nil, err
}
mntPath := filepath.Join(procPath, "mnt")
ns["mnt"], err = os.Readlink(mntPath)
if err != nil {
return nil, err
}
netPath := filepath.Join(procPath, "net")
ns["net"], err = os.Readlink(netPath)
if err != nil {
return nil, err
}
pidPath := filepath.Join(procPath, "pid")
ns["pid"], err = os.Readlink(pidPath)
if err != nil {
return nil, err
}
userPath := filepath.Join(procPath, "user")
ns["user"], err = os.Readlink(userPath)
if err != nil {
return nil, err
}
utsPath := filepath.Join(procPath, "uts")
ns["uts"], err = os.Readlink(utsPath)
if err != nil {
return nil, err
}
return ns, nil
}
func verifyNoStaleFiles(containerID string) error {
// Check /run/containerd/runc/default/containerID directory does not exist
dirPath := "/run/containerd/runc/default/" + containerID
_, err := os.Stat(dirPath)
if !os.IsNotExist(err) {
return fmt.Errorf("root directory %s still exists", dirPath)
}
// Check /run/containerd/runc/k8s.io/containerID directory does not exist
dirPath = "/run/containerd/runc/k8s.io/" + containerID
_, err = os.Stat(dirPath)
if !os.IsNotExist(err) {
return fmt.Errorf("root directory %s still exists", dirPath)
}
// Check /run/containerd/io.containerd.runtime.v2.task/default/containerID directory does not exist
dirPath = "/run/containerd/io.containerd.runtime.v2.task/default/" + containerID
_, err = os.Stat(dirPath)
if !os.IsNotExist(err) {
return fmt.Errorf("bundle directory %s still exists", dirPath)
}
// Check /run/containerd/io.containerd.runtime.v2.task/k8s.io/containerID directory does not exist
dirPath = "/run/containerd/io.containerd.runtime.v2.task/k8s.io/" + containerID
_, err = os.Stat(dirPath)
if !os.IsNotExist(err) {
return fmt.Errorf("bundle directory %s still exists", dirPath)
}
// Check /run/containerd/io.containerd.runtime.v2.task/urunc-test/containerID directory does not exist
dirPath = "/run/containerd/io.containerd.runtime.v2.task/" + testNamespace + "/" + containerID
_, err = os.Stat(dirPath)
if !os.IsNotExist(err) {
return fmt.Errorf("bundle directory %s still exists", dirPath)
}
return nil
}
func getAndCheckUGid(line string) (int, error) {
vals := strings.Split(line, ":")
ids := strings.Split(strings.TrimSpace(vals[1]), "\t")
if len(ids) != 4 {
return 0, fmt.Errorf("Invalid format of line. Expecting 4 values, got %d", len(ids))
}
if (ids[0] != ids[1]) || (ids[1] != ids[2]) || (ids[0] != ids[3]) {
return 0, fmt.Errorf("ids in line do not match")
}
return strconv.Atoi(ids[0])
}
func findLineInFile(filePath string, pattern string) (string, error) {
file, err := os.Open(filePath)
if err != nil {
return "", fmt.Errorf("Failed to open %s: %v", filePath, err)
}
scanner := bufio.NewScanner(file)
for scanner.Scan() {
line := scanner.Text()
if strings.Contains(line, pattern) {
return line, nil
}
}
return "", fmt.Errorf("Pattern %s was not found in any line of %s", pattern, filePath)
}