-
Notifications
You must be signed in to change notification settings - Fork 583
/
prober.go
144 lines (125 loc) · 3.53 KB
/
prober.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
/*
* Copyright 2020 The Knative 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 prober
import (
"time"
"go.uber.org/zap"
testlib "knative.dev/eventing/test/lib"
"knative.dev/eventing/test/lib/resources"
pkgupgrade "knative.dev/pkg/test/upgrade"
)
var (
// Interval is used to send events in specific rate.
Interval = 10 * time.Millisecond
)
// Runner will run continual verification with provided configuration.
type Runner interface {
// Setup will start a continual prober in background.
Setup(ctx pkgupgrade.Context)
// Verify will verify that all sent events propagated at least once.
Verify(ctx pkgupgrade.Context)
}
// NewRunner will create a runner compatible with NewContinualVerification
// func.
func NewRunner(config *Config, options ...testlib.SetupClientOption) Runner {
return &probeRunner{
prober: &prober{config: config},
options: options,
}
}
type probeRunner struct {
*prober
options []testlib.SetupClientOption
}
func (p *probeRunner) Setup(ctx pkgupgrade.Context) {
p.log = ctx.Log
p.client = testlib.Setup(ctx.T, false, p.options...)
p.deploy()
}
func (p *probeRunner) Verify(ctx pkgupgrade.Context) {
if p.client == nil {
ctx.T.Fatal("prober isn't initiated (client is nil)")
return
}
// use T from new test
p.client.T = ctx.T
t := ctx.T
p.Finish()
waitAfterFinished(p.prober)
errors, events := p.prober.Verify()
if len(errors) == 0 {
t.Logf("All %d events propagated well", events)
} else {
t.Logf("There were %d events propagated, but %d errors occurred. "+
"Listing them below.", events, len(errors))
}
p.ReportErrors(errors)
if !ctx.T.Failed() {
p.remove()
testlib.TearDown(p.client)
}
}
type prober struct {
log *zap.SugaredLogger
client *testlib.Client
config *Config
}
func (p *prober) servingClient() resources.ServingClient {
return resources.ServingClient{
Kube: p.client.Kube,
Dynamic: p.client.Dynamic,
}
}
func (p *prober) ReportErrors(errors []error) {
t := p.client.T
for _, err := range errors {
if p.config.FailOnErrors {
t.Error(err)
} else {
p.log.Warnf("Silenced FAIL: %v", err)
}
}
if len(errors) > 0 && !p.config.FailOnErrors {
t.Skipf(
"Found %d errors, but FailOnErrors is false. Skipping test.",
len(errors),
)
}
}
func (p *prober) deploy() {
p.log.Infof("Using namespace for probe testing: %v", p.client.Namespace)
p.deployConfiguration()
p.deployReceiver()
if p.config.Serving.Use {
p.deployForwarder()
}
p.client.WaitForAllTestResourcesReadyOrFail(p.config.Ctx)
p.deploySender()
p.ensureNoError(testlib.AwaitForAll(p.log))
// allow sender to send at least some events, 2 sec wait
time.Sleep(2 * time.Second)
p.log.Infof("Prober is now sending events with interval of %v in "+
"namespace: %v", p.config.Interval, p.client.Namespace)
}
func (p *prober) remove() {
if p.config.Serving.Use {
p.removeForwarder()
}
p.ensureNoError(p.client.Tracker.Clean(true))
}
func waitAfterFinished(p *prober) {
p.log.Infof("Waiting %v after sender finished...", p.config.FinishedSleep)
time.Sleep(p.config.FinishedSleep)
}