-
Notifications
You must be signed in to change notification settings - Fork 103
/
Copy pathcdi_linux_test.go
251 lines (242 loc) · 6.38 KB
/
cdi_linux_test.go
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
// Copyright (c) 2022, Sylabs Inc. All rights reserved.
// This software is licensed under a 3-clause BSD license. Please consult the
// LICENSE.md file distributed with the sources of this project regarding your
// rights to use or distribute this software.
// Package oci implements a Launcher that will configure and launch a container
// with an OCI runtime. It also provides implementations of OCI state
// transitions that can be called directly, Create/Start/Kill etc.
package oci
import (
"path/filepath"
"reflect"
"sort"
"testing"
"github.com/opencontainers/runtime-spec/specs-go"
"github.com/sylabs/singularity/v4/pkg/util/slice"
"tags.cncf.io/container-device-interface/pkg/cdi"
)
var specDirs = []string{filepath.Join("..", "..", "..", "..", "..", "test", "cdi")}
type mountsList []specs.Mount
func (a mountsList) Len() int { return len(a) }
func (a mountsList) Swap(i, j int) { a[i], a[j] = a[j], a[i] }
func (a mountsList) Less(i, j int) bool { return a[i].Destination < a[j].Destination }
func Test_addCDIDevice(t *testing.T) {
var wantUID uint32 = 1000
var wantGID uint32 = 1000
tests := []struct {
name string
devices []string
wantDevices []specs.LinuxDevice
wantMounts mountsList
wantErr bool
wantEnv []string
}{
{
name: "ValidOneDeviceKmsg",
devices: []string{
"singularityCEtesting.sylabs.io/device=kmsgDevice",
},
wantDevices: []specs.LinuxDevice{
{
Path: "/dev/kmsg",
Type: "c",
Major: 1,
Minor: 11,
FileMode: nil,
UID: &wantUID,
GID: &wantGID,
},
},
wantMounts: mountsList{
{
Source: "/tmp",
Destination: "/tmpmountforkmsg",
Type: "",
Options: []string{"rw"},
},
},
wantEnv: []string{
"FOO=VALID_SPEC",
"BAR=BARVALUE1",
},
},
{
name: "ValidTmpDevices",
devices: []string{
"singularityCEtesting.sylabs.io/device=tmpmountDevice17",
"singularityCEtesting.sylabs.io/device=tmpmountDevice1",
},
wantDevices: []specs.LinuxDevice{},
wantMounts: mountsList{
{
Source: "/tmp",
Destination: "/tmpmount1",
Type: "",
Options: []string{"ro"},
},
{
Source: "/tmp",
Destination: "/tmpmount3",
Type: "",
Options: []string{"rbind", "nosuid", "nodev"},
},
{
Source: "/tmp",
Destination: "/tmpmount13",
Type: "",
Options: []string{"rw"},
},
{
Source: "/tmp",
Destination: "/tmpmount17",
Type: "",
Options: []string{"r"},
},
},
wantEnv: []string{
"ABCD=QWERTY",
"EFGH=ASDFGH",
"IJKL=ZXCVBN",
"FOO=VALID_SPEC",
"BAR=BARVALUE1",
},
},
{
name: "ValidTmpDevicesFromOneJSON",
devices: []string{
"singularityCEtesting.sylabs.io/device=tmpmountDevice1",
},
wantDevices: []specs.LinuxDevice{},
wantMounts: mountsList{
{
Source: "/tmp",
Destination: "/tmpmount1",
Type: "",
Options: []string{"ro"},
},
{
Source: "/tmp",
Destination: "/tmpmount3",
Type: "",
Options: []string{"rbind", "nosuid", "nodev"},
},
{
Source: "/tmp",
Destination: "/tmpmount13",
Type: "",
Options: []string{"rw"},
},
},
wantEnv: []string{
"ABCD=QWERTY",
"EFGH=ASDFGH",
"IJKL=ZXCVBN",
},
},
{
name: "ValidMixedDevices",
devices: []string{
"singularityCEtesting.sylabs.io/device=tmpmountDevice17",
"singularityCEtesting.sylabs.io/device=kmsgDevice",
"singularityCEtesting.sylabs.io/device=tmpmountDevice1",
},
wantDevices: []specs.LinuxDevice{
{
Path: "/dev/kmsg",
Type: "c",
Major: 1,
Minor: 11,
FileMode: nil,
UID: &wantUID,
GID: &wantGID,
},
},
wantMounts: mountsList{
{
Source: "/tmp",
Destination: "/tmpmount1",
Type: "",
Options: []string{"ro"},
},
{
Source: "/tmp",
Destination: "/tmpmount3",
Type: "",
Options: []string{"rbind", "nosuid", "nodev"},
},
{
Source: "/tmp",
Destination: "/tmpmount13",
Type: "",
Options: []string{"rw"},
},
{
Source: "/tmp",
Destination: "/tmpmount17",
Type: "",
Options: []string{"r"},
},
{
Source: "/tmp",
Destination: "/tmpmountforkmsg",
Type: "",
Options: []string{"rw"},
},
},
wantEnv: []string{
"ABCD=QWERTY",
"EFGH=ASDFGH",
"IJKL=ZXCVBN",
"FOO=VALID_SPEC",
"BAR=BARVALUE1",
},
},
{
name: "InvalidNameOneDevice",
devices: []string{
"singularityCEtesting.sylabs.io/device=noSuchDevice",
},
wantErr: true,
},
{
name: "InvalidNameSeveralDevices",
devices: []string{
"singularityCEtesting.sylabs.io/device=noSuchDevice",
"singularityCEtesting.sylabs.io/device=noSuchDeviceEither",
},
wantErr: true,
},
{
name: "InvalidNameAmongValids",
devices: []string{
"singularityCEtesting.sylabs.io/device=tmpmountDevice17",
"singularityCEtesting.sylabs.io/device=noSuchDevice",
"singularityCEtesting.sylabs.io/device=tmpmountDevice1",
"singularityCEtesting.sylabs.io/device=kmsgDevice",
},
wantErr: true,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
spec := minimalSpec()
err := addCDIDevices(&spec, tt.devices, cdi.WithSpecDirs(specDirs...))
if (err != nil) != tt.wantErr {
t.Errorf("addCDIDevices() mismatched error values; expected %v, got %v.", tt.wantErr, err)
}
// We need this if-statement because the comparison below is done with reflection, and so a nil array and a non-nil but zero-length array will be considered different (which is not what we want here)
if (len(tt.wantMounts) > 0) || (len(spec.Mounts) > 0) {
gotMounts := mountsList(spec.Mounts)
sort.Sort(gotMounts)
sort.Sort(tt.wantMounts)
if !reflect.DeepEqual(gotMounts, tt.wantMounts) {
t.Errorf("addCDIDevices() mismatched mounts; expected %v, got %v.", tt.wantMounts, spec.Mounts)
}
}
envMissing := slice.Subtract(tt.wantEnv, spec.Process.Env)
if len(envMissing) > 0 {
t.Errorf("addCDIDevices() mismatched environment variables; expected, but did not find, the following environment variables: %v", envMissing)
}
})
}
}