-
Notifications
You must be signed in to change notification settings - Fork 3
/
config_test.go
142 lines (116 loc) · 4.95 KB
/
config_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
package waitfor
import (
"strings"
"testing"
"time"
"github.com/stretchr/testify/assert"
)
func TestConfig_fromYAML(t *testing.T) {
config, err := NewConfigFromFile(strings.NewReader(defaultConfigYaml()))
assert.NoError(t, err)
assert.Equal(t, time.Second*5, config.DefaultTimeout)
assert.Equal(t, 2, len(config.Targets))
assert.Equal(t, "http://localhost/health", config.Targets["http-connection"].Target)
assert.Equal(t, time.Second*10, config.Targets["http-connection"].Timeout)
assert.Equal(t, "http", config.Targets["http-connection"].Type)
assert.Equal(t, time.Second*5, config.Targets["http-connection"].HTTPClientTimeout)
assert.Equal(t, "localhost:80", config.Targets["tcp-connection"].Target)
assert.Equal(t, "tcp", config.Targets["tcp-connection"].Type)
assert.Equal(t, time.Second*5, config.Targets["tcp-connection"].Timeout)
}
func TestConfig_incorrectTimeDurationFails(t *testing.T) {
config, err := NewConfigFromFile(strings.NewReader(`targets:
timeout-connection:
type: http
target: http://localhost/health
timeout: not parsable`))
assert.Error(t, err)
assert.Nil(t, config)
}
func TestConfig_incorrectHTTPTimeoutDurationFails(t *testing.T) {
config, err := NewConfigFromFile(strings.NewReader(`targets:
timeout-connection:
type: http
target: http://localhost/health
timeout: 10s
http-client-timeout: not parsable`))
assert.Error(t, err)
assert.Nil(t, config)
}
func TestConfig_settingDefaultTimeoutWorks(t *testing.T) {
config, err := NewConfigFromFile(strings.NewReader(`
default-timeout: 18s
targets:
http-connection:
type: http
target: http://localhost/health`))
assert.NoError(t, err)
assert.Equal(t, time.Second*18, config.Targets["http-connection"].Timeout)
}
func TestConfig_settingDefaultHTTPTimeoutWorks(t *testing.T) {
config, err := NewConfigFromFile(strings.NewReader(`
default-http-client-timeout: 18s
targets:
http-connection:
type: http
target: http://localhost/health
timeout: 10s`))
assert.NoError(t, err)
assert.Equal(t, time.Second*18, config.Targets["http-connection"].HTTPClientTimeout)
}
func TestConfig_GotTarget(t *testing.T) {
config, _ := NewConfigFromFile(strings.NewReader(defaultConfigYaml()))
assert.True(t, config.GotTarget("http-connection"))
assert.False(t, config.GotTarget("non-existent"))
}
func TestConfig_AddFromString(t *testing.T) {
config := NewConfig()
assert.NoError(t, config.AddFromString("http://some-host/endpoint"))
assert.NoError(t, config.AddFromString("https://some-host/endpoint"))
assert.NoError(t, config.AddFromString("http://another-host/endpoint"))
assert.NoError(t, config.AddFromString("tcp:listener-tcp:9090"))
assert.NoError(t, config.AddFromString("dns:some.dns.com"))
assert.Error(t, config.AddFromString("udp:some-listener:9090"))
assert.Equal(t, 5, len(config.Targets))
assert.Equal(t, "http://some-host/endpoint", config.Targets["http://some-host/endpoint"].Target)
assert.Equal(t, "http", config.Targets["http://some-host/endpoint"].Type)
assert.Equal(t, time.Second*5, config.Targets["http://some-host/endpoint"].Timeout)
assert.Equal(t, time.Second, config.Targets["http://some-host/endpoint"].HTTPClientTimeout)
assert.Equal(t, "https://some-host/endpoint", config.Targets["https://some-host/endpoint"].Target)
assert.Equal(t, "http", config.Targets["https://some-host/endpoint"].Type)
assert.Equal(t, time.Second*5, config.Targets["https://some-host/endpoint"].Timeout)
assert.Equal(t, time.Second, config.Targets["http://some-host/endpoint"].HTTPClientTimeout)
assert.Equal(t, "http://another-host/endpoint", config.Targets["http://another-host/endpoint"].Target)
assert.Equal(t, "http", config.Targets["http://another-host/endpoint"].Type)
assert.Equal(t, time.Second*5, config.Targets["http://some-host/endpoint"].Timeout)
assert.Equal(t, time.Second, config.Targets["http://some-host/endpoint"].HTTPClientTimeout)
assert.Equal(t, "listener-tcp:9090", config.Targets["tcp:listener-tcp:9090"].Target)
assert.Equal(t, "tcp", config.Targets["tcp:listener-tcp:9090"].Type)
assert.Equal(t, time.Second*5, config.Targets["tcp:listener-tcp:9090"].Timeout)
assert.Equal(t, "some.dns.com", config.Targets["dns:some.dns.com"].Target)
assert.Equal(t, "dns", config.Targets["dns:some.dns.com"].Type)
assert.Equal(t, time.Second*5, config.Targets["dns:some.dns.com"].Timeout)
}
func TestConfig_Filters(t *testing.T) {
config := NewConfig()
_ = config.AddFromString("http://some-host/endpoint")
_ = config.AddFromString("https://some-host/endpoint")
_ = config.AddFromString("http://another-host/endpoint")
config.Targets["listener-tcp"] = TargetConfig{
Target: "tcp:listener-tcp:9090",
}
filtered := config.Filter([]string{"http://some-host/endpoint", "listener-tcp"})
assert.Equal(t, 2, len(filtered.Targets))
}
func defaultConfigYaml() string {
return `targets:
http-connection:
type: http
target: http://localhost/health
timeout: 10s
http-client-timeout: 5s
tcp-connection:
type: tcp
target: localhost:80
`
}