forked from dnnrly/wait-for
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathwaitfor_test.go
187 lines (153 loc) · 5.25 KB
/
waitfor_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
package waitfor
import (
"errors"
"fmt"
"testing"
"time"
"github.com/spf13/afero"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
func Test_isSuccess(t *testing.T) {
assert.True(t, isSuccess(200))
assert.True(t, isSuccess(214))
assert.False(t, isSuccess(300))
assert.False(t, isSuccess(199))
assert.False(t, isSuccess(100))
assert.False(t, isSuccess(500))
assert.False(t, isSuccess(407))
}
func TestOpenConfig_errorOnFileOpenFailure(t *testing.T) {
mockFS := afero.NewMemMapFs()
config, err := OpenConfig("./wait-for.yaml", "", "", afero.NewReadOnlyFs(mockFS))
assert.Error(t, err)
assert.Nil(t, config)
}
func TestOpenConfig_errorOnFileParsingFailure(t *testing.T) {
mockFS := afero.NewMemMapFs()
_ = afero.WriteFile(mockFS, "./wait-for.yaml", []byte("this isn't yaml!"), 0444)
config, err := OpenConfig("./wait-for.yaml", "", "", afero.NewReadOnlyFs(mockFS))
assert.Error(t, err)
assert.Nil(t, config)
}
func TestOpenConfig_errorOnParsingDefaultTimeout(t *testing.T) {
mockFS := afero.NewMemMapFs()
_ = afero.WriteFile(mockFS, "./wait-for.yaml", []byte(defaultConfigYaml()), 0444)
config, err := OpenConfig("./wait-for.yaml", "invalid duration", "1s", afero.NewReadOnlyFs(mockFS))
assert.Error(t, err)
assert.Nil(t, config)
}
func TestOpenConfig_errorOnParsingDefaultHTTPTimeout(t *testing.T) {
mockFS := afero.NewMemMapFs()
_ = afero.WriteFile(mockFS, "./wait-for.yaml", []byte(defaultConfigYaml()), 0444)
config, err := OpenConfig("./wait-for.yaml", "10s", "invalid duration", afero.NewReadOnlyFs(mockFS))
assert.Error(t, err)
assert.Nil(t, config)
}
func TestOpenConfig_defaultTimeoutCanBeSet(t *testing.T) {
mockFS := afero.NewMemMapFs()
_ = afero.WriteFile(mockFS, "./wait-for.yaml", []byte(defaultConfigYaml()), 0444)
config, err := OpenConfig("./wait-for.yaml", "19s", "1s", afero.NewReadOnlyFs(mockFS))
assert.NoError(t, err)
assert.NotNil(t, config)
assert.Equal(t, time.Second*19, config.DefaultTimeout)
}
func TestOpenConfig_defaultHTTPTimeoutCanBeSet(t *testing.T) {
mockFS := afero.NewMemMapFs()
_ = afero.WriteFile(mockFS, "./wait-for.yaml", []byte(defaultConfigYaml()), 0444)
config, err := OpenConfig("./wait-for.yaml", "19s", "20s", afero.NewReadOnlyFs(mockFS))
assert.NoError(t, err)
assert.NotNil(t, config)
assert.Equal(t, time.Second*20, config.DefaultHTTPClientTimeout)
}
func TestWaitOn_errorsInvalidTarget(t *testing.T) {
err := WaitOn(NewConfig(), NullLogger, []string{"localhost"}, map[string]WaiterFunc{})
assert.Error(t, err)
}
func TestRun_errorsOnParseFailure(t *testing.T) {
err := WaitOn(NewConfig(), NullLogger, []string{"http://localhost"}, map[string]WaiterFunc{})
assert.Error(t, err)
}
func TestWaitOnSingleTarget_succeedsImmediately(t *testing.T) {
var logs []string
doLog := func(f string, p ...interface{}) { logs = append(logs, fmt.Sprintf(f, p...)) }
err := waitOnSingleTarget(
"name",
doLog,
TargetConfig{Timeout: time.Second * 2},
func(name string, target *TargetConfig) error { return nil },
)
assert.NoError(t, err)
assert.Len(t, logs, 1)
assert.Equal(t, "finished waiting for name", logs[0])
}
func TestWaitOnSingleTarget_succeedsAfterWaiting(t *testing.T) {
var logs []string
doLog := func(f string, p ...interface{}) { logs = append(logs, fmt.Sprintf(f, p...)) }
waitUntil := time.Now().Add(time.Millisecond * 1100)
err := waitOnSingleTarget(
"name",
doLog,
TargetConfig{Timeout: time.Second * 2},
func(name string, target *TargetConfig) error {
if waitUntil.After(time.Now()) {
return fmt.Errorf("there was an error")
}
return nil
},
)
assert.NoError(t, err)
assert.Contains(t, logs, "error while waiting for name: there was an error")
assert.Contains(t, logs, "finished waiting for name")
}
func TestWaitOnSingleTarget_failsIfTimerExpires(t *testing.T) {
var logs []string
doLog := func(f string, p ...interface{}) { logs = append(logs, fmt.Sprintf(f, p...)) }
err := waitOnSingleTarget(
"name",
doLog,
TargetConfig{Timeout: time.Second * 2},
func(name string, target *TargetConfig) error {
return fmt.Errorf("")
},
)
assert.Error(t, err)
assert.NotContains(t, logs, "finished waiting for name")
}
func TestWaitOnTargets_failsForUnknownType(t *testing.T) {
err := waitOnTargets(
NullLogger,
map[string]TargetConfig{"unkown": {Type: "unknown type"}},
map[string]WaiterFunc{"type": func(string, *TargetConfig) error { return errors.New("") }},
)
require.Error(t, err)
assert.Equal(t, "unknown target type unknown type", err.Error())
}
func TestWaitOnTargets_selectsCorrectWaiter(t *testing.T) {
err := waitOnTargets(
NullLogger,
map[string]TargetConfig{
"type 1": {Type: "t1"},
},
map[string]WaiterFunc{
"t1": func(string, *TargetConfig) error { return nil },
"t2": func(string, *TargetConfig) error { return errors.New("an error") },
},
)
require.NoError(t, err)
}
func TestWaitOnTargets_failsWhenWaiterFails(t *testing.T) {
err := waitOnTargets(
NullLogger,
map[string]TargetConfig{
"type 1": {Type: "t1"},
"type 2": {Type: "t2"},
},
map[string]WaiterFunc{
"t1": func(string, *TargetConfig) error { return nil },
"t2": func(string, *TargetConfig) error { return errors.New("an error") },
},
)
require.Error(t, err)
assert.Equal(t, "timed out waiting for type 2: an error", err.Error())
}