forked from lf-edge/eve
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtestparam.go
More file actions
291 lines (263 loc) · 8.62 KB
/
Copy pathtestparam.go
File metadata and controls
291 lines (263 loc) · 8.62 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
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
// Copyright (c) 2026 Zededa, Inc.
// SPDX-License-Identifier: Apache-2.0
package evetest
import (
"os"
"strconv"
"github.com/lf-edge/eve/evetest/constants"
)
// TestParameterDescription provides metadata for a test parameter, used when
// listing available tests (see "make list-tests").
type TestParameterDescription struct {
// Summary is a human-readable explanation of what the parameter controls.
Summary string
// Default is the default value expressed as a human-readable string
// (e.g. "kvm", "false", "42").
// IMPORTANT: this field must be set to a string literal so that
// list-tests can extract it via static AST analysis. Do not use
// computed expressions such as method calls or variable references.
Default string
// AllowedValues describes the set of accepted values in human-readable form.
// For enum parameters the recommended format is "<val1>|<val2>|<val3>".
// For numeric ranges you might write "<minval>-<maxval>".
// Leave empty if unrestricted (any value of the given type is allowed).
// Same constraint as Default: must be a string literal.
AllowedValues string
}
// TestParameterDefinition describes a parameter that a test or test-suite
// can accept. Parameters may have a default value, can be overridden by
// test-suites, or via environment variables.
type TestParameterDefinition struct {
// Key is the unique identifier of the parameter.
Key string
// DefaultValue is the value used if the parameter is not explicitly set.
DefaultValue interface{}
// Description provides human-readable metadata about the parameter.
Description TestParameterDescription
}
// TestParameterValue represents a concrete value assigned to a test parameter,
// typically by a test-suite when running parameterized tests.
type TestParameterValue struct {
// Key is the identifier of the parameter.
Key string
// Value is the concrete value assigned to the parameter.
Value interface{}
}
// FromStringer should be implemented by a test parameter if its type is not a basic
// Go type.
type FromStringer interface {
FromString(string) error
}
// DefineTestParameters defines the set of parameters available to the
// currently executing test or test suite.
func DefineTestParameters(params ...TestParameterDefinition) {
th := getTestHarness()
th.testM.Lock()
defer th.testM.Unlock()
th.test.paramDefs = params
}
// GetTestParameter returns the value of a test parameter with the given key,
// resolved in the following order:
//
// 1. Value set explicitly by the test-suite
// 2. Value provided via environment variable EVETEST_<KEY>
// 3. Default value from the parameter definition
//
// The type parameter T must match the parameter’s declared type, otherwise
// the test will fail.
func GetTestParameter[T any](key string) T {
th := getTestHarness()
th.testM.Lock()
defer th.testM.Unlock()
// Check that the given parameter is defined for the current test.
var definition TestParameterDefinition
for _, param := range th.test.paramDefs {
if param.Key == key {
definition = param
break
}
}
if th.suite != nil {
for _, param := range th.suite.paramDefs {
if param.Key == key {
definition = param
break
}
}
}
if definition.Key == "" {
th.t.Fatalf("Parameter %q is not defined for test %q",
key, th.test.name)
}
// Check if RunTestSuite has set some value for the parameter.
for _, param := range th.test.paramVals {
if param.Key == key {
val, ok := param.Value.(T)
if !ok {
th.t.Fatalf(
"parameter %q has type %T, expected %T",
key, param.Value, *new(T),
)
}
return val
}
}
// Check environment variables.
val := os.Getenv(constants.EnvPrefix + key)
if val != "" {
var zero T
switch any(zero).(type) {
case string:
return any(val).(T)
case bool:
parsed, err := strconv.ParseBool(val)
if err != nil {
th.t.Fatalf("invalid boolean for %s: %v", key, err)
}
return any(parsed).(T)
case int:
parsed, err := strconv.Atoi(val)
if err != nil {
th.t.Fatalf("invalid int for %s: %v", key, err)
}
return any(parsed).(T)
case int8:
parsed, err := strconv.ParseInt(val, 10, 8)
if err != nil {
th.t.Fatalf("invalid int8 for %s: %v", key, err)
}
return any(int8(parsed)).(T)
case int16:
parsed, err := strconv.ParseInt(val, 10, 16)
if err != nil {
th.t.Fatalf("invalid int16 for %s: %v", key, err)
}
return any(int16(parsed)).(T)
case int32:
parsed, err := strconv.ParseInt(val, 10, 32)
if err != nil {
th.t.Fatalf("invalid int32 for %s: %v", key, err)
}
return any(int32(parsed)).(T)
case int64:
parsed, err := strconv.ParseInt(val, 10, 64)
if err != nil {
th.t.Fatalf("invalid int64 for %s: %v", key, err)
}
return any(parsed).(T)
case uint:
parsed, err := strconv.ParseUint(val, 10, 0)
if err != nil {
th.t.Fatalf("invalid uint for %s: %v", key, err)
}
return any(uint(parsed)).(T)
case uint8:
parsed, err := strconv.ParseUint(val, 10, 8)
if err != nil {
th.t.Fatalf("invalid uint8 for %s: %v", key, err)
}
return any(uint8(parsed)).(T)
case uint16:
parsed, err := strconv.ParseUint(val, 10, 16)
if err != nil {
th.t.Fatalf("invalid uint16 for %s: %v", key, err)
}
return any(uint16(parsed)).(T)
case uint32:
parsed, err := strconv.ParseUint(val, 10, 32)
if err != nil {
th.t.Fatalf("invalid uint32 for %s: %v", key, err)
}
return any(uint32(parsed)).(T)
case uint64:
parsed, err := strconv.ParseUint(val, 10, 64)
if err != nil {
th.t.Fatalf("invalid uint64 for %s: %v", key, err)
}
return any(parsed).(T)
}
// Try FromStringer interface
ptr := new(T)
if fm, ok := any(ptr).(FromStringer); ok {
if err := fm.FromString(val); err != nil {
th.t.Fatalf("error parsing %s from env: %v", key, err)
}
return *ptr
}
th.t.Fatalf("unsupported parameter type for key %s", key)
}
// Return default value.
defVal, ok := definition.DefaultValue.(T)
if !ok {
th.t.Fatalf(
"default value for parameter %q has type %T, expected %T",
key, definition.DefaultValue, *new(T),
)
}
return defVal
}
// HypervisorParameterKey is the key used for the Hypervisor parameter.
const HypervisorParameterKey = "HYPERVISOR"
// HypervisorParameter is a predefined TestParameterDefinition for the Hypervisor parameter.
func HypervisorParameter() TestParameterDefinition {
return TestParameterDefinition{
Key: HypervisorParameterKey,
DefaultValue: HypervisorKVM,
Description: TestParameterDescription{
Summary: "Hypervisor to use for the test",
Default: "kvm",
AllowedValues: "kvm|xen|kubevirt",
},
}
}
// GetHypervisorParameterValue returns the value set for the Hypervisor parameter.
func GetHypervisorParameterValue() Hypervisor {
return GetTestParameter[Hypervisor](HypervisorParameterKey)
}
// SkipIfHypervisorKubevirt skips the current test if the resolved HYPERVISOR
// parameter is HypervisorKubevirt. Kubevirt is only supported by tests under
// `evetest/tests/cluster`; non-cluster tests should call this helper right
// after defining the HypervisorParameter to ensure they are not accidentally
// exercised on a Kubevirt-flavored EVE build.
func SkipIfHypervisorKubevirt() {
th := getTestHarness()
if GetHypervisorParameterValue() == HypervisorKubevirt {
th.t.Skipf("Kubevirt hypervisor is only supported by cluster tests " +
"(under evetest/tests/cluster); use kvm or xen")
}
}
// FilesystemParameterKey is the key used for the Filesystem parameter.
const FilesystemParameterKey = "FILESYSTEM"
// FilesystemParameter is a predefined TestParameterDefinition for the Filesystem parameter.
func FilesystemParameter() TestParameterDefinition {
return TestParameterDefinition{
Key: FilesystemParameterKey,
DefaultValue: FilesystemEXT4,
Description: TestParameterDescription{
Summary: "Filesystem for persistent storage on the EVE device",
Default: "ext4",
AllowedValues: "ext4|zfs",
},
}
}
// GetFilesystemParameterValue returns the value set for the Filesystem parameter.
func GetFilesystemParameterValue() Filesystem {
return GetTestParameter[Filesystem](FilesystemParameterKey)
}
// TPMParameterKey is the key used for the TPM parameter.
const TPMParameterKey = "TPM"
// TPMParameter is a predefined TestParameterDefinition for the TPM parameter.
func TPMParameter() TestParameterDefinition {
return TestParameterDefinition{
Key: TPMParameterKey,
DefaultValue: true,
Description: TestParameterDescription{
Summary: "Enable or disable TPM emulation",
Default: "true",
},
}
}
// GetTPMParameterValue returns the value set for the TPM parameter.
func GetTPMParameterValue() (useTPM bool) {
return GetTestParameter[bool](TPMParameterKey)
}