From 4a4d19541939f13e3bdd5554872fd4a1430768d0 Mon Sep 17 00:00:00 2001 From: Kris Date: Tue, 10 Nov 2015 13:40:51 -0800 Subject: [PATCH 1/2] Exposing http.Client for configurable timeouts --- pkg/client/unversioned/fake/fake.go | 14 +++++--- .../unversioned/helper_blackbox_test.go | 7 ++-- pkg/client/unversioned/restclient.go | 6 +++- pkg/kubectl/cmd/apply_test.go | 3 +- pkg/kubectl/resource/helper_test.go | 36 ++++++++----------- test/e2e/monitoring.go | 7 +--- 6 files changed, 35 insertions(+), 38 deletions(-) diff --git a/pkg/client/unversioned/fake/fake.go b/pkg/client/unversioned/fake/fake.go index 0499122226f..7afa5f86317 100644 --- a/pkg/client/unversioned/fake/fake.go +++ b/pkg/client/unversioned/fake/fake.go @@ -28,15 +28,21 @@ import ( "k8s.io/kubernetes/pkg/runtime" ) -type HTTPClientFunc func(*http.Request) (*http.Response, error) +func HTTPClientFunc(f func(*http.Request) (*http.Response, error)) *http.Client { + return &http.Client{ + Transport: roundTripperFunc(f), + } +} -func (f HTTPClientFunc) Do(req *http.Request) (*http.Response, error) { +type roundTripperFunc func(*http.Request) (*http.Response, error) + +func (f roundTripperFunc) RoundTrip(req *http.Request) (*http.Response, error) { return f(req) } // RESTClient provides a fake RESTClient interface. type RESTClient struct { - Client unversioned.HTTPClient + Client *http.Client Codec runtime.Codec Req *http.Request Resp *http.Response @@ -68,7 +74,7 @@ func (c *RESTClient) Do(req *http.Request) (*http.Response, error) { return nil, c.Err } c.Req = req - if c.Client != unversioned.HTTPClient(nil) { + if c.Client != nil { return c.Client.Do(req) } return c.Resp, nil diff --git a/pkg/client/unversioned/helper_blackbox_test.go b/pkg/client/unversioned/helper_blackbox_test.go index b7aa927d3d4..74706c43a5c 100644 --- a/pkg/client/unversioned/helper_blackbox_test.go +++ b/pkg/client/unversioned/helper_blackbox_test.go @@ -19,7 +19,7 @@ package unversioned_test import ( "bytes" "encoding/json" - "fmt" + "errors" "io" "io/ioutil" "net/http" @@ -41,7 +41,6 @@ func objBody(object interface{}) io.ReadCloser { } func TestNegotiateVersion(t *testing.T) { - refusedErr := fmt.Errorf("connection refused") tests := []struct { name, version, expectedVersion string serverVersions []string @@ -87,8 +86,8 @@ func TestNegotiateVersion(t *testing.T) { config: &unversioned.Config{Version: testapi.Default.Version()}, serverVersions: []string{"version1"}, clientVersions: []string{"version1", testapi.Default.Version()}, - sendErr: refusedErr, - expectErr: func(err error) bool { return err == refusedErr }, + sendErr: errors.New("connection refused"), + expectErr: func(err error) bool { return strings.Contains(err.Error(), "connection refused") }, }, } codec := testapi.Default.Codec() diff --git a/pkg/client/unversioned/restclient.go b/pkg/client/unversioned/restclient.go index d7f064cc76c..6c2881deedb 100644 --- a/pkg/client/unversioned/restclient.go +++ b/pkg/client/unversioned/restclient.go @@ -17,6 +17,7 @@ limitations under the License. package unversioned import ( + "net/http" "net/url" "strings" @@ -43,7 +44,7 @@ type RESTClient struct { // Set specific behavior of the client. If not set http.DefaultClient will be // used. - Client HTTPClient + Client *http.Client // TODO extract this into a wrapper interface via the RESTClient interface in kubectl. Throttle util.RateLimiter @@ -88,6 +89,9 @@ func (c *RESTClient) Verb(verb string) *Request { if c.Throttle != nil { c.Throttle.Accept() } + if c.Client == nil { + return NewRequest(nil, verb, c.baseURL, c.apiVersion, c.Codec) + } return NewRequest(c.Client, verb, c.baseURL, c.apiVersion, c.Codec) } diff --git a/pkg/kubectl/cmd/apply_test.go b/pkg/kubectl/cmd/apply_test.go index 517b93bf7e8..1246cc819bf 100644 --- a/pkg/kubectl/cmd/apply_test.go +++ b/pkg/kubectl/cmd/apply_test.go @@ -28,7 +28,6 @@ import ( "github.com/spf13/cobra" "k8s.io/kubernetes/pkg/api" - "k8s.io/kubernetes/pkg/api/errors" "k8s.io/kubernetes/pkg/client/unversioned/fake" "k8s.io/kubernetes/pkg/kubectl" cmdutil "k8s.io/kubernetes/pkg/kubectl/cmd/util" @@ -220,7 +219,7 @@ func TestApplyNonExistObject(t *testing.T) { Client: fake.HTTPClientFunc(func(req *http.Request) (*http.Response, error) { switch p, m := req.URL.Path, req.Method; { case p == pathNameRC && m == "GET": - return &http.Response{StatusCode: 404}, errors.NewNotFound("ReplicationController", "") + return &http.Response{StatusCode: 404, Body: ioutil.NopCloser(bytes.NewReader(nil))}, nil case p == pathRC && m == "POST": bodyRC := ioutil.NopCloser(bytes.NewReader(currentRC)) return &http.Response{StatusCode: 201, Body: bodyRC}, nil diff --git a/pkg/kubectl/resource/helper_test.go b/pkg/kubectl/resource/helper_test.go index 3616ecf1a9a..25ce8de4258 100644 --- a/pkg/kubectl/resource/helper_test.go +++ b/pkg/kubectl/resource/helper_test.go @@ -131,11 +131,10 @@ func TestHelperCreate(t *testing.T) { } tests := []struct { - Resp *http.Response - RespFunc fake.HTTPClientFunc - HttpErr error - Modify bool - Object runtime.Object + Resp *http.Response + HttpErr error + Modify bool + Object runtime.Object ExpectObject runtime.Object Err bool @@ -188,9 +187,6 @@ func TestHelperCreate(t *testing.T) { Resp: test.Resp, Err: test.HttpErr, } - if test.RespFunc != nil { - client.Client = test.RespFunc - } modifier := &Helper{ RESTClient: client, Codec: testapi.Default.Codec(), @@ -380,11 +376,11 @@ func TestHelperReplace(t *testing.T) { } tests := []struct { - Resp *http.Response - RespFunc fake.HTTPClientFunc - HttpErr error - Overwrite bool - Object runtime.Object + Resp *http.Response + HTTPClient *http.Client + HttpErr error + Overwrite bool + Object runtime.Object ExpectObject runtime.Object Err bool @@ -421,12 +417,12 @@ func TestHelperReplace(t *testing.T) { Spec: apitesting.DeepEqualSafePodSpec(), }, Overwrite: true, - RespFunc: func(req *http.Request) (*http.Response, error) { + HTTPClient: fake.HTTPClientFunc(func(req *http.Request) (*http.Response, error) { if req.Method == "PUT" { return &http.Response{StatusCode: http.StatusOK, Body: objBody(&unversioned.Status{Status: unversioned.StatusSuccess})}, nil } return &http.Response{StatusCode: http.StatusOK, Body: objBody(&api.Pod{ObjectMeta: api.ObjectMeta{Name: "foo", ResourceVersion: "10"}})}, nil - }, + }), Req: expectPut, }, { @@ -438,12 +434,10 @@ func TestHelperReplace(t *testing.T) { } for i, test := range tests { client := &fake.RESTClient{ - Codec: testapi.Default.Codec(), - Resp: test.Resp, - Err: test.HttpErr, - } - if test.RespFunc != nil { - client.Client = test.RespFunc + Client: test.HTTPClient, + Codec: testapi.Default.Codec(), + Resp: test.Resp, + Err: test.HttpErr, } modifier := &Helper{ RESTClient: client, diff --git a/test/e2e/monitoring.go b/test/e2e/monitoring.go index 54027e9750f..6fffcf65ba9 100644 --- a/test/e2e/monitoring.go +++ b/test/e2e/monitoring.go @@ -18,7 +18,6 @@ package e2e import ( "fmt" - "net/http" "net/url" "time" @@ -133,10 +132,6 @@ func getAllNodesInCluster(c *client.Client) ([]string, error) { } func getInfluxdbClient(c *client.Client) (*influxdb.Client, error) { - kubeMasterHttpClient, ok := c.Client.(*http.Client) - if !ok { - Failf("failed to get master http client") - } proxyUrl := fmt.Sprintf("%s/api/v1/proxy/namespaces/%s/services/%s:api/", getMasterHost(), api.NamespaceSystem, influxdbService) config := &influxdb.ClientConfig{ Host: proxyUrl, @@ -144,7 +139,7 @@ func getInfluxdbClient(c *client.Client) (*influxdb.Client, error) { Username: influxdbUser, Password: influxdbPW, Database: influxdbDatabaseName, - HttpClient: kubeMasterHttpClient, + HttpClient: c.Client, IsSecure: true, } return influxdb.NewClient(config) From eacf65ddce26ad39dfbec682d40ce31e5c675eeb Mon Sep 17 00:00:00 2001 From: Kris Date: Wed, 11 Nov 2015 11:54:58 -0800 Subject: [PATCH 2/2] Renaming HTTPClientFunc to CreateHTTPClient --- pkg/client/unversioned/fake/fake.go | 4 ++-- .../unversioned/helper_blackbox_test.go | 2 +- pkg/kubectl/cmd/annotate_test.go | 6 ++--- pkg/kubectl/cmd/apply_test.go | 6 ++--- pkg/kubectl/cmd/attach_test.go | 6 ++--- pkg/kubectl/cmd/create_test.go | 6 ++--- pkg/kubectl/cmd/delete_test.go | 24 +++++++++---------- pkg/kubectl/cmd/describe_test.go | 2 +- pkg/kubectl/cmd/exec_test.go | 4 ++-- pkg/kubectl/cmd/expose_test.go | 2 +- pkg/kubectl/cmd/get_test.go | 20 ++++++++-------- pkg/kubectl/cmd/label_test.go | 4 ++-- pkg/kubectl/cmd/log_test.go | 2 +- pkg/kubectl/cmd/patch_test.go | 4 ++-- pkg/kubectl/cmd/portforward_test.go | 4 ++-- pkg/kubectl/cmd/replace_test.go | 8 +++---- pkg/kubectl/cmd/run_test.go | 2 +- pkg/kubectl/cmd/util/factory_test.go | 2 +- pkg/kubectl/resource/builder_test.go | 2 +- pkg/kubectl/resource/helper_test.go | 2 +- pkg/kubectl/rolling_updater_test.go | 4 ++-- 21 files changed, 58 insertions(+), 58 deletions(-) diff --git a/pkg/client/unversioned/fake/fake.go b/pkg/client/unversioned/fake/fake.go index 7afa5f86317..b4a867d567a 100644 --- a/pkg/client/unversioned/fake/fake.go +++ b/pkg/client/unversioned/fake/fake.go @@ -28,9 +28,9 @@ import ( "k8s.io/kubernetes/pkg/runtime" ) -func HTTPClientFunc(f func(*http.Request) (*http.Response, error)) *http.Client { +func CreateHTTPClient(roundTripper func(*http.Request) (*http.Response, error)) *http.Client { return &http.Client{ - Transport: roundTripperFunc(f), + Transport: roundTripperFunc(roundTripper), } } diff --git a/pkg/client/unversioned/helper_blackbox_test.go b/pkg/client/unversioned/helper_blackbox_test.go index 74706c43a5c..d449b46ece9 100644 --- a/pkg/client/unversioned/helper_blackbox_test.go +++ b/pkg/client/unversioned/helper_blackbox_test.go @@ -99,7 +99,7 @@ func TestNegotiateVersion(t *testing.T) { StatusCode: 200, Body: objBody(&unversionedapi.APIVersions{Versions: test.serverVersions}), }, - Client: fake.HTTPClientFunc(func(req *http.Request) (*http.Response, error) { + Client: fake.CreateHTTPClient(func(req *http.Request) (*http.Response, error) { if test.sendErr != nil { return nil, test.sendErr } diff --git a/pkg/kubectl/cmd/annotate_test.go b/pkg/kubectl/cmd/annotate_test.go index 5060a4dc4a1..e7597ecc40d 100644 --- a/pkg/kubectl/cmd/annotate_test.go +++ b/pkg/kubectl/cmd/annotate_test.go @@ -423,7 +423,7 @@ func TestAnnotateObject(t *testing.T) { tf.Printer = &testPrinter{} tf.Client = &fake.RESTClient{ Codec: codec, - Client: fake.HTTPClientFunc(func(req *http.Request) (*http.Response, error) { + Client: fake.CreateHTTPClient(func(req *http.Request) (*http.Response, error) { switch req.Method { case "GET": switch req.URL.Path { @@ -470,7 +470,7 @@ func TestAnnotateObjectFromFile(t *testing.T) { tf.Printer = &testPrinter{} tf.Client = &fake.RESTClient{ Codec: codec, - Client: fake.HTTPClientFunc(func(req *http.Request) (*http.Response, error) { + Client: fake.CreateHTTPClient(func(req *http.Request) (*http.Response, error) { switch req.Method { case "GET": switch req.URL.Path { @@ -518,7 +518,7 @@ func TestAnnotateMultipleObjects(t *testing.T) { tf.Printer = &testPrinter{} tf.Client = &fake.RESTClient{ Codec: codec, - Client: fake.HTTPClientFunc(func(req *http.Request) (*http.Response, error) { + Client: fake.CreateHTTPClient(func(req *http.Request) (*http.Response, error) { switch req.Method { case "GET": switch req.URL.Path { diff --git a/pkg/kubectl/cmd/apply_test.go b/pkg/kubectl/cmd/apply_test.go index 1246cc819bf..8773d22cfc0 100644 --- a/pkg/kubectl/cmd/apply_test.go +++ b/pkg/kubectl/cmd/apply_test.go @@ -177,7 +177,7 @@ func TestApplyObject(t *testing.T) { tf.Printer = &testPrinter{} tf.Client = &fake.RESTClient{ Codec: codec, - Client: fake.HTTPClientFunc(func(req *http.Request) (*http.Response, error) { + Client: fake.CreateHTTPClient(func(req *http.Request) (*http.Response, error) { switch p, m := req.URL.Path, req.Method; { case p == pathRC && m == "GET": bodyRC := ioutil.NopCloser(bytes.NewReader(currentRC)) @@ -216,7 +216,7 @@ func TestApplyNonExistObject(t *testing.T) { tf.Printer = &testPrinter{} tf.Client = &fake.RESTClient{ Codec: codec, - Client: fake.HTTPClientFunc(func(req *http.Request) (*http.Response, error) { + Client: fake.CreateHTTPClient(func(req *http.Request) (*http.Response, error) { switch p, m := req.URL.Path, req.Method; { case p == pathNameRC && m == "GET": return &http.Response{StatusCode: 404, Body: ioutil.NopCloser(bytes.NewReader(nil))}, nil @@ -255,7 +255,7 @@ func TestApplyMultipleObject(t *testing.T) { tf.Printer = &testPrinter{} tf.Client = &fake.RESTClient{ Codec: codec, - Client: fake.HTTPClientFunc(func(req *http.Request) (*http.Response, error) { + Client: fake.CreateHTTPClient(func(req *http.Request) (*http.Response, error) { switch p, m := req.URL.Path, req.Method; { case p == pathRC && m == "GET": bodyRC := ioutil.NopCloser(bytes.NewReader(currentRC)) diff --git a/pkg/kubectl/cmd/attach_test.go b/pkg/kubectl/cmd/attach_test.go index 9d8e46283cc..7870ef68f2f 100644 --- a/pkg/kubectl/cmd/attach_test.go +++ b/pkg/kubectl/cmd/attach_test.go @@ -83,7 +83,7 @@ func TestPodAndContainerAttach(t *testing.T) { f, tf, codec := NewAPIFactory() tf.Client = &fake.RESTClient{ Codec: codec, - Client: fake.HTTPClientFunc(func(req *http.Request) (*http.Response, error) { return nil, nil }), + Client: fake.CreateHTTPClient(func(req *http.Request) (*http.Response, error) { return nil, nil }), } tf.Namespace = "test" tf.ClientConfig = &client.Config{} @@ -136,7 +136,7 @@ func TestAttach(t *testing.T) { f, tf, codec := NewAPIFactory() tf.Client = &fake.RESTClient{ Codec: codec, - Client: fake.HTTPClientFunc(func(req *http.Request) (*http.Response, error) { + Client: fake.CreateHTTPClient(func(req *http.Request) (*http.Response, error) { switch p, m := req.URL.Path, req.Method; { case p == test.podPath && m == "GET": body := objBody(codec, test.pod) @@ -214,7 +214,7 @@ func TestAttachWarnings(t *testing.T) { f, tf, codec := NewAPIFactory() tf.Client = &fake.RESTClient{ Codec: codec, - Client: fake.HTTPClientFunc(func(req *http.Request) (*http.Response, error) { + Client: fake.CreateHTTPClient(func(req *http.Request) (*http.Response, error) { switch p, m := req.URL.Path, req.Method; { case p == test.podPath && m == "GET": body := objBody(codec, test.pod) diff --git a/pkg/kubectl/cmd/create_test.go b/pkg/kubectl/cmd/create_test.go index d1f2cfe0f9b..eaa9408bdd7 100644 --- a/pkg/kubectl/cmd/create_test.go +++ b/pkg/kubectl/cmd/create_test.go @@ -44,7 +44,7 @@ func TestCreateObject(t *testing.T) { tf.Printer = &testPrinter{} tf.Client = &fake.RESTClient{ Codec: codec, - Client: fake.HTTPClientFunc(func(req *http.Request) (*http.Response, error) { + Client: fake.CreateHTTPClient(func(req *http.Request) (*http.Response, error) { switch p, m := req.URL.Path, req.Method; { case p == "/namespaces/test/replicationcontrollers" && m == "POST": return &http.Response{StatusCode: 201, Body: objBody(codec, &rc.Items[0])}, nil @@ -75,7 +75,7 @@ func TestCreateMultipleObject(t *testing.T) { tf.Printer = &testPrinter{} tf.Client = &fake.RESTClient{ Codec: codec, - Client: fake.HTTPClientFunc(func(req *http.Request) (*http.Response, error) { + Client: fake.CreateHTTPClient(func(req *http.Request) (*http.Response, error) { switch p, m := req.URL.Path, req.Method; { case p == "/namespaces/test/services" && m == "POST": return &http.Response{StatusCode: 201, Body: objBody(codec, &svc.Items[0])}, nil @@ -110,7 +110,7 @@ func TestCreateDirectory(t *testing.T) { tf.Printer = &testPrinter{} tf.Client = &fake.RESTClient{ Codec: codec, - Client: fake.HTTPClientFunc(func(req *http.Request) (*http.Response, error) { + Client: fake.CreateHTTPClient(func(req *http.Request) (*http.Response, error) { switch p, m := req.URL.Path, req.Method; { case p == "/namespaces/test/services" && m == "POST": return &http.Response{StatusCode: 201, Body: objBody(codec, &svc.Items[0])}, nil diff --git a/pkg/kubectl/cmd/delete_test.go b/pkg/kubectl/cmd/delete_test.go index c74a1c07942..df9687814fc 100644 --- a/pkg/kubectl/cmd/delete_test.go +++ b/pkg/kubectl/cmd/delete_test.go @@ -36,7 +36,7 @@ func TestDeleteObjectByTuple(t *testing.T) { tf.Printer = &testPrinter{} tf.Client = &fake.RESTClient{ Codec: codec, - Client: fake.HTTPClientFunc(func(req *http.Request) (*http.Response, error) { + Client: fake.CreateHTTPClient(func(req *http.Request) (*http.Response, error) { switch p, m := req.URL.Path, req.Method; { case p == "/namespaces/test/replicationcontrollers/redis-master-controller" && m == "DELETE": return &http.Response{StatusCode: 200, Body: objBody(codec, &rc.Items[0])}, nil @@ -68,7 +68,7 @@ func TestDeleteNamedObject(t *testing.T) { tf.Printer = &testPrinter{} tf.Client = &fake.RESTClient{ Codec: codec, - Client: fake.HTTPClientFunc(func(req *http.Request) (*http.Response, error) { + Client: fake.CreateHTTPClient(func(req *http.Request) (*http.Response, error) { switch p, m := req.URL.Path, req.Method; { case p == "/namespaces/test/replicationcontrollers/redis-master-controller" && m == "DELETE": return &http.Response{StatusCode: 200, Body: objBody(codec, &rc.Items[0])}, nil @@ -100,7 +100,7 @@ func TestDeleteObject(t *testing.T) { tf.Printer = &testPrinter{} tf.Client = &fake.RESTClient{ Codec: codec, - Client: fake.HTTPClientFunc(func(req *http.Request) (*http.Response, error) { + Client: fake.CreateHTTPClient(func(req *http.Request) (*http.Response, error) { switch p, m := req.URL.Path, req.Method; { case p == "/namespaces/test/replicationcontrollers/redis-master" && m == "DELETE": return &http.Response{StatusCode: 200, Body: objBody(codec, &rc.Items[0])}, nil @@ -130,7 +130,7 @@ func TestDeleteObjectNotFound(t *testing.T) { tf.Printer = &testPrinter{} tf.Client = &fake.RESTClient{ Codec: codec, - Client: fake.HTTPClientFunc(func(req *http.Request) (*http.Response, error) { + Client: fake.CreateHTTPClient(func(req *http.Request) (*http.Response, error) { switch p, m := req.URL.Path, req.Method; { case p == "/namespaces/test/replicationcontrollers/redis-master" && m == "DELETE": return &http.Response{StatusCode: 404, Body: stringBody("")}, nil @@ -160,7 +160,7 @@ func TestDeleteObjectIgnoreNotFound(t *testing.T) { tf.Printer = &testPrinter{} tf.Client = &fake.RESTClient{ Codec: codec, - Client: fake.HTTPClientFunc(func(req *http.Request) (*http.Response, error) { + Client: fake.CreateHTTPClient(func(req *http.Request) (*http.Response, error) { switch p, m := req.URL.Path, req.Method; { case p == "/namespaces/test/replicationcontrollers/redis-master" && m == "DELETE": return &http.Response{StatusCode: 404, Body: stringBody("")}, nil @@ -197,7 +197,7 @@ func TestDeleteAllNotFound(t *testing.T) { tf.Printer = &testPrinter{} tf.Client = &fake.RESTClient{ Codec: codec, - Client: fake.HTTPClientFunc(func(req *http.Request) (*http.Response, error) { + Client: fake.CreateHTTPClient(func(req *http.Request) (*http.Response, error) { switch p, m := req.URL.Path, req.Method; { case p == "/namespaces/test/services" && m == "GET": return &http.Response{StatusCode: 200, Body: objBody(codec, svc)}, nil @@ -239,7 +239,7 @@ func TestDeleteAllIgnoreNotFound(t *testing.T) { tf.Printer = &testPrinter{} tf.Client = &fake.RESTClient{ Codec: codec, - Client: fake.HTTPClientFunc(func(req *http.Request) (*http.Response, error) { + Client: fake.CreateHTTPClient(func(req *http.Request) (*http.Response, error) { switch p, m := req.URL.Path, req.Method; { case p == "/namespaces/test/services" && m == "GET": return &http.Response{StatusCode: 200, Body: objBody(codec, svc)}, nil @@ -274,7 +274,7 @@ func TestDeleteMultipleObject(t *testing.T) { tf.Printer = &testPrinter{} tf.Client = &fake.RESTClient{ Codec: codec, - Client: fake.HTTPClientFunc(func(req *http.Request) (*http.Response, error) { + Client: fake.CreateHTTPClient(func(req *http.Request) (*http.Response, error) { switch p, m := req.URL.Path, req.Method; { case p == "/namespaces/test/replicationcontrollers/redis-master" && m == "DELETE": return &http.Response{StatusCode: 200, Body: objBody(codec, &rc.Items[0])}, nil @@ -308,7 +308,7 @@ func TestDeleteMultipleObjectContinueOnMissing(t *testing.T) { tf.Printer = &testPrinter{} tf.Client = &fake.RESTClient{ Codec: codec, - Client: fake.HTTPClientFunc(func(req *http.Request) (*http.Response, error) { + Client: fake.CreateHTTPClient(func(req *http.Request) (*http.Response, error) { switch p, m := req.URL.Path, req.Method; { case p == "/namespaces/test/replicationcontrollers/redis-master" && m == "DELETE": return &http.Response{StatusCode: 404, Body: stringBody("")}, nil @@ -345,7 +345,7 @@ func TestDeleteMultipleResourcesWithTheSameName(t *testing.T) { tf.Printer = &testPrinter{} tf.Client = &fake.RESTClient{ Codec: codec, - Client: fake.HTTPClientFunc(func(req *http.Request) (*http.Response, error) { + Client: fake.CreateHTTPClient(func(req *http.Request) (*http.Response, error) { switch p, m := req.URL.Path, req.Method; { case p == "/namespaces/test/replicationcontrollers/baz" && m == "DELETE": return &http.Response{StatusCode: 200, Body: objBody(codec, &rc.Items[0])}, nil @@ -381,7 +381,7 @@ func TestDeleteDirectory(t *testing.T) { tf.Printer = &testPrinter{} tf.Client = &fake.RESTClient{ Codec: codec, - Client: fake.HTTPClientFunc(func(req *http.Request) (*http.Response, error) { + Client: fake.CreateHTTPClient(func(req *http.Request) (*http.Response, error) { switch p, m := req.URL.Path, req.Method; { case strings.HasPrefix(p, "/namespaces/test/services/") && m == "DELETE": return &http.Response{StatusCode: 200, Body: objBody(codec, &svc.Items[0])}, nil @@ -414,7 +414,7 @@ func TestDeleteMultipleSelector(t *testing.T) { tf.Printer = &testPrinter{} tf.Client = &fake.RESTClient{ Codec: codec, - Client: fake.HTTPClientFunc(func(req *http.Request) (*http.Response, error) { + Client: fake.CreateHTTPClient(func(req *http.Request) (*http.Response, error) { switch p, m := req.URL.Path, req.Method; { case p == "/namespaces/test/pods" && m == "GET": if req.URL.Query().Get(unversioned.LabelSelectorQueryParam(testapi.Default.Version())) != "a=b" { diff --git a/pkg/kubectl/cmd/describe_test.go b/pkg/kubectl/cmd/describe_test.go index 4c473eb9e82..35a87f2e53c 100644 --- a/pkg/kubectl/cmd/describe_test.go +++ b/pkg/kubectl/cmd/describe_test.go @@ -56,7 +56,7 @@ func TestDescribeObject(t *testing.T) { tf.Describer = d tf.Client = &fake.RESTClient{ Codec: codec, - Client: fake.HTTPClientFunc(func(req *http.Request) (*http.Response, error) { + Client: fake.CreateHTTPClient(func(req *http.Request) (*http.Response, error) { switch p, m := req.URL.Path, req.Method; { case p == "/namespaces/test/replicationcontrollers/redis-master" && m == "GET": return &http.Response{StatusCode: 200, Body: objBody(codec, &rc.Items[0])}, nil diff --git a/pkg/kubectl/cmd/exec_test.go b/pkg/kubectl/cmd/exec_test.go index 0090dcd6afc..6cb9bc1ea28 100644 --- a/pkg/kubectl/cmd/exec_test.go +++ b/pkg/kubectl/cmd/exec_test.go @@ -126,7 +126,7 @@ func TestPodAndContainer(t *testing.T) { f, tf, codec := NewAPIFactory() tf.Client = &fake.RESTClient{ Codec: codec, - Client: fake.HTTPClientFunc(func(req *http.Request) (*http.Response, error) { return nil, nil }), + Client: fake.CreateHTTPClient(func(req *http.Request) (*http.Response, error) { return nil, nil }), } tf.Namespace = "test" tf.ClientConfig = &client.Config{} @@ -182,7 +182,7 @@ func TestExec(t *testing.T) { f, tf, codec := NewAPIFactory() tf.Client = &fake.RESTClient{ Codec: codec, - Client: fake.HTTPClientFunc(func(req *http.Request) (*http.Response, error) { + Client: fake.CreateHTTPClient(func(req *http.Request) (*http.Response, error) { switch p, m := req.URL.Path, req.Method; { case p == test.podPath && m == "GET": body := objBody(codec, test.pod) diff --git a/pkg/kubectl/cmd/expose_test.go b/pkg/kubectl/cmd/expose_test.go index 2ccbb3beaac..113375e09f4 100644 --- a/pkg/kubectl/cmd/expose_test.go +++ b/pkg/kubectl/cmd/expose_test.go @@ -341,7 +341,7 @@ func TestRunExposeService(t *testing.T) { tf.Printer = &kubectl.JSONPrinter{} tf.Client = &fake.RESTClient{ Codec: codec, - Client: fake.HTTPClientFunc(func(req *http.Request) (*http.Response, error) { + Client: fake.CreateHTTPClient(func(req *http.Request) (*http.Response, error) { switch p, m := req.URL.Path, req.Method; { case p == test.calls[m] && m == "GET": return &http.Response{StatusCode: test.status, Body: objBody(codec, test.input)}, nil diff --git a/pkg/kubectl/cmd/get_test.go b/pkg/kubectl/cmd/get_test.go index 514d313aff8..d33c409a775 100644 --- a/pkg/kubectl/cmd/get_test.go +++ b/pkg/kubectl/cmd/get_test.go @@ -179,7 +179,7 @@ func TestGetUnknownSchemaObjectListGeneric(t *testing.T) { apiCodec := runtime.CodecFor(api.Scheme, testapi.Default.Version()) regularClient := &fake.RESTClient{ Codec: apiCodec, - Client: fake.HTTPClientFunc(func(req *http.Request) (*http.Response, error) { + Client: fake.CreateHTTPClient(func(req *http.Request) (*http.Response, error) { return &http.Response{StatusCode: 200, Body: objBody(apiCodec, &api.ReplicationController{ObjectMeta: api.ObjectMeta{Name: "foo"}})}, nil }), } @@ -188,7 +188,7 @@ func TestGetUnknownSchemaObjectListGeneric(t *testing.T) { tf.Printer = &testPrinter{} tf.Client = &fake.RESTClient{ Codec: codec, - Client: fake.HTTPClientFunc(func(req *http.Request) (*http.Response, error) { + Client: fake.CreateHTTPClient(func(req *http.Request) (*http.Response, error) { return &http.Response{StatusCode: 200, Body: objBody(codec, &internalType{Name: "foo"})}, nil }), } @@ -407,7 +407,7 @@ func TestGetMultipleTypeObjects(t *testing.T) { tf.Printer = &testPrinter{} tf.Client = &fake.RESTClient{ Codec: codec, - Client: fake.HTTPClientFunc(func(req *http.Request) (*http.Response, error) { + Client: fake.CreateHTTPClient(func(req *http.Request) (*http.Response, error) { switch req.URL.Path { case "/namespaces/test/pods": return &http.Response{StatusCode: 200, Body: objBody(codec, pods)}, nil @@ -446,7 +446,7 @@ func TestGetMultipleTypeObjectsAsList(t *testing.T) { tf.Printer = &testPrinter{} tf.Client = &fake.RESTClient{ Codec: codec, - Client: fake.HTTPClientFunc(func(req *http.Request) (*http.Response, error) { + Client: fake.CreateHTTPClient(func(req *http.Request) (*http.Response, error) { switch req.URL.Path { case "/namespaces/test/pods": return &http.Response{StatusCode: 200, Body: objBody(codec, pods)}, nil @@ -506,7 +506,7 @@ func TestGetMultipleTypeObjectsWithSelector(t *testing.T) { tf.Printer = &testPrinter{} tf.Client = &fake.RESTClient{ Codec: codec, - Client: fake.HTTPClientFunc(func(req *http.Request) (*http.Response, error) { + Client: fake.CreateHTTPClient(func(req *http.Request) (*http.Response, error) { if req.URL.Query().Get(unversioned.LabelSelectorQueryParam(testapi.Default.Version())) != "a=b" { t.Fatalf("unexpected request: %#v\n%#v", req.URL, req) } @@ -558,7 +558,7 @@ func TestGetMultipleTypeObjectsWithDirectReference(t *testing.T) { tf.Printer = &testPrinter{} tf.Client = &fake.RESTClient{ Codec: codec, - Client: fake.HTTPClientFunc(func(req *http.Request) (*http.Response, error) { + Client: fake.CreateHTTPClient(func(req *http.Request) (*http.Response, error) { switch req.URL.Path { case "/nodes/foo": return &http.Response{StatusCode: 200, Body: objBody(codec, node)}, nil @@ -632,7 +632,7 @@ func TestWatchSelector(t *testing.T) { tf.Printer = &testPrinter{} tf.Client = &fake.RESTClient{ Codec: codec, - Client: fake.HTTPClientFunc(func(req *http.Request) (*http.Response, error) { + Client: fake.CreateHTTPClient(func(req *http.Request) (*http.Response, error) { if req.URL.Query().Get(unversioned.LabelSelectorQueryParam(testapi.Default.Version())) != "a=b" { t.Fatalf("unexpected request: %#v\n%#v", req.URL, req) } @@ -674,7 +674,7 @@ func TestWatchResource(t *testing.T) { tf.Printer = &testPrinter{} tf.Client = &fake.RESTClient{ Codec: codec, - Client: fake.HTTPClientFunc(func(req *http.Request) (*http.Response, error) { + Client: fake.CreateHTTPClient(func(req *http.Request) (*http.Response, error) { switch req.URL.Path { case "/namespaces/test/pods/foo": return &http.Response{StatusCode: 200, Body: objBody(codec, &pods[0])}, nil @@ -712,7 +712,7 @@ func TestWatchResourceIdentifiedByFile(t *testing.T) { tf.Printer = &testPrinter{} tf.Client = &fake.RESTClient{ Codec: codec, - Client: fake.HTTPClientFunc(func(req *http.Request) (*http.Response, error) { + Client: fake.CreateHTTPClient(func(req *http.Request) (*http.Response, error) { switch req.URL.Path { case "/namespaces/test/pods/cassandra": return &http.Response{StatusCode: 200, Body: objBody(codec, &pods[0])}, nil @@ -751,7 +751,7 @@ func TestWatchOnlyResource(t *testing.T) { tf.Printer = &testPrinter{} tf.Client = &fake.RESTClient{ Codec: codec, - Client: fake.HTTPClientFunc(func(req *http.Request) (*http.Response, error) { + Client: fake.CreateHTTPClient(func(req *http.Request) (*http.Response, error) { switch req.URL.Path { case "/namespaces/test/pods/foo": return &http.Response{StatusCode: 200, Body: objBody(codec, &pods[0])}, nil diff --git a/pkg/kubectl/cmd/label_test.go b/pkg/kubectl/cmd/label_test.go index 750f30ace31..7a3f8db6224 100644 --- a/pkg/kubectl/cmd/label_test.go +++ b/pkg/kubectl/cmd/label_test.go @@ -329,7 +329,7 @@ func TestLabelForResourceFromFile(t *testing.T) { f, tf, codec := NewAPIFactory() tf.Client = &fake.RESTClient{ Codec: codec, - Client: fake.HTTPClientFunc(func(req *http.Request) (*http.Response, error) { + Client: fake.CreateHTTPClient(func(req *http.Request) (*http.Response, error) { switch req.Method { case "GET": switch req.URL.Path { @@ -376,7 +376,7 @@ func TestLabelMultipleObjects(t *testing.T) { f, tf, codec := NewAPIFactory() tf.Client = &fake.RESTClient{ Codec: codec, - Client: fake.HTTPClientFunc(func(req *http.Request) (*http.Response, error) { + Client: fake.CreateHTTPClient(func(req *http.Request) (*http.Response, error) { switch req.Method { case "GET": switch req.URL.Path { diff --git a/pkg/kubectl/cmd/log_test.go b/pkg/kubectl/cmd/log_test.go index e04f9def5e9..0227db11771 100644 --- a/pkg/kubectl/cmd/log_test.go +++ b/pkg/kubectl/cmd/log_test.go @@ -49,7 +49,7 @@ func TestLog(t *testing.T) { f, tf, codec := NewAPIFactory() tf.Client = &fake.RESTClient{ Codec: codec, - Client: fake.HTTPClientFunc(func(req *http.Request) (*http.Response, error) { + Client: fake.CreateHTTPClient(func(req *http.Request) (*http.Response, error) { switch p, m := req.URL.Path, req.Method; { case p == test.podPath && m == "GET": body := objBody(codec, test.pod) diff --git a/pkg/kubectl/cmd/patch_test.go b/pkg/kubectl/cmd/patch_test.go index 2819bb372d2..fd22dcaf4ef 100644 --- a/pkg/kubectl/cmd/patch_test.go +++ b/pkg/kubectl/cmd/patch_test.go @@ -31,7 +31,7 @@ func TestPatchObject(t *testing.T) { tf.Printer = &testPrinter{} tf.Client = &fake.RESTClient{ Codec: codec, - Client: fake.HTTPClientFunc(func(req *http.Request) (*http.Response, error) { + Client: fake.CreateHTTPClient(func(req *http.Request) (*http.Response, error) { switch p, m := req.URL.Path, req.Method; { case p == "/namespaces/test/services/frontend" && (m == "PATCH" || m == "GET"): return &http.Response{StatusCode: 200, Body: objBody(codec, &svc.Items[0])}, nil @@ -63,7 +63,7 @@ func TestPatchObjectFromFile(t *testing.T) { tf.Printer = &testPrinter{} tf.Client = &fake.RESTClient{ Codec: codec, - Client: fake.HTTPClientFunc(func(req *http.Request) (*http.Response, error) { + Client: fake.CreateHTTPClient(func(req *http.Request) (*http.Response, error) { switch p, m := req.URL.Path, req.Method; { case p == "/namespaces/test/services/frontend" && (m == "PATCH" || m == "GET"): return &http.Response{StatusCode: 200, Body: objBody(codec, &svc.Items[0])}, nil diff --git a/pkg/kubectl/cmd/portforward_test.go b/pkg/kubectl/cmd/portforward_test.go index 7092eb7aac8..7d7d45d56a9 100644 --- a/pkg/kubectl/cmd/portforward_test.go +++ b/pkg/kubectl/cmd/portforward_test.go @@ -70,7 +70,7 @@ func TestPortForward(t *testing.T) { f, tf, codec := NewAPIFactory() tf.Client = &fake.RESTClient{ Codec: codec, - Client: fake.HTTPClientFunc(func(req *http.Request) (*http.Response, error) { + Client: fake.CreateHTTPClient(func(req *http.Request) (*http.Response, error) { switch p, m := req.URL.Path, req.Method; { case p == test.podPath && m == "GET": body := objBody(codec, test.pod) @@ -140,7 +140,7 @@ func TestPortForwardWithPFlag(t *testing.T) { f, tf, codec := NewAPIFactory() tf.Client = &fake.RESTClient{ Codec: codec, - Client: fake.HTTPClientFunc(func(req *http.Request) (*http.Response, error) { + Client: fake.CreateHTTPClient(func(req *http.Request) (*http.Response, error) { switch p, m := req.URL.Path, req.Method; { case p == test.podPath && m == "GET": body := objBody(codec, test.pod) diff --git a/pkg/kubectl/cmd/replace_test.go b/pkg/kubectl/cmd/replace_test.go index 6b7e98dcf92..5ca2b2ec817 100644 --- a/pkg/kubectl/cmd/replace_test.go +++ b/pkg/kubectl/cmd/replace_test.go @@ -32,7 +32,7 @@ func TestReplaceObject(t *testing.T) { tf.Printer = &testPrinter{} tf.Client = &fake.RESTClient{ Codec: codec, - Client: fake.HTTPClientFunc(func(req *http.Request) (*http.Response, error) { + Client: fake.CreateHTTPClient(func(req *http.Request) (*http.Response, error) { switch p, m := req.URL.Path, req.Method; { case p == "/namespaces/test/replicationcontrollers/redis-master" && (m == "GET" || m == "PUT" || m == "DELETE"): return &http.Response{StatusCode: 200, Body: objBody(codec, &rc.Items[0])}, nil @@ -75,7 +75,7 @@ func TestReplaceMultipleObject(t *testing.T) { tf.Printer = &testPrinter{} tf.Client = &fake.RESTClient{ Codec: codec, - Client: fake.HTTPClientFunc(func(req *http.Request) (*http.Response, error) { + Client: fake.CreateHTTPClient(func(req *http.Request) (*http.Response, error) { switch p, m := req.URL.Path, req.Method; { case p == "/namespaces/test/replicationcontrollers/redis-master" && (m == "GET" || m == "PUT" || m == "DELETE"): return &http.Response{StatusCode: 200, Body: objBody(codec, &rc.Items[0])}, nil @@ -122,7 +122,7 @@ func TestReplaceDirectory(t *testing.T) { tf.Printer = &testPrinter{} tf.Client = &fake.RESTClient{ Codec: codec, - Client: fake.HTTPClientFunc(func(req *http.Request) (*http.Response, error) { + Client: fake.CreateHTTPClient(func(req *http.Request) (*http.Response, error) { switch p, m := req.URL.Path, req.Method; { case strings.HasPrefix(p, "/namespaces/test/services/") && (m == "GET" || m == "PUT" || m == "DELETE"): return &http.Response{StatusCode: 200, Body: objBody(codec, &svc.Items[0])}, nil @@ -169,7 +169,7 @@ func TestForceReplaceObjectNotFound(t *testing.T) { tf.Printer = &testPrinter{} tf.Client = &fake.RESTClient{ Codec: codec, - Client: fake.HTTPClientFunc(func(req *http.Request) (*http.Response, error) { + Client: fake.CreateHTTPClient(func(req *http.Request) (*http.Response, error) { switch p, m := req.URL.Path, req.Method; { case p == "/namespaces/test/replicationcontrollers/redis-master" && m == "DELETE": return &http.Response{StatusCode: 404, Body: stringBody("")}, nil diff --git a/pkg/kubectl/cmd/run_test.go b/pkg/kubectl/cmd/run_test.go index 04281663936..7de519cef5f 100644 --- a/pkg/kubectl/cmd/run_test.go +++ b/pkg/kubectl/cmd/run_test.go @@ -203,7 +203,7 @@ func TestGenerateService(t *testing.T) { tf.ClientConfig = &client.Config{Version: testapi.Default.Version()} tf.Client = &fake.RESTClient{ Codec: codec, - Client: fake.HTTPClientFunc(func(req *http.Request) (*http.Response, error) { + Client: fake.CreateHTTPClient(func(req *http.Request) (*http.Response, error) { switch p, m := req.URL.Path, req.Method; { case test.expectPOST && m == "POST" && p == "/namespaces/namespace/services": sawPOST = true diff --git a/pkg/kubectl/cmd/util/factory_test.go b/pkg/kubectl/cmd/util/factory_test.go index 41af6303ea8..3d4c8d20757 100644 --- a/pkg/kubectl/cmd/util/factory_test.go +++ b/pkg/kubectl/cmd/util/factory_test.go @@ -230,7 +230,7 @@ func TestValidateCachesSchema(t *testing.T) { c := &fake.RESTClient{ Codec: testapi.Default.Codec(), - Client: fake.HTTPClientFunc(func(req *http.Request) (*http.Response, error) { + Client: fake.CreateHTTPClient(func(req *http.Request) (*http.Response, error) { switch p, m := req.URL.Path, req.Method; { case strings.HasPrefix(p, "/swaggerapi") && m == "GET": requests[p] = requests[p] + 1 diff --git a/pkg/kubectl/resource/builder_test.go b/pkg/kubectl/resource/builder_test.go index 44d75e67224..dafdd1d153e 100644 --- a/pkg/kubectl/resource/builder_test.go +++ b/pkg/kubectl/resource/builder_test.go @@ -64,7 +64,7 @@ func fakeClientWith(testName string, t *testing.T, data map[string]string) Clien return ClientMapperFunc(func(*meta.RESTMapping) (RESTClient, error) { return &fake.RESTClient{ Codec: testapi.Default.Codec(), - Client: fake.HTTPClientFunc(func(req *http.Request) (*http.Response, error) { + Client: fake.CreateHTTPClient(func(req *http.Request) (*http.Response, error) { p := req.URL.Path q := req.URL.RawQuery if len(q) != 0 { diff --git a/pkg/kubectl/resource/helper_test.go b/pkg/kubectl/resource/helper_test.go index 25ce8de4258..711be667053 100644 --- a/pkg/kubectl/resource/helper_test.go +++ b/pkg/kubectl/resource/helper_test.go @@ -417,7 +417,7 @@ func TestHelperReplace(t *testing.T) { Spec: apitesting.DeepEqualSafePodSpec(), }, Overwrite: true, - HTTPClient: fake.HTTPClientFunc(func(req *http.Request) (*http.Response, error) { + HTTPClient: fake.CreateHTTPClient(func(req *http.Request) (*http.Response, error) { if req.Method == "PUT" { return &http.Response{StatusCode: http.StatusOK, Body: objBody(&unversioned.Status{Status: unversioned.StatusSuccess})}, nil } diff --git a/pkg/kubectl/rolling_updater_test.go b/pkg/kubectl/rolling_updater_test.go index f48f08d7c15..b096c7bd4cc 100644 --- a/pkg/kubectl/rolling_updater_test.go +++ b/pkg/kubectl/rolling_updater_test.go @@ -1051,7 +1051,7 @@ func TestUpdateWithRetries(t *testing.T) { } fakeClient := &fake.RESTClient{ Codec: codec, - Client: fake.HTTPClientFunc(func(req *http.Request) (*http.Response, error) { + Client: fake.CreateHTTPClient(func(req *http.Request) (*http.Response, error) { switch p, m := req.URL.Path, req.Method; { case p == testapi.Default.ResourcePath("replicationcontrollers", "default", "rc") && m == "PUT": update := updates[0] @@ -1142,7 +1142,7 @@ func TestAddDeploymentHash(t *testing.T) { updatedRc := false fakeClient := &fake.RESTClient{ Codec: codec, - Client: fake.HTTPClientFunc(func(req *http.Request) (*http.Response, error) { + Client: fake.CreateHTTPClient(func(req *http.Request) (*http.Response, error) { switch p, m := req.URL.Path, req.Method; { case p == testapi.Default.ResourcePath("pods", "default", "") && m == "GET": if req.URL.RawQuery != "labelSelector=foo%3Dbar" {