Skip to content

Commit

Permalink
Adds a flag to allow customized request headers in conformance tests (#…
Browse files Browse the repository at this point in the history
…12540)

* add flag

* add validation

* add comment

* check empty
  • Loading branch information
yanweiguo authored Feb 3, 2022
1 parent 792260d commit 1d95294
Show file tree
Hide file tree
Showing 16 changed files with 91 additions and 22 deletions.
1 change: 1 addition & 0 deletions test/conformance/api/shared/util.go
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,7 @@ func sendRequests(ctx context.Context, client *spoof.SpoofingClient, url *url.UR
if err != nil {
return err
}
spoof.WithHeader(test.ServingFlags.RequestHeader())(req)

resp, err := client.Do(req)
if err != nil {
Expand Down
3 changes: 2 additions & 1 deletion test/conformance/api/v1/blue_green_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -138,7 +138,8 @@ func TestBlueGreenRoute(t *testing.T) {
spoof.IsStatusOK,
"CheckSuccessfulResponse",
test.ServingFlags.ResolvableDomain,
test.AddRootCAtoTransport(context.Background(), t.Logf, clients, test.ServingFlags.HTTPS)); err != nil {
test.AddRootCAtoTransport(context.Background(), t.Logf, clients, test.ServingFlags.HTTPS),
spoof.WithHeader(test.ServingFlags.RequestHeader())); err != nil {
t.Fatalf("Error probing %s: %v", greenURL, err)
}

Expand Down
3 changes: 2 additions & 1 deletion test/conformance/api/v1/generatename_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,8 @@ func canServeRequests(t *testing.T, clients *test.Clients, route *v1.Route) erro
spoof.MatchesAllOf(spoof.IsStatusOK, spoof.MatchesBody(test.HelloWorldText)),
"CheckEndpointToServeText",
test.ServingFlags.ResolvableDomain,
test.AddRootCAtoTransport(context.Background(), t.Logf, clients, test.ServingFlags.HTTPS))
test.AddRootCAtoTransport(context.Background(), t.Logf, clients, test.ServingFlags.HTTPS),
spoof.WithHeader(test.ServingFlags.RequestHeader()))
if err != nil {
return fmt.Errorf("the endpoint for Route %s at %s didn't serve the expected text %q: %w", route.Name, url, test.HelloWorldText, err)
}
Expand Down
4 changes: 3 additions & 1 deletion test/conformance/api/v1/resources_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,8 @@ func TestCustomResourcesLimits(t *testing.T) {
spoof.MatchesAllOf(spoof.IsStatusOK),
"ResourceTestServesText",
test.ServingFlags.ResolvableDomain,
test.AddRootCAtoTransport(context.Background(), t.Logf, clients, test.ServingFlags.HTTPS))
test.AddRootCAtoTransport(context.Background(), t.Logf, clients, test.ServingFlags.HTTPS),
spoof.WithHeader(test.ServingFlags.RequestHeader()))
if err != nil {
t.Fatalf("Error probing %s: %v", endpoint, err)
}
Expand All @@ -88,6 +89,7 @@ func TestCustomResourcesLimits(t *testing.T) {
if err != nil {
return nil, err
}
spoof.WithHeader(test.ServingFlags.RequestHeader())(req)
return client.Do(req)
}

