/
analyzer.go
341 lines (289 loc) · 8.47 KB
/
analyzer.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
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
// Copyright Istio Authors
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package framework
import (
"fmt"
"os"
"strings"
"testing"
"github.com/hashicorp/go-multierror"
"gopkg.in/yaml.v3"
"istio.io/istio/pkg/log"
"istio.io/istio/pkg/test/env"
"istio.io/istio/pkg/test/framework/features"
"istio.io/istio/pkg/test/framework/label"
"istio.io/istio/pkg/test/framework/resource"
"istio.io/istio/pkg/test/scopes"
"istio.io/istio/pkg/util/sets"
)
type commonAnalyzer struct {
labels label.Set
minCusters, maxClusters int
minIstioVersion, skipMessage string
}
func newCommonAnalyzer() commonAnalyzer {
return commonAnalyzer{
labels: label.NewSet(),
skipMessage: "",
minIstioVersion: "",
minCusters: 1,
maxClusters: -1,
}
}
type suiteAnalyzer struct {
testID string
mRun mRunFn
osExit func(int)
commonAnalyzer
envFactoryCalls int
}
func newSuiteAnalyzer(testID string, fn mRunFn, osExit func(int)) Suite {
return &suiteAnalyzer{
testID: testID,
mRun: fn,
osExit: osExit,
commonAnalyzer: newCommonAnalyzer(),
}
}
func (s *suiteAnalyzer) EnvironmentFactory(_ resource.EnvironmentFactory) Suite {
if s.envFactoryCalls > 0 {
scopes.Framework.Warn("EnvironmentFactory overridden multiple times for Suite")
}
s.envFactoryCalls++
return s
}
func (s *suiteAnalyzer) Label(labels ...label.Instance) Suite {
s.labels = s.labels.Add(labels...)
return s
}
func (s *suiteAnalyzer) Skip(reason string) Suite {
s.skipMessage = reason
return s
}
func (s *suiteAnalyzer) SkipIf(reason string, _ resource.ShouldSkipFn) Suite {
s.skipMessage = reason
return s
}
func (s *suiteAnalyzer) RequireMinClusters(minClusters int) Suite {
if minClusters <= 0 {
minClusters = 1
}
s.minCusters = minClusters
return s
}
func (s *suiteAnalyzer) RequireMaxClusters(maxClusters int) Suite {
s.maxClusters = maxClusters
return s
}
func (s *suiteAnalyzer) RequireSingleCluster() Suite {
// nolint: staticcheck
return s.RequireMinClusters(1).RequireMaxClusters(1)
}
func (s *suiteAnalyzer) RequireMultiPrimary() Suite {
return s
}
func (s *suiteAnalyzer) SkipExternalControlPlaneTopology() Suite {
return s
}
func (s *suiteAnalyzer) RequireExternalControlPlaneTopology() Suite {
return s
}
func (s *suiteAnalyzer) RequireMinVersion(minorVersion uint) Suite {
return s
}
func (s *suiteAnalyzer) RequireMaxVersion(uint) Suite {
return s
}
func (s *suiteAnalyzer) Setup(resource.SetupFn) Suite {
// TODO track setup fns?
return s
}
func (s *suiteAnalyzer) SetupParallel(_ ...resource.SetupFn) Suite {
// TODO track setup fns?
return s
}
func (s *suiteAnalyzer) Run() {
s.osExit(s.run())
}
func (s *suiteAnalyzer) run() int {
initAnalysis(s.track())
defer finishAnalysis()
scopes.Framework.Infof("=== Begin: Analysis of %s ===", analysis.SuiteID)
err := s.validate()
ret := s.mRun(nil)
if err != nil {
scopes.Framework.Error(err)
if ret == 0 {
ret = 1
}
}
// tests will add their results to the suiteAnalysis during mRun
return ret
}
// track generates the final analysis for this suite. track should not be called if more
// modification will happen to this suiteAnalyzer.
func (s *suiteAnalyzer) track() *suiteAnalysis {
return &suiteAnalysis{
SuiteID: s.testID,
SkipReason: s.skipMessage,
Labels: s.labels.All(),
MultiCluster: s.maxClusters != 1,
MultiClusterOnly: s.minCusters > 1,
Tests: map[string]*testAnalysis{},
}
}
var analyzerAllowlist = loadAllowlist(env.IstioSrc + "/pkg/test/framework/analyzer-allowlist.yaml")
type allowlist struct {
Suites map[string][]string `yaml:"suites"`
}
func loadAllowlist(path string) *allowlist {
out := &allowlist{Suites: map[string][]string{}}
data, err := os.ReadFile(path)
if err != nil {
scopes.Framework.Warnf("failed reading suite analyzer allowlist from %s: %v", path, err)
return out
}
if err := yaml.Unmarshal(data, out); err != nil {
scopes.Framework.Warnf("failed unmarshalling suite analyzer allowlist from %s: %v", path, err)
return out
}
return out
}
// validate must be called after initAnalysis/track
func (s *suiteAnalyzer) validate() error {
var err *multierror.Error
for name, validator := range sutieValidators {
if sets.New(analyzerAllowlist.Suites[name]...).Contains(s.testID) {
continue
}
if vErr := validator(s); vErr != nil {
err = multierror.Append(err, vErr)
}
}
// add any other suite-level validation here
return err.ErrorOrNil()
}
var sutieValidators = map[string]func(s *suiteAnalyzer) error{
"supportMultipleClusters": func(s *suiteAnalyzer) error {
if s.maxClusters >= 0 && s.maxClusters < 3 {
return fmt.Errorf(
"%s supports a maximum of %d clusters; "+
"suites must be compatible with an arbitrary number of clusters",
s.testID, s.maxClusters,
)
}
return nil
},
}
func newTestAnalyzer(t *testing.T) Test {
return &testAnalyzer{
commonAnalyzer: newCommonAnalyzer(),
goTest: t,
featureLabels: map[features.Feature][]string{},
}
}
type testAnalyzer struct {
goTest *testing.T
commonAnalyzer
notImplemented bool
featureLabels map[features.Feature][]string
hasRun bool
}
func (t *testAnalyzer) Label(labels ...label.Instance) Test {
t.labels = t.labels.Add(labels...)
return t
}
func (t *testAnalyzer) Features(feats ...features.Feature) Test {
if err := addFeatureLabels(t.featureLabels, feats...); err != nil {
log.Error(err)
t.goTest.FailNow()
}
return t
}
func addFeatureLabels(featureLabels map[features.Feature][]string, feats ...features.Feature) error {
c, err := features.BuildChecker(env.IstioSrc + "/pkg/test/framework/features/features.yaml")
if err != nil {
return fmt.Errorf("unable to build feature checker: %v", err)
}
err = nil
for _, f := range feats {
check, scenario := c.Check(f)
if !check {
err = multierror.Append(err, fmt.Errorf("feature %s is not a leaf in /pkg/test/framework/features/features.yaml", f))
continue
}
// feats actually contains feature and scenario. split them here.
onlyFeature := features.Feature(strings.Replace(string(f), scenario, "", 1))
featureLabels[onlyFeature] = append(featureLabels[onlyFeature], scenario)
}
return err
}
func (t *testAnalyzer) NotImplementedYet(features ...features.Feature) Test {
t.notImplemented = true
t.Features(features...)
return t
}
func (t *testAnalyzer) RequiresMinClusters(minClusters int) Test {
t.minCusters = minClusters
return t
}
func (t *testAnalyzer) RequiresMaxClusters(maxClusters int) Test {
t.maxClusters = maxClusters
return t
}
func (t *testAnalyzer) RequireIstioVersion(version string) Test {
t.minIstioVersion = version
return t
}
func (t *testAnalyzer) RequiresSingleCluster() Test {
t.RequiresMinClusters(1)
t.RequiresMaxClusters(1)
return t
}
func (t *testAnalyzer) RequiresLocalControlPlane() Test {
return t
}
func (t *testAnalyzer) RequiresSingleNetwork() Test {
return t
}
func (t *testAnalyzer) Run(_ func(ctx TestContext)) {
defer t.track()
if t.hasRun {
t.goTest.Fatalf("multiple Run calls for %s", t.goTest.Name())
}
t.hasRun = true
// don't fail tests that would otherwise be skipped
if analysis.SkipReason != "" || t.skipMessage != "" {
return
}
// TODO: should we also block new cases?
if len(t.featureLabels) < 1 && !features.GlobalAllowlist.Contains(analysis.SuiteID, t.goTest.Name()) {
t.goTest.Fatalf("Detected new test %s in suite %s with no feature labels. "+
"See istio/istio/pkg/test/framework/features/README.md", t.goTest.Name(), analysis.SuiteID)
}
}
func (t *testAnalyzer) RunParallel(fn func(ctx TestContext)) {
t.Run(fn)
}
func (t *testAnalyzer) track() {
analysis.addTest(t.goTest.Name(), &testAnalysis{
SkipReason: t.skipMessage,
Labels: t.labels.All(), // TODO should this be merged with suite labels?
Features: t.featureLabels,
Invalid: t.goTest.Failed(),
MultiCluster: t.maxClusters != 1 && analysis.MultiCluster,
MultiClusterOnly: t.minCusters > 1 || analysis.MultiClusterOnly,
NotImplemented: t.notImplemented,
})
}