/
failer.go
149 lines (130 loc) · 3.45 KB
/
failer.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
// 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 test
import (
"errors"
"fmt"
"os"
"runtime"
"sync"
"testing"
"istio.io/istio/pkg/log"
)
var (
_ Failer = &testing.T{}
_ Failer = &testing.B{}
_ Failer = &errorWrapper{}
)
// Failer is an interface to be provided to test functions of the form XXXOrFail. This is a
// substitute for testing.TB, which cannot be implemented outside of the testing
// package.
type Failer interface {
Fail()
FailNow()
Fatal(args ...any)
Fatalf(format string, args ...any)
Log(args ...any)
Logf(format string, args ...any)
TempDir() string
Helper()
Cleanup(func())
Skip(args ...any)
}
// Fuzzer abstracts *testing.F
type Fuzzer interface {
Fuzz(ff any)
Add(args ...any)
}
// errorWrapper is a Failer that can be used to just extract an `error`. This allows mixing
// functions that take in a Failer and those that take an error.
// The function must be called within a goroutine, or calls to Fatal will try to terminate the outer
// test context, which will cause the test to panic. The Wrap function handles this automatically
type errorWrapper struct {
mu sync.RWMutex
failed error
cleanup func()
}
// Wrap executes a function with a fake Failer, and returns an error if the test failed. This allows
// calling functions that take a Failer and using them with functions that expect an error, or
// allowing calling functions that would cause a test to immediately fail to instead return an error.
// Wrap handles Cleanup() and short-circuiting of Fatal() just like the real testing.T.
func Wrap(f func(t Failer)) error {
done := make(chan struct{})
w := &errorWrapper{}
go func() {
defer close(done)
f(w)
}()
<-done
return w.ToErrorCleanup()
}
// ToErrorCleanup returns any errors encountered and executes any cleanup actions
func (e *errorWrapper) ToErrorCleanup() error {
e.mu.RLock()
defer e.mu.RUnlock()
if e.cleanup != nil {
e.cleanup()
}
return e.failed
}
func (e *errorWrapper) Fail() {
e.Fatal("fail called")
}
func (e *errorWrapper) FailNow() {
e.Fatal("fail now called")
}
func (e *errorWrapper) Fatal(args ...any) {
e.mu.Lock()
defer e.mu.Unlock()
if e.failed == nil {
e.failed = errors.New(fmt.Sprint(args...))
}
runtime.Goexit()
}
func (e *errorWrapper) Fatalf(format string, args ...any) {
e.Fatal(fmt.Sprintf(format, args...))
}
func (e *errorWrapper) Helper() {
}
func (e *errorWrapper) Skip(args ...any) {
e.Fatal(args...)
}
func (e *errorWrapper) Cleanup(f func()) {
e.mu.Lock()
defer e.mu.Unlock()
oldCleanup := e.cleanup
e.cleanup = func() {
if oldCleanup != nil {
defer func() {
oldCleanup()
}()
}
f()
}
}
func (e *errorWrapper) Log(args ...any) {
log.Info(fmt.Sprint(args...))
}
func (e *errorWrapper) Logf(format string, args ...any) {
log.Infof(format, args...)
}
func (e *errorWrapper) TempDir() string {
tempDir, err := os.MkdirTemp("", "test")
if err == nil {
e.Cleanup(func() {
os.RemoveAll(tempDir)
})
}
return tempDir
}