-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathcslb_test.go
151 lines (129 loc) · 4.01 KB
/
cslb_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
package cslb
import (
"io/ioutil"
"net/http"
"os"
"strings"
"testing"
"time"
)
func unsetAll() {
os.Unsetenv(cslbEnvPrefix + "options")
os.Unsetenv(cslbEnvPrefix + "hc_ok")
os.Unsetenv(cslbEnvPrefix + "dial_veto")
os.Unsetenv(cslbEnvPrefix + "hc_freq")
os.Unsetenv(cslbEnvPrefix + "nxd_ttl")
os.Unsetenv(cslbEnvPrefix + "srv_ttl")
os.Unsetenv(cslbEnvPrefix + "tar_ttl")
os.Unsetenv(cslbEnvPrefix + "timeout")
}
// Test that newCslb notices good env variables. This blows away any env variables that might have
// been inherited by the test executable.
func TestCSLBGoodOptions(t *testing.T) {
os.Setenv(cslbEnvPrefix+"options", "dhirsHCN")
os.Setenv(cslbEnvPrefix+"hc_ok", "BIG OK")
os.Setenv(cslbEnvPrefix+"dial_veto", "5m")
os.Setenv(cslbEnvPrefix+"hc_freq", "10m")
os.Setenv(cslbEnvPrefix+"nxd_ttl", "15m")
os.Setenv(cslbEnvPrefix+"srv_ttl", "20m")
os.Setenv(cslbEnvPrefix+"tar_ttl", "25m")
os.Setenv(cslbEnvPrefix+"timeout", "30m")
cslb := newCslb()
if !cslb.PrintHCResults || !cslb.PrintIntercepts || !cslb.PrintSRVLookup || !cslb.PrintDialContext ||
!cslb.PrintDialResults || !cslb.DisableHealthChecks || !cslb.DisableInterception ||
!cslb.AllowNumericServices {
t.Error("At least one option not set", cslb.config)
}
if cslb.HealthCheckContentOk != "BIG OK" {
t.Error("HealthCheckContentOk not set")
}
if cslb.DialVetoDuration != time.Minute*5 {
t.Error("DialVetoDuration not set")
}
if cslb.HealthCheckFrequency != time.Minute*10 {
t.Error("HealthCheckFrequency not set")
}
if cslb.NotFoundSRVTTL != time.Minute*15 {
t.Error("NotFoundSRVTTL not set")
}
if cslb.FoundSRVTTL != time.Minute*20 {
t.Error("FoundSRVTTL not set")
}
if cslb.HealthTTL != time.Minute*25 {
t.Error("HealthTTL not set")
}
if cslb.InterceptTimeout != time.Minute*30 {
t.Error("InterceptTimeout not set")
}
unsetAll()
}
func TestCSLBBadOptions(t *testing.T) {
os.Setenv(cslbEnvPrefix+"options", "xxXX")
os.Setenv(cslbEnvPrefix+"dial_veto", "0s") // Cover
os.Setenv(cslbEnvPrefix+"hc_freq", "2h") // all
os.Setenv(cslbEnvPrefix+"nxd_ttl", "junk") // error paths
os.Setenv(cslbEnvPrefix+"srv_ttl", "junk")
os.Setenv(cslbEnvPrefix+"tar_ttl", "junk")
os.Setenv(cslbEnvPrefix+"timeout", "junk")
cslb := newCslb()
if cslb.PrintHCResults || cslb.PrintIntercepts || cslb.PrintSRVLookup || cslb.PrintDialContext ||
cslb.DisableHealthChecks || cslb.DisableInterception {
t.Error("At least one option unexpectedly set", cslb.config)
}
if cslb.DialVetoDuration != defaultDialVetoDuration {
t.Error("DialVetoDuration was set")
}
if cslb.HealthCheckFrequency != defaultHealthCheckFrequency {
t.Error("HealthCheckFrequency was set")
}
if cslb.NotFoundSRVTTL != defaultNotFoundSRVTTL {
t.Error("NotFoundSRVTTL was set")
}
if cslb.FoundSRVTTL != defaultFoundSRVTTL {
t.Error("FoundSRVTTL was set")
}
if cslb.HealthTTL != defaultHealthTTL {
t.Error("HealthTTL was set")
}
if cslb.InterceptTimeout != defaultInterceptTimeout {
t.Error("InterceptTimeout was set")
}
unsetAll()
}
func TestCloneStats(t *testing.T) {
cslb := newCslb()
var ls cslbStats
ls.FailedDials = 23
ls.Deadline = 12
ls.DialContext = 101
cslb.addStats(&ls)
s := cslb.cloneStats()
if s.FailedDials != ls.FailedDials || s.Deadline != ls.Deadline || s.DialContext != ls.DialContext {
t.Error("cloneStats does not agree with added stats", ls, s)
}
}
func TestCslbStartStop(t *testing.T) {
cslb := newCslb()
cslb.StatusServerAddress = sssListen
cslb.start()
time.Sleep(time.Second) // Give server a chance to start
resp, err := http.Get("http://" + sssListen + "/")
if err != nil {
t.Fatal(err)
}
body, err := ioutil.ReadAll(resp.Body)
resp.Body.Close()
if err != nil {
t.Fatal(err)
}
str := string(body)
if !strings.Contains(str, "Client Side Load Balancing") {
t.Error("GET of status page did not return title 'Client Side Load Balancing'", trimTo(str, 200))
}
cslb.stop()
time.Sleep(time.Second)
resp, err = http.Get("http://" + sssListen + "/")
if err == nil {
t.Error("Expected connection refused after cslb.stop()")
}
}