forked from bitpoke/mysql-operator
-
Notifications
You must be signed in to change notification settings - Fork 0
/
testutil.go
98 lines (80 loc) · 2.65 KB
/
testutil.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
/*
Copyright 2018 Pressinfra SRL
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 testutil
import (
"io"
"time"
g "github.com/onsi/gomega"
gs "github.com/onsi/gomega/gstruct"
gomegatypes "github.com/onsi/gomega/types"
// loggging
"github.com/go-logr/logr"
"github.com/go-logr/zapr"
"go.uber.org/zap"
"go.uber.org/zap/zapcore"
core "k8s.io/api/core/v1"
"sigs.k8s.io/controller-runtime/pkg/manager"
"sigs.k8s.io/controller-runtime/pkg/reconcile"
api "github.com/presslabs/mysql-operator/pkg/apis/mysql/v1alpha1"
)
const (
drainTimeout = 200 * time.Millisecond
)
// DrainChan drains the request chan time for drainTimeout
func DrainChan(requests <-chan reconcile.Request) {
for {
select {
case <-requests:
continue
case <-time.After(drainTimeout):
return
}
}
}
// BackupHaveCondition is a helper func that returns a matcher to check for an
// existing condition in condition list list
func BackupHaveCondition(condType api.BackupConditionType, status core.ConditionStatus) gomegatypes.GomegaMatcher {
return gs.PointTo(gs.MatchFields(gs.IgnoreExtras, gs.Fields{
"Status": gs.MatchFields(gs.IgnoreExtras, gs.Fields{
"Conditions": g.ContainElement(gs.MatchFields(gs.IgnoreExtras, gs.Fields{
"Type": g.Equal(condType),
"Status": g.Equal(status),
})),
}),
}))
}
// NewTestLogger returns a logger good for tests
func NewTestLogger(w io.Writer, options ...zap.Option) logr.Logger {
encoderCfg := zapcore.EncoderConfig{
MessageKey: "msg",
LevelKey: "level",
NameKey: "logger",
EncodeLevel: zapcore.LowercaseLevelEncoder,
EncodeTime: zapcore.ISO8601TimeEncoder,
EncodeDuration: zapcore.StringDurationEncoder,
}
sink := zapcore.AddSync(w)
core := zapcore.NewCore(zapcore.NewConsoleEncoder(encoderCfg), sink, zap.DebugLevel)
return zapr.NewLogger(zap.New(core).WithOptions(options...))
}
// IsManagerStarted returns a chnnel that receive a signal when controller is
// started
func IsManagerStarted(mgr manager.Manager) chan struct{} {
start := make(chan struct{})
test := func(stop <-chan struct{}) error {
start <- struct{}{}
return nil
}
g.Expect(mgr.Add(manager.RunnableFunc(test))).To(g.Succeed())
return start
}