diff --git a/test/conformance/ingress/basic_test.go b/test/conformance/ingress/basic_test.go index b3172479e105..a05b77b4092a 100644 --- a/test/conformance/ingress/basic_test.go +++ b/test/conformance/ingress/basic_test.go @@ -19,7 +19,6 @@ limitations under the License. package ingress import ( - "net/http" "testing" "k8s.io/apimachinery/pkg/util/intstr" @@ -56,13 +55,5 @@ func TestBasics(t *testing.T) { }) defer cancel() - // Make a request and check the response. - resp, err := client.Get("http://" + name + ".example.com") - if err != nil { - t.Fatalf("Error creating Ingress: %v", err) - } - defer resp.Body.Close() - if resp.StatusCode != http.StatusOK { - t.Errorf("Got non-OK status: %d", resp.StatusCode) - } + RuntimeRequest(t, client, "http://"+name+".example.com") } diff --git a/test/conformance/ingress/hosts_test.go b/test/conformance/ingress/hosts_test.go index cbd9c1d44237..5ccaebfc9670 100644 --- a/test/conformance/ingress/hosts_test.go +++ b/test/conformance/ingress/hosts_test.go @@ -19,7 +19,6 @@ limitations under the License. package ingress import ( - "net/http" "testing" "k8s.io/apimachinery/pkg/util/intstr" @@ -66,14 +65,6 @@ func TestMultipleHosts(t *testing.T) { defer cancel() for _, host := range hosts { - // Make a request and check the response. - resp, err := client.Get("http://" + host) - if err != nil { - t.Fatalf("Error creating Ingress: %v", err) - } - defer resp.Body.Close() - if resp.StatusCode != http.StatusOK { - t.Errorf("Got non-OK status: %d", resp.StatusCode) - } + RuntimeRequest(t, client, "http://"+host) } } diff --git a/test/conformance/ingress/path_test.go b/test/conformance/ingress/path_test.go index 1f4b1ed7be75..387c0c694d9f 100644 --- a/test/conformance/ingress/path_test.go +++ b/test/conformance/ingress/path_test.go @@ -19,16 +19,12 @@ limitations under the License. package ingress import ( - "encoding/json" - "io/ioutil" - "net/http" "testing" "k8s.io/apimachinery/pkg/util/intstr" "knative.dev/serving/pkg/apis/networking" "knative.dev/serving/pkg/apis/networking/v1alpha1" "knative.dev/serving/test" - "knative.dev/serving/test/types" ) // TestPath verifies that an Ingress properly dispatches to backends based on the path of the URL. @@ -134,20 +130,9 @@ func TestPath(t *testing.T) { for path, want := range tests { t.Run(path, func(t *testing.T) { - // Make a request and check the response. - resp, err := client.Get("http://" + name + ".example.com" + path) - if err != nil { - t.Fatalf("Error creating Ingress: %v", err) - } - defer resp.Body.Close() - if resp.StatusCode != http.StatusOK { - t.Errorf("Got non-OK status: %d", resp.StatusCode) - } - - b, err := ioutil.ReadAll(resp.Body) - ri := &types.RuntimeInfo{} - if err := json.Unmarshal(b, ri); err != nil { - t.Fatalf("Unable to parse runtime image's response payload: %v", err) + ri := RuntimeRequest(t, client, "http://"+name+".example.com"+path) + if ri == nil { + return } got := ri.Request.Headers.Get(headerName) diff --git a/test/conformance/ingress/percentage_test.go b/test/conformance/ingress/percentage_test.go index 1962934c0be8..d480f7d41534 100644 --- a/test/conformance/ingress/percentage_test.go +++ b/test/conformance/ingress/percentage_test.go @@ -19,17 +19,13 @@ limitations under the License. package ingress import ( - "encoding/json" - "io/ioutil" "math" - "net/http" "testing" "k8s.io/apimachinery/pkg/util/intstr" "knative.dev/serving/pkg/apis/networking" "knative.dev/serving/pkg/apis/networking/v1alpha1" "knative.dev/serving/test" - "knative.dev/serving/test/types" ) // TestPercentage verifies that an Ingress splitting over multiple backends respects @@ -103,22 +99,10 @@ func TestPercentage(t *testing.T) { margin = 5.0 ) for i := 0.0; i < totalRequests; i++ { - // Make a request and check the response. - resp, err := client.Get("http://" + name + ".example.com") - if err != nil { - t.Fatalf("Error making GET request: %v", err) - } - defer resp.Body.Close() - if resp.StatusCode != http.StatusOK { - t.Errorf("Got non-OK status: %d", resp.StatusCode) + ri := RuntimeRequest(t, client, "http://"+name+".example.com") + if ri == nil { continue } - - b, err := ioutil.ReadAll(resp.Body) - ri := &types.RuntimeInfo{} - if err := json.Unmarshal(b, ri); err != nil { - t.Fatalf("Unable to parse runtime image's response payload: %v", err) - } seen[ri.Request.Headers.Get(headerName)] += increment } diff --git a/test/conformance/ingress/timeout_test.go b/test/conformance/ingress/timeout_test.go index 0fb2bb850ac8..3636558a8786 100644 --- a/test/conformance/ingress/timeout_test.go +++ b/test/conformance/ingress/timeout_test.go @@ -106,5 +106,6 @@ func checkTimeout(t *testing.T, client *http.Client, name string, code int, init defer resp.Body.Close() if resp.StatusCode != code { t.Errorf("Unexpected status code: %d, wanted %d", resp.StatusCode, code) + DumpResponse(t, resp) } } diff --git a/test/conformance/ingress/util.go b/test/conformance/ingress/util.go index 20ca986f8665..454c21e9d48a 100644 --- a/test/conformance/ingress/util.go +++ b/test/conformance/ingress/util.go @@ -18,7 +18,9 @@ package ingress import ( "context" + "encoding/json" "errors" + "io/ioutil" "math/rand" "net" "net/http" @@ -35,6 +37,7 @@ import ( "knative.dev/serving/pkg/apis/networking" "knative.dev/serving/pkg/apis/networking/v1alpha1" "knative.dev/serving/test" + "knative.dev/serving/test/types" v1a1test "knative.dev/serving/test/v1alpha1" ) @@ -341,3 +344,37 @@ func CreateDialContext(t *testing.T, ing *v1alpha1.Ingress, clients *test.Client return nil, errors.New("Service ingress does not contain dialing information.") } } + +func RuntimeRequest(t *testing.T, client *http.Client, url string) *types.RuntimeInfo { + t.Helper() + + // Make a request and check the response. + resp, err := client.Get(url) + if err != nil { + t.Errorf("Error making GET request: %v", err) + return nil + } + defer resp.Body.Close() + if resp.StatusCode != http.StatusOK { + t.Errorf("Got non-OK status: %d", resp.StatusCode) + DumpResponse(t, resp) + return nil + } + + b, err := ioutil.ReadAll(resp.Body) + ri := &types.RuntimeInfo{} + if err := json.Unmarshal(b, ri); err != nil { + t.Errorf("Unable to parse runtime image's response payload: %v", err) + return nil + } + return ri +} + +func DumpResponse(t *testing.T, resp *http.Response) { + t.Helper() + + t.Logf("%s %d %s", resp.Proto, resp.StatusCode, resp.Status) + for key, value := range resp.Header { + t.Logf("%s: %v", key, value) + } +}