-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathconsul_test.go
125 lines (104 loc) · 3.08 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
package consul
import (
"errors"
"fmt"
"net"
"reflect"
"testing"
)
func ExampleLookup() {
service := "authentication.service.consul"
r, err := Lookup(service)
if err != nil {
fmt.Printf("Error Looking up consul service %q - %s\n", service, err.Error())
}
//services-dev-03.node.dc1.consul:8081
fmt.Println(*r)
}
func TestLookup(t *testing.T) {
dummyLookup := func(service, proto, name string) (cname string, addrs []*net.SRV, err error) {
result := net.SRV{
Target: "node1.example.com.consul.",
Port: 8089,
}
results := []*net.SRV{&result}
return "", results, nil
}
service := "example.com.consul"
r, err := lookupWihLookupSRV(service, dummyLookup)
assertNil(t, err, "Error should have been nil")
assertEqual(t, "node1.example.com.consul:8089", *r)
}
func TestLookup_HandleProtacol(t *testing.T) {
dummyLookup := func(service, proto, name string) (cname string, addrs []*net.SRV, err error) {
if name != "example.com.consul" {
return "", nil, nil
}
result := net.SRV{
Target: "node1.example.com.consul.",
Port: 8089,
}
results := []*net.SRV{&result}
return "", results, nil
}
service := "http://example.com.consul"
r, err := lookupWihLookupSRV(service, dummyLookup)
assertNil(t, err, "Error should have been nil")
assertEqual(t, "http://node1.example.com.consul:8089", *r)
}
func TestLookup_NotConsul(t *testing.T) {
service := "example.com"
r, err := Lookup(service)
assertNil(t, err, "Error should have been nil")
assertNil(t, r, "Result should have been nil")
}
func TestLookup_InvalidName(t *testing.T) {
service := ".consul"
r, err := Lookup(service)
assertEqual(t, "Error performing SRV DNS Lookup for .consul - lookup : invalid domain name", err.Error())
assertNil(t, r, "Result should have been nil")
}
func TestLookup_LookupError(t *testing.T) {
errorLookup := func(service, proto, name string) (cname string, addrs []*net.SRV, err error) {
return "", nil, errors.New("Boom")
}
service := "example.com.consul"
r, err := lookupWihLookupSRV(service, errorLookup)
assertEqual(t, "Error performing SRV DNS Lookup for example.com.consul - Boom", err.Error())
assertNil(t, r, "Result should have been nil")
}
func TestLookup_LookupNilResults(t *testing.T) {
errorLookup := func(service, proto, name string) (cname string, addrs []*net.SRV, err error) {
return "", nil, nil
}
service := "example.com.consul"
r, err := lookupWihLookupSRV(service, errorLookup)
assertNil(t, err, "Error should have been nil")
assertNil(t, r, "Result should have been nil")
}
func assertNil(t *testing.T, i interface{}, message string) {
if !isNil(i) {
t.Fatal(message)
}
}
func assertNotNil(t *testing.T, i interface{}, message string) {
if isNil(i) {
t.Fatal(message)
}
}
func isNil(object interface{}) bool {
if object == nil {
return true
}
value := reflect.ValueOf(object)
kind := value.Kind()
if kind >= reflect.Chan && kind <= reflect.Slice && value.IsNil() {
return true
}
return false
}
func assertEqual(t *testing.T, expected string, result string) {
if result != expected {
t.Fatalf("Expected %q got %q", expected, result)
}
}