1/*
2Copyright 2020 The Kubernetes Authors.
3
4Licensed under the Apache License, Version 2.0 (the "License");
5you may not use this file except in compliance with the License.
6You may obtain a copy of the License at
7
8    http://www.apache.org/licenses/LICENSE-2.0
9
10Unless required by applicable law or agreed to in writing, software
11distributed under the License is distributed on an "AS IS" BASIS,
12WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13See the License for the specific language governing permissions and
14limitations under the License.
15*/
16
17package apimachinery
18
19import (
20	"io/ioutil"
21	"net/http"
22	"strings"
23
24	"github.com/onsi/ginkgo"
25	"k8s.io/client-go/rest"
26	"k8s.io/kubernetes/test/e2e/framework"
27)
28
29const (
30	invalidTimeoutMessageExpected = "invalid timeout specified in the request URL"
31)
32
33var _ = SIGDescribe("Server request timeout", func() {
34	f := framework.NewDefaultFramework("request-timeout")
35
36	ginkgo.It("should return HTTP status code 400 if the user specifies an invalid timeout in the request URL", func() {
37		rt := getRoundTripper(f)
38		req := newRequest(f, "invalid")
39
40		response, err := rt.RoundTrip(req)
41		framework.ExpectNoError(err)
42		defer response.Body.Close()
43
44		if response.StatusCode != http.StatusBadRequest {
45			framework.Failf("expected HTTP status code: %d, but got: %d", http.StatusBadRequest, response.StatusCode)
46		}
47
48		messageGot := readBody(response)
49		if !strings.Contains(messageGot, invalidTimeoutMessageExpected) {
50			framework.Failf("expected HTTP status message to contain: %s, but got: %s", invalidTimeoutMessageExpected, messageGot)
51		}
52	})
53
54	ginkgo.It("the request should be served with a default timeout if the specified timeout in the request URL exceeds maximum allowed", func() {
55		rt := getRoundTripper(f)
56		// Choose a timeout that exceeds the default timeout (60s) enforced by the apiserver
57		req := newRequest(f, "3m")
58
59		response, err := rt.RoundTrip(req)
60		framework.ExpectNoError(err)
61		defer response.Body.Close()
62
63		if response.StatusCode != http.StatusOK {
64			framework.Failf("expected HTTP status code: %d, but got: %d", http.StatusOK, response.StatusCode)
65		}
66	})
67
68	ginkgo.It("default timeout should be used if the specified timeout in the request URL is 0s", func() {
69		rt := getRoundTripper(f)
70		req := newRequest(f, "0s")
71
72		response, err := rt.RoundTrip(req)
73		framework.ExpectNoError(err)
74		defer response.Body.Close()
75
76		if response.StatusCode != http.StatusOK {
77			framework.Failf("expected HTTP status code: %d, but got: %d", http.StatusOK, response.StatusCode)
78		}
79	})
80})
81
82func getRoundTripper(f *framework.Framework) http.RoundTripper {
83	config := rest.CopyConfig(f.ClientConfig())
84
85	// ensure we don't enforce any transport timeout from the client side.
86	config.Timeout = 0
87
88	roundTripper, err := rest.TransportFor(config)
89	framework.ExpectNoError(err)
90
91	return roundTripper
92}
93
94func newRequest(f *framework.Framework, timeout string) *http.Request {
95	req, err := http.NewRequest(http.MethodGet, f.ClientSet.CoreV1().RESTClient().Get().
96		Param("timeout", timeout).AbsPath("version").URL().String(), nil)
97	framework.ExpectNoError(err)
98
99	return req
100}
101
102func readBody(response *http.Response) string {
103	raw, err := ioutil.ReadAll(response.Body)
104	framework.ExpectNoError(err)
105
106	return string(raw)
107}
108