Expand Down
4 changes: 3 additions & 1 deletion test/conformance/api/v1/revision_timeout_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@ func sendRequest(t *testing.T, clients *test.Clients, endpoint *url.URL,
if err != nil {
return fmt.Errorf("failed to create new HTTP request: %w", err)
}
spoof.WithHeader(test.ServingFlags.RequestHeader())(req)

resp, err := client.Do(req)
if err != nil {
Expand Down Expand Up @@ -130,7 +131,8 @@ func TestRevisionTimeout(t *testing.T) {
spoof.IsOneOfStatusCodes(http.StatusOK, http.StatusGatewayTimeout),
"CheckSuccessfulResponse",
test.ServingFlags.ResolvableDomain,
test.AddRootCAtoTransport(context.Background(), t.Logf, clients, test.ServingFlags.HTTPS)); err != nil {
test.AddRootCAtoTransport(context.Background(), t.Logf, clients, test.ServingFlags.HTTPS),
spoof.WithHeader(test.ServingFlags.RequestHeader())); err != nil {
t.Fatalf("Error probing %s: %v", serviceURL, err)
}

Expand Down
3 changes: 2 additions & 1 deletion test/conformance/api/v1/route_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,8 @@ func assertResourcesUpdatedWhenRevisionIsReady(t *testing.T, clients *test.Clien
spoof.MatchesAllOf(spoof.IsStatusOK, spoof.MatchesBody(expectedText)),
"CheckEndpointToServeText",
test.ServingFlags.ResolvableDomain,
test.AddRootCAtoTransport(context.Background(), t.Logf, clients, test.ServingFlags.HTTPS))
test.AddRootCAtoTransport(context.Background(), t.Logf, clients, test.ServingFlags.HTTPS),
spoof.WithHeader(test.ServingFlags.RequestHeader()))
if err != nil {
t.Fatalf("The endpoint for Route %s at %s didn't serve the expected text %q: %v", names.Route, url, expectedText, err)
}
Expand Down
14 changes: 12 additions & 2 deletions test/conformance/api/v1/service_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ import (
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"knative.dev/pkg/ptr"
pkgtest "knative.dev/pkg/test"
"knative.dev/pkg/test/spoof"
v1 "knative.dev/serving/pkg/apis/serving/v1"
"knative.dev/serving/test"
v1test "knative.dev/serving/test/v1"
Expand Down Expand Up @@ -130,7 +131,11 @@ func TestServiceCreateAndUpdate(t *testing.T) {
}

// We start a background prober to test if Route is always healthy even during Route update.
prober := test.RunRouteProber(t.Logf, clients, names.URL, test.AddRootCAtoTransport(context.Background(), t.Logf, clients, test.ServingFlags.HTTPS))
prober := test.RunRouteProber(
t.Logf,
clients, names.URL,
test.AddRootCAtoTransport(context.Background(), t.Logf, clients, test.ServingFlags.HTTPS),
spoof.WithHeader(test.ServingFlags.RequestHeader()))
defer test.AssertProberDefault(t, prober)

// Update Container Image
Expand Down Expand Up @@ -287,7 +292,12 @@ func TestServiceBYOName(t *testing.T) {
}

// We start a background prober to test if Route is always healthy even during Route update.
prober := test.RunRouteProber(t.Logf, clients, names.URL, test.AddRootCAtoTransport(context.Background(), t.Logf, clients, test.ServingFlags.HTTPS))
prober := test.RunRouteProber(
t.Logf,
clients,
names.URL,
test.AddRootCAtoTransport(context.Background(), t.Logf, clients, test.ServingFlags.HTTPS),
spoof.WithHeader(test.ServingFlags.RequestHeader()))
defer test.AssertProberDefault(t, prober)

// Update Container Image
Expand Down
4 changes: 3 additions & 1 deletion test/conformance/api/v1/single_threaded_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,8 @@ func TestSingleConcurrency(t *testing.T) {
spoof.IsStatusOK,
"CheckSuccessfulResponse",
test.ServingFlags.ResolvableDomain,
test.AddRootCAtoTransport(context.Background(), t.Logf, clients, test.ServingFlags.HTTPS)); err != nil {
test.AddRootCAtoTransport(context.Background(), t.Logf, clients, test.ServingFlags.HTTPS),
spoof.WithHeader(test.ServingFlags.RequestHeader())); err != nil {
t.Fatalf("Error probing %s: %v", url, err)
}

Expand All @@ -94,6 +95,7 @@ func TestSingleConcurrency(t *testing.T) {
if err != nil {
return fmt.Errorf("error creating http request: %w", err)
}
spoof.WithHeader(test.ServingFlags.RequestHeader())(req)

for {
select {
Expand Down
4 changes: 3 additions & 1 deletion test/conformance/api/v1/util.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ func checkForExpectedResponses(ctx context.Context, t testing.TB, clients *test.
if err != nil {
return err
}
spoof.WithHeader(test.ServingFlags.RequestHeader())(req)
_, err = client.Poll(req, spoof.MatchesAllOf(spoof.IsStatusOK, spoof.MatchesAllBodies(expectedResponses...)))
return err
}
Expand Down Expand Up @@ -136,7 +137,8 @@ func validateDataPlane(t testing.TB, clients *test.Clients, names test.ResourceN
spoof.MatchesAllOf(spoof.IsStatusOK, spoof.MatchesBody(expectedText)),
"WaitForEndpointToServeText",
test.ServingFlags.ResolvableDomain,
test.AddRootCAtoTransport(context.Background(), t.Logf, clients, test.ServingFlags.HTTPS))
test.AddRootCAtoTransport(context.Background(), t.Logf, clients, test.ServingFlags.HTTPS),
spoof.WithHeader(test.ServingFlags.RequestHeader()))
if err != nil {
return fmt.Errorf("the endpoint for Route %s at %s didn't serve the expected text %q: %w", names.Route, names.URL, expectedText, err)
}
Expand Down
3 changes: 2 additions & 1 deletion test/conformance/api/v1/volumes_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -480,7 +480,8 @@ func TestProjectedServiceAccountToken(t *testing.T) {
spoof.MatchesAllOf(spoof.IsStatusOK, isJWT),
"CheckEndpointToServeTheToken",
test.ServingFlags.ResolvableDomain,
test.AddRootCAtoTransport(context.Background(), t.Logf, clients, test.ServingFlags.HTTPS)); err != nil {
test.AddRootCAtoTransport(context.Background(), t.Logf, clients, test.ServingFlags.HTTPS),
spoof.WithHeader(test.ServingFlags.RequestHeader())); err != nil {
t.Error(err)
}
}
Expand Down
9 changes: 6 additions & 3 deletions test/conformance/api/v1alpha1/domain_mapping_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,8 @@ func TestDomainMapping(t *testing.T) {
spoof.MatchesAllOf(spoof.IsStatusOK, spoof.MatchesBody(test.PizzaPlanetText1)),
"CheckSuccessfulResponse",
resolvableCustomDomain,
test.AddRootCAtoTransport(context.Background(), t.Logf, clients, test.ServingFlags.HTTPS)); err != nil {
test.AddRootCAtoTransport(context.Background(), t.Logf, clients, test.ServingFlags.HTTPS),
spoof.WithHeader(test.ServingFlags.RequestHeader())); err != nil {
t.Fatalf("Error probing %s: %v", endpoint, err)
}

Expand Down Expand Up @@ -184,7 +185,8 @@ func TestDomainMapping(t *testing.T) {
spoof.MatchesAllOf(spoof.IsStatusOK, spoof.MatchesBody(test.PizzaPlanetText1)),
"CheckSuccessfulResponse",
resolvableCustomDomain,
test.AddRootCAtoTransport(context.Background(), t.Logf, clients, test.ServingFlags.HTTPS)); err != nil {
test.AddRootCAtoTransport(context.Background(), t.Logf, clients, test.ServingFlags.HTTPS),
spoof.WithHeader(test.ServingFlags.RequestHeader())); err != nil {
t.Fatalf("Error probing %s: %v", endpoint, err)
}

Expand Down Expand Up @@ -217,7 +219,8 @@ func TestDomainMapping(t *testing.T) {
spoof.MatchesAllOf(spoof.IsStatusOK, spoof.MatchesBody(test.PizzaPlanetText2)),
"CheckSuccessfulResponse",
resolvableCustomDomain,
test.AddRootCAtoTransport(context.Background(), t.Logf, clients, test.ServingFlags.HTTPS)); err != nil {
test.AddRootCAtoTransport(context.Background(), t.Logf, clients, test.ServingFlags.HTTPS),
spoof.WithHeader(test.ServingFlags.RequestHeader())); err != nil {
t.Fatalf("Error probing %s: %v", endpoint, err)
}
}
9 changes: 6 additions & 3 deletions test/conformance/api/v1beta1/domain_mapping_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,8 @@ func TestDomainMapping(t *testing.T) {
spoof.MatchesAllOf(spoof.IsStatusOK, spoof.MatchesBody(test.PizzaPlanetText1)),
"CheckSuccessfulResponse",
resolvableCustomDomain,
test.AddRootCAtoTransport(context.Background(), t.Logf, clients, test.ServingFlags.HTTPS)); err != nil {
test.AddRootCAtoTransport(context.Background(), t.Logf, clients, test.ServingFlags.HTTPS),
spoof.WithHeader(test.ServingFlags.RequestHeader())); err != nil {
t.Fatalf("Error probing %s: %v", endpoint, err)
}

Expand Down Expand Up @@ -184,7 +185,8 @@ func TestDomainMapping(t *testing.T) {
spoof.MatchesAllOf(spoof.IsStatusOK, spoof.MatchesBody(test.PizzaPlanetText1)),
"CheckSuccessfulResponse",
resolvableCustomDomain,
test.AddRootCAtoTransport(context.Background(), t.Logf, clients, test.ServingFlags.HTTPS)); err != nil {
test.AddRootCAtoTransport(context.Background(), t.Logf, clients, test.ServingFlags.HTTPS),
spoof.WithHeader(test.ServingFlags.RequestHeader())); err != nil {
t.Fatalf("Error probing %s: %v", endpoint, err)
}

Expand Down Expand Up @@ -217,7 +219,8 @@ func TestDomainMapping(t *testing.T) {
spoof.MatchesAllOf(spoof.IsStatusOK, spoof.MatchesBody(test.PizzaPlanetText2)),
"CheckSuccessfulResponse",
resolvableCustomDomain,
test.AddRootCAtoTransport(context.Background(), t.Logf, clients, test.ServingFlags.HTTPS)); err != nil {
test.AddRootCAtoTransport(context.Background(), t.Logf, clients, test.ServingFlags.HTTPS),
spoof.WithHeader(test.ServingFlags.RequestHeader())); err != nil {
t.Fatalf("Error probing %s: %v", endpoint, err)
}
}
2 changes: 2 additions & 0 deletions test/conformance/runtime/readiness_probe_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -135,6 +135,7 @@ func TestProbeRuntime(t *testing.T) {
"readinessIsReady",
test.ServingFlags.ResolvableDomain,
test.AddRootCAtoTransport(context.Background(), t.Logf, clients, test.ServingFlags.HTTPS),
spoof.WithHeader(test.ServingFlags.RequestHeader()),
); err != nil {
t.Fatalf("The endpoint for Route %s at %s didn't return success: %v", names.Route, url, err)
}
Expand Down Expand Up @@ -240,6 +241,7 @@ func waitReadyThenStartFailing(t *testing.T, clients *test.Clients, names test.R
"readinessIsReady",
test.ServingFlags.ResolvableDomain,
test.AddRootCAtoTransport(context.Background(), t.Logf, clients, test.ServingFlags.HTTPS),
spoof.WithHeader(test.ServingFlags.RequestHeader()),
); err != nil {
t.Fatalf("The endpoint for Route %s at %s didn't return success: %v", names.Route, url, err)
}
Expand Down
2 changes: 1 addition & 1 deletion test/conformance/runtime/util.go
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ func fetchRuntimeInfo(
spoof.IsStatusOK,
"RuntimeInfo",
test.ServingFlags.ResolvableDomain,
append(reqOpts, test.AddRootCAtoTransport(context.Background(), t.Logf, clients, test.ServingFlags.HTTPS))...)
append(reqOpts, spoof.WithHeader(test.ServingFlags.RequestHeader()), test.AddRootCAtoTransport(context.Background(), t.Logf, clients, test.ServingFlags.HTTPS))...)
if err != nil {
return nil, nil, err
}
Expand Down
26 changes: 25 additions & 1 deletion test/e2e_flags.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@ package test

