-
Notifications
You must be signed in to change notification settings - Fork 3
/
expect.go
190 lines (170 loc) · 5.96 KB
/
expect.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
/*
Copyright 2022 The Numaproj 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 fixtures
import (
"context"
"fmt"
"testing"
"time"
apierr "k8s.io/apimachinery/pkg/api/errors"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/client-go/kubernetes"
"k8s.io/client-go/rest"
dfv1 "github.com/numaproj/numaflow/pkg/apis/numaflow/v1alpha1"
flowpkg "github.com/numaproj/numaflow/pkg/client/clientset/versioned/typed/numaflow/v1alpha1"
)
type Expect struct {
t *testing.T
isbSvcClient flowpkg.InterStepBufferServiceInterface
pipelineClient flowpkg.PipelineInterface
vertexClient flowpkg.VertexInterface
isbSvc *dfv1.InterStepBufferService
pipeline *dfv1.Pipeline
restConfig *rest.Config
kubeClient kubernetes.Interface
}
func (t *Expect) SinkContains(sinkName string, targetStr string, opts ...SinkCheckOption) *Expect {
t.t.Helper()
ctx := context.Background()
contains := RedisContains(ctx, t.pipeline.Name, sinkName, targetStr, opts...)
if !contains {
t.t.Fatalf("Expected redis contains target string %s written by pipeline %s, sink %s.", targetStr, t.pipeline.Name, sinkName)
}
return t
}
func (t *Expect) SinkNotContains(sinkName string, targetStr string) *Expect {
t.t.Helper()
ctx := context.Background()
notContains := RedisNotContains(ctx, t.pipeline.Name, sinkName, targetStr)
if !notContains {
t.t.Fatalf("Not expected redis contains target string %s written by pipeline %s, sink %s.", targetStr, t.pipeline.Name, sinkName)
}
return t
}
func (t *Expect) ISBSvcDeleted(timeout time.Duration) *Expect {
t.t.Helper()
ctx := context.Background()
_, err := t.isbSvcClient.Get(ctx, t.isbSvc.Name, metav1.GetOptions{})
if err == nil || !apierr.IsNotFound(err) {
t.t.Fatalf("Expected ISB svc to be deleted: %v", err)
}
labelSelector := fmt.Sprintf("%s=isbsvc-controller,%s=%s", dfv1.KeyManagedBy, dfv1.KeyISBSvcName, ISBSvcName)
opts := metav1.ListOptions{LabelSelector: labelSelector}
timeoutCh := make(chan bool, 1)
go func() {
time.Sleep(timeout)
timeoutCh <- true
}()
for {
podList, err := t.kubeClient.CoreV1().Pods(Namespace).List(ctx, opts)
if err != nil && !apierr.IsNotFound(err) {
t.t.Fatalf("Failed to check if ISB svc pods have been deleted: %v", err)
}
if len(podList.Items) == 0 {
return t
}
select {
case <-timeoutCh:
t.t.Fatalf("Timeout after %v waiting for ISB svc to be deleted", timeout)
default:
}
}
}
func (t *Expect) VertexPodsRunning() *Expect {
t.t.Helper()
ctx := context.Background()
for _, v := range t.pipeline.Spec.Vertices {
_, err := t.vertexClient.Get(ctx, t.pipeline.Name+"-"+v.Name, metav1.GetOptions{})
if err != nil {
t.t.Fatalf("Expected vertex %q existing: %v", v.Name, err)
}
}
// check pods running
timeout := 2 * time.Minute
for _, v := range t.pipeline.Spec.Vertices {
if err := WaitForVertexPodRunning(t.kubeClient, t.vertexClient, Namespace, t.pipeline.Name, v.Name, timeout); err != nil {
t.t.Fatalf("Expected vertex %q pod running: %v", v.Name, err)
}
}
return t
}
func (t *Expect) VertexSizeScaledTo(v string, size int) *Expect {
t.t.Helper()
ctx := context.Background()
if _, err := t.vertexClient.Get(ctx, t.pipeline.Name+"-"+v, metav1.GetOptions{}); err != nil {
t.t.Fatalf("Expected vertex %s existing: %v", v, err)
}
// check expected number of pods running
timeout := 2 * time.Minute
if err := WaitForVertexPodScalingTo(t.kubeClient, t.vertexClient, Namespace, t.pipeline.Name, v, timeout, size); err != nil {
t.t.Fatalf("Expected %d pods running on vertex %s : %v", size, v, err)
}
return t
}
func (t *Expect) VertexPodLogContains(vertexName, regex string, opts ...PodLogCheckOption) *Expect {
t.t.Helper()
ctx := context.Background()
contains, err := VertexPodLogContains(ctx, t.kubeClient, Namespace, t.pipeline.Name, vertexName, regex, opts...)
if err != nil {
t.t.Fatalf("Failed to check vertex %q pod logs: %v", vertexName, err)
}
if !contains {
t.t.Fatalf("Expected vertex [%q] pod log to contain [%q] but didn't.", vertexName, regex)
}
t.t.Logf("Expected vertex %q pod contains %q", vertexName, regex)
return t
}
func (t *Expect) VertexPodLogNotContains(vertexName, regex string, opts ...PodLogCheckOption) *Expect {
t.t.Helper()
ctx := context.Background()
yes, err := VertexPodLogNotContains(ctx, t.kubeClient, Namespace, t.pipeline.Name, vertexName, regex, opts...)
if err != nil {
t.t.Fatalf("Failed to check vertex pod logs: %v", err)
}
if !yes {
t.t.Fatalf("Not expected vertex %q pod log contains %q", vertexName, regex)
}
return t
}
func (t *Expect) DaemonPodsRunning() *Expect {
t.t.Helper()
timeout := 2 * time.Minute
if err := WaitForDaemonPodsRunning(t.kubeClient, Namespace, t.pipeline.Name, timeout); err != nil {
t.t.Fatalf("Expected daemon pods of pipeline %q running: %v", t.pipeline.Name, err)
}
return t
}
func (t *Expect) DaemonPodLogContains(pipelineName, regex string, opts ...PodLogCheckOption) *Expect {
t.t.Helper()
ctx := context.Background()
contains, err := DaemonPodLogContains(ctx, t.kubeClient, Namespace, t.pipeline.Name, regex, opts...)
if err != nil {
t.t.Fatalf("Failed to check daemon pod logs: %v", err)
}
if !contains {
t.t.Fatalf("Expected daemon pod log contains %q", regex)
}
return t
}
func (t *Expect) When() *When {
return &When{
t: t.t,
isbSvcClient: t.isbSvcClient,
pipelineClient: t.pipelineClient,
vertexClient: t.vertexClient,
isbSvc: t.isbSvc,
pipeline: t.pipeline,
restConfig: t.restConfig,
kubeClient: t.kubeClient,
}
}