forked from sgirones/kube2consul
/
consul_worker_test.go
224 lines (171 loc) · 4.61 KB
/
consul_worker_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
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
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
package main
import (
"errors"
"testing"
consulapi "github.com/hashicorp/consul/api"
"github.com/hashicorp/consul/testutil"
kapi "k8s.io/kubernetes/pkg/api"
)
func makeTestService(name string) *kapi.Service {
return &kapi.Service{
ObjectMeta: kapi.ObjectMeta{
Name: name,
Namespace: "bar",
},
Spec: kapi.ServiceSpec{
Type: kapi.ServiceTypeNodePort,
ClusterIP: "192.168.1.1",
Ports: []kapi.ServicePort{
kapi.ServicePort{
Name: "tcp",
Protocol: kapi.ProtocolTCP,
Port: 80,
NodePort: 8080,
},
},
},
}
}
func makeDefaultTestService() *kapi.Service {
return makeTestService("Default")
}
//Copied from consul api tests
type configCallback func(c *consulapi.Config)
func makeClient(t *testing.T) (*consulapi.Client, *testutil.TestServer) {
return makeClientWithConfig(t, nil, nil)
}
func makeClientWithConfig(
t *testing.T,
cb1 configCallback,
cb2 testutil.ServerConfigCallback) (*consulapi.Client, *testutil.TestServer) {
// Make client config
conf := consulapi.DefaultConfig()
if cb1 != nil {
cb1(conf)
}
// Create server
server := testutil.NewTestServerConfig(t, cb2)
conf.Address = server.HTTPAddr
// Create client
client, err := consulapi.NewClient(conf)
if err != nil {
t.Fatalf("err: %v", err)
}
return client, server
}
func makeTestAgentWorker(t *testing.T) (*consulapi.Client, *ConsulAgentWorker, *testutil.TestServer) {
c, s := makeClient(t)
w := NewConsulAgentWorker(c)
return c, w, s
}
func makeTestCatalogWorker(t *testing.T) (*consulapi.Client, *ConsulCatalogWorker, *testutil.TestServer) {
c, s := makeClient(t)
w := NewConsulCatalogWorker(c, makeCatalogConfig())
return c, w, s
}
func makeDNSInfo() DNSInfo {
return DNSInfo{
BaseID: "Foo",
IPAddress: "192.168.1.1",
}
}
func makeCatalogConfig() *ConsulWorkerConfig {
return &ConsulWorkerConfig{
TCPDomain: "test",
}
}
func waitForServiceCount(t *testing.T, catalog *consulapi.Catalog, serviceName string, count int) {
testutil.WaitForResult(func() (bool, error) {
x, _, err := catalog.Service(serviceName, "", nil)
if err != nil {
return false, err
}
if len(x) != count {
return false, errors.New("Wrong service count")
}
return true, nil
}, func(err error) {
t.Fatal("Error: ", err)
})
}
func TestAgentAddDNS(t *testing.T) {
c, w, s := makeTestAgentWorker(t)
defer s.Stop()
dns := makeDNSInfo()
if fail := w.AddDNS(dns, makeDefaultTestService()); fail != nil {
t.Fatal("AddDNS failed. ", fail)
}
testutil.WaitForResult(func() (bool, error) {
catalog := c.Catalog()
x, _, err := catalog.Services(nil)
if err != nil {
return false, err
}
for x := range x {
print("Service:", x, "\n")
}
return true, nil
}, func(err error) {
t.Fatal("Error: ", err)
})
}
func TestCatalogAddDNS(t *testing.T) {
c, w, s := makeTestCatalogWorker(t)
defer s.Stop()
dns := makeDNSInfo()
service := makeDefaultTestService()
if fail := w.AddDNS(dns, service); fail != nil {
t.Fatal("AddDNS failed. ", fail)
}
waitForServiceCount(t, c.Catalog(), service.Name, 1)
}
func TestCatalogRemoveDNS(t *testing.T) {
c, w, s := makeTestCatalogWorker(t)
defer s.Stop()
dns := makeDNSInfo()
service := makeDefaultTestService()
if fail := w.AddDNS(dns, service); fail != nil {
t.Fatal("AddDNS failed. ", fail)
}
waitForServiceCount(t, c.Catalog(), service.Name, 1)
w.RemoveDNS(dns)
waitForServiceCount(t, c.Catalog(), service.Name, 0)
}
func TestCatalogMultipleDNS(t *testing.T) {
c, w, s := makeTestCatalogWorker(t)
defer s.Stop()
dns1 := makeDNSInfo()
service := makeDefaultTestService()
if fail := w.AddDNS(dns1, service); fail != nil {
t.Fatal("AddDNS failed. ", fail)
}
dns2 := makeDNSInfo()
dns2.BaseID = "testing2"
if fail := w.AddDNS(dns2, service); fail != nil {
t.Fatal("AddDNS failed on 2. ", fail)
}
waitForServiceCount(t, c.Catalog(), service.Name, 1)
w.RemoveDNS(dns1)
waitForServiceCount(t, c.Catalog(), service.Name, 1)
w.RemoveDNS(dns2)
waitForServiceCount(t, c.Catalog(), service.Name, 0)
}
func TestCatalogPurgeDNS(t *testing.T) {
c, w, s := makeTestCatalogWorker(t)
defer s.Stop()
dns := makeDNSInfo()
service := makeDefaultTestService()
if fail := w.AddDNS(dns, service); fail != nil {
t.Fatal("AddDNS failed. ", fail)
}
dns.BaseID = "testing2"
service2 := makeTestService("Bar")
if fail := w.AddDNS(dns, service2); fail != nil {
t.Fatal("AddDNS failed. ", fail)
}
waitForServiceCount(t, c.Catalog(), service.Name, 1)
waitForServiceCount(t, c.Catalog(), service2.Name, 1)
w.PurgeDNS()
waitForServiceCount(t, c.Catalog(), service.Name, 0)
waitForServiceCount(t, c.Catalog(), service2.Name, 0)
}