mirror of
https://github.com/k3s-io/kubernetes.git
synced 2025-07-29 06:27:05 +00:00
Merge pull request #29215 from ericchiang/http-probe-checker-test-dont-sr-compare-errors
Automatic merge from submit-queue pkg/probe/http: don't compare error strings in tests TestHTTPProbeChecker fails on the Go1.7 release candidates. The package's history show that this was the case for Go1.5 and Go1.6 as well. The test depend on errors holding specific string values, behavior not guarenteed in the standard library API, and causing new test failures every minor Go release. Just look for an error rather than trying to inspect it using string comparison. If we feel this impacts coverage we can add more test cases. Fixes #15952
This commit is contained in:
commit
c09956e13f
@ -32,15 +32,6 @@ import (
|
|||||||
|
|
||||||
const FailureCode int = -1
|
const FailureCode int = -1
|
||||||
|
|
||||||
func containsAny(s string, substrs []string) bool {
|
|
||||||
for _, substr := range substrs {
|
|
||||||
if strings.Contains(s, substr) {
|
|
||||||
return true
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return false
|
|
||||||
}
|
|
||||||
|
|
||||||
func TestHTTPProbeChecker(t *testing.T) {
|
func TestHTTPProbeChecker(t *testing.T) {
|
||||||
handleReq := func(s int, body string) func(w http.ResponseWriter, r *http.Request) {
|
handleReq := func(s int, body string) func(w http.ResponseWriter, r *http.Request) {
|
||||||
return func(w http.ResponseWriter, r *http.Request) {
|
return func(w http.ResponseWriter, r *http.Request) {
|
||||||
@ -54,20 +45,17 @@ func TestHTTPProbeChecker(t *testing.T) {
|
|||||||
handler func(w http.ResponseWriter, r *http.Request)
|
handler func(w http.ResponseWriter, r *http.Request)
|
||||||
reqHeaders http.Header
|
reqHeaders http.Header
|
||||||
health probe.Result
|
health probe.Result
|
||||||
// go1.5: error message changed for timeout, need to support
|
accBody string
|
||||||
// both old and new
|
|
||||||
accBodies []string
|
|
||||||
}{
|
}{
|
||||||
// The probe will be filled in below. This is primarily testing that an HTTP GET happens.
|
// The probe will be filled in below. This is primarily testing that an HTTP GET happens.
|
||||||
{
|
{
|
||||||
handleReq(http.StatusOK, "ok body"),
|
handler: handleReq(http.StatusOK, "ok body"),
|
||||||
nil,
|
health: probe.Success,
|
||||||
probe.Success,
|
accBody: "ok body",
|
||||||
[]string{"ok body"},
|
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
// Echo handler that returns the contents of request headers in the body
|
// Echo handler that returns the contents of request headers in the body
|
||||||
func(w http.ResponseWriter, r *http.Request) {
|
handler: func(w http.ResponseWriter, r *http.Request) {
|
||||||
w.WriteHeader(200)
|
w.WriteHeader(200)
|
||||||
output := ""
|
output := ""
|
||||||
for k, arr := range r.Header {
|
for k, arr := range r.Header {
|
||||||
@ -77,50 +65,40 @@ func TestHTTPProbeChecker(t *testing.T) {
|
|||||||
}
|
}
|
||||||
w.Write([]byte(output))
|
w.Write([]byte(output))
|
||||||
},
|
},
|
||||||
http.Header{
|
reqHeaders: http.Header{
|
||||||
"X-Muffins-Or-Cupcakes": {"muffins"},
|
"X-Muffins-Or-Cupcakes": {"muffins"},
|
||||||
},
|
},
|
||||||
probe.Success,
|
health: probe.Success,
|
||||||
[]string{
|
accBody: "X-Muffins-Or-Cupcakes: muffins",
|
||||||
"X-Muffins-Or-Cupcakes: muffins",
|
|
||||||
},
|
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
// Echo handler that returns the contents of Host in the body
|
// Echo handler that returns the contents of Host in the body
|
||||||
func(w http.ResponseWriter, r *http.Request) {
|
handler: func(w http.ResponseWriter, r *http.Request) {
|
||||||
w.WriteHeader(200)
|
w.WriteHeader(200)
|
||||||
w.Write([]byte(r.Host))
|
w.Write([]byte(r.Host))
|
||||||
},
|
},
|
||||||
http.Header{
|
reqHeaders: http.Header{
|
||||||
"Host": {"muffins.cupcakes.org"},
|
"Host": {"muffins.cupcakes.org"},
|
||||||
},
|
},
|
||||||
probe.Success,
|
health: probe.Success,
|
||||||
[]string{
|
accBody: "muffins.cupcakes.org",
|
||||||
"muffins.cupcakes.org",
|
|
||||||
},
|
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
handleReq(FailureCode, "fail body"),
|
handler: handleReq(FailureCode, "fail body"),
|
||||||
nil,
|
health: probe.Failure,
|
||||||
probe.Failure,
|
|
||||||
[]string{
|
|
||||||
fmt.Sprintf("HTTP probe failed with statuscode: %d", FailureCode),
|
|
||||||
fmt.Sprintf("malformed HTTP status code \"%d\"", FailureCode),
|
|
||||||
},
|
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
func(w http.ResponseWriter, r *http.Request) {
|
handler: handleReq(http.StatusInternalServerError, "fail body"),
|
||||||
|
health: probe.Failure,
|
||||||
|
},
|
||||||
|
{
|
||||||
|
handler: func(w http.ResponseWriter, r *http.Request) {
|
||||||
time.Sleep(3 * time.Second)
|
time.Sleep(3 * time.Second)
|
||||||
},
|
},
|
||||||
nil,
|
health: probe.Failure,
|
||||||
probe.Failure,
|
|
||||||
[]string{
|
|
||||||
"use of closed network connection",
|
|
||||||
"request canceled (Client.Timeout exceeded while awaiting headers)",
|
|
||||||
},
|
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
for _, test := range testCases {
|
for i, test := range testCases {
|
||||||
func() {
|
func() {
|
||||||
server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||||
test.handler(w, r)
|
test.handler(w, r)
|
||||||
@ -128,28 +106,30 @@ func TestHTTPProbeChecker(t *testing.T) {
|
|||||||
defer server.Close()
|
defer server.Close()
|
||||||
u, err := url.Parse(server.URL)
|
u, err := url.Parse(server.URL)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
t.Errorf("Unexpected error: %v", err)
|
t.Errorf("case %d: unexpected error: %v", i, err)
|
||||||
}
|
}
|
||||||
_, port, err := net.SplitHostPort(u.Host)
|
_, port, err := net.SplitHostPort(u.Host)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
t.Errorf("Unexpected error: %v", err)
|
t.Errorf("case %d: unexpected error: %v", i, err)
|
||||||
}
|
}
|
||||||
_, err = strconv.Atoi(port)
|
_, err = strconv.Atoi(port)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
t.Errorf("Unexpected error: %v", err)
|
t.Errorf("case %d: unexpected error: %v", i, err)
|
||||||
}
|
}
|
||||||
health, output, err := prober.Probe(u, test.reqHeaders, 1*time.Second)
|
health, output, err := prober.Probe(u, test.reqHeaders, 1*time.Second)
|
||||||
if test.health == probe.Unknown && err == nil {
|
if test.health == probe.Unknown && err == nil {
|
||||||
t.Errorf("Expected error")
|
t.Errorf("case %d: expected error", i)
|
||||||
}
|
}
|
||||||
if test.health != probe.Unknown && err != nil {
|
if test.health != probe.Unknown && err != nil {
|
||||||
t.Errorf("Unexpected error: %v", err)
|
t.Errorf("case %d: unexpected error: %v", i, err)
|
||||||
}
|
}
|
||||||
if health != test.health {
|
if health != test.health {
|
||||||
t.Errorf("Expected %v, got %v", test.health, health)
|
t.Errorf("case %d: expected %v, got %v", i, test.health, health)
|
||||||
}
|
}
|
||||||
if !containsAny(output, test.accBodies) {
|
if health != probe.Failure && test.health != probe.Failure {
|
||||||
t.Errorf("Expected one of %#v, got %v", test.accBodies, output)
|
if !strings.Contains(output, test.accBody) {
|
||||||
|
t.Errorf("Expected response body to contain %v, got %v", test.accBody, output)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}()
|
}()
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user