forked from kata-containers/kata-containers
-
Notifications
You must be signed in to change notification settings - Fork 1
/
constraints.go
382 lines (318 loc) · 9.51 KB
/
constraints.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
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
// Copyright (c) 2019 Intel Corporation
//
// SPDX-License-Identifier: Apache-2.0
//
package katatestutils
import (
"errors"
"fmt"
"os"
"strconv"
"strings"
"github.com/blang/semver"
)
const (
TestDisabledNeedRoot = "Test disabled as requires root user"
TestDisabledNeedNonRoot = "Test disabled as requires non-root user"
)
var errInvalidOpForConstraint = errors.New("invalid operator for constraint type")
// String converts the operator to a human-readable value.
func (o Operator) String() (s string) {
switch o {
case eqOperator:
s = "=="
case geOperator:
s = ">="
case gtOperator:
s = ">"
case leOperator:
s = "<="
case ltOperator:
s = "<"
case neOperator:
s = "!="
}
return s
}
// Result is the outcome of a Constraint test
type Result struct {
// Details of the constraint
// (human-readable result of testing for a Constraint).
Description string
// true if constraint was valid
Success bool
}
// GetFileContents return the file contents as a string.
func getFileContents(file string) (string, error) {
bytes, err := os.ReadFile(file)
if err != nil {
return "", err
}
return string(bytes), nil
}
func getKernelVersion() (string, error) {
const procVersion = "/proc/version"
contents, err := getFileContents(procVersion)
if err != nil {
return "", err
}
fields := strings.Fields(contents)
l := len(fields)
if l < 3 {
return "", fmt.Errorf("unexpected contents in %v", procVersion)
}
return fixKernelVersion(fields[2]), nil
}
// fixKernelVersion replaces underscores with dashes in a version string.
// This change is primarily for Fedora, RHEL and CentOS version numbers which
// can contain underscores. By replacing them with dashes, a valid semantic
// version string is created.
//
// Examples of actual kernel versions which can be made into valid semver
// format by calling this function:
//
// centos: 3.10.0-957.12.1.el7.x86_64
// fedora: 5.0.9-200.fc29.x86_64
//
// For some self compiled kernel, the kernel version will be with "+" as its suffix
// For example:
//
// 5.12.0-rc4+
//
// These kernel version can't be parsed by the current lib and lead to panic
// therefore the '+' should be removed.
func fixKernelVersion(version string) string {
version = strings.Replace(version, "_", "-", -1)
return strings.Replace(version, "+", "", -1)
}
// handleKernelVersion checks that the current kernel version is compatible with
// the constraint specified by the arguments.
func (tc *TestConstraint) handleKernelVersion(version string, op Operator) (result Result, err error) {
return handleVersionType("kernel", tc.KernelVersion, op, version)
}
// handleVersionType checks that the current and new versions are compatible with
// the constraint specified by the arguments. The versionName argument is a
// human-readable value to represent the currentVersion.
func handleVersionType(versionName, newVersion string, op Operator, currentVersion string) (result Result, err error) {
if versionName == "" {
return Result{}, fmt.Errorf("version name cannot be blank")
}
if newVersion == "" {
return Result{}, fmt.Errorf("new version cannot be blank")
}
if currentVersion == "" {
return Result{}, fmt.Errorf("current version cannot be blank")
}
newVersion = strings.ToLower(newVersion)
currentVersion = strings.ToLower(currentVersion)
newVersionElements := len(strings.Split(newVersion, "."))
currentVersionElements := len(strings.Split(currentVersion, "."))
var success bool
// Determine the type of version string based on the current version
switch currentVersionElements {
case 1:
// A simple integer version number.
if newVersionElements != 1 {
return Result{}, fmt.Errorf("%s version type (%q) is integer, but specified version (%s) is not",
versionName, currentVersion, newVersion)
}
success, err = evalIntVersion(newVersion, op, currentVersion)
case 2:
// A "floating point" version number in format "a.b".
if newVersionElements > 2 {
return Result{}, fmt.Errorf("%s version type (%q) is float, but specified version (%s) is not float or int",
versionName, currentVersion, newVersion)
}
success, err = evalFloatVersion(newVersion, op, currentVersion)
default:
// Assumed to be a semver format version string
// in format "a.b.c."
//
// Cannot check specified version here as semver is more
// complex - let the eval function detail with it.
success, err = evalSemverVersion(newVersion, op, currentVersion)
}
if err != nil {
return Result{}, err
}
descr := fmt.Sprintf("need %s version %s %q, got version %q",
versionName, op, currentVersion, newVersion)
result = Result{
Description: descr,
Success: success,
}
return result, nil
}
// evalIntVersion deals with integer version numbers
// (in format "a").
func evalIntVersion(newVersionStr string, op Operator, currentVersionStr string) (success bool, err error) {
newVersion, err := strconv.Atoi(newVersionStr)
if err != nil {
return false, err
}
currentVersion, err := strconv.Atoi(currentVersionStr)
if err != nil {
return false, err
}
switch op {
case eqOperator:
success = newVersion == currentVersion
case geOperator:
success = newVersion >= currentVersion
case gtOperator:
success = newVersion > currentVersion
case leOperator:
success = newVersion <= currentVersion
case ltOperator:
success = newVersion < currentVersion
case neOperator:
success = newVersion != currentVersion
default:
return false, errInvalidOpForConstraint
}
return success, err
}
// evalFloatVersion deals with "floating point" version numbers
// (in format "a.b").
//
// Note that (implicitly) the specified version number provided by the user
// may in fact be an integer value which will be converted into a float.
func evalFloatVersion(newVersionStr string, op Operator, currentVersionStr string) (success bool, err error) {
// If this many bits is insufficient to represent a version number, we
// have problems...!
const bitSize = 32
newVersion, err := strconv.ParseFloat(newVersionStr, bitSize)
if err != nil {
return false, err
}
currentVersion, err := strconv.ParseFloat(currentVersionStr, bitSize)
if err != nil {
return false, err
}
switch op {
case eqOperator:
success = newVersion == currentVersion
case geOperator:
success = newVersion >= currentVersion
case gtOperator:
success = newVersion > currentVersion
case leOperator:
success = newVersion <= currentVersion
case ltOperator:
success = newVersion < currentVersion
case neOperator:
success = newVersion != currentVersion
default:
return false, errInvalidOpForConstraint
}
return success, err
}
// evalSemverVersion deals with semantic versioning format version strings
// (in version "a.b.c").
//
// See: https://semver.org
func evalSemverVersion(newVersionStr string, op Operator, currentVersionStr string) (success bool, err error) {
newVersion, err := semver.Make(newVersionStr)
if err != nil {
return false, err
}
currentVersion, err := semver.Make(currentVersionStr)
if err != nil {
return false, err
}
switch op {
case eqOperator:
success = newVersion.EQ(currentVersion)
case geOperator:
success = newVersion.GE(currentVersion)
case gtOperator:
success = newVersion.GT(currentVersion)
case leOperator:
success = newVersion.LE(currentVersion)
case ltOperator:
success = newVersion.LT(currentVersion)
case neOperator:
success = newVersion.NE(currentVersion)
default:
return false, errInvalidOpForConstraint
}
return success, err
}
// handleUID checks that the current UID is compatible with the constraint
// specified by the arguments.
func (tc *TestConstraint) handleUID(uid int, op Operator) (result Result, err error) {
if uid < 0 {
return Result{}, fmt.Errorf("uid must be >= 0, got %d", uid)
}
var success bool
switch op {
case eqOperator:
success = tc.ActualEUID == uid
case neOperator:
success = tc.ActualEUID != uid
default:
return Result{}, errInvalidOpForConstraint
}
descr := fmt.Sprintf("need uid %s %d, got euid %d", op, uid, tc.ActualEUID)
result = Result{
Description: descr,
Success: success,
}
return result, nil
}
// handleResults is the common handler for all constraint types. It deals with
// errors found trying to check constraints, stores results and displays
// details of valid constraints.
func (tc *TestConstraint) handleResults(result Result, err error) {
if err != nil {
var extra string
if tc.Issue != "" {
extra = fmt.Sprintf(" (issue %s)", tc.Issue)
}
// Display the TestConstraint object as it's may provide
// helpful information for the caller.
panic(fmt.Sprintf("%+v: failed to check test constraints: error: %s%s\n",
tc, err, extra))
}
if !result.Success {
tc.Failed = append(tc.Failed, result)
} else {
tc.Passed = append(tc.Passed, result)
}
if tc.Debug {
var outcome string
if result.Success {
outcome = "valid"
} else {
outcome = "invalid"
}
fmt.Printf("Constraint %s: %s\n", outcome, result.Description)
}
}
// constraintValid handles the specified constraint, returning true if the
// constraint is valid, else false.
func (tc *TestConstraint) constraintValid(fn Constraint) bool {
c := Constraints{}
// Call the constraint function that sets the Constraints values
fn(&c)
if c.Issue != "" {
// Just record it
tc.Issue = c.Issue
}
if c.UIDSet {
result, err := tc.handleUID(c.UID, c.Operator)
tc.handleResults(result, err)
if !result.Success {
return false
}
}
if c.KernelVersion != "" {
result, err := tc.handleKernelVersion(c.KernelVersion, c.Operator)
tc.handleResults(result, err)
if !result.Success {
return false
}
}
// Constraint is valid
return true
}