forked from kedacore/http-add-on
-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathforward_wait_func_test.go
129 lines (114 loc) · 3.1 KB
/
forward_wait_func_test.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
package main
import (
"context"
"testing"
"time"
"github.com/go-logr/logr"
"github.com/stretchr/testify/require"
"golang.org/x/sync/errgroup"
corev1 "k8s.io/api/core/v1"
"k8s.io/apimachinery/pkg/watch"
"github.com/kedacore/http-add-on/pkg/k8s"
)
// Test to make sure the wait function returns a nil error if there is immediately
// one replica on the target deployment
func TestForwardWaitFuncOneReplica(t *testing.T) {
ctx := context.Background()
const waitFuncWait = 1 * time.Second
r := require.New(t)
const ns = "testNS"
const deployName = "TestForwardingHandlerDeploy"
cache := k8s.NewFakeDeploymentCache()
cache.AddDeployment(*newDeployment(
ns,
deployName,
"myimage",
[]int32{123},
nil,
map[string]string{},
corev1.PullAlways,
))
ctx, done := context.WithTimeout(ctx, waitFuncWait)
defer done()
group, ctx := errgroup.WithContext(ctx)
waitFunc := newDeployReplicasForwardWaitFunc(
logr.Discard(),
cache,
)
group.Go(func() error {
_, err := waitFunc(ctx, ns, deployName)
return err
})
r.NoError(group.Wait(), "wait function failed, but it shouldn't have")
}
// Test to make sure the wait function returns an error if there are no replicas, and that doesn't change
// within a timeout
func TestForwardWaitFuncNoReplicas(t *testing.T) {
ctx := context.Background()
const waitFuncWait = 1 * time.Second
r := require.New(t)
const ns = "testNS"
const deployName = "TestForwardingHandlerHoldsDeployment"
deployment := newDeployment(
ns,
deployName,
"myimage",
[]int32{123},
nil,
map[string]string{},
corev1.PullAlways,
)
deployment.Status.ReadyReplicas = 0
cache := k8s.NewFakeDeploymentCache()
cache.AddDeployment(*deployment)
ctx, done := context.WithTimeout(ctx, waitFuncWait)
defer done()
waitFunc := newDeployReplicasForwardWaitFunc(
logr.Discard(),
cache,
)
_, err := waitFunc(ctx, ns, deployName)
r.Error(err)
}
func TestWaitFuncWaitsUntilReplicas(t *testing.T) {
ctx := context.Background()
r := require.New(t)
totalWaitDur := 500 * time.Millisecond
const ns = "testNS"
const deployName = "TestForwardingHandlerHoldsDeployment"
deployment := newDeployment(
ns,
deployName,
"myimage",
[]int32{123},
nil,
map[string]string{},
corev1.PullAlways,
)
deployment.Spec.Replicas = k8s.Int32P(0)
cache := k8s.NewFakeDeploymentCache()
cache.AddDeployment(*deployment)
// create a watcher first so that the goroutine
// can later fetch it and send a message on it
_, err := cache.Watch(ns, deployName)
r.NoError(err)
ctx, done := context.WithTimeout(ctx, totalWaitDur)
waitFunc := newDeployReplicasForwardWaitFunc(
logr.Discard(),
cache,
)
// this channel will be closed immediately after the replicas were increased
replicasIncreasedCh := make(chan struct{})
go func() {
time.Sleep(totalWaitDur / 2)
watcher := cache.GetWatcher(ns, deployName)
r.NotNil(watcher, "watcher was not found")
modifiedDeployment := deployment.DeepCopy()
modifiedDeployment.Spec.Replicas = k8s.Int32P(1)
watcher.Action(watch.Modified, modifiedDeployment)
close(replicasIncreasedCh)
}()
_, err = waitFunc(ctx, ns, deployName)
r.NoError(err)
done()
}