forked from linkerd/linkerd2
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathendpoint_profile_translator_test.go
129 lines (114 loc) · 3.24 KB
/
endpoint_profile_translator_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 destination
import (
"testing"
"time"
pb "github.com/linkerd/linkerd2-proxy-api/go/destination"
"github.com/linkerd/linkerd2/controller/api/destination/watcher"
consts "github.com/linkerd/linkerd2/pkg/k8s"
logging "github.com/sirupsen/logrus"
corev1 "k8s.io/api/core/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
)
func TestEndpointProfileTranslator(t *testing.T) {
// logging.SetLevel(logging.TraceLevel)
// defer logging.SetLevel(logging.PanicLevel)
addr := &watcher.Address{
IP: "10.10.11.11",
Port: 8080,
}
podAddr := &watcher.Address{
IP: "10.10.11.11",
Port: 8080,
Pod: &corev1.Pod{
ObjectMeta: metav1.ObjectMeta{
Annotations: map[string]string{
consts.ProxyOpaquePortsAnnotation: "8080",
},
},
},
}
t.Run("Sends update", func(t *testing.T) {
mockGetProfileServer := &mockDestinationGetProfileServer{
profilesReceived: make(chan *pb.DestinationProfile), // UNBUFFERED
}
log := logging.WithField("test", t.Name())
translator := newEndpointProfileTranslator(
true, "cluster", "identity", make(map[uint32]struct{}),
mockGetProfileServer,
nil,
log,
)
translator.Start()
defer translator.Stop()
if err := translator.Update(addr); err != nil {
t.Fatal("Expected update")
}
select {
case p := <-mockGetProfileServer.profilesReceived:
log.Debugf("Received update: %v", p)
case <-time.After(1 * time.Second):
t.Fatal("No update received")
}
if err := translator.Update(addr); err != nil {
t.Fatal("Unexpected update")
}
select {
case p := <-mockGetProfileServer.profilesReceived:
t.Fatalf("Duplicate update sent: %v", p)
case <-time.After(1 * time.Second):
}
if err := translator.Update(podAddr); err != nil {
t.Fatal("Expected update")
}
select {
case p := <-mockGetProfileServer.profilesReceived:
log.Debugf("Received update: %v", p)
case <-time.After(1 * time.Second):
}
})
t.Run("Handles overflow", func(t *testing.T) {
mockGetProfileServer := &mockDestinationGetProfileServer{
profilesReceived: make(chan *pb.DestinationProfile, 1),
}
log := logging.WithField("test", t.Name())
endStream := make(chan struct{})
translator := newEndpointProfileTranslator(
true, "cluster", "identity", make(map[uint32]struct{}),
mockGetProfileServer,
endStream,
log,
)
translator.Start()
defer translator.Stop()
for i := 0; i < updateQueueCapacity/2; i++ {
if err := translator.Update(podAddr); err != nil {
t.Fatal("Expected update")
}
select {
case <-endStream:
t.Fatal("Stream ended prematurely")
default:
}
if err := translator.Update(addr); err != nil {
t.Fatal("Expected update")
}
select {
case <-endStream:
t.Fatal("Stream ended prematurely")
default:
}
}
// The queue should be full and the next update should fail.
t.Logf("Queue length=%d capacity=%d", translator.queueLen(), updateQueueCapacity)
if err := translator.Update(podAddr); err == nil {
t.Fatalf("Expected update to fail; queue=%d; capacity=%d", translator.queueLen(), updateQueueCapacity)
}
select {
case <-endStream:
default:
t.Fatal("Stream should have ended")
}
// XXX We should assert that endpointProfileUpdatesQueueOverflowCounter
// == 1 but we can't read counter values.
})
}