forked from kedacore/http-add-on
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathforward_wait_func_test.go
131 lines (113 loc) · 3.36 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
130
131
package main
import (
"context"
"testing"
"time"
"github.com/go-logr/logr"
"github.com/stretchr/testify/require"
"golang.org/x/sync/errgroup"
v1 "k8s.io/api/core/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/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 active endpoint 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 endpointsName = "TestForwardingHandler"
endpoints := *newEndpoint(ns, endpointsName)
cache := k8s.NewFakeEndpointsCache()
cache.Set(endpoints)
r.NoError(cache.SetSubsets(ns, endpointsName, 1))
ctx, done := context.WithTimeout(ctx, waitFuncWait)
defer done()
group, ctx := errgroup.WithContext(ctx)
waitFunc := newWorkloadReplicasForwardWaitFunc(
logr.Discard(),
cache,
)
group.Go(func() error {
_, err := waitFunc(ctx, ns, endpointsName)
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 active endpoints, 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 endpointsName = "TestForwardWaitFuncNoReplicas"
endpoints := *newEndpoint(ns, endpointsName)
cache := k8s.NewFakeEndpointsCache()
cache.Set(endpoints)
ctx, done := context.WithTimeout(ctx, waitFuncWait)
defer done()
waitFunc := newWorkloadReplicasForwardWaitFunc(
logr.Discard(),
cache,
)
_, err := waitFunc(ctx, ns, endpointsName)
r.Error(err)
}
func TestWaitFuncWaitsUntilReplicas(t *testing.T) {
ctx := context.Background()
r := require.New(t)
totalWaitDur := 500 * time.Millisecond
const ns = "testNS"
const endpointsName = "TestForwardingHandlerHolds"
endpoints := *newEndpoint(ns, endpointsName)
cache := k8s.NewFakeEndpointsCache()
cache.Set(endpoints)
// create a watcher first so that the goroutine
// can later fetch it and send a message on it
_, err := cache.Watch(ns, endpointsName)
r.NoError(err)
ctx, done := context.WithTimeout(ctx, totalWaitDur)
waitFunc := newWorkloadReplicasForwardWaitFunc(
logr.Discard(),
cache,
)
// this channel will be closed immediately after the active endpoints were increased
activeEndpointsIncreasedCh := make(chan struct{})
go func() {
time.Sleep(totalWaitDur / 2)
watcher := cache.GetWatcher(ns, endpointsName)
r.NotNil(watcher, "watcher was not found")
modifiedEndpoints := endpoints.DeepCopy()
modifiedEndpoints.Subsets = []v1.EndpointSubset{
{
Addresses: []v1.EndpointAddress{
{IP: "1.2.3.4"},
},
},
}
watcher.Action(watch.Modified, modifiedEndpoints)
close(activeEndpointsIncreasedCh)
}()
_, err = waitFunc(ctx, ns, endpointsName)
r.NoError(err)
done()
}
// newEndpoint creates a new endpoints object
// with the given name and the given image. This does not actually create
// the endpoints in the cluster, it just creates the endpoints object
// in memory
func newEndpoint(
namespace,
name string,
) *v1.Endpoints {
endpoints := &v1.Endpoints{
ObjectMeta: metav1.ObjectMeta{
Name: name,
Namespace: namespace,
},
}
return endpoints
}