-
Notifications
You must be signed in to change notification settings - Fork 1
/
consul_test.go
132 lines (106 loc) · 3.15 KB
/
consul_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
// Copyright © 2022 Roberto Hidalgo <[email protected]>
// SPDX-License-Identifier: Apache-2.0
package catalog_test
import (
"testing"
. "github.com/unRob/coredns-consul"
)
var serviceProxyName = "traefik"
func TestFetchStaticServiceKey(t *testing.T) {
src := NewWatch(&WatchKVPath{Key: "static/path"})
c, _, _ := NewTestCatalog(true, src)
t.Run("static target", func(t *testing.T) {
svc := c.ServiceFor("static-consul")
if svc == nil {
t.Fatalf("Service static-consul not found, got: %+v", c.Services())
}
if svc.Target != serviceProxyName {
t.Fatalf("Unexpected target: %v", svc.Target)
}
})
t.Run("static addresses", func(t *testing.T) {
svc := c.ServiceFor("static-addr")
if svc == nil {
t.Fatalf("Service static-addr not found, got: %+v", c.Services())
}
if len(svc.Addresses) != 1 {
t.Fatalf("Unexpected addresses: %v", svc.Target)
}
if ip := svc.Addresses[0].String(); ip != "127.0.0.1" {
t.Fatalf("Unexpected address for static-addr: %v", ip)
}
})
t.Run("invalid static addresses", func(t *testing.T) {
svc := c.ServiceFor("static-addr-invalid")
if svc == nil {
t.Fatalf("Service static-addr-invalid not found, got: %+v", c.Services())
}
if len(svc.Addresses) != 1 {
t.Fatalf("Unexpected addresses for static-addr-invalid: %v", svc.Target)
}
if ip := svc.Addresses[0].String(); ip != "127.0.0.1" {
t.Fatalf("Unexpected address for static-addr-invalid: %v", ip)
}
})
t.Run("ignored static", func(t *testing.T) {
svc := c.ServiceFor("static-ignored")
if svc != nil {
t.Fatalf("Service static-addr found with invalid config, got: %+v", c.Services())
}
})
}
func TestFetchStaticServicePrefix(t *testing.T) {
src := NewWatch(&WatcKVPrefix{Prefix: "static/prefix"})
c, _, _ := NewTestCatalog(true, src)
svc := c.ServiceFor("prefixed-static")
if svc == nil {
t.Fatalf("Service consul not found")
}
if svc.Target != serviceProxyName {
t.Fatalf("Unexpected target: %v", svc.Target)
}
}
func TestFetchServices(t *testing.T) {
c, client, _ := NewTestCatalog(true)
services := c.Services()
if len(services) != 3 {
t.Fatalf("Unexpected number of services: %d", len(services))
}
svcTests := map[string]string{
"nomad": ServiceProxyTag,
serviceProxyName: ServiceProxyTag,
"git": "git",
}
for svc, expected := range svcTests {
target, exists := services[svc]
if !exists {
t.Fatalf("Expected service %s not found", svc)
}
if target.Target != expected {
t.Fatalf("Unexpected target: %v", target)
}
}
lastUpdate := c.LastUpdated()
err := c.ReloadAll()
if err != nil {
t.Fatalf("Fetch services: %v", err)
}
if lastUpdate != c.LastUpdated() {
t.Fatalf("Services changed after timeout")
}
err = c.ReloadAll()
if err != nil {
t.Fatalf("Fetch services: %v", err)
}
testclient := client.(*testCatalogClient)
testclient.DeleteService("git")
if err := c.ReloadAll(); err != nil {
t.Fatalf("could not fetch services: %s", err)
}
if lastUpdate == c.LastUpdated() {
t.Fatalf("Services did not change after update")
}
if newCount := len(c.Services()); newCount != 2 {
t.Fatalf("Unexpected number of services after update: %d", newCount)
}
}