-
Notifications
You must be signed in to change notification settings - Fork 492
Expand file tree
/
Copy pathvalidate.go
More file actions
256 lines (226 loc) · 5.95 KB
/
Copy pathvalidate.go
File metadata and controls
256 lines (226 loc) · 5.95 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
package resource
import (
"fmt"
"io"
"reflect"
"strings"
"time"
"github.com/goss-org/goss/matchers"
)
const (
Value = iota
Values
Contains
)
const (
SUCCESS = iota
FAIL
SKIP
UNKNOWN
)
const (
OutcomePass = "pass"
OutcomeFail = "fail"
OutcomeSkip = "skip"
OutcomeUnknown = "unknown"
)
var humanOutcomes map[int]string = map[int]string{
UNKNOWN: OutcomeUnknown,
SUCCESS: OutcomePass,
FAIL: OutcomeFail,
SKIP: OutcomeSkip,
}
func HumanOutcomes() map[int]string {
return humanOutcomes
}
type ValidateError string
func (g ValidateError) Error() string { return string(g) }
func toValidateError(err error) *ValidateError {
if err == nil {
return nil
}
ve := ValidateError(err.Error())
return &ve
}
type TestResult struct {
Successful bool `json:"successful" yaml:"successful"`
Skipped bool `json:"skipped" yaml:"skipped"`
// Resource data
ResourceId string `json:"resource-id" yaml:"resource-id"`
ResourceType string `json:"resource-type" yaml:"resource-type"`
Property string `json:"property" yaml:"property"`
// User added info
Title string `json:"title" yaml:"title"`
Meta meta `json:"meta" yaml:"meta"`
// Result
Result int `json:"result" yaml:"result"`
Err *ValidateError `json:"err" yaml:"err"`
MatcherResult matchers.MatcherResult `json:"matcher-result" yaml:"matcher-result"`
StartTime time.Time `json:"start-time" yaml:"start-time"`
EndTime time.Time `json:"end-time" yaml:"end-time"`
Duration time.Duration `json:"duration" yaml:"duration"`
}
// ToOutcome converts the enum to a human-friendly string.
func (tr TestResult) ToOutcome() string {
switch tr.Result {
case SUCCESS:
return OutcomePass
case FAIL:
return OutcomeFail
case SKIP:
return OutcomeSkip
default:
return OutcomeUnknown
}
}
func (t TestResult) SortKey() string {
return fmt.Sprintf("%s:%s", t.ResourceType, t.ResourceId)
}
func skipResult(typeS string, id string, title string, meta meta, property string, startTime time.Time) TestResult {
endTime := time.Now()
return TestResult{
Result: SKIP,
Skipped: true,
ResourceType: typeS,
ResourceId: id,
Title: title,
Meta: meta,
Property: property,
StartTime: startTime,
EndTime: endTime,
Duration: endTime.Sub(startTime),
}
}
func ValidateValue(res ResourceRead, property string, expectedValue any, actual any, skip bool) TestResult {
if f, ok := actual.(func() (io.Reader, error)); ok {
if _, ok := expectedValue.([]any); !ok {
actual = func() (string, error) {
v, err := f()
if err != nil {
return "", err
}
i, err := matchers.ReaderToString{}.Transform(v)
if err != nil {
return "", err
}
return i.(string), nil
}
}
}
return ValidateGomegaValue(res, property, expectedValue, actual, skip)
}
func ValidateValueWithRetry(res ResourceRead, property string, expectedValue any, actualFunc func() (any, error), skip bool, retryCount int, retryDelay int) TestResult {
if skip {
// Return skip result immediately
skipFunc := func() (any, error) { return nil, nil }
return ValidateValue(res, property, expectedValue, skipFunc, skip)
}
maxRetries := retryCount + 1
if retryCount < 0 {
maxRetries = 1
}
delay := time.Duration(retryDelay) * time.Second
if delay <= 0 {
delay = 1 * time.Second // Default delay if not specified or invalid
}
var lastResult TestResult
for attempt := 0; attempt < maxRetries; attempt++ {
actual, err := actualFunc()
// Create a function that returns the current result
currentFunc := func() (any, error) { return actual, err }
result := ValidateValue(res, property, expectedValue, currentFunc, skip)
lastResult = result
if result.Result == SUCCESS {
return result
}
// If not the last attempt, wait before retrying
if attempt < maxRetries-1 {
time.Sleep(delay)
}
}
return lastResult
}
func ValidateGomegaValue(res ResourceRead, property string, expectedValue any, actual any, skip bool) TestResult {
id := res.ID()
title := res.GetTitle()
meta := res.GetMeta()
typ := reflect.TypeOf(res)
typeS := strings.Split(typ.String(), ".")[1]
startTime := time.Now()
if skip {
return skipResult(
typeS,
id,
title,
meta,
property,
startTime,
)
}
var foundValue any
var gomegaMatcher matchers.GossMatcher
var err error
switch f := actual.(type) {
case func() (bool, error):
foundValue, err = f()
case func() (string, error):
foundValue, err = f()
case func() (int, error):
foundValue, err = f()
case func() ([]string, error):
foundValue, err = f()
case func() (any, error):
foundValue, err = f()
case func() (io.Reader, error):
foundValue, err = f()
gomegaMatcher = matchers.HavePatterns(expectedValue)
default:
err = fmt.Errorf("Unknown method signature: %t", f)
}
var success bool
if gomegaMatcher == nil && err == nil {
gomegaMatcher, err = matcherToGomegaMatcher(expectedValue)
}
if err != nil {
endTime := time.Now()
return TestResult{
Result: FAIL,
ResourceType: typeS,
ResourceId: id,
Title: title,
Meta: meta,
Property: property,
Err: toValidateError(err),
StartTime: startTime,
EndTime: endTime,
Duration: endTime.Sub(startTime),
}
}
success, err = gomegaMatcher.Match(foundValue)
var matcherResult matchers.MatcherResult
result := SUCCESS
if success {
matcherResult = matchers.MatcherResult{
Actual: foundValue,
Message: "matches expectation",
Expected: expectedValue,
}
} else {
matcherResult = gomegaMatcher.FailureResult(foundValue)
result = FAIL
}
endTime := time.Now()
return TestResult{
Result: result,
ResourceType: typeS,
ResourceId: id,
Title: title,
Meta: meta,
Property: property,
MatcherResult: matcherResult,
Err: toValidateError(err),
StartTime: startTime,
EndTime: endTime,
Duration: endTime.Sub(startTime),
}
}