-
Notifications
You must be signed in to change notification settings - Fork 1
/
selector_test.go
122 lines (109 loc) · 2.44 KB
/
selector_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
package selector
import (
"context"
"testing"
"time"
kubernetes "github.com/castai/k8s-client-go"
)
func TestServerList(t *testing.T) {
endpoints := &mockEndpoints{
get: &kubernetes.Endpoints{
Subsets: []kubernetes.Subset{
{
Addresses: []kubernetes.Address{
{
IP: "10.10.0.15",
},
},
Ports: []kubernetes.Port{
{
Port: 11211,
},
},
},
},
},
watch: &endpointsWatch{
ch: make(chan kubernetes.Event[*kubernetes.Endpoints]),
},
}
ctx, cancel := context.WithTimeout(context.Background(), 30*time.Millisecond)
defer cancel()
s, err := startServerList(ctx, "memcache:11211", &defaultLogger{}, endpoints)
if err != nil {
t.Fatal(err)
}
addr, err := s.PickServer("key1")
if err != nil {
t.Fatal(err)
}
expectedAddr := "10.10.0.15:11211"
if addr.String() != expectedAddr {
t.Fatalf("expected addr %s, actual %s", expectedAddr, addr.String())
}
}
func TestParseTargetInfo(t *testing.T) {
tests := []struct {
in string
out targetInfo
}{
{
in: "memcache:11211",
out: targetInfo{
namespace: getCurrentNamespaceOrDefault(),
name: "memcache",
port: "11211",
namedPort: false,
},
},
{
in: "memcache.my-namespace:11211",
out: targetInfo{
namespace: "my-namespace",
name: "memcache",
port: "11211",
namedPort: false,
},
},
{
in: "memcache.my-namespace:named-port",
out: targetInfo{
namespace: "my-namespace",
name: "memcache",
port: "named-port",
namedPort: true,
},
},
}
for _, test := range tests {
test := test
t.Run(test.in, func(t *testing.T) {
out, err := parseTargetInfo(test.in)
if err != nil {
t.Fatal(err)
}
if test.out != out {
t.Fatalf("expected %+v, actual %+v", test.out, out)
}
})
}
}
type mockEndpoints struct {
get *kubernetes.Endpoints
watch *endpointsWatch
}
func (m *mockEndpoints) Get(ctx context.Context, namespace, name string, _ kubernetes.GetOptions) (*kubernetes.Endpoints, error) {
return m.get, nil
}
func (m *mockEndpoints) Watch(ctx context.Context, namespace, name string, _ kubernetes.ListOptions) (kubernetes.WatchInterface[*kubernetes.Endpoints], error) {
return m.watch, nil
}
type endpointsWatch struct {
ch chan kubernetes.Event[*kubernetes.Endpoints]
}
func (e endpointsWatch) Stop() {
close(e.ch)
}
func (e endpointsWatch) ResultChan() <-chan kubernetes.Event[*kubernetes.Endpoints] {
return e.ch
}