Skip to content

Commit

Permalink
Share some of the common logic for issuing requests. (#6334)
Browse files Browse the repository at this point in the history
Factors a small library for making a GET request to produce a RuntimeInfo.

Also add a small debug dumping utility on failures, the hope of consolidating some of this is to have a common point for instrumenting as we chase 404s.
  • Loading branch information
mattmoor authored and knative-prow-robot committed Dec 26, 2019
1 parent b0a2410 commit cf768d9
Show file tree
Hide file tree
Showing 6 changed files with 45 additions and 56 deletions.
11 changes: 1 addition & 10 deletions test/conformance/ingress/basic_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@ limitations under the License.
package ingress

import (
"net/http"
"testing"

"k8s.io/apimachinery/pkg/util/intstr"
Expand Down Expand Up @@ -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")
}
11 changes: 1 addition & 10 deletions test/conformance/ingress/hosts_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@ limitations under the License.
package ingress

import (
"net/http"
"testing"

"k8s.io/apimachinery/pkg/util/intstr"
Expand Down Expand Up @@ -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)
}
}
21 changes: 3 additions & 18 deletions test/conformance/ingress/path_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -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)
Expand Down
20 changes: 2 additions & 18 deletions test/conformance/ingress/percentage_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
}

Expand Down
1 change: 1 addition & 0 deletions test/conformance/ingress/timeout_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
}
37 changes: 37 additions & 0 deletions test/conformance/ingress/util.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,9 @@ package ingress

import (
"context"
"encoding/json"
"errors"
"io/ioutil"
"math/rand"
"net"
"net/http"
Expand All @@ -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"
)

Expand Down Expand Up @@ -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)
}
}

0 comments on commit cf768d9

Please sign in to comment.