-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathtest.go
More file actions
159 lines (135 loc) · 3.92 KB
/
test.go
File metadata and controls
159 lines (135 loc) · 3.92 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
package framework
import (
"fmt"
"strings"
"testing"
"github.com/stackrox/helmtest/internal/parser"
helmUtil "github.com/stackrox/helmtest/internal/rox-imported/helmutil"
"github.com/stackrox/helmtest/internal/rox-imported/pointers"
"github.com/pkg/errors"
"helm.sh/helm/v3/pkg/chartutil"
)
// applySetOptions takes the values specified in the `set` stanza and merges them into the otherwise defined values.
func (t *Test) applySetOptions() error {
for keyPathStr, val := range t.Set {
vals, err := helmUtil.ValuesForKVPair(keyPathStr, val)
if err != nil {
return errors.Wrap(err, "in 'set'")
}
t.Values = chartutil.CoalesceTables(vals, t.Values)
}
t.Set = nil // no longer used, but make sure this is idempotent.
return nil
}
// parseDefs parses the `Defs` section into a slice of `*gojq.FuncDef`s, and populates the `funcDefs` field.
func (t *Test) parseDefs() error {
defsStr := strings.TrimSpace(t.Defs)
if defsStr == "" {
return nil
}
if !strings.HasSuffix(defsStr, ";") {
return errors.New("definitions block must end with a semicolon")
}
parsedDefs, err := parser.ParseQuery(defsStr)
if err != nil {
return errors.Wrap(err, "parsing definitions")
}
t.funcDefs = parsedDefs.FuncDefs
return nil
}
// parsePredicates parses the `Expect` section into a slice of `*gojq.Query` objects, and populates the `predicates`
// field.
func (t *Test) parsePredicates() error {
expectStr := strings.TrimSpace(t.Expect)
if expectStr == "" {
return nil
}
predicates, err := parser.ParseExpectations(expectStr)
if err != nil {
return errors.Wrap(err, "parsing expectations")
}
t.predicates = predicates
return nil
}
// initialize initializes the test, parsing some string-based values into their semantic counterparts. It also
// recursively initializes the sub-tests. initialize assumes that a name as well as the parent pointer has been set, and
// that the parent is fully initialized.
func (t *Test) initialize() error {
if err := t.applySetOptions(); err != nil {
return err
}
if err := t.parseDefs(); err != nil {
return err
}
if t.ExpectError == nil {
if t.parent != nil {
t.ExpectError = t.parent.ExpectError
} else {
t.ExpectError = pointers.Bool(false)
}
}
if err := t.parsePredicates(); err != nil {
return errors.Wrap(err, "parsing predicates")
}
for i, subTest := range t.Tests {
subTest.parent = t
if subTest.Flavour == "" {
subTest.Flavour = t.Flavour
}
if subTest.Name == "" {
subTest.Name = fmt.Sprintf("#%d", i)
}
if subTest.Flavour != "" {
subTest.Name = fmt.Sprintf("%s(%s)", subTest.Name, subTest.Flavour)
}
if err := subTest.initialize(); err != nil {
return errors.Wrapf(err, "initializing %q", subTest.Name)
}
}
return nil
}
// Run runs a test against the given target.
func (t *Test) Run(testingT *testing.T, tgt *Target) {
testingT.Run(t.Name, func(testingT *testing.T) {
testingT.Parallel()
t.DoRun(testingT, tgt)
})
}
// DoRun runs a test directly, without an intermediate `testingT.Run` invocation.
func (t *Test) DoRun(testingT *testing.T, tgt *Target) {
if len(t.Tests) > 0 {
// non-leaf case
for _, subTest := range t.Tests {
subTest.Run(testingT, tgt)
}
return
}
if t.Condition.IfFlavour != "" && t.Condition.IfFlavour != t.Flavour {
// We skip the test.
return
}
// leaf case
runner := &runner{
t: testingT,
test: t,
tgt: tgt,
}
runner.Run()
}
// forEachScopeBottomUp runs the given doFn function for each test in the hierarchy, starting with the current
// test and ending at the root (suite).
func (t *Test) forEachScopeBottomUp(doFn func(t *Test)) {
doFn(t)
if t.parent == nil {
return
}
t.parent.forEachScopeBottomUp(doFn)
}
// forEachScopeTopDown runs the given doFn function for each test in the hierarchy, starting with the root (suite)
// and ending at the current test.
func (t *Test) forEachScopeTopDown(doFn func(t *Test)) {
if t.parent != nil {
t.parent.forEachScopeTopDown(doFn)
}
doFn(t)
}