import (
"flag"
"net/http"
"strings"

// Load the generic flags of knative.dev/pkg too.
_ "knative.dev/pkg/test"
Expand All @@ -45,7 +47,7 @@ type ServingEnvironmentFlags struct {
AltTestNamespace string // Alternative namespace for running cross-namespace tests in
TLSTestNamespace string // Namespace for Serving TLS tests
ExceedingMemoryLimitSize int // Memory size used to trigger a non-200 response when the service is set with 300MB memory limit.

RequestHeaders string // Extra HTTP request headers sent to the testing deployed KServices.
}

func initializeServingFlags() *ServingEnvironmentFlags {
Expand Down Expand Up @@ -93,5 +95,27 @@ func initializeServingFlags() *ServingEnvironmentFlags {
"Set this flag to the MB of memory consumed by your service in resource limit tests. "+
"You service is set with 300 MB memory limit and shoud return a non-200 response when consuming such amount of memory.")

flag.StringVar(&f.RequestHeaders, "request-headers", "",
"Set this flag to add extra HTTP request headers sent to the testing deployed KServices. "+
"Format: -request-headers=key1,value1,key2,value2")

return &f
}

// RequestHeader returns a http.Header object including key-value header pairs passed via testing flag.
func (f *ServingEnvironmentFlags) RequestHeader() http.Header {
header := make(http.Header)

if f.RequestHeaders != "" {
headers := strings.Split(f.RequestHeaders, ",")
if len(headers)%2 != 0 {
panic("incorrect input of request headers: " + f.RequestHeaders)
}

for i := 0; i < len(headers); i += 2 {
header.Add(headers[i], headers[i+1])
}
}

return header
}
22 changes: 18 additions & 4 deletions test/prober.go
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,7 @@ type manager struct {
m sync.RWMutex
probes map[*url.URL]Prober
transportOptions []spoof.TransportOption
requestOptions []spoof.RequestOption
}

var _ ProberManager = (*manager)(nil)
Expand Down Expand Up @@ -149,6 +150,9 @@ func (m *manager) Spawn(url *url.URL) Prober {
if err != nil {
return fmt.Errorf("failed to generate request: %w", err)
}
for _, o := range m.requestOptions {
o(req)
}

// We keep polling the domain and accumulate success rates
// to ultimately establish the SLI and compare to the SLO.
Expand Down Expand Up @@ -212,18 +216,28 @@ func (m *manager) Foreach(f func(url *url.URL, p Prober)) {
}

// NewProberManager creates a new manager for probes.
func NewProberManager(logf logging.FormatLogger, clients *Clients, minProbes int64, opts ...spoof.TransportOption) ProberManager {
return &manager{
func NewProberManager(logf logging.FormatLogger, clients *Clients, minProbes int64, opts ...interface{}) ProberManager {
m := manager{
logf: logf,
clients: clients,
minProbes: minProbes,
probes: make(map[*url.URL]Prober),
transportOptions: opts,
transportOptions: []spoof.TransportOption{},
requestOptions: []spoof.RequestOption{},
}
for _, opt := range opts {
switch o := opt.(type) {
case spoof.RequestOption:
m.requestOptions = append(m.requestOptions, o)
case spoof.TransportOption:
m.transportOptions = append(m.transportOptions, o)
}
}
return &m
}

// RunRouteProber starts a single Prober of the given domain.
func RunRouteProber(logf logging.FormatLogger, clients *Clients, url *url.URL, opts ...spoof.TransportOption) Prober {
func RunRouteProber(logf logging.FormatLogger, clients *Clients, url *url.URL, opts ...interface{}) Prober {
// Default to 10 probes
pm := NewProberManager(logf, clients, 10, opts...)
pm.Spawn(url)
Expand Down

0 comments on commit 1d95294

Please sign in to comment.