-
Notifications
You must be signed in to change notification settings - Fork 29
/
runner.go
111 lines (89 loc) · 1.94 KB
/
runner.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
package testrunner
import (
"bytes"
"encoding/json"
"fmt"
"os"
"path/filepath"
"strings"
"testing"
"sigs.k8s.io/yaml"
)
type TestRunner struct {
T *testing.T
Path string
}
func NewTestRunner(t *testing.T, path string) *TestRunner {
return &TestRunner{
T: t,
Path: path,
}
}
func (tr *TestRunner) Run() {
var t = tr.T
testCases, err := tr.loadFromPath(tr.Path)
if err != nil {
t.Fatalf(err.Error())
}
hasFocus := false
for _, testCase := range testCases {
if testCase.Focus {
if hasFocus {
t.Fatal("multiple focus test cases")
}
hasFocus = true
break
}
}
for _, testCase := range testCases {
tc := testCase
t.Run(tc.FullName(), func(t *testing.T) {
t.Parallel()
if hasFocus && !tc.Focus {
t.SkipNow()
return
}
tc.Run(t)
})
}
}
func (tr *TestRunner) loadFromPath(path string) ([]TestCase, error) {
var t = tr.T
var testCases []TestCase
err := filepath.Walk(path, func(path string, info os.FileInfo, err error) error {
if err != nil {
return err
}
if strings.HasSuffix(path, "test.yaml") == false {
return nil
}
data, err := os.ReadFile(path)
if err != nil {
t.Errorf("failed to read file %s: %v", path, err)
}
documents := bytes.SplitN(data, []byte("\n---"), -1)
for i, testcaseRaw := range documents {
var testCase TestCase
jsonData, err := yaml.YAMLToJSON(testcaseRaw)
if err != nil {
t.Errorf("failed to convert yaml to json at %s#%d%v", path, i+1, err)
continue
}
var invalidSchemaMessage = fmt.Sprintf("invalid schema at %s#%d", path, i+1)
err = TestCaseSchema.Validator().ValidateWithMessage(bytes.NewReader(jsonData), invalidSchemaMessage)
if err != nil {
t.Errorf(err.Error())
continue
}
err = json.Unmarshal(jsonData, &testCase)
if err != nil {
t.Errorf(err.Error())
continue
}
testCase.Path = path
testCases = append(testCases, testCase)
}
return nil
})
return testCases, err
}