-
Notifications
You must be signed in to change notification settings - Fork 11
/
Copy pathcmd_test.go
225 lines (204 loc) · 5.55 KB
/
cmd_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
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
package main
import (
"context"
"fmt"
"testing"
"time"
"github.com/stretchr/testify/assert"
)
type testFn func(port int, state string, host string, timeout int, t *testing.T) error
func TestWaitForPortCmd_ExecuteValidations(t *testing.T) {
validPort := 1000
for _, tt := range []struct {
name string
port int
state string
host string
timeout int
expectedErr interface{}
}{
{
name: "Detects invalid port (too low)", port: -1000, state: PortFree, timeout: 10,
expectedErr: "port out of range: port must be greater than zero",
},
{
name: "Detects invalid port (too low)", port: 0, state: PortFree, timeout: 10,
expectedErr: "port out of range: port must be greater than zero",
},
{
name: "Detects invalid port (too high)", port: 65536, state: PortFree, timeout: 10,
expectedErr: "port out of range: port must be <= 65535",
},
{
name: "Detects invalid host (nonexistent)", host: "someveryrandomhostfrombitnami", port: validPort, state: PortFree, timeout: 10,
expectedErr: `cannot resolve host "someveryrandomhostfrombitnami"`,
},
{
name: "Detects invalid state", port: validPort, state: "", timeout: 10,
expectedErr: "unknown state",
},
{
name: "Detects invalid state", port: validPort, state: "foobar",
expectedErr: "unknown state",
},
} {
t.Run(tt.name, func(t *testing.T) {
err := testViaStruct(tt.port, tt.state, tt.host, tt.timeout, t)
if err == nil {
t.Errorf("WaitForPortCmd.Execute() was expected to fail but succeeded")
} else if tt.expectedErr != nil {
assert.Regexp(t, tt.expectedErr, err, "Expected error %v to match %v", err, tt.expectedErr)
}
})
}
}
func TestWaitForPortCmd_Execute_Timeout(t *testing.T) {
freePort := getNextFreePort()
takenPort := getNextFreePort()
ctx, cancel := context.WithCancel(context.Background())
defer cancel()
if err := takePort(ctx, takenPort); err != nil {
t.Fatalf("cannot take port %d: %v", takenPort, err)
}
maxTimeDelta := 1 * time.Second
timeout := 2
duration := time.Duration(timeout) * time.Second
for _, spec := range []struct {
testFn testFn
suffix string
}{
{testFn: testViaStruct, suffix: "using struct"},
{testFn: testViaCli, suffix: "using cli"},
} {
t.Run("Timeout waiting for port to be in use "+spec.suffix, func(t *testing.T) {
start := time.Now()
err := spec.testFn(freePort, PortInUse, "", timeout, t)
end := time.Now()
assert.WithinDuration(t, end, start.Add(duration), maxTimeDelta)
if err == nil {
t.Errorf("expected check to fail with timeout, but it succeeded")
} else {
assert.Regexp(t, `timeout reached before the port went into state "inuse"`, err)
}
})
t.Run("Timeout waiting for port to be free "+spec.suffix, func(t *testing.T) {
start := time.Now()
err := spec.testFn(takenPort, PortFree, "", timeout, t)
end := time.Now()
assert.WithinDuration(t, end, start.Add(duration), maxTimeDelta)
if err == nil {
t.Errorf("expected check to fail with timeout, but it succeeded")
} else {
assert.Regexp(t, `timeout reached before the port went into state "free"`, err)
}
})
}
}
func TestWaitForPortCmd_Execute(t *testing.T) {
timeout := 10
for _, sleep := range []int{0, 5} {
for _, tt := range []struct {
name string
sleep int
state string
host string
}{
{
name: "Detect port was taken",
state: "inuse",
},
{
name: "Detect port is freed",
state: "free",
},
} {
duration := time.Duration(sleep) * time.Second
maxTimeDelta := time.Second
preFn := getPreambleFunc(tt.state, duration)
for _, spec := range []struct {
testFn testFn
suffix string
}{
{testFn: testViaStruct, suffix: "using struct"},
{testFn: testViaCli, suffix: "using cli"},
} {
testFn := spec.testFn
newTitle := fmt.Sprintf("%s %s", tt.name, spec.suffix)
if sleep != 0 {
newTitle = fmt.Sprintf("%s (after %d seconds)", newTitle, sleep)
}
t.Run(newTitle, func(t *testing.T) {
port := getNextFreePort()
ctx, cancel := context.WithCancel(context.Background())
defer cancel()
if err := preFn(ctx, port, t); err != nil {
t.Fatalf("Cannot execute test preamble: %v", err)
return
}
start := time.Now()
if err := testFn(port, tt.state, tt.host, timeout, t); err != nil {
t.Errorf("Expected command to succeed but got: %v", err)
}
end := time.Now()
assert.WithinDuration(t, end, start.Add(duration), maxTimeDelta)
})
}
}
}
}
func Test_isInUse(t *testing.T) {
ctx, cancel := context.WithCancel(context.Background())
defer cancel()
freePort := getNextFreePort()
takenPort := getNextFreePort()
if err := takePort(ctx, takenPort); err != nil {
t.Fatalf("cannot take port %d: %v", takenPort, err)
}
type args struct {
host string
port int
}
tests := []struct {
name string
args args
want bool
}{
{
name: "Free port detected (no host)",
want: false,
args: args{
port: freePort,
},
},
{
name: "Free port detected (localhost)",
want: false,
args: args{
port: freePort,
host: "localhost",
},
},
{
name: "Taken port detected (no host)",
want: true,
args: args{
port: takenPort,
},
},
{
name: "Taken port detected (localhost)",
want: true,
args: args{
port: takenPort,
host: "localhost",
},
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
if got := portIsInUse(context.Background(), tt.args.host, tt.args.port); got != tt.want {
t.Errorf("portIsInUse() = %v, want %v", got, tt.want)
}
})
}
}