2014-06-06 23:40:48 +00:00
|
|
|
/*
|
|
|
|
Copyright 2014 Google Inc. All rights reserved.
|
|
|
|
|
|
|
|
Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
you may not use this file except in compliance with the License.
|
|
|
|
You may obtain a copy of the License at
|
|
|
|
|
|
|
|
http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
|
|
|
|
Unless required by applicable law or agreed to in writing, software
|
|
|
|
distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
See the License for the specific language governing permissions and
|
|
|
|
limitations under the License.
|
|
|
|
*/
|
2014-06-23 18:32:11 +00:00
|
|
|
|
2014-06-06 23:40:48 +00:00
|
|
|
package apiserver
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
2014-07-31 18:16:13 +00:00
|
|
|
"encoding/json"
|
|
|
|
"errors"
|
2014-06-06 23:40:48 +00:00
|
|
|
"fmt"
|
|
|
|
"io/ioutil"
|
|
|
|
"net/http"
|
|
|
|
"net/http/httptest"
|
|
|
|
"reflect"
|
2014-07-31 18:26:34 +00:00
|
|
|
"strings"
|
2014-06-19 04:04:11 +00:00
|
|
|
"sync"
|
2014-06-06 23:40:48 +00:00
|
|
|
"testing"
|
2014-06-25 20:21:32 +00:00
|
|
|
"time"
|
2014-06-17 01:03:44 +00:00
|
|
|
|
2014-06-20 23:18:36 +00:00
|
|
|
"github.com/GoogleCloudPlatform/kubernetes/pkg/api"
|
2014-09-03 21:16:00 +00:00
|
|
|
apierrs "github.com/GoogleCloudPlatform/kubernetes/pkg/api/errors"
|
2014-09-11 17:02:53 +00:00
|
|
|
"github.com/GoogleCloudPlatform/kubernetes/pkg/api/latest"
|
2014-11-06 01:22:18 +00:00
|
|
|
"github.com/GoogleCloudPlatform/kubernetes/pkg/api/testapi"
|
2014-06-17 01:03:44 +00:00
|
|
|
"github.com/GoogleCloudPlatform/kubernetes/pkg/labels"
|
2014-09-02 17:55:27 +00:00
|
|
|
"github.com/GoogleCloudPlatform/kubernetes/pkg/runtime"
|
2014-09-09 21:05:18 +00:00
|
|
|
"github.com/GoogleCloudPlatform/kubernetes/pkg/util"
|
2014-07-31 18:16:13 +00:00
|
|
|
"github.com/GoogleCloudPlatform/kubernetes/pkg/version"
|
2014-07-17 17:05:14 +00:00
|
|
|
"github.com/GoogleCloudPlatform/kubernetes/pkg/watch"
|
2014-06-06 23:40:48 +00:00
|
|
|
)
|
|
|
|
|
2014-09-08 04:14:18 +00:00
|
|
|
func convert(obj runtime.Object) (runtime.Object, error) {
|
2014-07-16 21:30:28 +00:00
|
|
|
return obj, nil
|
|
|
|
}
|
|
|
|
|
2014-11-06 01:22:18 +00:00
|
|
|
var codec = testapi.Codec()
|
2014-09-26 00:20:28 +00:00
|
|
|
var selfLinker = latest.SelfLinker
|
2014-08-06 03:10:48 +00:00
|
|
|
|
2014-06-20 23:18:36 +00:00
|
|
|
func init() {
|
2014-09-11 17:02:53 +00:00
|
|
|
api.Scheme.AddKnownTypes("", &Simple{}, &SimpleList{})
|
2014-11-06 01:22:18 +00:00
|
|
|
api.Scheme.AddKnownTypes(testapi.Version(), &Simple{}, &SimpleList{})
|
2014-06-20 23:18:36 +00:00
|
|
|
}
|
|
|
|
|
2014-06-06 23:40:48 +00:00
|
|
|
type Simple struct {
|
2014-10-23 20:51:34 +00:00
|
|
|
api.TypeMeta `yaml:",inline" json:",inline"`
|
2014-11-06 01:22:18 +00:00
|
|
|
api.ObjectMeta `yaml:"metadata" json:"metadata"`
|
2014-10-23 20:51:34 +00:00
|
|
|
Other string `yaml:"other,omitempty" json:"other,omitempty"`
|
2014-06-06 23:40:48 +00:00
|
|
|
}
|
|
|
|
|
2014-09-08 04:14:18 +00:00
|
|
|
func (*Simple) IsAnAPIObject() {}
|
|
|
|
|
2014-06-06 23:40:48 +00:00
|
|
|
type SimpleList struct {
|
2014-10-07 15:12:16 +00:00
|
|
|
api.TypeMeta `yaml:",inline" json:",inline"`
|
2014-10-23 20:51:34 +00:00
|
|
|
api.ListMeta `yaml:"metadata,inline" json:"metadata,inline"`
|
2014-06-20 23:50:56 +00:00
|
|
|
Items []Simple `yaml:"items,omitempty" json:"items,omitempty"`
|
2014-06-06 23:40:48 +00:00
|
|
|
}
|
|
|
|
|
2014-09-08 04:14:18 +00:00
|
|
|
func (*SimpleList) IsAnAPIObject() {}
|
|
|
|
|
2014-11-06 01:22:18 +00:00
|
|
|
func TestSimpleSetupRight(t *testing.T) {
|
|
|
|
s := &Simple{ObjectMeta: api.ObjectMeta{Name: "aName"}}
|
|
|
|
wire, err := codec.Encode(s)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
s2, err := codec.Decode(wire)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
if !reflect.DeepEqual(s, s2) {
|
|
|
|
t.Fatalf("encode/decode broken:\n%#v\n%#v\n", s, s2)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-06-06 23:40:48 +00:00
|
|
|
type SimpleRESTStorage struct {
|
2014-07-16 05:27:15 +00:00
|
|
|
errors map[string]error
|
2014-06-06 23:40:48 +00:00
|
|
|
list []Simple
|
|
|
|
item Simple
|
|
|
|
deleted string
|
2014-08-06 03:10:48 +00:00
|
|
|
updated *Simple
|
|
|
|
created *Simple
|
2014-06-25 20:21:32 +00:00
|
|
|
|
2014-08-06 21:55:37 +00:00
|
|
|
// These are set when Watch is called
|
2014-10-30 16:55:17 +00:00
|
|
|
fakeWatch *watch.FakeWatcher
|
|
|
|
requestedLabelSelector labels.Selector
|
|
|
|
requestedFieldSelector labels.Selector
|
|
|
|
requestedResourceVersion string
|
|
|
|
requestedResourceNamespace string
|
2014-08-05 20:33:25 +00:00
|
|
|
|
2014-08-27 23:10:44 +00:00
|
|
|
// The id requested, and location to return for ResourceLocation
|
2014-08-25 21:36:15 +00:00
|
|
|
requestedResourceLocationID string
|
2014-08-27 23:10:44 +00:00
|
|
|
resourceLocation string
|
2014-10-20 20:25:08 +00:00
|
|
|
expectedResourceNamespace string
|
2014-08-25 21:36:15 +00:00
|
|
|
|
2014-06-26 17:51:29 +00:00
|
|
|
// If non-nil, called inside the WorkFunc when answering update, delete, create.
|
2014-07-28 13:15:50 +00:00
|
|
|
// obj receives the original input to the update, delete, or create call.
|
2014-09-08 04:14:18 +00:00
|
|
|
injectedFunction func(obj runtime.Object) (returnObj runtime.Object, err error)
|
2014-06-06 23:40:48 +00:00
|
|
|
}
|
|
|
|
|
2014-09-26 15:46:04 +00:00
|
|
|
func (storage *SimpleRESTStorage) List(ctx api.Context, label, field labels.Selector) (runtime.Object, error) {
|
2014-06-20 23:18:36 +00:00
|
|
|
result := &SimpleList{
|
2014-06-06 23:40:48 +00:00
|
|
|
Items: storage.list,
|
|
|
|
}
|
2014-07-16 05:27:15 +00:00
|
|
|
return result, storage.errors["list"]
|
2014-06-06 23:40:48 +00:00
|
|
|
}
|
|
|
|
|
2014-09-26 15:46:04 +00:00
|
|
|
func (storage *SimpleRESTStorage) Get(ctx api.Context, id string) (runtime.Object, error) {
|
2014-09-11 17:02:53 +00:00
|
|
|
return api.Scheme.CopyOrDie(&storage.item), storage.errors["get"]
|
2014-06-06 23:40:48 +00:00
|
|
|
}
|
|
|
|
|
2014-10-24 17:16:02 +00:00
|
|
|
func (storage *SimpleRESTStorage) Delete(ctx api.Context, id string) (<-chan RESTResult, error) {
|
2014-06-06 23:40:48 +00:00
|
|
|
storage.deleted = id
|
2014-07-17 17:05:14 +00:00
|
|
|
if err := storage.errors["delete"]; err != nil {
|
|
|
|
return nil, err
|
2014-06-25 20:21:32 +00:00
|
|
|
}
|
2014-09-08 04:14:18 +00:00
|
|
|
return MakeAsync(func() (runtime.Object, error) {
|
2014-06-25 20:21:32 +00:00
|
|
|
if storage.injectedFunction != nil {
|
2014-10-23 20:51:34 +00:00
|
|
|
return storage.injectedFunction(&Simple{ObjectMeta: api.ObjectMeta{Name: id}})
|
2014-06-25 20:21:32 +00:00
|
|
|
}
|
2014-08-05 20:33:25 +00:00
|
|
|
return &api.Status{Status: api.StatusSuccess}, nil
|
2014-06-25 20:21:32 +00:00
|
|
|
}), nil
|
2014-06-06 23:40:48 +00:00
|
|
|
}
|
|
|
|
|
2014-09-08 04:14:18 +00:00
|
|
|
func (storage *SimpleRESTStorage) New() runtime.Object {
|
2014-08-06 03:10:48 +00:00
|
|
|
return &Simple{}
|
2014-06-06 23:40:48 +00:00
|
|
|
}
|
|
|
|
|
2014-10-24 17:16:02 +00:00
|
|
|
func (storage *SimpleRESTStorage) Create(ctx api.Context, obj runtime.Object) (<-chan RESTResult, error) {
|
2014-08-06 03:10:48 +00:00
|
|
|
storage.created = obj.(*Simple)
|
2014-07-17 17:05:14 +00:00
|
|
|
if err := storage.errors["create"]; err != nil {
|
|
|
|
return nil, err
|
2014-06-25 20:21:32 +00:00
|
|
|
}
|
2014-09-08 04:14:18 +00:00
|
|
|
return MakeAsync(func() (runtime.Object, error) {
|
2014-06-25 20:21:32 +00:00
|
|
|
if storage.injectedFunction != nil {
|
|
|
|
return storage.injectedFunction(obj)
|
|
|
|
}
|
|
|
|
return obj, nil
|
|
|
|
}), nil
|
2014-06-06 23:40:48 +00:00
|
|
|
}
|
|
|
|
|
2014-10-24 17:16:02 +00:00
|
|
|
func (storage *SimpleRESTStorage) Update(ctx api.Context, obj runtime.Object) (<-chan RESTResult, error) {
|
2014-08-06 03:10:48 +00:00
|
|
|
storage.updated = obj.(*Simple)
|
2014-07-17 17:05:14 +00:00
|
|
|
if err := storage.errors["update"]; err != nil {
|
|
|
|
return nil, err
|
2014-06-25 20:21:32 +00:00
|
|
|
}
|
2014-09-08 04:14:18 +00:00
|
|
|
return MakeAsync(func() (runtime.Object, error) {
|
2014-06-25 20:21:32 +00:00
|
|
|
if storage.injectedFunction != nil {
|
|
|
|
return storage.injectedFunction(obj)
|
|
|
|
}
|
|
|
|
return obj, nil
|
|
|
|
}), nil
|
2014-06-06 23:40:48 +00:00
|
|
|
}
|
|
|
|
|
2014-07-17 17:05:14 +00:00
|
|
|
// Implement ResourceWatcher.
|
2014-10-07 20:51:28 +00:00
|
|
|
func (storage *SimpleRESTStorage) Watch(ctx api.Context, label, field labels.Selector, resourceVersion string) (watch.Interface, error) {
|
2014-08-05 20:33:25 +00:00
|
|
|
storage.requestedLabelSelector = label
|
|
|
|
storage.requestedFieldSelector = field
|
|
|
|
storage.requestedResourceVersion = resourceVersion
|
2014-10-30 16:55:17 +00:00
|
|
|
storage.requestedResourceNamespace = api.Namespace(ctx)
|
2014-08-06 21:55:37 +00:00
|
|
|
if err := storage.errors["watch"]; err != nil {
|
2014-07-17 17:05:14 +00:00
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
storage.fakeWatch = watch.NewFake()
|
|
|
|
return storage.fakeWatch, nil
|
|
|
|
}
|
|
|
|
|
2014-08-25 21:36:15 +00:00
|
|
|
// Implement Redirector.
|
2014-09-26 15:46:04 +00:00
|
|
|
func (storage *SimpleRESTStorage) ResourceLocation(ctx api.Context, id string) (string, error) {
|
2014-10-20 20:25:08 +00:00
|
|
|
// validate that the namespace context on the request matches the expected input
|
2014-10-30 16:55:17 +00:00
|
|
|
storage.requestedResourceNamespace = api.Namespace(ctx)
|
|
|
|
if storage.expectedResourceNamespace != storage.requestedResourceNamespace {
|
|
|
|
return "", fmt.Errorf("Expected request namespace %s, but got namespace %s", storage.expectedResourceNamespace, storage.requestedResourceNamespace)
|
2014-10-20 20:25:08 +00:00
|
|
|
}
|
2014-08-25 21:36:15 +00:00
|
|
|
storage.requestedResourceLocationID = id
|
|
|
|
if err := storage.errors["resourceLocation"]; err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
2014-08-27 23:10:44 +00:00
|
|
|
return storage.resourceLocation, nil
|
2014-08-25 21:36:15 +00:00
|
|
|
}
|
|
|
|
|
2014-09-08 04:14:18 +00:00
|
|
|
func extractBody(response *http.Response, object runtime.Object) (string, error) {
|
2014-06-06 23:40:48 +00:00
|
|
|
defer response.Body.Close()
|
|
|
|
body, err := ioutil.ReadAll(response.Body)
|
|
|
|
if err != nil {
|
|
|
|
return string(body), err
|
|
|
|
}
|
2014-08-06 03:10:48 +00:00
|
|
|
err = codec.DecodeInto(body, object)
|
2014-06-06 23:40:48 +00:00
|
|
|
return string(body), err
|
|
|
|
}
|
|
|
|
|
2014-07-31 18:16:13 +00:00
|
|
|
func TestNotFound(t *testing.T) {
|
|
|
|
type T struct {
|
|
|
|
Method string
|
|
|
|
Path string
|
|
|
|
}
|
|
|
|
cases := map[string]T{
|
2014-08-05 20:33:25 +00:00
|
|
|
"PATCH method": {"PATCH", "/prefix/version/foo"},
|
|
|
|
"GET long prefix": {"GET", "/prefix/"},
|
|
|
|
"GET missing storage": {"GET", "/prefix/version/blah"},
|
|
|
|
"GET with extra segment": {"GET", "/prefix/version/foo/bar/baz"},
|
|
|
|
"POST with extra segment": {"POST", "/prefix/version/foo/bar"},
|
|
|
|
"DELETE without extra segment": {"DELETE", "/prefix/version/foo"},
|
|
|
|
"DELETE with extra segment": {"DELETE", "/prefix/version/foo/bar/baz"},
|
|
|
|
"PUT without extra segment": {"PUT", "/prefix/version/foo"},
|
|
|
|
"PUT with extra segment": {"PUT", "/prefix/version/foo/bar/baz"},
|
|
|
|
"watch missing storage": {"GET", "/prefix/version/watch/"},
|
|
|
|
"watch with bad method": {"POST", "/prefix/version/watch/foo/bar"},
|
2014-07-31 18:16:13 +00:00
|
|
|
}
|
2014-08-09 21:12:55 +00:00
|
|
|
handler := Handle(map[string]RESTStorage{
|
2014-07-31 18:16:13 +00:00
|
|
|
"foo": &SimpleRESTStorage{},
|
2014-09-26 00:20:28 +00:00
|
|
|
}, codec, "/prefix/version", selfLinker)
|
2014-07-31 18:16:13 +00:00
|
|
|
server := httptest.NewServer(handler)
|
2014-10-31 01:15:44 +00:00
|
|
|
defer server.Close()
|
2014-07-31 18:16:13 +00:00
|
|
|
client := http.Client{}
|
|
|
|
for k, v := range cases {
|
|
|
|
request, err := http.NewRequest(v.Method, server.URL+v.Path, nil)
|
2014-08-04 00:01:15 +00:00
|
|
|
if err != nil {
|
2014-10-20 22:23:28 +00:00
|
|
|
t.Fatalf("unexpected error: %v", err)
|
2014-08-04 00:01:15 +00:00
|
|
|
}
|
|
|
|
|
2014-07-31 18:16:13 +00:00
|
|
|
response, err := client.Do(request)
|
2014-08-04 00:01:15 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Errorf("unexpected error: %v", err)
|
|
|
|
}
|
|
|
|
|
2014-07-31 18:16:13 +00:00
|
|
|
if response.StatusCode != http.StatusNotFound {
|
|
|
|
t.Errorf("Expected %d for %s (%s), Got %#v", http.StatusNotFound, v, k, response)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestVersion(t *testing.T) {
|
2014-09-26 00:20:28 +00:00
|
|
|
handler := Handle(map[string]RESTStorage{}, codec, "/prefix/version", selfLinker)
|
2014-07-31 18:16:13 +00:00
|
|
|
server := httptest.NewServer(handler)
|
2014-10-31 01:15:44 +00:00
|
|
|
defer server.Close()
|
2014-07-31 18:16:13 +00:00
|
|
|
client := http.Client{}
|
|
|
|
|
|
|
|
request, err := http.NewRequest("GET", server.URL+"/version", nil)
|
2014-08-04 00:01:15 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Errorf("unexpected error: %v", err)
|
|
|
|
}
|
|
|
|
|
2014-07-31 18:16:13 +00:00
|
|
|
response, err := client.Do(request)
|
2014-08-04 00:01:15 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Errorf("unexpected error: %v", err)
|
|
|
|
}
|
|
|
|
|
2014-07-31 18:16:13 +00:00
|
|
|
var info version.Info
|
|
|
|
err = json.NewDecoder(response.Body).Decode(&info)
|
2014-08-04 00:01:15 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Errorf("unexpected error: %v", err)
|
|
|
|
}
|
|
|
|
|
2014-07-31 18:16:13 +00:00
|
|
|
if !reflect.DeepEqual(version.Get(), info) {
|
|
|
|
t.Errorf("Expected %#v, Got %#v", version.Get(), info)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-06-06 23:40:48 +00:00
|
|
|
func TestSimpleList(t *testing.T) {
|
|
|
|
storage := map[string]RESTStorage{}
|
|
|
|
simpleStorage := SimpleRESTStorage{}
|
|
|
|
storage["simple"] = &simpleStorage
|
2014-09-26 00:20:28 +00:00
|
|
|
selfLinker := &setTestSelfLinker{
|
|
|
|
t: t,
|
|
|
|
expectedSet: "/prefix/version/simple",
|
|
|
|
}
|
|
|
|
handler := Handle(storage, codec, "/prefix/version", selfLinker)
|
2014-06-06 23:40:48 +00:00
|
|
|
server := httptest.NewServer(handler)
|
2014-10-31 01:15:44 +00:00
|
|
|
defer server.Close()
|
2014-06-06 23:40:48 +00:00
|
|
|
|
|
|
|
resp, err := http.Get(server.URL + "/prefix/version/simple")
|
2014-08-04 00:01:15 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Errorf("unexpected error: %v", err)
|
|
|
|
}
|
2014-06-06 23:40:48 +00:00
|
|
|
|
2014-07-16 05:27:15 +00:00
|
|
|
if resp.StatusCode != http.StatusOK {
|
|
|
|
t.Errorf("Unexpected status: %d, Expected: %d, %#v", resp.StatusCode, http.StatusOK, resp)
|
2014-06-06 23:40:48 +00:00
|
|
|
}
|
2014-09-26 00:20:28 +00:00
|
|
|
if !selfLinker.called {
|
|
|
|
t.Errorf("Never set self link")
|
|
|
|
}
|
2014-06-06 23:40:48 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestErrorList(t *testing.T) {
|
|
|
|
storage := map[string]RESTStorage{}
|
|
|
|
simpleStorage := SimpleRESTStorage{
|
2014-07-16 05:27:15 +00:00
|
|
|
errors: map[string]error{"list": fmt.Errorf("test Error")},
|
2014-06-06 23:40:48 +00:00
|
|
|
}
|
|
|
|
storage["simple"] = &simpleStorage
|
2014-09-26 00:20:28 +00:00
|
|
|
handler := Handle(storage, codec, "/prefix/version", selfLinker)
|
2014-06-06 23:40:48 +00:00
|
|
|
server := httptest.NewServer(handler)
|
2014-10-31 01:15:44 +00:00
|
|
|
defer server.Close()
|
2014-06-06 23:40:48 +00:00
|
|
|
|
|
|
|
resp, err := http.Get(server.URL + "/prefix/version/simple")
|
2014-08-04 00:01:15 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Errorf("unexpected error: %v", err)
|
|
|
|
}
|
2014-06-06 23:40:48 +00:00
|
|
|
|
2014-07-16 05:27:15 +00:00
|
|
|
if resp.StatusCode != http.StatusInternalServerError {
|
|
|
|
t.Errorf("Unexpected status: %d, Expected: %d, %#v", resp.StatusCode, http.StatusOK, resp)
|
2014-06-06 23:40:48 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestNonEmptyList(t *testing.T) {
|
|
|
|
storage := map[string]RESTStorage{}
|
|
|
|
simpleStorage := SimpleRESTStorage{
|
|
|
|
list: []Simple{
|
2014-06-12 21:09:40 +00:00
|
|
|
{
|
2014-10-07 15:12:16 +00:00
|
|
|
TypeMeta: api.TypeMeta{Kind: "Simple"},
|
2014-10-22 17:02:02 +00:00
|
|
|
Other: "foo",
|
2014-06-06 23:40:48 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
storage["simple"] = &simpleStorage
|
2014-09-26 00:20:28 +00:00
|
|
|
handler := Handle(storage, codec, "/prefix/version", selfLinker)
|
2014-06-06 23:40:48 +00:00
|
|
|
server := httptest.NewServer(handler)
|
2014-10-31 01:15:44 +00:00
|
|
|
defer server.Close()
|
2014-06-06 23:40:48 +00:00
|
|
|
|
|
|
|
resp, err := http.Get(server.URL + "/prefix/version/simple")
|
2014-08-04 00:01:15 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Errorf("unexpected error: %v", err)
|
|
|
|
}
|
2014-06-06 23:40:48 +00:00
|
|
|
|
2014-07-16 05:27:15 +00:00
|
|
|
if resp.StatusCode != http.StatusOK {
|
|
|
|
t.Errorf("Unexpected status: %d, Expected: %d, %#v", resp.StatusCode, http.StatusOK, resp)
|
2014-10-23 20:51:34 +00:00
|
|
|
body, _ := ioutil.ReadAll(resp.Body)
|
|
|
|
t.Logf("Data: %s", string(body))
|
2014-06-06 23:40:48 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
var listOut SimpleList
|
|
|
|
body, err := extractBody(resp, &listOut)
|
2014-08-04 00:01:15 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Errorf("unexpected error: %v", err)
|
|
|
|
}
|
|
|
|
|
2014-06-06 23:40:48 +00:00
|
|
|
if len(listOut.Items) != 1 {
|
|
|
|
t.Errorf("Unexpected response: %#v", listOut)
|
2014-06-20 23:18:36 +00:00
|
|
|
return
|
2014-06-06 23:40:48 +00:00
|
|
|
}
|
2014-10-22 17:02:02 +00:00
|
|
|
if listOut.Items[0].Other != simpleStorage.list[0].Other {
|
2014-06-06 23:40:48 +00:00
|
|
|
t.Errorf("Unexpected data: %#v, %s", listOut.Items[0], string(body))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestGet(t *testing.T) {
|
|
|
|
storage := map[string]RESTStorage{}
|
|
|
|
simpleStorage := SimpleRESTStorage{
|
|
|
|
item: Simple{
|
2014-10-22 17:02:02 +00:00
|
|
|
Other: "foo",
|
2014-06-06 23:40:48 +00:00
|
|
|
},
|
|
|
|
}
|
2014-09-26 00:20:28 +00:00
|
|
|
selfLinker := &setTestSelfLinker{
|
|
|
|
t: t,
|
|
|
|
expectedSet: "/prefix/version/simple/id",
|
|
|
|
}
|
2014-06-06 23:40:48 +00:00
|
|
|
storage["simple"] = &simpleStorage
|
2014-09-26 00:20:28 +00:00
|
|
|
handler := Handle(storage, codec, "/prefix/version", selfLinker)
|
2014-06-06 23:40:48 +00:00
|
|
|
server := httptest.NewServer(handler)
|
2014-10-31 01:15:44 +00:00
|
|
|
defer server.Close()
|
2014-06-06 23:40:48 +00:00
|
|
|
|
|
|
|
resp, err := http.Get(server.URL + "/prefix/version/simple/id")
|
|
|
|
var itemOut Simple
|
|
|
|
body, err := extractBody(resp, &itemOut)
|
2014-08-04 00:01:15 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Errorf("unexpected error: %v", err)
|
|
|
|
}
|
|
|
|
|
2014-06-06 23:40:48 +00:00
|
|
|
if itemOut.Name != simpleStorage.item.Name {
|
|
|
|
t.Errorf("Unexpected data: %#v, expected %#v (%s)", itemOut, simpleStorage.item, string(body))
|
|
|
|
}
|
2014-09-26 00:20:28 +00:00
|
|
|
if !selfLinker.called {
|
|
|
|
t.Errorf("Never set self link")
|
|
|
|
}
|
2014-06-06 23:40:48 +00:00
|
|
|
}
|
|
|
|
|
2014-07-16 05:27:15 +00:00
|
|
|
func TestGetMissing(t *testing.T) {
|
|
|
|
storage := map[string]RESTStorage{}
|
|
|
|
simpleStorage := SimpleRESTStorage{
|
2014-09-03 21:16:00 +00:00
|
|
|
errors: map[string]error{"get": apierrs.NewNotFound("simple", "id")},
|
2014-07-16 05:27:15 +00:00
|
|
|
}
|
|
|
|
storage["simple"] = &simpleStorage
|
2014-09-26 00:20:28 +00:00
|
|
|
handler := Handle(storage, codec, "/prefix/version", selfLinker)
|
2014-07-16 05:27:15 +00:00
|
|
|
server := httptest.NewServer(handler)
|
2014-10-31 01:15:44 +00:00
|
|
|
defer server.Close()
|
2014-07-16 05:27:15 +00:00
|
|
|
|
|
|
|
resp, err := http.Get(server.URL + "/prefix/version/simple/id")
|
2014-08-04 00:01:15 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Errorf("unexpected error: %v", err)
|
|
|
|
}
|
|
|
|
|
2014-07-16 05:27:15 +00:00
|
|
|
if resp.StatusCode != http.StatusNotFound {
|
|
|
|
t.Errorf("Unexpected response %#v", resp)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-06-06 23:40:48 +00:00
|
|
|
func TestDelete(t *testing.T) {
|
|
|
|
storage := map[string]RESTStorage{}
|
|
|
|
simpleStorage := SimpleRESTStorage{}
|
|
|
|
ID := "id"
|
|
|
|
storage["simple"] = &simpleStorage
|
2014-09-26 00:20:28 +00:00
|
|
|
handler := Handle(storage, codec, "/prefix/version", selfLinker)
|
2014-06-06 23:40:48 +00:00
|
|
|
server := httptest.NewServer(handler)
|
2014-10-31 01:15:44 +00:00
|
|
|
defer server.Close()
|
2014-06-06 23:40:48 +00:00
|
|
|
|
|
|
|
client := http.Client{}
|
|
|
|
request, err := http.NewRequest("DELETE", server.URL+"/prefix/version/simple/"+ID, nil)
|
|
|
|
_, err = client.Do(request)
|
2014-08-04 00:01:15 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Errorf("unexpected error: %v", err)
|
|
|
|
}
|
|
|
|
|
2014-06-06 23:40:48 +00:00
|
|
|
if simpleStorage.deleted != ID {
|
2014-07-18 19:03:22 +00:00
|
|
|
t.Errorf("Unexpected delete: %s, expected %s", simpleStorage.deleted, ID)
|
2014-06-06 23:40:48 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-07-16 05:27:15 +00:00
|
|
|
func TestDeleteMissing(t *testing.T) {
|
|
|
|
storage := map[string]RESTStorage{}
|
|
|
|
ID := "id"
|
|
|
|
simpleStorage := SimpleRESTStorage{
|
2014-09-03 21:16:00 +00:00
|
|
|
errors: map[string]error{"delete": apierrs.NewNotFound("simple", ID)},
|
2014-07-16 05:27:15 +00:00
|
|
|
}
|
|
|
|
storage["simple"] = &simpleStorage
|
2014-09-26 00:20:28 +00:00
|
|
|
handler := Handle(storage, codec, "/prefix/version", selfLinker)
|
2014-07-16 05:27:15 +00:00
|
|
|
server := httptest.NewServer(handler)
|
2014-10-31 01:15:44 +00:00
|
|
|
defer server.Close()
|
2014-07-16 05:27:15 +00:00
|
|
|
|
|
|
|
client := http.Client{}
|
|
|
|
request, err := http.NewRequest("DELETE", server.URL+"/prefix/version/simple/"+ID, nil)
|
|
|
|
response, err := client.Do(request)
|
2014-08-04 00:01:15 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Errorf("unexpected error: %v", err)
|
|
|
|
}
|
|
|
|
|
2014-07-16 05:27:15 +00:00
|
|
|
if response.StatusCode != http.StatusNotFound {
|
|
|
|
t.Errorf("Unexpected response %#v", response)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-06-06 23:40:48 +00:00
|
|
|
func TestUpdate(t *testing.T) {
|
|
|
|
storage := map[string]RESTStorage{}
|
|
|
|
simpleStorage := SimpleRESTStorage{}
|
|
|
|
ID := "id"
|
|
|
|
storage["simple"] = &simpleStorage
|
2014-09-26 00:20:28 +00:00
|
|
|
selfLinker := &setTestSelfLinker{
|
|
|
|
t: t,
|
|
|
|
expectedSet: "/prefix/version/simple/" + ID,
|
|
|
|
}
|
|
|
|
handler := Handle(storage, codec, "/prefix/version", selfLinker)
|
2014-06-06 23:40:48 +00:00
|
|
|
server := httptest.NewServer(handler)
|
2014-10-31 01:15:44 +00:00
|
|
|
defer server.Close()
|
2014-06-06 23:40:48 +00:00
|
|
|
|
2014-09-08 04:14:18 +00:00
|
|
|
item := &Simple{
|
2014-10-22 17:02:02 +00:00
|
|
|
Other: "bar",
|
2014-06-06 23:40:48 +00:00
|
|
|
}
|
2014-08-06 03:10:48 +00:00
|
|
|
body, err := codec.Encode(item)
|
2014-08-04 00:01:15 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Errorf("unexpected error: %v", err)
|
|
|
|
}
|
|
|
|
|
2014-06-06 23:40:48 +00:00
|
|
|
client := http.Client{}
|
|
|
|
request, err := http.NewRequest("PUT", server.URL+"/prefix/version/simple/"+ID, bytes.NewReader(body))
|
|
|
|
_, err = client.Do(request)
|
2014-08-04 00:01:15 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Errorf("unexpected error: %v", err)
|
|
|
|
}
|
|
|
|
|
2014-06-06 23:40:48 +00:00
|
|
|
if simpleStorage.updated.Name != item.Name {
|
|
|
|
t.Errorf("Unexpected update value %#v, expected %#v.", simpleStorage.updated, item)
|
|
|
|
}
|
2014-09-26 00:20:28 +00:00
|
|
|
if !selfLinker.called {
|
|
|
|
t.Errorf("Never set self link")
|
|
|
|
}
|
2014-06-06 23:40:48 +00:00
|
|
|
}
|
|
|
|
|
2014-07-16 05:27:15 +00:00
|
|
|
func TestUpdateMissing(t *testing.T) {
|
|
|
|
storage := map[string]RESTStorage{}
|
|
|
|
ID := "id"
|
|
|
|
simpleStorage := SimpleRESTStorage{
|
2014-09-03 21:16:00 +00:00
|
|
|
errors: map[string]error{"update": apierrs.NewNotFound("simple", ID)},
|
2014-07-16 05:27:15 +00:00
|
|
|
}
|
|
|
|
storage["simple"] = &simpleStorage
|
2014-09-26 00:20:28 +00:00
|
|
|
handler := Handle(storage, codec, "/prefix/version", selfLinker)
|
2014-07-16 05:27:15 +00:00
|
|
|
server := httptest.NewServer(handler)
|
2014-10-31 01:15:44 +00:00
|
|
|
defer server.Close()
|
2014-07-16 05:27:15 +00:00
|
|
|
|
2014-09-08 04:14:18 +00:00
|
|
|
item := &Simple{
|
2014-10-22 17:02:02 +00:00
|
|
|
Other: "bar",
|
2014-07-16 05:27:15 +00:00
|
|
|
}
|
2014-08-06 03:10:48 +00:00
|
|
|
body, err := codec.Encode(item)
|
2014-08-04 00:01:15 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Errorf("unexpected error: %v", err)
|
|
|
|
}
|
|
|
|
|
2014-07-16 05:27:15 +00:00
|
|
|
client := http.Client{}
|
|
|
|
request, err := http.NewRequest("PUT", server.URL+"/prefix/version/simple/"+ID, bytes.NewReader(body))
|
|
|
|
response, err := client.Do(request)
|
2014-08-04 00:01:15 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Errorf("unexpected error: %v", err)
|
|
|
|
}
|
|
|
|
|
2014-07-16 05:27:15 +00:00
|
|
|
if response.StatusCode != http.StatusNotFound {
|
|
|
|
t.Errorf("Unexpected response %#v", response)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-06-06 23:40:48 +00:00
|
|
|
func TestCreate(t *testing.T) {
|
2014-11-02 20:38:31 +00:00
|
|
|
wait := sync.WaitGroup{}
|
|
|
|
wait.Add(1)
|
|
|
|
simpleStorage := &SimpleRESTStorage{
|
|
|
|
injectedFunction: func(obj runtime.Object) (returnObj runtime.Object, err error) {
|
|
|
|
wait.Wait()
|
|
|
|
return &Simple{}, nil
|
|
|
|
},
|
|
|
|
}
|
2014-08-09 21:12:55 +00:00
|
|
|
handler := Handle(map[string]RESTStorage{
|
2014-06-25 20:21:32 +00:00
|
|
|
"foo": simpleStorage,
|
2014-09-26 00:20:28 +00:00
|
|
|
}, codec, "/prefix/version", selfLinker)
|
2014-08-09 21:12:55 +00:00
|
|
|
handler.(*defaultAPIServer).group.handler.asyncOpWait = 0
|
2014-06-06 23:40:48 +00:00
|
|
|
server := httptest.NewServer(handler)
|
2014-10-31 01:15:44 +00:00
|
|
|
defer server.Close()
|
2014-06-06 23:40:48 +00:00
|
|
|
client := http.Client{}
|
|
|
|
|
2014-09-08 04:14:18 +00:00
|
|
|
simple := &Simple{
|
2014-10-22 17:02:02 +00:00
|
|
|
Other: "foo",
|
2014-07-16 21:30:28 +00:00
|
|
|
}
|
2014-08-06 03:10:48 +00:00
|
|
|
data, _ := codec.Encode(simple)
|
2014-06-06 23:40:48 +00:00
|
|
|
request, err := http.NewRequest("POST", server.URL+"/prefix/version/foo", bytes.NewBuffer(data))
|
2014-08-04 00:01:15 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Errorf("unexpected error: %v", err)
|
|
|
|
}
|
|
|
|
|
2014-06-06 23:40:48 +00:00
|
|
|
response, err := client.Do(request)
|
2014-08-04 00:01:15 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Errorf("unexpected error: %v", err)
|
|
|
|
}
|
|
|
|
|
2014-06-19 04:04:11 +00:00
|
|
|
if response.StatusCode != http.StatusAccepted {
|
2014-06-06 23:40:48 +00:00
|
|
|
t.Errorf("Unexpected response %#v", response)
|
|
|
|
}
|
|
|
|
|
2014-06-25 20:21:32 +00:00
|
|
|
var itemOut api.Status
|
2014-06-06 23:40:48 +00:00
|
|
|
body, err := extractBody(response, &itemOut)
|
2014-08-04 00:01:15 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Errorf("unexpected error: %v", err)
|
|
|
|
}
|
|
|
|
|
Evolve the api.Status object with Reason/Details
Contains breaking API change on api.Status#Details (type change)
Turn Details from string -> StatusDetails - a general
bucket for keyed error behavior. Define an open enumeration
ReasonType exposed as Reason on the status object to provide
machine readable subcategorization beyond HTTP Status Code. Define
a human readable field Message which is common convention (previously
this was joined into Details).
Precedence order: HTTP Status Code, Reason, Details. apiserver would
impose restraints on the ReasonTypes defined by the main apiobject,
and ensure their use is consistent.
There are four long term scenarios this change supports:
1. Allow a client access to a machine readable field that can be
easily switched on for improving or translating the generic
server Message.
2. Return a 404 when a composite operation on multiple resources
fails with enough data so that a client can distinguish which
item does not exist. E.g. resource Parent and resource Child,
POST /parents/1/children to create a new Child, but /parents/1
is deleted. POST returns 404, ReasonTypeNotFound, and
Details.ID = "1", Details.Kind = "parent"
3. Allow a client to receive validation data that is keyed by
attribute for building user facing UIs around field submission.
Validation is usually expressed as map[string][]string, but
that type is less appropriate for many other uses.
4. Allow specific API errors to return more granular failure status
for specific operations. An example might be a minion proxy,
where the operation that failed may be both proxying OR the
minion itself. In this case a reason may be defined "proxy_failed"
corresponding to 502, where the Details field may be extended
to contain a nested error object.
At this time only ID and Kind are exposed
2014-07-31 18:12:26 +00:00
|
|
|
if itemOut.Status != api.StatusWorking || itemOut.Details == nil || itemOut.Details.ID == "" {
|
2014-06-25 20:21:32 +00:00
|
|
|
t.Errorf("Unexpected status: %#v (%s)", itemOut, string(body))
|
|
|
|
}
|
2014-11-02 20:38:31 +00:00
|
|
|
wait.Done()
|
2014-06-25 20:21:32 +00:00
|
|
|
}
|
|
|
|
|
2014-07-16 05:27:15 +00:00
|
|
|
func TestCreateNotFound(t *testing.T) {
|
2014-08-09 21:12:55 +00:00
|
|
|
handler := Handle(map[string]RESTStorage{
|
2014-07-31 18:16:13 +00:00
|
|
|
"simple": &SimpleRESTStorage{
|
|
|
|
// storage.Create can fail with not found error in theory.
|
|
|
|
// See https://github.com/GoogleCloudPlatform/kubernetes/pull/486#discussion_r15037092.
|
2014-09-03 21:16:00 +00:00
|
|
|
errors: map[string]error{"create": apierrs.NewNotFound("simple", "id")},
|
2014-07-31 18:16:13 +00:00
|
|
|
},
|
2014-09-26 00:20:28 +00:00
|
|
|
}, codec, "/prefix/version", selfLinker)
|
2014-07-16 05:27:15 +00:00
|
|
|
server := httptest.NewServer(handler)
|
2014-10-31 01:15:44 +00:00
|
|
|
defer server.Close()
|
2014-07-16 05:27:15 +00:00
|
|
|
client := http.Client{}
|
|
|
|
|
2014-10-22 17:02:02 +00:00
|
|
|
simple := &Simple{Other: "foo"}
|
2014-08-06 03:10:48 +00:00
|
|
|
data, _ := codec.Encode(simple)
|
2014-07-31 18:16:13 +00:00
|
|
|
request, err := http.NewRequest("POST", server.URL+"/prefix/version/simple", bytes.NewBuffer(data))
|
2014-08-04 00:01:15 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Errorf("unexpected error: %v", err)
|
|
|
|
}
|
|
|
|
|
2014-07-16 05:27:15 +00:00
|
|
|
response, err := client.Do(request)
|
2014-08-04 00:01:15 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Errorf("unexpected error: %v", err)
|
|
|
|
}
|
|
|
|
|
2014-07-16 05:27:15 +00:00
|
|
|
if response.StatusCode != http.StatusNotFound {
|
|
|
|
t.Errorf("Unexpected response %#v", response)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-06-25 20:21:32 +00:00
|
|
|
func TestParseTimeout(t *testing.T) {
|
|
|
|
if d := parseTimeout(""); d != 30*time.Second {
|
|
|
|
t.Errorf("blank timeout produces %v", d)
|
|
|
|
}
|
|
|
|
if d := parseTimeout("not a timeout"); d != 30*time.Second {
|
|
|
|
t.Errorf("bad timeout produces %v", d)
|
|
|
|
}
|
|
|
|
if d := parseTimeout("10s"); d != 10*time.Second {
|
|
|
|
t.Errorf("10s timeout produced: %v", d)
|
2014-06-06 23:40:48 +00:00
|
|
|
}
|
|
|
|
}
|
2014-06-19 04:04:11 +00:00
|
|
|
|
2014-09-26 00:20:28 +00:00
|
|
|
type setTestSelfLinker struct {
|
|
|
|
t *testing.T
|
|
|
|
expectedSet string
|
2014-10-23 02:54:34 +00:00
|
|
|
name string
|
2014-09-26 00:20:28 +00:00
|
|
|
called bool
|
|
|
|
}
|
|
|
|
|
2014-10-23 02:54:34 +00:00
|
|
|
func (s *setTestSelfLinker) Name(runtime.Object) (string, error) { return s.name, nil }
|
2014-09-26 00:20:28 +00:00
|
|
|
func (*setTestSelfLinker) SelfLink(runtime.Object) (string, error) { return "", nil }
|
|
|
|
func (s *setTestSelfLinker) SetSelfLink(obj runtime.Object, selfLink string) error {
|
|
|
|
if e, a := s.expectedSet, selfLink; e != a {
|
|
|
|
s.t.Errorf("expected '%v', got '%v'", e, a)
|
|
|
|
}
|
|
|
|
s.called = true
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2014-06-19 04:04:11 +00:00
|
|
|
func TestSyncCreate(t *testing.T) {
|
|
|
|
storage := SimpleRESTStorage{
|
2014-09-08 04:14:18 +00:00
|
|
|
injectedFunction: func(obj runtime.Object) (runtime.Object, error) {
|
2014-07-31 18:16:13 +00:00
|
|
|
time.Sleep(5 * time.Millisecond)
|
2014-06-25 20:21:32 +00:00
|
|
|
return obj, nil
|
|
|
|
},
|
2014-06-19 04:04:11 +00:00
|
|
|
}
|
2014-09-26 00:20:28 +00:00
|
|
|
selfLinker := &setTestSelfLinker{
|
|
|
|
t: t,
|
2014-10-23 02:54:34 +00:00
|
|
|
name: "bar",
|
2014-09-26 00:20:28 +00:00
|
|
|
expectedSet: "/prefix/version/foo/bar",
|
|
|
|
}
|
2014-08-09 21:12:55 +00:00
|
|
|
handler := Handle(map[string]RESTStorage{
|
2014-06-19 04:04:11 +00:00
|
|
|
"foo": &storage,
|
2014-09-26 00:20:28 +00:00
|
|
|
}, codec, "/prefix/version", selfLinker)
|
2014-06-19 04:04:11 +00:00
|
|
|
server := httptest.NewServer(handler)
|
2014-10-31 01:15:44 +00:00
|
|
|
defer server.Close()
|
2014-06-19 04:04:11 +00:00
|
|
|
client := http.Client{}
|
|
|
|
|
2014-09-08 04:14:18 +00:00
|
|
|
simple := &Simple{
|
2014-10-22 17:02:02 +00:00
|
|
|
Other: "bar",
|
2014-07-16 21:30:28 +00:00
|
|
|
}
|
2014-08-06 03:10:48 +00:00
|
|
|
data, _ := codec.Encode(simple)
|
2014-06-19 04:04:11 +00:00
|
|
|
request, err := http.NewRequest("POST", server.URL+"/prefix/version/foo?sync=true", bytes.NewBuffer(data))
|
2014-08-04 00:01:15 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Errorf("unexpected error: %v", err)
|
|
|
|
}
|
|
|
|
|
2014-06-19 04:04:11 +00:00
|
|
|
wg := sync.WaitGroup{}
|
|
|
|
wg.Add(1)
|
|
|
|
var response *http.Response
|
|
|
|
go func() {
|
|
|
|
response, err = client.Do(request)
|
|
|
|
wg.Done()
|
|
|
|
}()
|
|
|
|
wg.Wait()
|
2014-08-04 00:01:15 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Errorf("unexpected error: %v", err)
|
|
|
|
}
|
|
|
|
|
2014-06-19 04:04:11 +00:00
|
|
|
var itemOut Simple
|
|
|
|
body, err := extractBody(response, &itemOut)
|
2014-08-04 00:01:15 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Errorf("unexpected error: %v", err)
|
|
|
|
}
|
|
|
|
|
2014-09-08 04:14:18 +00:00
|
|
|
if !reflect.DeepEqual(&itemOut, simple) {
|
2014-06-19 04:04:11 +00:00
|
|
|
t.Errorf("Unexpected data: %#v, expected %#v (%s)", itemOut, simple, string(body))
|
|
|
|
}
|
2014-06-25 20:21:32 +00:00
|
|
|
if response.StatusCode != http.StatusOK {
|
|
|
|
t.Errorf("Unexpected status: %d, Expected: %d, %#v", response.StatusCode, http.StatusOK, response)
|
|
|
|
}
|
2014-09-26 00:20:28 +00:00
|
|
|
if !selfLinker.called {
|
|
|
|
t.Errorf("Never set self link")
|
|
|
|
}
|
2014-06-19 04:04:11 +00:00
|
|
|
}
|
|
|
|
|
2014-07-31 18:16:13 +00:00
|
|
|
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 {
|
2014-07-31 18:26:34 +00:00
|
|
|
t.Fatalf("unexpected error on %s %s: %v", method, url, err)
|
2014-07-31 18:16:13 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
var status api.Status
|
|
|
|
_, err = extractBody(response, &status)
|
|
|
|
if err != nil {
|
2014-07-31 18:26:34 +00:00
|
|
|
t.Fatalf("unexpected error on %s %s: %v", method, url, err)
|
2014-07-31 18:16:13 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
if code != response.StatusCode {
|
2014-07-31 18:26:34 +00:00
|
|
|
t.Fatalf("Expected %s %s to return %d, Got %d", method, url, code, response.StatusCode)
|
2014-07-31 18:16:13 +00:00
|
|
|
}
|
|
|
|
return &status
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestAsyncDelayReturnsError(t *testing.T) {
|
|
|
|
storage := SimpleRESTStorage{
|
2014-09-08 04:14:18 +00:00
|
|
|
injectedFunction: func(obj runtime.Object) (runtime.Object, error) {
|
2014-09-03 21:16:00 +00:00
|
|
|
return nil, apierrs.NewAlreadyExists("foo", "bar")
|
2014-07-31 18:16:13 +00:00
|
|
|
},
|
|
|
|
}
|
2014-09-26 00:20:28 +00:00
|
|
|
handler := Handle(map[string]RESTStorage{"foo": &storage}, codec, "/prefix/version", selfLinker)
|
2014-08-09 21:12:55 +00:00
|
|
|
handler.(*defaultAPIServer).group.handler.asyncOpWait = time.Millisecond / 2
|
2014-07-31 18:16:13 +00:00
|
|
|
server := httptest.NewServer(handler)
|
2014-10-31 01:15:44 +00:00
|
|
|
defer server.Close()
|
2014-07-31 18:16:13 +00:00
|
|
|
|
2014-07-31 18:26:34 +00:00
|
|
|
status := expectApiStatus(t, "DELETE", fmt.Sprintf("%s/prefix/version/foo/bar", server.URL), nil, http.StatusConflict)
|
2014-08-26 17:36:09 +00:00
|
|
|
if status.Status != api.StatusFailure || status.Message == "" || status.Details == nil || status.Reason != api.StatusReasonAlreadyExists {
|
2014-07-31 18:16:13 +00:00
|
|
|
t.Errorf("Unexpected status %#v", status)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestAsyncCreateError(t *testing.T) {
|
|
|
|
ch := make(chan struct{})
|
|
|
|
storage := SimpleRESTStorage{
|
2014-09-08 04:14:18 +00:00
|
|
|
injectedFunction: func(obj runtime.Object) (runtime.Object, error) {
|
2014-07-31 18:16:13 +00:00
|
|
|
<-ch
|
2014-09-03 21:16:00 +00:00
|
|
|
return nil, apierrs.NewAlreadyExists("foo", "bar")
|
2014-07-31 18:16:13 +00:00
|
|
|
},
|
|
|
|
}
|
2014-09-26 00:20:28 +00:00
|
|
|
selfLinker := &setTestSelfLinker{
|
|
|
|
t: t,
|
2014-10-23 02:54:34 +00:00
|
|
|
name: "bar",
|
2014-09-26 00:20:28 +00:00
|
|
|
expectedSet: "/prefix/version/foo/bar",
|
|
|
|
}
|
|
|
|
handler := Handle(map[string]RESTStorage{"foo": &storage}, codec, "/prefix/version", selfLinker)
|
2014-08-09 21:12:55 +00:00
|
|
|
handler.(*defaultAPIServer).group.handler.asyncOpWait = 0
|
2014-07-31 18:16:13 +00:00
|
|
|
server := httptest.NewServer(handler)
|
2014-10-31 01:15:44 +00:00
|
|
|
defer server.Close()
|
2014-07-31 18:16:13 +00:00
|
|
|
|
2014-10-22 17:02:02 +00:00
|
|
|
simple := &Simple{Other: "bar"}
|
2014-08-06 03:10:48 +00:00
|
|
|
data, _ := codec.Encode(simple)
|
2014-07-31 18:16:13 +00:00
|
|
|
|
|
|
|
status := expectApiStatus(t, "POST", fmt.Sprintf("%s/prefix/version/foo", server.URL), data, http.StatusAccepted)
|
Evolve the api.Status object with Reason/Details
Contains breaking API change on api.Status#Details (type change)
Turn Details from string -> StatusDetails - a general
bucket for keyed error behavior. Define an open enumeration
ReasonType exposed as Reason on the status object to provide
machine readable subcategorization beyond HTTP Status Code. Define
a human readable field Message which is common convention (previously
this was joined into Details).
Precedence order: HTTP Status Code, Reason, Details. apiserver would
impose restraints on the ReasonTypes defined by the main apiobject,
and ensure their use is consistent.
There are four long term scenarios this change supports:
1. Allow a client access to a machine readable field that can be
easily switched on for improving or translating the generic
server Message.
2. Return a 404 when a composite operation on multiple resources
fails with enough data so that a client can distinguish which
item does not exist. E.g. resource Parent and resource Child,
POST /parents/1/children to create a new Child, but /parents/1
is deleted. POST returns 404, ReasonTypeNotFound, and
Details.ID = "1", Details.Kind = "parent"
3. Allow a client to receive validation data that is keyed by
attribute for building user facing UIs around field submission.
Validation is usually expressed as map[string][]string, but
that type is less appropriate for many other uses.
4. Allow specific API errors to return more granular failure status
for specific operations. An example might be a minion proxy,
where the operation that failed may be both proxying OR the
minion itself. In this case a reason may be defined "proxy_failed"
corresponding to 502, where the Details field may be extended
to contain a nested error object.
At this time only ID and Kind are exposed
2014-07-31 18:12:26 +00:00
|
|
|
if status.Status != api.StatusWorking || status.Details == nil || status.Details.ID == "" {
|
2014-07-31 18:16:13 +00:00
|
|
|
t.Errorf("Unexpected status %#v", status)
|
|
|
|
}
|
|
|
|
|
Evolve the api.Status object with Reason/Details
Contains breaking API change on api.Status#Details (type change)
Turn Details from string -> StatusDetails - a general
bucket for keyed error behavior. Define an open enumeration
ReasonType exposed as Reason on the status object to provide
machine readable subcategorization beyond HTTP Status Code. Define
a human readable field Message which is common convention (previously
this was joined into Details).
Precedence order: HTTP Status Code, Reason, Details. apiserver would
impose restraints on the ReasonTypes defined by the main apiobject,
and ensure their use is consistent.
There are four long term scenarios this change supports:
1. Allow a client access to a machine readable field that can be
easily switched on for improving or translating the generic
server Message.
2. Return a 404 when a composite operation on multiple resources
fails with enough data so that a client can distinguish which
item does not exist. E.g. resource Parent and resource Child,
POST /parents/1/children to create a new Child, but /parents/1
is deleted. POST returns 404, ReasonTypeNotFound, and
Details.ID = "1", Details.Kind = "parent"
3. Allow a client to receive validation data that is keyed by
attribute for building user facing UIs around field submission.
Validation is usually expressed as map[string][]string, but
that type is less appropriate for many other uses.
4. Allow specific API errors to return more granular failure status
for specific operations. An example might be a minion proxy,
where the operation that failed may be both proxying OR the
minion itself. In this case a reason may be defined "proxy_failed"
corresponding to 502, where the Details field may be extended
to contain a nested error object.
At this time only ID and Kind are exposed
2014-07-31 18:12:26 +00:00
|
|
|
otherStatus := expectApiStatus(t, "GET", fmt.Sprintf("%s/prefix/version/operations/%s", server.URL, status.Details.ID), []byte{}, http.StatusAccepted)
|
2014-07-31 18:16:13 +00:00
|
|
|
if !reflect.DeepEqual(status, otherStatus) {
|
|
|
|
t.Errorf("Expected %#v, Got %#v", status, otherStatus)
|
|
|
|
}
|
|
|
|
|
|
|
|
ch <- struct{}{}
|
|
|
|
time.Sleep(time.Millisecond)
|
|
|
|
|
Evolve the api.Status object with Reason/Details
Contains breaking API change on api.Status#Details (type change)
Turn Details from string -> StatusDetails - a general
bucket for keyed error behavior. Define an open enumeration
ReasonType exposed as Reason on the status object to provide
machine readable subcategorization beyond HTTP Status Code. Define
a human readable field Message which is common convention (previously
this was joined into Details).
Precedence order: HTTP Status Code, Reason, Details. apiserver would
impose restraints on the ReasonTypes defined by the main apiobject,
and ensure their use is consistent.
There are four long term scenarios this change supports:
1. Allow a client access to a machine readable field that can be
easily switched on for improving or translating the generic
server Message.
2. Return a 404 when a composite operation on multiple resources
fails with enough data so that a client can distinguish which
item does not exist. E.g. resource Parent and resource Child,
POST /parents/1/children to create a new Child, but /parents/1
is deleted. POST returns 404, ReasonTypeNotFound, and
Details.ID = "1", Details.Kind = "parent"
3. Allow a client to receive validation data that is keyed by
attribute for building user facing UIs around field submission.
Validation is usually expressed as map[string][]string, but
that type is less appropriate for many other uses.
4. Allow specific API errors to return more granular failure status
for specific operations. An example might be a minion proxy,
where the operation that failed may be both proxying OR the
minion itself. In this case a reason may be defined "proxy_failed"
corresponding to 502, where the Details field may be extended
to contain a nested error object.
At this time only ID and Kind are exposed
2014-07-31 18:12:26 +00:00
|
|
|
finalStatus := expectApiStatus(t, "GET", fmt.Sprintf("%s/prefix/version/operations/%s?after=1", server.URL, status.Details.ID), []byte{}, http.StatusOK)
|
2014-09-03 21:16:00 +00:00
|
|
|
expectedErr := apierrs.NewAlreadyExists("foo", "bar")
|
2014-07-31 18:16:13 +00:00
|
|
|
expectedStatus := &api.Status{
|
|
|
|
Status: api.StatusFailure,
|
2014-07-31 18:26:34 +00:00
|
|
|
Code: http.StatusConflict,
|
2014-09-28 04:32:53 +00:00
|
|
|
Reason: "AlreadyExists",
|
2014-07-31 18:26:34 +00:00
|
|
|
Message: expectedErr.Error(),
|
|
|
|
Details: &api.StatusDetails{
|
|
|
|
Kind: "foo",
|
|
|
|
ID: "bar",
|
|
|
|
},
|
2014-07-31 18:16:13 +00:00
|
|
|
}
|
|
|
|
if !reflect.DeepEqual(expectedStatus, finalStatus) {
|
|
|
|
t.Errorf("Expected %#v, Got %#v", expectedStatus, finalStatus)
|
2014-07-31 18:26:34 +00:00
|
|
|
if finalStatus.Details != nil {
|
|
|
|
t.Logf("Details %#v, Got %#v", *expectedStatus.Details, *finalStatus.Details)
|
|
|
|
}
|
2014-07-31 18:16:13 +00:00
|
|
|
}
|
2014-09-26 00:20:28 +00:00
|
|
|
if !selfLinker.called {
|
|
|
|
t.Errorf("Never set self link")
|
|
|
|
}
|
2014-07-31 18:16:13 +00:00
|
|
|
}
|
|
|
|
|
2014-09-08 04:14:18 +00:00
|
|
|
type UnregisteredAPIObject struct {
|
|
|
|
Value string
|
|
|
|
}
|
|
|
|
|
|
|
|
func (*UnregisteredAPIObject) IsAnAPIObject() {}
|
|
|
|
|
2014-07-31 18:16:13 +00:00
|
|
|
func TestWriteJSONDecodeError(t *testing.T) {
|
|
|
|
server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, req *http.Request) {
|
2014-09-16 19:56:26 +00:00
|
|
|
writeJSON(http.StatusOK, latest.Codec, &UnregisteredAPIObject{"Undecodable"}, w)
|
2014-07-31 18:16:13 +00:00
|
|
|
}))
|
2014-10-31 01:15:44 +00:00
|
|
|
defer server.Close()
|
2014-07-31 18:26:34 +00:00
|
|
|
status := expectApiStatus(t, "GET", server.URL, nil, http.StatusInternalServerError)
|
2014-08-26 17:36:09 +00:00
|
|
|
if status.Reason != api.StatusReasonUnknown {
|
2014-07-31 18:26:34 +00:00
|
|
|
t.Errorf("unexpected reason %#v", status)
|
2014-08-04 00:01:15 +00:00
|
|
|
}
|
2014-09-08 04:14:18 +00:00
|
|
|
if !strings.Contains(status.Message, "type apiserver.UnregisteredAPIObject is not registered") {
|
2014-07-31 18:26:34 +00:00
|
|
|
t.Errorf("unexpected message %#v", status)
|
2014-07-31 18:16:13 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
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)
|
|
|
|
}))
|
2014-10-31 01:15:44 +00:00
|
|
|
defer server.Close()
|
2014-07-31 18:16:13 +00:00
|
|
|
client := http.Client{}
|
|
|
|
resp, err := client.Get(server.URL)
|
2014-08-04 00:01:15 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Errorf("unexpected error: %v", err)
|
|
|
|
}
|
|
|
|
|
2014-07-31 18:16:13 +00:00
|
|
|
if resp.StatusCode != http.StatusInternalServerError {
|
|
|
|
t.Errorf("unexpected status code %d", resp.StatusCode)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-06-19 04:04:11 +00:00
|
|
|
func TestSyncCreateTimeout(t *testing.T) {
|
2014-08-12 00:12:53 +00:00
|
|
|
testOver := make(chan struct{})
|
|
|
|
defer close(testOver)
|
2014-06-25 20:21:32 +00:00
|
|
|
storage := SimpleRESTStorage{
|
2014-09-08 04:14:18 +00:00
|
|
|
injectedFunction: func(obj runtime.Object) (runtime.Object, error) {
|
2014-08-12 00:12:53 +00:00
|
|
|
// Eliminate flakes by ensuring the create operation takes longer than this test.
|
|
|
|
<-testOver
|
2014-06-25 20:21:32 +00:00
|
|
|
return obj, nil
|
|
|
|
},
|
|
|
|
}
|
2014-08-09 21:12:55 +00:00
|
|
|
handler := Handle(map[string]RESTStorage{
|
2014-06-25 20:21:32 +00:00
|
|
|
"foo": &storage,
|
2014-09-26 00:20:28 +00:00
|
|
|
}, codec, "/prefix/version", selfLinker)
|
2014-06-19 04:04:11 +00:00
|
|
|
server := httptest.NewServer(handler)
|
2014-10-31 01:15:44 +00:00
|
|
|
defer server.Close()
|
2014-06-19 04:04:11 +00:00
|
|
|
|
2014-10-22 17:02:02 +00:00
|
|
|
simple := &Simple{Other: "foo"}
|
2014-08-06 03:10:48 +00:00
|
|
|
data, _ := codec.Encode(simple)
|
2014-07-31 18:16:13 +00:00
|
|
|
itemOut := expectApiStatus(t, "POST", server.URL+"/prefix/version/foo?sync=true&timeout=4ms", data, http.StatusAccepted)
|
Evolve the api.Status object with Reason/Details
Contains breaking API change on api.Status#Details (type change)
Turn Details from string -> StatusDetails - a general
bucket for keyed error behavior. Define an open enumeration
ReasonType exposed as Reason on the status object to provide
machine readable subcategorization beyond HTTP Status Code. Define
a human readable field Message which is common convention (previously
this was joined into Details).
Precedence order: HTTP Status Code, Reason, Details. apiserver would
impose restraints on the ReasonTypes defined by the main apiobject,
and ensure their use is consistent.
There are four long term scenarios this change supports:
1. Allow a client access to a machine readable field that can be
easily switched on for improving or translating the generic
server Message.
2. Return a 404 when a composite operation on multiple resources
fails with enough data so that a client can distinguish which
item does not exist. E.g. resource Parent and resource Child,
POST /parents/1/children to create a new Child, but /parents/1
is deleted. POST returns 404, ReasonTypeNotFound, and
Details.ID = "1", Details.Kind = "parent"
3. Allow a client to receive validation data that is keyed by
attribute for building user facing UIs around field submission.
Validation is usually expressed as map[string][]string, but
that type is less appropriate for many other uses.
4. Allow specific API errors to return more granular failure status
for specific operations. An example might be a minion proxy,
where the operation that failed may be both proxying OR the
minion itself. In this case a reason may be defined "proxy_failed"
corresponding to 502, where the Details field may be extended
to contain a nested error object.
At this time only ID and Kind are exposed
2014-07-31 18:12:26 +00:00
|
|
|
if itemOut.Status != api.StatusWorking || itemOut.Details == nil || itemOut.Details.ID == "" {
|
2014-06-25 20:21:32 +00:00
|
|
|
t.Errorf("Unexpected status %#v", itemOut)
|
|
|
|
}
|
2014-06-19 04:04:11 +00:00
|
|
|
}
|
2014-09-02 16:28:24 +00:00
|
|
|
|
2014-09-09 21:05:18 +00:00
|
|
|
func TestCORSAllowedOrigins(t *testing.T) {
|
|
|
|
table := []struct {
|
|
|
|
allowedOrigins util.StringList
|
|
|
|
origin string
|
|
|
|
allowed bool
|
|
|
|
}{
|
|
|
|
{[]string{}, "example.com", false},
|
|
|
|
{[]string{"example.com"}, "example.com", true},
|
|
|
|
{[]string{"example.com"}, "not-allowed.com", false},
|
|
|
|
{[]string{"not-matching.com", "example.com"}, "example.com", true},
|
|
|
|
{[]string{".*"}, "example.com", true},
|
2014-09-03 18:33:52 +00:00
|
|
|
}
|
|
|
|
|
2014-09-09 21:05:18 +00:00
|
|
|
for _, item := range table {
|
|
|
|
allowedOriginRegexps, err := util.CompileRegexps(item.allowedOrigins)
|
|
|
|
if err != nil {
|
|
|
|
t.Errorf("unexpected error: %v", err)
|
|
|
|
}
|
2014-09-03 18:33:52 +00:00
|
|
|
|
2014-09-26 00:20:28 +00:00
|
|
|
handler := CORS(
|
|
|
|
Handle(map[string]RESTStorage{}, codec, "/prefix/version", selfLinker),
|
|
|
|
allowedOriginRegexps, nil, nil, "true",
|
|
|
|
)
|
2014-09-09 21:05:18 +00:00
|
|
|
server := httptest.NewServer(handler)
|
2014-10-31 01:15:44 +00:00
|
|
|
defer server.Close()
|
2014-09-09 21:05:18 +00:00
|
|
|
client := http.Client{}
|
2014-09-03 18:33:52 +00:00
|
|
|
|
2014-09-09 21:05:18 +00:00
|
|
|
request, err := http.NewRequest("GET", server.URL+"/version", nil)
|
|
|
|
if err != nil {
|
|
|
|
t.Errorf("unexpected error: %v", err)
|
|
|
|
}
|
|
|
|
request.Header.Set("Origin", item.origin)
|
2014-09-03 18:33:52 +00:00
|
|
|
|
2014-09-09 21:05:18 +00:00
|
|
|
response, err := client.Do(request)
|
|
|
|
if err != nil {
|
|
|
|
t.Errorf("unexpected error: %v", err)
|
|
|
|
}
|
2014-09-03 18:33:52 +00:00
|
|
|
|
2014-09-09 21:05:18 +00:00
|
|
|
if item.allowed {
|
|
|
|
if !reflect.DeepEqual(item.origin, response.Header.Get("Access-Control-Allow-Origin")) {
|
|
|
|
t.Errorf("Expected %#v, Got %#v", item.origin, response.Header.Get("Access-Control-Allow-Origin"))
|
|
|
|
}
|
|
|
|
|
|
|
|
if response.Header.Get("Access-Control-Allow-Credentials") == "" {
|
|
|
|
t.Errorf("Expected Access-Control-Allow-Credentials header to be set")
|
|
|
|
}
|
|
|
|
|
|
|
|
if response.Header.Get("Access-Control-Allow-Headers") == "" {
|
|
|
|
t.Errorf("Expected Access-Control-Allow-Headers header to be set")
|
|
|
|
}
|
|
|
|
|
|
|
|
if response.Header.Get("Access-Control-Allow-Methods") == "" {
|
|
|
|
t.Errorf("Expected Access-Control-Allow-Methods header to be set")
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
if response.Header.Get("Access-Control-Allow-Origin") != "" {
|
|
|
|
t.Errorf("Expected Access-Control-Allow-Origin header to not be set")
|
|
|
|
}
|
|
|
|
|
|
|
|
if response.Header.Get("Access-Control-Allow-Credentials") != "" {
|
|
|
|
t.Errorf("Expected Access-Control-Allow-Credentials header to not be set")
|
|
|
|
}
|
|
|
|
|
|
|
|
if response.Header.Get("Access-Control-Allow-Headers") != "" {
|
|
|
|
t.Errorf("Expected Access-Control-Allow-Headers header to not be set")
|
|
|
|
}
|
|
|
|
|
|
|
|
if response.Header.Get("Access-Control-Allow-Methods") != "" {
|
|
|
|
t.Errorf("Expected Access-Control-Allow-Methods header to not be set")
|
|
|
|
}
|
|
|
|
}
|
2014-09-03 18:33:52 +00:00
|
|
|
}
|
|
|
|
}
|