forked from knative/serving
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathexecprobe_test.go
176 lines (142 loc) · 4.54 KB
/
execprobe_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
/*
Copyright 2020 The Knative Authors
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package main
import (
"net/http"
"net/http/httptest"
"net/url"
"os"
"strconv"
"testing"
"time"
"go.uber.org/atomic"
network "knative.dev/networking/pkg"
"knative.dev/serving/pkg/queue/readiness"
)
func TestProbeQueueInvalidPort(t *testing.T) {
t.Cleanup(func() { os.Unsetenv(queuePortEnvVar) })
for _, port := range []string{"-1", "0", "66000"} {
os.Setenv(queuePortEnvVar, port)
if rv := standaloneProbeMain(1, http.DefaultTransport); rv != 1 {
t.Error("Unexpected return code", rv)
}
}
}
func TestProbeQueueConnectionFailure(t *testing.T) {
if err := probeQueueHealthPath(1, 12345, http.DefaultTransport); err == nil {
t.Error("Expected error, got nil")
}
}
func TestProbeQueueNotReady(t *testing.T) {
var probed atomic.Bool
port := newProbeTestServer(t, func(w http.ResponseWriter) {
probed.Store(true)
w.WriteHeader(http.StatusBadRequest)
})
err := probeQueueHealthPath(time.Second, port, http.DefaultTransport)
if err == nil || err.Error() != "probe returned not ready" {
t.Error("Unexpected not ready error:", err)
}
if !probed.Load() {
t.Error("Expected the queue proxy server to be probed")
}
}
func TestProbeShuttingDown(t *testing.T) {
var probed atomic.Bool
port := newProbeTestServer(t, func(w http.ResponseWriter) {
probed.Store(true)
w.WriteHeader(http.StatusGone)
})
err := probeQueueHealthPath(time.Second, port, http.DefaultTransport)
if err == nil || err.Error() != "failed to probe: failing probe deliberately for shutdown" {
t.Error("Unexpected error:", err)
}
if !probed.Load() {
t.Error("Expected the queue proxy server to be probed")
}
}
func TestProbeQueueShuttingDownFailsFast(t *testing.T) {
port := newProbeTestServer(t, func(w http.ResponseWriter) {
w.WriteHeader(http.StatusGone)
})
start := time.Now()
if err := probeQueueHealthPath(1, port, http.DefaultTransport); err == nil {
t.Error("probeQueueHealthPath did not fail")
}
// if fails due to timeout and not cancelation, then it took too long.
if time.Since(start) >= 1*time.Second {
t.Error("took too long to fail")
}
}
func TestProbeQueueReady(t *testing.T) {
var probed atomic.Bool
port := newProbeTestServer(t, func(w http.ResponseWriter) {
probed.Store(true)
w.WriteHeader(http.StatusOK)
})
t.Cleanup(func() { os.Unsetenv(queuePortEnvVar) })
os.Setenv(queuePortEnvVar, strconv.Itoa(port))
if rv := standaloneProbeMain(0 /*use default*/, nil); rv != 0 {
t.Error("Unexpected return value from standaloneProbeMain:", rv)
}
if !probed.Load() {
t.Error("Expected the queue proxy server to be probed")
}
}
func TestProbeQueueTimeout(t *testing.T) {
var probed atomic.Bool
port := newProbeTestServer(t, func(w http.ResponseWriter) {
probed.Store(true)
time.Sleep(2 * time.Second)
w.WriteHeader(http.StatusOK)
})
t.Cleanup(func() { os.Unsetenv(queuePortEnvVar) })
os.Setenv(queuePortEnvVar, strconv.Itoa(port))
const timeout = time.Second
if rv := standaloneProbeMain(timeout, nil); rv == 0 {
t.Error("Unexpected return value from standaloneProbeMain:", rv)
}
if !probed.Load() {
t.Error("Expected the queue proxy server to be probed")
}
}
func TestProbeQueueDelayedReady(t *testing.T) {
var count atomic.Int64
port := newProbeTestServer(t, func(w http.ResponseWriter) {
if count.Inc() < 9 {
w.WriteHeader(http.StatusBadRequest)
return
}
w.WriteHeader(http.StatusOK)
})
if err := probeQueueHealthPath(readiness.PollTimeout, port, http.DefaultTransport); err != nil {
t.Errorf("probeQueueHealthPath(%d) = %s", port, err)
}
}
func newProbeTestServer(t *testing.T, f func(w http.ResponseWriter)) (port int) {
ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
if r.Header.Get(network.UserAgentKey) == network.QueueProxyUserAgent {
f(w)
}
}))
t.Cleanup(ts.Close)
u, err := url.Parse(ts.URL)
if err != nil {
t.Fatalf("%s is not a valid URL: %v", ts.URL, err)
}
port, err = strconv.Atoi(u.Port())
if err != nil {
t.Fatalf("Failed to convert port(%s) to int: %v", u.Port(), err)
}
return port
}