From e9e74aa98b0a99f2fc663c976ea965ceb9edbb8f Mon Sep 17 00:00:00 2001 From: Stephen Heywood Date: Mon, 12 Oct 2020 17:19:12 +1300 Subject: [PATCH 1/4] Create e2e test for pod & service ProxyWithPath endpoints --- test/e2e/network/BUILD | 1 + test/e2e/network/proxy.go | 119 ++++++++++++++++++++++++++++++++++++++ 2 files changed, 120 insertions(+) diff --git a/test/e2e/network/BUILD b/test/e2e/network/BUILD index 9d21a4e6679..83c8f4ff5eb 100644 --- a/test/e2e/network/BUILD +++ b/test/e2e/network/BUILD @@ -65,6 +65,7 @@ go_library( "//staging/src/k8s.io/client-go/kubernetes:go_default_library", "//staging/src/k8s.io/client-go/tools/cache:go_default_library", "//staging/src/k8s.io/client-go/tools/watch:go_default_library", + "//staging/src/k8s.io/client-go/transport:go_default_library", "//staging/src/k8s.io/client-go/util/flowcontrol:go_default_library", "//staging/src/k8s.io/client-go/util/retry:go_default_library", "//staging/src/k8s.io/client-go/util/workqueue:go_default_library", diff --git a/test/e2e/network/proxy.go b/test/e2e/network/proxy.go index 24dadf0c473..77067a09a76 100644 --- a/test/e2e/network/proxy.go +++ b/test/e2e/network/proxy.go @@ -32,7 +32,9 @@ import ( metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" "k8s.io/apimachinery/pkg/util/intstr" "k8s.io/apimachinery/pkg/util/net" + "k8s.io/apimachinery/pkg/util/wait" clientset "k8s.io/client-go/kubernetes" + "k8s.io/client-go/transport" "k8s.io/kubernetes/test/e2e/framework" e2enode "k8s.io/kubernetes/test/e2e/framework/node" e2erc "k8s.io/kubernetes/test/e2e/framework/rc" @@ -52,6 +54,8 @@ const ( // We have seen one of these calls take just over 15 seconds, so putting this at 30. proxyHTTPCallTimeout = 30 * time.Second + podRetryPeriod = 1 * time.Second + podRetryTimeout = 1 * time.Minute ) var _ = SIGDescribe("Proxy", func() { @@ -258,9 +262,124 @@ var _ = SIGDescribe("Proxy", func() { framework.Failf(strings.Join(errs, "\n")) } }) + + ginkgo.It("A set of valid responses are returned for both pod and service ProxyWithPath", func() { + + ns := f.Namespace.Name + + framework.Logf("Creating pod...") + _, err := f.ClientSet.CoreV1().Pods(ns).Create(context.TODO(), &v1.Pod{ + ObjectMeta: metav1.ObjectMeta{ + Name: "agnhost", + Labels: map[string]string{ + "test": "response"}, + }, + Spec: v1.PodSpec{ + Containers: []v1.Container{{ + Image: imageutils.GetE2EImage(imageutils.Agnhost), + Name: "agnhost", + Command: []string{"/agnhost", "porter"}, + Env: []v1.EnvVar{{ + Name: "SERVE_PORT_80", + Value: "foo", + }}, + }}, + RestartPolicy: v1.RestartPolicyNever, + }}, metav1.CreateOptions{}) + framework.ExpectNoError(err, "failed to create pod") + + err = wait.PollImmediate(podRetryPeriod, podRetryTimeout, checkPodStatus(f, "test=response")) + framework.ExpectNoError(err, "Pod didn't start within time out period") + + framework.Logf("Creating service...") + _, err = f.ClientSet.CoreV1().Services(ns).Create(context.TODO(), &v1.Service{ + ObjectMeta: metav1.ObjectMeta{ + Name: "test-service", + Namespace: ns, + Labels: map[string]string{ + "test": "response", + }, + }, + Spec: v1.ServiceSpec{ + Ports: []v1.ServicePort{{ + Port: 80, + TargetPort: intstr.FromInt(80), + Protocol: v1.ProtocolTCP, + }}, + Selector: map[string]string{ + "test": "response", + }, + }}, metav1.CreateOptions{}) + framework.ExpectNoError(err, "Failed to create the service") + + transportCfg, err := f.ClientConfig().TransportConfig() + framework.ExpectNoError(err, "Error creating transportCfg") + restTransport, err := transport.New(transportCfg) + framework.ExpectNoError(err, "Error creating restTransport") + + client := &http.Client{ + CheckRedirect: func(req *http.Request, via []*http.Request) error { + return http.ErrUseLastResponse + }, + Transport: restTransport, + } + + // All methods for Pod ProxyWithPath return 200 + httpVerbs := []string{"DELETE", "GET", "HEAD", "OPTIONS", "PATCH", "POST", "PUT"} + for _, httpVerb := range httpVerbs { + + urlString := f.ClientConfig().Host + "/api/v1/namespaces/" + ns + "/pods/agnhost/proxy/some/path/with/" + httpVerb + framework.Logf("Starting http.Client for %s", urlString) + request, err := http.NewRequest(httpVerb, urlString, nil) + framework.ExpectNoError(err, "processing request") + + resp, err := client.Do(request) + framework.ExpectNoError(err, "processing response") + defer resp.Body.Close() + + framework.Logf("http.Client request:%s StatusCode:%d", httpVerb, resp.StatusCode) + framework.ExpectEqual(resp.StatusCode, 200, "The resp.StatusCode returned: %d", resp.StatusCode) + } + + // All methods for Service ProxyWithPath return 200 + for _, httpVerb := range httpVerbs { + + urlString := f.ClientConfig().Host + "/api/v1/namespaces/" + ns + "/services/test-service/proxy/some/path/with/" + httpVerb + framework.Logf("Starting http.Client for %s", urlString) + request, err := http.NewRequest(httpVerb, urlString, nil) + framework.ExpectNoError(err, "processing request") + + resp, err := client.Do(request) + framework.ExpectNoError(err, "processing response") + defer resp.Body.Close() + + framework.Logf("http.Client request:%s StatusCode:%d", httpVerb, resp.StatusCode) + framework.ExpectEqual(resp.StatusCode, 200, "The resp.StatusCode returned: %d", resp.StatusCode) + } + }) }) }) +func checkPodStatus(f *framework.Framework, label string) func() (bool, error) { + return func() (bool, error) { + var err error + + list, err := f.ClientSet.CoreV1().Pods(f.Namespace.Name).List(context.TODO(), metav1.ListOptions{ + LabelSelector: label}) + + if err != nil { + return false, err + } + + if list.Items[0].Status.Phase != "Running" { + framework.Logf("Pod Quantity: %d Status: %s", len(list.Items), list.Items[0].Status.Phase) + return false, err + } + framework.Logf("Pod Status: %v", list.Items[0].Status.Phase) + return true, nil + } +} + func doProxy(f *framework.Framework, path string, i int) (body []byte, statusCode int, d time.Duration, err error) { // About all of the proxy accesses in this file: // * AbsPath is used because it preserves the trailing '/'. From 226f7c28e00b9cfa267a7834e383c5dfd3837cb8 Mon Sep 17 00:00:00 2001 From: Stephen Heywood Date: Thu, 15 Oct 2020 14:02:53 +1300 Subject: [PATCH 2/4] Response body checked for all requests other than http HEAD --- test/e2e/network/proxy.go | 25 +++++++++++++++++++++++-- 1 file changed, 23 insertions(+), 2 deletions(-) diff --git a/test/e2e/network/proxy.go b/test/e2e/network/proxy.go index 77067a09a76..1644dcbe4f9 100644 --- a/test/e2e/network/proxy.go +++ b/test/e2e/network/proxy.go @@ -19,6 +19,7 @@ limitations under the License. package network import ( + "bytes" "context" "fmt" "math" @@ -266,6 +267,7 @@ var _ = SIGDescribe("Proxy", func() { ginkgo.It("A set of valid responses are returned for both pod and service ProxyWithPath", func() { ns := f.Namespace.Name + msg := "foo" framework.Logf("Creating pod...") _, err := f.ClientSet.CoreV1().Pods(ns).Create(context.TODO(), &v1.Pod{ @@ -281,7 +283,7 @@ var _ = SIGDescribe("Proxy", func() { Command: []string{"/agnhost", "porter"}, Env: []v1.EnvVar{{ Name: "SERVE_PORT_80", - Value: "foo", + Value: msg, }}, }}, RestartPolicy: v1.RestartPolicyNever, @@ -325,6 +327,7 @@ var _ = SIGDescribe("Proxy", func() { } // All methods for Pod ProxyWithPath return 200 + // response body returns 'foo' for all methods but HEAD httpVerbs := []string{"DELETE", "GET", "HEAD", "OPTIONS", "PATCH", "POST", "PUT"} for _, httpVerb := range httpVerbs { @@ -337,11 +340,21 @@ var _ = SIGDescribe("Proxy", func() { framework.ExpectNoError(err, "processing response") defer resp.Body.Close() + buf := new(bytes.Buffer) + buf.ReadFrom(resp.Body) + response := buf.String() + framework.Logf("http.Client request:%s StatusCode:%d", httpVerb, resp.StatusCode) + framework.Logf("http.Client request:%s | StatusCode:%d | Response: %s", httpVerb, resp.StatusCode, response) framework.ExpectEqual(resp.StatusCode, 200, "The resp.StatusCode returned: %d", resp.StatusCode) + + if httpVerb != "HEAD" { + framework.ExpectEqual(response, msg, "The resp.Body returned: %v", resp.Body) + } } // All methods for Service ProxyWithPath return 200 + // response body returns 'foo' for all methods but HEAD for _, httpVerb := range httpVerbs { urlString := f.ClientConfig().Host + "/api/v1/namespaces/" + ns + "/services/test-service/proxy/some/path/with/" + httpVerb @@ -353,8 +366,16 @@ var _ = SIGDescribe("Proxy", func() { framework.ExpectNoError(err, "processing response") defer resp.Body.Close() - framework.Logf("http.Client request:%s StatusCode:%d", httpVerb, resp.StatusCode) + buf := new(bytes.Buffer) + buf.ReadFrom(resp.Body) + response := buf.String() + + framework.Logf("http.Client request:%s | StatusCode:%d | Response: %s", httpVerb, resp.StatusCode, response) framework.ExpectEqual(resp.StatusCode, 200, "The resp.StatusCode returned: %d", resp.StatusCode) + + if httpVerb != "HEAD" { + framework.ExpectEqual(response, msg, "The resp.Body returned: %v", resp.Body) + } } }) }) From e29a3f21bc322793b9b77a20fed5f5463c42f25d Mon Sep 17 00:00:00 2001 From: Stephen Heywood Date: Tue, 20 Oct 2020 11:57:45 +1300 Subject: [PATCH 3/4] Remove obsolete log message --- test/e2e/network/proxy.go | 1 - 1 file changed, 1 deletion(-) diff --git a/test/e2e/network/proxy.go b/test/e2e/network/proxy.go index 1644dcbe4f9..c43b307385b 100644 --- a/test/e2e/network/proxy.go +++ b/test/e2e/network/proxy.go @@ -344,7 +344,6 @@ var _ = SIGDescribe("Proxy", func() { buf.ReadFrom(resp.Body) response := buf.String() - framework.Logf("http.Client request:%s StatusCode:%d", httpVerb, resp.StatusCode) framework.Logf("http.Client request:%s | StatusCode:%d | Response: %s", httpVerb, resp.StatusCode, response) framework.ExpectEqual(resp.StatusCode, 200, "The resp.StatusCode returned: %d", resp.StatusCode) From 6c1f3b7d5dfe6f3c3577ac89e425a529495c6953 Mon Sep 17 00:00:00 2001 From: Stephen Heywood Date: Mon, 18 Jan 2021 12:52:15 +1300 Subject: [PATCH 4/4] Use json response to validate method sent via proxy --- test/e2e/network/proxy.go | 46 +++++++++++++++++++++++++++++---------- 1 file changed, 35 insertions(+), 11 deletions(-) diff --git a/test/e2e/network/proxy.go b/test/e2e/network/proxy.go index c43b307385b..61ec1671226 100644 --- a/test/e2e/network/proxy.go +++ b/test/e2e/network/proxy.go @@ -21,6 +21,7 @@ package network import ( "bytes" "context" + "encoding/json" "fmt" "math" "net/http" @@ -59,6 +60,11 @@ const ( podRetryTimeout = 1 * time.Minute ) +type jsonResponse struct { + Method string + Body string +} + var _ = SIGDescribe("Proxy", func() { version := "v1" ginkgo.Context("version "+version, func() { @@ -280,7 +286,7 @@ var _ = SIGDescribe("Proxy", func() { Containers: []v1.Container{{ Image: imageutils.GetE2EImage(imageutils.Agnhost), Name: "agnhost", - Command: []string{"/agnhost", "porter"}, + Command: []string{"/agnhost", "porter", "--json-response"}, Env: []v1.EnvVar{{ Name: "SERVE_PORT_80", Value: msg, @@ -327,7 +333,7 @@ var _ = SIGDescribe("Proxy", func() { } // All methods for Pod ProxyWithPath return 200 - // response body returns 'foo' for all methods but HEAD + // For all methods other than HEAD the response body returns 'foo' with the received http method httpVerbs := []string{"DELETE", "GET", "HEAD", "OPTIONS", "PATCH", "POST", "PUT"} for _, httpVerb := range httpVerbs { @@ -344,16 +350,25 @@ var _ = SIGDescribe("Proxy", func() { buf.ReadFrom(resp.Body) response := buf.String() - framework.Logf("http.Client request:%s | StatusCode:%d | Response: %s", httpVerb, resp.StatusCode, response) - framework.ExpectEqual(resp.StatusCode, 200, "The resp.StatusCode returned: %d", resp.StatusCode) + switch httpVerb { + case "HEAD": + framework.Logf("http.Client request:%s | StatusCode:%d", httpVerb, resp.StatusCode) + framework.ExpectEqual(resp.StatusCode, 200, "The resp.StatusCode returned: %d", resp.StatusCode) + default: + var jr *jsonResponse + err = json.Unmarshal([]byte(response), &jr) + framework.ExpectNoError(err, "Failed to process jsonResponse: %v | err: %v ", buf.String(), err) - if httpVerb != "HEAD" { - framework.ExpectEqual(response, msg, "The resp.Body returned: %v", resp.Body) + framework.Logf("http.Client request:%s | StatusCode:%d | Response: %s | Method: %s", httpVerb, resp.StatusCode, jr.Body, jr.Method) + framework.ExpectEqual(resp.StatusCode, 200, "The resp.StatusCode returned: %d", resp.StatusCode) + + framework.ExpectEqual(msg, jr.Body, "The resp.Body returned: %v", jr.Body) + framework.ExpectEqual(httpVerb, jr.Method, "The resp.Body returned: %v", jr.Body) } } // All methods for Service ProxyWithPath return 200 - // response body returns 'foo' for all methods but HEAD + // For all methods other than HEAD the response body returns 'foo' with the received http method for _, httpVerb := range httpVerbs { urlString := f.ClientConfig().Host + "/api/v1/namespaces/" + ns + "/services/test-service/proxy/some/path/with/" + httpVerb @@ -369,11 +384,20 @@ var _ = SIGDescribe("Proxy", func() { buf.ReadFrom(resp.Body) response := buf.String() - framework.Logf("http.Client request:%s | StatusCode:%d | Response: %s", httpVerb, resp.StatusCode, response) - framework.ExpectEqual(resp.StatusCode, 200, "The resp.StatusCode returned: %d", resp.StatusCode) + switch httpVerb { + case "HEAD": + framework.Logf("http.Client request:%s | StatusCode:%d", httpVerb, resp.StatusCode) + framework.ExpectEqual(resp.StatusCode, 200, "The resp.StatusCode returned: %d", resp.StatusCode) + default: + var jr *jsonResponse + err = json.Unmarshal([]byte(response), &jr) + framework.ExpectNoError(err, "Failed to process jsonResponse: %v | err: %v ", buf.String(), err) - if httpVerb != "HEAD" { - framework.ExpectEqual(response, msg, "The resp.Body returned: %v", resp.Body) + framework.Logf("http.Client request:%s | StatusCode:%d | Response: %s | Method: %s", httpVerb, resp.StatusCode, jr.Body, jr.Method) + framework.ExpectEqual(resp.StatusCode, 200, "The resp.StatusCode returned: %d", resp.StatusCode) + + framework.ExpectEqual(msg, jr.Body, "The resp.Body returned: %v", jr.Body) + framework.ExpectEqual(httpVerb, jr.Method, "The resp.Body returned: %v", jr.Body) } } })