mirror of
https://github.com/k3s-io/kubernetes.git
synced 2025-07-28 22:17:14 +00:00
Move FakeRESTClient to client/fake.go
This commit is contained in:
parent
df0981bc01
commit
9eb4ca43d8
@ -17,7 +17,11 @@ limitations under the License.
|
|||||||
package client
|
package client
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"net/http"
|
||||||
|
"net/url"
|
||||||
|
|
||||||
"github.com/GoogleCloudPlatform/kubernetes/pkg/api"
|
"github.com/GoogleCloudPlatform/kubernetes/pkg/api"
|
||||||
|
"github.com/GoogleCloudPlatform/kubernetes/pkg/runtime"
|
||||||
"github.com/GoogleCloudPlatform/kubernetes/pkg/version"
|
"github.com/GoogleCloudPlatform/kubernetes/pkg/version"
|
||||||
"github.com/GoogleCloudPlatform/kubernetes/pkg/watch"
|
"github.com/GoogleCloudPlatform/kubernetes/pkg/watch"
|
||||||
)
|
)
|
||||||
@ -75,3 +79,38 @@ func (c *Fake) ServerAPIVersions() (*api.APIVersions, error) {
|
|||||||
c.Actions = append(c.Actions, FakeAction{Action: "get-apiversions", Value: nil})
|
c.Actions = append(c.Actions, FakeAction{Action: "get-apiversions", Value: nil})
|
||||||
return &api.APIVersions{Versions: []string{"v1beta1", "v1beta2"}}, nil
|
return &api.APIVersions{Versions: []string{"v1beta1", "v1beta2"}}, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
type HttpClientFunc func(*http.Request) (*http.Response, error)
|
||||||
|
|
||||||
|
func (f HttpClientFunc) Do(req *http.Request) (*http.Response, error) {
|
||||||
|
return f(req)
|
||||||
|
}
|
||||||
|
|
||||||
|
// FakeRESTClient provides a fake RESTClient interface.
|
||||||
|
type FakeRESTClient struct {
|
||||||
|
Client HTTPClient
|
||||||
|
Codec runtime.Codec
|
||||||
|
Req *http.Request
|
||||||
|
Resp *http.Response
|
||||||
|
Err error
|
||||||
|
}
|
||||||
|
|
||||||
|
func (c *FakeRESTClient) Get() *Request {
|
||||||
|
return NewRequest(c, "GET", &url.URL{Host: "localhost"}, c.Codec)
|
||||||
|
}
|
||||||
|
func (c *FakeRESTClient) Put() *Request {
|
||||||
|
return NewRequest(c, "PUT", &url.URL{Host: "localhost"}, c.Codec)
|
||||||
|
}
|
||||||
|
func (c *FakeRESTClient) Post() *Request {
|
||||||
|
return NewRequest(c, "POST", &url.URL{Host: "localhost"}, c.Codec)
|
||||||
|
}
|
||||||
|
func (c *FakeRESTClient) Delete() *Request {
|
||||||
|
return NewRequest(c, "DELETE", &url.URL{Host: "localhost"}, c.Codec)
|
||||||
|
}
|
||||||
|
func (c *FakeRESTClient) Do(req *http.Request) (*http.Response, error) {
|
||||||
|
c.Req = req
|
||||||
|
if c.Client != HTTPClient(nil) {
|
||||||
|
return c.Client.Do(req)
|
||||||
|
}
|
||||||
|
return c.Resp, c.Err
|
||||||
|
}
|
||||||
|
@ -22,7 +22,6 @@ import (
|
|||||||
"io"
|
"io"
|
||||||
"io/ioutil"
|
"io/ioutil"
|
||||||
"net/http"
|
"net/http"
|
||||||
"net/url"
|
|
||||||
"reflect"
|
"reflect"
|
||||||
"strings"
|
"strings"
|
||||||
"testing"
|
"testing"
|
||||||
@ -34,39 +33,6 @@ import (
|
|||||||
"github.com/GoogleCloudPlatform/kubernetes/pkg/runtime"
|
"github.com/GoogleCloudPlatform/kubernetes/pkg/runtime"
|
||||||
)
|
)
|
||||||
|
|
||||||
type httpClientFunc func(*http.Request) (*http.Response, error)
|
|
||||||
|
|
||||||
func (f httpClientFunc) Do(req *http.Request) (*http.Response, error) {
|
|
||||||
return f(req)
|
|
||||||
}
|
|
||||||
|
|
||||||
type FakeRESTClient struct {
|
|
||||||
Client client.HTTPClient
|
|
||||||
Req *http.Request
|
|
||||||
Resp *http.Response
|
|
||||||
Err error
|
|
||||||
}
|
|
||||||
|
|
||||||
func (c *FakeRESTClient) Get() *client.Request {
|
|
||||||
return client.NewRequest(c, "GET", &url.URL{Host: "localhost"}, testapi.Codec())
|
|
||||||
}
|
|
||||||
func (c *FakeRESTClient) Put() *client.Request {
|
|
||||||
return client.NewRequest(c, "PUT", &url.URL{Host: "localhost"}, testapi.Codec())
|
|
||||||
}
|
|
||||||
func (c *FakeRESTClient) Post() *client.Request {
|
|
||||||
return client.NewRequest(c, "POST", &url.URL{Host: "localhost"}, testapi.Codec())
|
|
||||||
}
|
|
||||||
func (c *FakeRESTClient) Delete() *client.Request {
|
|
||||||
return client.NewRequest(c, "DELETE", &url.URL{Host: "localhost"}, testapi.Codec())
|
|
||||||
}
|
|
||||||
func (c *FakeRESTClient) Do(req *http.Request) (*http.Response, error) {
|
|
||||||
c.Req = req
|
|
||||||
if c.Client != client.HTTPClient(nil) {
|
|
||||||
return c.Client.Do(req)
|
|
||||||
}
|
|
||||||
return c.Resp, c.Err
|
|
||||||
}
|
|
||||||
|
|
||||||
func objBody(obj runtime.Object) io.ReadCloser {
|
func objBody(obj runtime.Object) io.ReadCloser {
|
||||||
return ioutil.NopCloser(bytes.NewReader([]byte(runtime.EncodeOrDie(testapi.Codec(), obj))))
|
return ioutil.NopCloser(bytes.NewReader([]byte(runtime.EncodeOrDie(testapi.Codec(), obj))))
|
||||||
}
|
}
|
||||||
@ -112,9 +78,10 @@ func TestRESTHelperDelete(t *testing.T) {
|
|||||||
},
|
},
|
||||||
}
|
}
|
||||||
for _, test := range tests {
|
for _, test := range tests {
|
||||||
client := &FakeRESTClient{
|
client := &client.FakeRESTClient{
|
||||||
Resp: test.Resp,
|
Codec: testapi.Codec(),
|
||||||
Err: test.HttpErr,
|
Resp: test.Resp,
|
||||||
|
Err: test.HttpErr,
|
||||||
}
|
}
|
||||||
modifier := &RESTHelper{
|
modifier := &RESTHelper{
|
||||||
RESTClient: client,
|
RESTClient: client,
|
||||||
@ -147,7 +114,7 @@ func TestRESTHelperCreate(t *testing.T) {
|
|||||||
|
|
||||||
tests := []struct {
|
tests := []struct {
|
||||||
Resp *http.Response
|
Resp *http.Response
|
||||||
RespFunc httpClientFunc
|
RespFunc client.HttpClientFunc
|
||||||
HttpErr error
|
HttpErr error
|
||||||
Modify bool
|
Modify bool
|
||||||
Object runtime.Object
|
Object runtime.Object
|
||||||
@ -192,9 +159,10 @@ func TestRESTHelperCreate(t *testing.T) {
|
|||||||
},
|
},
|
||||||
}
|
}
|
||||||
for i, test := range tests {
|
for i, test := range tests {
|
||||||
client := &FakeRESTClient{
|
client := &client.FakeRESTClient{
|
||||||
Resp: test.Resp,
|
Codec: testapi.Codec(),
|
||||||
Err: test.HttpErr,
|
Resp: test.Resp,
|
||||||
|
Err: test.HttpErr,
|
||||||
}
|
}
|
||||||
if test.RespFunc != nil {
|
if test.RespFunc != nil {
|
||||||
client.Client = test.RespFunc
|
client.Client = test.RespFunc
|
||||||
@ -275,9 +243,10 @@ func TestRESTHelperGet(t *testing.T) {
|
|||||||
},
|
},
|
||||||
}
|
}
|
||||||
for _, test := range tests {
|
for _, test := range tests {
|
||||||
client := &FakeRESTClient{
|
client := &client.FakeRESTClient{
|
||||||
Resp: test.Resp,
|
Codec: testapi.Codec(),
|
||||||
Err: test.HttpErr,
|
Resp: test.Resp,
|
||||||
|
Err: test.HttpErr,
|
||||||
}
|
}
|
||||||
modifier := &RESTHelper{
|
modifier := &RESTHelper{
|
||||||
RESTClient: client,
|
RESTClient: client,
|
||||||
@ -317,7 +286,7 @@ func TestRESTHelperUpdate(t *testing.T) {
|
|||||||
|
|
||||||
tests := []struct {
|
tests := []struct {
|
||||||
Resp *http.Response
|
Resp *http.Response
|
||||||
RespFunc httpClientFunc
|
RespFunc client.HttpClientFunc
|
||||||
HttpErr error
|
HttpErr error
|
||||||
Overwrite bool
|
Overwrite bool
|
||||||
Object runtime.Object
|
Object runtime.Object
|
||||||
@ -368,9 +337,10 @@ func TestRESTHelperUpdate(t *testing.T) {
|
|||||||
},
|
},
|
||||||
}
|
}
|
||||||
for i, test := range tests {
|
for i, test := range tests {
|
||||||
client := &FakeRESTClient{
|
client := &client.FakeRESTClient{
|
||||||
Resp: test.Resp,
|
Codec: testapi.Codec(),
|
||||||
Err: test.HttpErr,
|
Resp: test.Resp,
|
||||||
|
Err: test.HttpErr,
|
||||||
}
|
}
|
||||||
if test.RespFunc != nil {
|
if test.RespFunc != nil {
|
||||||
client.Client = test.RespFunc
|
client.Client = test.RespFunc
|
||||||
|
Loading…
Reference in New Issue
Block a user