mirror of
https://github.com/k3s-io/kubernetes.git
synced 2025-07-25 12:43:23 +00:00
Expand apiserver tests
This commit is contained in:
parent
8986a0fc4b
commit
af4d57b39a
@ -18,6 +18,8 @@ package apiserver
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"bytes"
|
"bytes"
|
||||||
|
"encoding/json"
|
||||||
|
"errors"
|
||||||
"fmt"
|
"fmt"
|
||||||
"io/ioutil"
|
"io/ioutil"
|
||||||
"net/http"
|
"net/http"
|
||||||
@ -29,6 +31,7 @@ import (
|
|||||||
|
|
||||||
"github.com/GoogleCloudPlatform/kubernetes/pkg/api"
|
"github.com/GoogleCloudPlatform/kubernetes/pkg/api"
|
||||||
"github.com/GoogleCloudPlatform/kubernetes/pkg/labels"
|
"github.com/GoogleCloudPlatform/kubernetes/pkg/labels"
|
||||||
|
"github.com/GoogleCloudPlatform/kubernetes/pkg/version"
|
||||||
"github.com/GoogleCloudPlatform/kubernetes/pkg/watch"
|
"github.com/GoogleCloudPlatform/kubernetes/pkg/watch"
|
||||||
)
|
)
|
||||||
|
|
||||||
@ -162,6 +165,57 @@ func extractBody(response *http.Response, object interface{}) (string, error) {
|
|||||||
return string(body), err
|
return string(body), err
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func TestNotFound(t *testing.T) {
|
||||||
|
type T struct {
|
||||||
|
Method string
|
||||||
|
Path string
|
||||||
|
}
|
||||||
|
cases := map[string]T{
|
||||||
|
"PATCH method": T{"PATCH", "/prefix/version/foo"},
|
||||||
|
"GET long prefix": T{"GET", "/prefix/"},
|
||||||
|
"GET missing storage": T{"GET", "/prefix/version/blah"},
|
||||||
|
"GET with extra segment": T{"GET", "/prefix/version/foo/bar/baz"},
|
||||||
|
"POST with extra segment": T{"POST", "/prefix/version/foo/bar"},
|
||||||
|
"DELETE without extra segment": T{"DELETE", "/prefix/version/foo"},
|
||||||
|
"DELETE with extra segment": T{"DELETE", "/prefix/version/foo/bar/baz"},
|
||||||
|
"PUT without extra segment": T{"PUT", "/prefix/version/foo"},
|
||||||
|
"PUT with extra segment": T{"PUT", "/prefix/version/foo/bar/baz"},
|
||||||
|
"watch missing storage": T{"GET", "/prefix/version/watch/"},
|
||||||
|
"watch with bad method": T{"POST", "/prefix/version/watch/foo/bar"},
|
||||||
|
}
|
||||||
|
handler := New(map[string]RESTStorage{
|
||||||
|
"foo": &SimpleRESTStorage{},
|
||||||
|
}, "/prefix/version")
|
||||||
|
server := httptest.NewServer(handler)
|
||||||
|
client := http.Client{}
|
||||||
|
for k, v := range cases {
|
||||||
|
request, err := http.NewRequest(v.Method, server.URL+v.Path, nil)
|
||||||
|
expectNoError(t, err)
|
||||||
|
response, err := client.Do(request)
|
||||||
|
expectNoError(t, err)
|
||||||
|
if response.StatusCode != http.StatusNotFound {
|
||||||
|
t.Errorf("Expected %d for %s (%s), Got %#v", http.StatusNotFound, v, k, response)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestVersion(t *testing.T) {
|
||||||
|
handler := New(map[string]RESTStorage{}, "/prefix/version")
|
||||||
|
server := httptest.NewServer(handler)
|
||||||
|
client := http.Client{}
|
||||||
|
|
||||||
|
request, err := http.NewRequest("GET", server.URL+"/version", nil)
|
||||||
|
expectNoError(t, err)
|
||||||
|
response, err := client.Do(request)
|
||||||
|
expectNoError(t, err)
|
||||||
|
var info version.Info
|
||||||
|
err = json.NewDecoder(response.Body).Decode(&info)
|
||||||
|
expectNoError(t, err)
|
||||||
|
if !reflect.DeepEqual(version.Get(), info) {
|
||||||
|
t.Errorf("Expected %#v, Got %#v", version.Get(), info)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
func TestSimpleList(t *testing.T) {
|
func TestSimpleList(t *testing.T) {
|
||||||
storage := map[string]RESTStorage{}
|
storage := map[string]RESTStorage{}
|
||||||
simpleStorage := SimpleRESTStorage{}
|
simpleStorage := SimpleRESTStorage{}
|
||||||
@ -359,36 +413,6 @@ func TestBadPath(t *testing.T) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestMissingPath(t *testing.T) {
|
|
||||||
handler := New(map[string]RESTStorage{}, "/prefix/version")
|
|
||||||
server := httptest.NewServer(handler)
|
|
||||||
client := http.Client{}
|
|
||||||
|
|
||||||
request, err := http.NewRequest("GET", server.URL+"/prefix/version", nil)
|
|
||||||
expectNoError(t, err)
|
|
||||||
response, err := client.Do(request)
|
|
||||||
expectNoError(t, err)
|
|
||||||
if response.StatusCode != http.StatusNotFound {
|
|
||||||
t.Errorf("Unexpected response %#v", response)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
func TestMissingStorage(t *testing.T) {
|
|
||||||
handler := New(map[string]RESTStorage{
|
|
||||||
"foo": &SimpleRESTStorage{},
|
|
||||||
}, "/prefix/version")
|
|
||||||
server := httptest.NewServer(handler)
|
|
||||||
client := http.Client{}
|
|
||||||
|
|
||||||
request, err := http.NewRequest("GET", server.URL+"/prefix/version/foobar", nil)
|
|
||||||
expectNoError(t, err)
|
|
||||||
response, err := client.Do(request)
|
|
||||||
expectNoError(t, err)
|
|
||||||
if response.StatusCode != http.StatusNotFound {
|
|
||||||
t.Errorf("Unexpected response %#v", response)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
func TestCreate(t *testing.T) {
|
func TestCreate(t *testing.T) {
|
||||||
simpleStorage := &SimpleRESTStorage{}
|
simpleStorage := &SimpleRESTStorage{}
|
||||||
handler := New(map[string]RESTStorage{
|
handler := New(map[string]RESTStorage{
|
||||||
@ -419,20 +443,19 @@ func TestCreate(t *testing.T) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func TestCreateNotFound(t *testing.T) {
|
func TestCreateNotFound(t *testing.T) {
|
||||||
simpleStorage := &SimpleRESTStorage{
|
|
||||||
// storage.Create can fail with not found error in theory.
|
|
||||||
// See https://github.com/GoogleCloudPlatform/kubernetes/pull/486#discussion_r15037092.
|
|
||||||
errors: map[string]error{"create": NewNotFoundErr("simple", "id")},
|
|
||||||
}
|
|
||||||
handler := New(map[string]RESTStorage{
|
handler := New(map[string]RESTStorage{
|
||||||
"foo": simpleStorage,
|
"simple": &SimpleRESTStorage{
|
||||||
|
// storage.Create can fail with not found error in theory.
|
||||||
|
// See https://github.com/GoogleCloudPlatform/kubernetes/pull/486#discussion_r15037092.
|
||||||
|
errors: map[string]error{"create": NewNotFoundErr("simple", "id")},
|
||||||
|
},
|
||||||
}, "/prefix/version")
|
}, "/prefix/version")
|
||||||
server := httptest.NewServer(handler)
|
server := httptest.NewServer(handler)
|
||||||
client := http.Client{}
|
client := http.Client{}
|
||||||
|
|
||||||
simple := Simple{Name: "foo"}
|
simple := Simple{Name: "foo"}
|
||||||
data, _ := api.Encode(simple)
|
data, _ := api.Encode(simple)
|
||||||
request, err := http.NewRequest("POST", server.URL+"/prefix/version/foo", bytes.NewBuffer(data))
|
request, err := http.NewRequest("POST", server.URL+"/prefix/version/simple", bytes.NewBuffer(data))
|
||||||
expectNoError(t, err)
|
expectNoError(t, err)
|
||||||
response, err := client.Do(request)
|
response, err := client.Do(request)
|
||||||
expectNoError(t, err)
|
expectNoError(t, err)
|
||||||
@ -456,7 +479,7 @@ func TestParseTimeout(t *testing.T) {
|
|||||||
func TestSyncCreate(t *testing.T) {
|
func TestSyncCreate(t *testing.T) {
|
||||||
storage := SimpleRESTStorage{
|
storage := SimpleRESTStorage{
|
||||||
injectedFunction: func(obj interface{}) (interface{}, error) {
|
injectedFunction: func(obj interface{}) (interface{}, error) {
|
||||||
time.Sleep(200 * time.Millisecond)
|
time.Sleep(5 * time.Millisecond)
|
||||||
return obj, nil
|
return obj, nil
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
@ -492,10 +515,124 @@ func TestSyncCreate(t *testing.T) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func expectApiStatus(t *testing.T, method, url string, data []byte, code int) *api.Status {
|
||||||
|
client := http.Client{}
|
||||||
|
request, err := http.NewRequest(method, url, bytes.NewBuffer(data))
|
||||||
|
if err != nil {
|
||||||
|
t.Fatalf("unexpected error %#v", err)
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
response, err := client.Do(request)
|
||||||
|
if err != nil {
|
||||||
|
t.Fatalf("unexpected error %#v", err)
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
var status api.Status
|
||||||
|
_, err = extractBody(response, &status)
|
||||||
|
if err != nil {
|
||||||
|
t.Fatalf("unexpected error %#v", err)
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
if code != response.StatusCode {
|
||||||
|
t.Fatalf("Expected %d, Got %d", code, response.StatusCode)
|
||||||
|
}
|
||||||
|
return &status
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestAsyncDelayReturnsError(t *testing.T) {
|
||||||
|
storage := SimpleRESTStorage{
|
||||||
|
injectedFunction: func(obj interface{}) (interface{}, error) {
|
||||||
|
return nil, errors.New("error")
|
||||||
|
},
|
||||||
|
}
|
||||||
|
handler := New(map[string]RESTStorage{"foo": &storage}, "/prefix/version")
|
||||||
|
handler.asyncOpWait = time.Millisecond / 2
|
||||||
|
server := httptest.NewServer(handler)
|
||||||
|
|
||||||
|
status := expectApiStatus(t, "DELETE", fmt.Sprintf("%s/prefix/version/foo/bar", server.URL), nil, http.StatusInternalServerError)
|
||||||
|
if status.Status != api.StatusFailure || status.Details == "" {
|
||||||
|
t.Errorf("Unexpected status %#v", status)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestAsyncCreateError(t *testing.T) {
|
||||||
|
ch := make(chan struct{})
|
||||||
|
storage := SimpleRESTStorage{
|
||||||
|
injectedFunction: func(obj interface{}) (interface{}, error) {
|
||||||
|
<-ch
|
||||||
|
return nil, errors.New("error")
|
||||||
|
},
|
||||||
|
}
|
||||||
|
handler := New(map[string]RESTStorage{"foo": &storage}, "/prefix/version")
|
||||||
|
handler.asyncOpWait = 0
|
||||||
|
server := httptest.NewServer(handler)
|
||||||
|
|
||||||
|
simple := Simple{Name: "foo"}
|
||||||
|
data, _ := api.Encode(simple)
|
||||||
|
|
||||||
|
status := expectApiStatus(t, "POST", fmt.Sprintf("%s/prefix/version/foo", server.URL), data, http.StatusAccepted)
|
||||||
|
if status.Status != api.StatusWorking || status.Details == "" {
|
||||||
|
t.Errorf("Unexpected status %#v", status)
|
||||||
|
}
|
||||||
|
|
||||||
|
otherStatus := expectApiStatus(t, "GET", fmt.Sprintf("%s/prefix/version/operations/%s", server.URL, status.Details), []byte{}, http.StatusAccepted)
|
||||||
|
if !reflect.DeepEqual(status, otherStatus) {
|
||||||
|
t.Errorf("Expected %#v, Got %#v", status, otherStatus)
|
||||||
|
}
|
||||||
|
|
||||||
|
ch <- struct{}{}
|
||||||
|
time.Sleep(time.Millisecond)
|
||||||
|
|
||||||
|
finalStatus := expectApiStatus(t, "GET", fmt.Sprintf("%s/prefix/version/operations/%s?after=1", server.URL, status.Details), []byte{}, http.StatusOK)
|
||||||
|
expectedStatus := &api.Status{
|
||||||
|
Code: http.StatusInternalServerError,
|
||||||
|
Details: "error",
|
||||||
|
Status: api.StatusFailure,
|
||||||
|
}
|
||||||
|
if !reflect.DeepEqual(expectedStatus, finalStatus) {
|
||||||
|
t.Errorf("Expected %#v, Got %#v", expectedStatus, finalStatus)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestWriteJSONDecodeError(t *testing.T) {
|
||||||
|
server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, req *http.Request) {
|
||||||
|
type T struct {
|
||||||
|
Value string
|
||||||
|
}
|
||||||
|
writeJSON(http.StatusOK, &T{"Undecodable"}, w)
|
||||||
|
}))
|
||||||
|
client := http.Client{}
|
||||||
|
resp, err := client.Get(server.URL)
|
||||||
|
expectNoError(t, err)
|
||||||
|
if resp.StatusCode != http.StatusInternalServerError {
|
||||||
|
t.Errorf("unexpected status code %d", resp.StatusCode)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
type marshalError struct {
|
||||||
|
err error
|
||||||
|
}
|
||||||
|
|
||||||
|
func (m *marshalError) MarshalJSON() ([]byte, error) {
|
||||||
|
return []byte{}, m.err
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestWriteRAWJSONMarshalError(t *testing.T) {
|
||||||
|
server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, req *http.Request) {
|
||||||
|
writeRawJSON(http.StatusOK, &marshalError{errors.New("Undecodable")}, w)
|
||||||
|
}))
|
||||||
|
client := http.Client{}
|
||||||
|
resp, err := client.Get(server.URL)
|
||||||
|
expectNoError(t, err)
|
||||||
|
if resp.StatusCode != http.StatusInternalServerError {
|
||||||
|
t.Errorf("unexpected status code %d", resp.StatusCode)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
func TestSyncCreateTimeout(t *testing.T) {
|
func TestSyncCreateTimeout(t *testing.T) {
|
||||||
storage := SimpleRESTStorage{
|
storage := SimpleRESTStorage{
|
||||||
injectedFunction: func(obj interface{}) (interface{}, error) {
|
injectedFunction: func(obj interface{}) (interface{}, error) {
|
||||||
time.Sleep(400 * time.Millisecond)
|
time.Sleep(5 * time.Millisecond)
|
||||||
return obj, nil
|
return obj, nil
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
@ -503,28 +640,11 @@ func TestSyncCreateTimeout(t *testing.T) {
|
|||||||
"foo": &storage,
|
"foo": &storage,
|
||||||
}, "/prefix/version")
|
}, "/prefix/version")
|
||||||
server := httptest.NewServer(handler)
|
server := httptest.NewServer(handler)
|
||||||
client := http.Client{}
|
|
||||||
|
|
||||||
simple := Simple{Name: "foo"}
|
simple := Simple{Name: "foo"}
|
||||||
data, _ := api.Encode(simple)
|
data, _ := api.Encode(simple)
|
||||||
request, err := http.NewRequest("POST", server.URL+"/prefix/version/foo?sync=true&timeout=200ms", bytes.NewBuffer(data))
|
itemOut := expectApiStatus(t, "POST", server.URL+"/prefix/version/foo?sync=true&timeout=4ms", data, http.StatusAccepted)
|
||||||
expectNoError(t, err)
|
|
||||||
wg := sync.WaitGroup{}
|
|
||||||
wg.Add(1)
|
|
||||||
var response *http.Response
|
|
||||||
go func() {
|
|
||||||
response, err = client.Do(request)
|
|
||||||
wg.Done()
|
|
||||||
}()
|
|
||||||
wg.Wait()
|
|
||||||
expectNoError(t, err)
|
|
||||||
var itemOut api.Status
|
|
||||||
_, err = extractBody(response, &itemOut)
|
|
||||||
expectNoError(t, err)
|
|
||||||
if itemOut.Status != api.StatusWorking || itemOut.Details == "" {
|
if itemOut.Status != api.StatusWorking || itemOut.Details == "" {
|
||||||
t.Errorf("Unexpected status %#v", itemOut)
|
t.Errorf("Unexpected status %#v", itemOut)
|
||||||
}
|
}
|
||||||
if response.StatusCode != http.StatusAccepted {
|
|
||||||
t.Errorf("Unexpected status: %d, Expected: %d, %#v", response.StatusCode, 202, response)
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user