forked from kata-containers/runtime
-
Notifications
You must be signed in to change notification settings - Fork 0
/
constraints_api.go
309 lines (259 loc) · 8.53 KB
/
constraints_api.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
// Copyright (c) 2019 Intel Corporation
//
// SPDX-License-Identifier: Apache-2.0
//
// This file contains the public API for the test constraints facility.
package katatestutils
import (
"os"
"strings"
)
// Operator represents an operator to apply to a test constraint value.
type Operator int
const (
eqOperator Operator = iota
geOperator Operator = iota
gtOperator Operator = iota
leOperator Operator = iota
ltOperator Operator = iota
neOperator Operator = iota
)
// Constraints encapsulates all information about a test constraint.
type Constraints struct {
Issue string
UID int
// Not ideal: set when UID needs to be checked. This allows
// a test for UID 0 to be detected.
UIDSet bool
// DistroName is the name of a distro in all lower-case letters.
DistroName string
// DistroVersion is the version of the particular distro in string
// format. It may contain periods and dashes.
DistroVersion string
// KernelVersion is the version of a particular kernel.
KernelVersion string
// Operator is the operator to apply to one of the constraints.
Operator Operator
}
// Constraint is a function that operates on a Constraints object to set
// particular values.
type Constraint func(c *Constraints)
// TestConstraint records details about test constraints.
type TestConstraint struct {
Debug bool
// Effective user ID of running test
ActualEUID int
DistroName string
DistroVersion string
KernelVersion string
// Used to record all passed and failed constraints in
// human-readable form.
Passed []Result
Failed []Result
// Optionally used to record an issue number that relates to the
// constraint.
Issue string
}
// NewKataTest creates a new TestConstraint object and is the main interface
// to the test constraints feature.
func NewTestConstraint(debug bool) TestConstraint {
distroName, distroVersion, err := getDistroDetails()
if err != nil {
panic(err)
}
kernelVersion, err := getKernelVersion()
if err != nil {
panic(err)
}
return TestConstraint{
Debug: debug,
ActualEUID: os.Geteuid(),
DistroName: distroName,
DistroVersion: distroVersion,
KernelVersion: kernelVersion,
}
}
// NotValid checks if the specified list of constraints are all valid,
// returning true if any _fail_.
//
// Notes:
//
// - Constraints are applied in the order specified.
// - A constraint type (user, distro) can only be specified once.
// - If the function fails to determine whether it can check the constraints,
// it will panic. Since this is facility is used for testing, this seems like
// the best approach as it unburdens the caller from checking for an error
// (which should never be ignored).
func (tc *TestConstraint) NotValid(constraints ...Constraint) bool {
if len(constraints) == 0 {
panic("need atleast one constraint")
}
// Reset in case of a previous call
tc.Passed = nil
tc.Failed = nil
tc.Issue = ""
for _, c := range constraints {
valid := tc.constraintValid(c)
if !valid {
return true
}
}
return false
}
// NeedUID skips the test unless running as a user with the specified user ID.
func NeedUID(uid int, op Operator) Constraint {
return func(c *Constraints) {
c.Operator = op
c.UID = uid
c.UIDSet = true
}
}
// NeedNonRoot skips the test unless running as root.
func NeedRoot() Constraint {
return NeedUID(0, eqOperator)
}
// NeedNonRoot skips the test if running as the root user.
func NeedNonRoot() Constraint {
return NeedUID(0, neOperator)
}
// NeedDistroWithOp skips the test unless the distro constraint specified by
// the arguments is true.
func NeedDistroWithOp(distro string, op Operator) Constraint {
return func(c *Constraints) {
c.DistroName = strings.ToLower(distro)
c.Operator = op
}
}
// NeedDistroEquals will skip the test unless running on the specified distro.
func NeedDistroEquals(distro string) Constraint {
return NeedDistroWithOp(distro, eqOperator)
}
// NeedDistroNotEquals will skip the test unless run a distro that does not
// match the specified name.
func NeedDistroNotEquals(distro string) Constraint {
return NeedDistroWithOp(distro, neOperator)
}
// NeedDistro will skip the test unless running on the specified distro.
func NeedDistro(distro string) Constraint {
return NeedDistroEquals(distro)
}
// NeedDistroVersionWithOp skips the test unless the distro version constraint
// specified by the arguments is true.
//
// Note: distro versions vary in format.
func NeedDistroVersionWithOp(version string, op Operator) Constraint {
return func(c *Constraints) {
c.DistroVersion = version
c.Operator = op
}
}
// NeedDistroVersionEquals will skip the test unless the distro version is the
// same as the specified version.
//
// Note: distro versions vary in format.
func NeedDistroVersionEquals(version string) Constraint {
return NeedDistroVersionWithOp(version, eqOperator)
}
// NeedDistroVersionNotEquals will skip the test unless the distro version is
// different to the specified version.
//
// Note: distro versions vary in format.
func NeedDistroVersionNotEquals(version string) Constraint {
return NeedDistroVersionWithOp(version, neOperator)
}
// NeedDistroVersionLE will skip the test unless the distro version is older
// than or the same as the specified version.
//
// Note: distro versions vary in format.
func NeedDistroVersionLE(version string) Constraint {
return NeedDistroVersionWithOp(version, leOperator)
}
// NeedDistroVersionLT will skip the test unless the distro version is older
// than the specified version.
//
// Note: distro versions vary in format.
func NeedDistroVersionLT(version string) Constraint {
return NeedDistroVersionWithOp(version, ltOperator)
}
// NeedDistroVersionGE will skip the test unless the distro version is newer
// than or the same as the specified version.
//
// Note: distro versions vary in format.
func NeedDistroVersionGE(version string) Constraint {
return NeedDistroVersionWithOp(version, geOperator)
}
// NeedDistroVersionGT will skip the test unless the distro version is newer
// than the specified version.
//
// Note: distro versions vary in format.
func NeedDistroVersionGT(version string) Constraint {
return NeedDistroVersionWithOp(version, gtOperator)
}
// NeedDistroVersion will skip the test unless running on the specified
// (exact) version of some distro.
//
// Note: distro versions vary in format.
func NeedDistroVersion(version string) Constraint {
return NeedDistroVersionEquals(version)
}
// NeedKernelVersionWithOp skips the test unless the distro version constraint
// specified by the arguments is true.
func NeedKernelVersionWithOp(version string, op Operator) Constraint {
return func(c *Constraints) {
c.KernelVersion = version
c.Operator = op
}
}
// NeedKernelVersionLT will skip the test unless the distro version is older
// than the specified version.
func NeedKernelVersionEquals(version string) Constraint {
return NeedKernelVersionWithOp(version, eqOperator)
}
// NeedKernelVersionNotEquals will skip the test unless the distro version is
// different to the specified version.
func NeedKernelVersionNotEquals(version string) Constraint {
return NeedKernelVersionWithOp(version, neOperator)
}
// NeedKernelVersionLT will skip the test unless the distro version is older
// than the specified version.
//
// Note: distro versions vary in format.
func NeedKernelVersionLT(version string) Constraint {
return NeedKernelVersionWithOp(version, ltOperator)
}
// NeedKernelVersionLE will skip the test unless the distro version is older
// than or the same as the specified version.
//
// Note: distro versions vary in format.
func NeedKernelVersionLE(version string) Constraint {
return NeedKernelVersionWithOp(version, leOperator)
}
// NeedKernelVersionGT will skip the test unless the distro version is newer
// than the specified version.
//
// Note: distro versions vary in format.
func NeedKernelVersionGT(version string) Constraint {
return NeedKernelVersionWithOp(version, gtOperator)
}
// NeedKernelVersionGE will skip the test unless the distro version is newer
// than or the same as the specified version.
//
// Note: distro versions vary in format.
func NeedKernelVersionGE(version string) Constraint {
return NeedKernelVersionWithOp(version, geOperator)
}
// NeedKernelVersion will skip the test unless running on the specified
// (exact) version of some distro.
//
// Note: distro versions vary in format.
func NeedKernelVersion(version string) Constraint {
return NeedKernelVersionEquals(version)
}
// WithIssue allows the specification of an issue URL.
//
// Note that the issue is not checked for validity.
func WithIssue(issue string) Constraint {
return func(c *Constraints) {
c.Issue = issue
}
}