Merge pull request #15362 from wojtek-t/refactor_test_objects

Refactor test objects to a separate package
pull/6/head
Dawn Chen 2015-10-13 11:52:47 -07:00
commit e4c910a35a
8 changed files with 248 additions and 171 deletions

View File

@ -38,6 +38,7 @@ import (
"k8s.io/kubernetes/pkg/api/meta"
"k8s.io/kubernetes/pkg/api/rest"
"k8s.io/kubernetes/pkg/api/unversioned"
apiservertesting "k8s.io/kubernetes/pkg/apiserver/testing"
"k8s.io/kubernetes/pkg/fields"
"k8s.io/kubernetes/pkg/labels"
"k8s.io/kubernetes/pkg/runtime"
@ -102,7 +103,9 @@ func addTestTypes() {
Watch bool `json:"watch,omitempty"`
ResourceVersion string `json:"resourceVersion,omitempty"`
}
api.Scheme.AddKnownTypes(testVersion, &Simple{}, &SimpleList{}, &unversioned.Status{}, &ListOptions{}, &api.DeleteOptions{}, &SimpleGetOptions{}, &SimpleRoot{})
api.Scheme.AddKnownTypes(
testVersion, &apiservertesting.Simple{}, &apiservertesting.SimpleList{}, &unversioned.Status{},
&ListOptions{}, &api.DeleteOptions{}, &apiservertesting.SimpleGetOptions{}, &apiservertesting.SimpleRoot{})
api.Scheme.AddKnownTypes(testVersion, &api.Pod{})
}
@ -115,7 +118,9 @@ func addNewTestTypes() {
Watch bool `json:"watch,omitempty"`
ResourceVersion string `json:"resourceVersion,omitempty"`
}
api.Scheme.AddKnownTypes(newVersion, &Simple{}, &SimpleList{}, &unversioned.Status{}, &ListOptions{}, &api.DeleteOptions{}, &SimpleGetOptions{}, &SimpleRoot{})
api.Scheme.AddKnownTypes(
newVersion, &apiservertesting.Simple{}, &apiservertesting.SimpleList{}, &unversioned.Status{},
&ListOptions{}, &api.DeleteOptions{}, &apiservertesting.SimpleGetOptions{}, &apiservertesting.SimpleRoot{})
}
func init() {
@ -123,7 +128,9 @@ func init() {
// api.Status is returned in errors
// "internal" version
api.Scheme.AddKnownTypes("", &Simple{}, &SimpleList{}, &unversioned.Status{}, &api.ListOptions{}, &SimpleGetOptions{}, &SimpleRoot{})
api.Scheme.AddKnownTypes(
"", &apiservertesting.Simple{}, &apiservertesting.SimpleList{}, &unversioned.Status{},
&api.ListOptions{}, &apiservertesting.SimpleGetOptions{}, &apiservertesting.SimpleRoot{})
addTestTypes()
addNewTestTypes()
@ -234,50 +241,8 @@ func handleInternal(legacy bool, storage map[string]rest.Storage, admissionContr
return &defaultAPIServer{mux, group, container}
}
type Simple struct {
unversioned.TypeMeta `json:",inline"`
api.ObjectMeta `json:"metadata"`
Other string `json:"other,omitempty"`
Labels map[string]string `json:"labels,omitempty"`
}
func (*Simple) IsAnAPIObject() {}
type SimpleRoot struct {
unversioned.TypeMeta `json:",inline"`
api.ObjectMeta `json:"metadata"`
Other string `json:"other,omitempty"`
Labels map[string]string `json:"labels,omitempty"`
}
func (*SimpleRoot) IsAnAPIObject() {}
type SimpleGetOptions struct {
unversioned.TypeMeta `json:",inline"`
Param1 string `json:"param1"`
Param2 string `json:"param2"`
Path string `json:"atAPath"`
}
func (SimpleGetOptions) SwaggerDoc() map[string]string {
return map[string]string{
"param1": "description for param1",
"param2": "description for param2",
}
}
func (*SimpleGetOptions) IsAnAPIObject() {}
type SimpleList struct {
unversioned.TypeMeta `json:",inline"`
unversioned.ListMeta `json:"metadata,inline"`
Items []Simple `json:"items,omitempty"`
}
func (*SimpleList) IsAnAPIObject() {}
func TestSimpleSetupRight(t *testing.T) {
s := &Simple{ObjectMeta: api.ObjectMeta{Name: "aName"}}
s := &apiservertesting.Simple{ObjectMeta: api.ObjectMeta{Name: "aName"}}
wire, err := codec.Encode(s)
if err != nil {
t.Fatal(err)
@ -292,7 +257,7 @@ func TestSimpleSetupRight(t *testing.T) {
}
func TestSimpleOptionsSetupRight(t *testing.T) {
s := &SimpleGetOptions{}
s := &apiservertesting.SimpleGetOptions{}
wire, err := codec.Encode(s)
if err != nil {
t.Fatal(err)
@ -308,11 +273,11 @@ func TestSimpleOptionsSetupRight(t *testing.T) {
type SimpleRESTStorage struct {
errors map[string]error
list []Simple
item Simple
list []apiservertesting.Simple
item apiservertesting.Simple
updated *Simple
created *Simple
updated *apiservertesting.Simple
created *apiservertesting.Simple
stream *SimpleStream
@ -342,7 +307,7 @@ type SimpleRESTStorage struct {
func (storage *SimpleRESTStorage) List(ctx api.Context, label labels.Selector, field fields.Selector) (runtime.Object, error) {
storage.checkContext(ctx)
result := &SimpleList{
result := &apiservertesting.SimpleList{
Items: storage.list,
}
storage.requestedLabelSelector = label
@ -408,22 +373,22 @@ func (storage *SimpleRESTStorage) Delete(ctx api.Context, id string, options *ap
var obj runtime.Object = &unversioned.Status{Status: unversioned.StatusSuccess}
var err error
if storage.injectedFunction != nil {
obj, err = storage.injectedFunction(&Simple{ObjectMeta: api.ObjectMeta{Name: id}})
obj, err = storage.injectedFunction(&apiservertesting.Simple{ObjectMeta: api.ObjectMeta{Name: id}})
}
return obj, err
}
func (storage *SimpleRESTStorage) New() runtime.Object {
return &Simple{}
return &apiservertesting.Simple{}
}
func (storage *SimpleRESTStorage) NewList() runtime.Object {
return &SimpleList{}
return &apiservertesting.SimpleList{}
}
func (storage *SimpleRESTStorage) Create(ctx api.Context, obj runtime.Object) (runtime.Object, error) {
storage.checkContext(ctx)
storage.created = obj.(*Simple)
storage.created = obj.(*apiservertesting.Simple)
if err := storage.errors["create"]; err != nil {
return nil, err
}
@ -436,7 +401,7 @@ func (storage *SimpleRESTStorage) Create(ctx api.Context, obj runtime.Object) (r
func (storage *SimpleRESTStorage) Update(ctx api.Context, obj runtime.Object) (runtime.Object, bool, error) {
storage.checkContext(ctx)
storage.updated = obj.(*Simple)
storage.updated = obj.(*apiservertesting.Simple)
if err := storage.errors["update"]; err != nil {
return nil, false, err
}
@ -494,7 +459,7 @@ type ConnecterRESTStorage struct {
var _ = rest.Connecter(&ConnecterRESTStorage{})
func (s *ConnecterRESTStorage) New() runtime.Object {
return &Simple{}
return &apiservertesting.Simple{}
}
func (s *ConnecterRESTStorage) Connect(ctx api.Context, id string, options runtime.Object) (rest.ConnectHandler, error) {
@ -540,7 +505,7 @@ type GetWithOptionsRESTStorage struct {
}
func (r *GetWithOptionsRESTStorage) Get(ctx api.Context, name string, options runtime.Object) (runtime.Object, error) {
if _, ok := options.(*SimpleGetOptions); !ok {
if _, ok := options.(*apiservertesting.SimpleGetOptions); !ok {
return nil, fmt.Errorf("Unexpected options object: %#v", options)
}
r.optionsReceived = options
@ -549,9 +514,9 @@ func (r *GetWithOptionsRESTStorage) Get(ctx api.Context, name string, options ru
func (r *GetWithOptionsRESTStorage) NewGetOptions() (runtime.Object, bool, string) {
if len(r.takesPath) > 0 {
return &SimpleGetOptions{}, true, r.takesPath
return &apiservertesting.SimpleGetOptions{}, true, r.takesPath
}
return &SimpleGetOptions{}, false, ""
return &apiservertesting.SimpleGetOptions{}, false, ""
}
var _ rest.GetterWithOptions = &GetWithOptionsRESTStorage{}
@ -563,7 +528,7 @@ type NamedCreaterRESTStorage struct {
func (storage *NamedCreaterRESTStorage) Create(ctx api.Context, name string, obj runtime.Object) (runtime.Object, error) {
storage.checkContext(ctx)
storage.created = obj.(*Simple)
storage.created = obj.(*apiservertesting.Simple)
storage.createdName = name
if err := storage.errors["create"]; err != nil {
return nil, err
@ -673,7 +638,7 @@ func TestNotFound(t *testing.T) {
type UnimplementedRESTStorage struct{}
func (UnimplementedRESTStorage) New() runtime.Object {
return &Simple{}
return &apiservertesting.Simple{}
}
// TestUnimplementedRESTStorage ensures that if a rest.Storage does not implement a given
@ -910,7 +875,7 @@ func TestErrorList(t *testing.T) {
func TestNonEmptyList(t *testing.T) {
storage := map[string]rest.Storage{}
simpleStorage := SimpleRESTStorage{
list: []Simple{
list: []apiservertesting.Simple{
{
ObjectMeta: api.ObjectMeta{Name: "something", Namespace: "other"},
Other: "foo",
@ -936,7 +901,7 @@ func TestNonEmptyList(t *testing.T) {
t.Logf("Data: %s", string(body))
}
var listOut SimpleList
var listOut apiservertesting.SimpleList
body, err := extractBody(resp, &listOut)
if err != nil {
t.Fatalf("unexpected error: %v", err)
@ -961,7 +926,7 @@ func TestNonEmptyList(t *testing.T) {
func TestSelfLinkSkipsEmptyName(t *testing.T) {
storage := map[string]rest.Storage{}
simpleStorage := SimpleRESTStorage{
list: []Simple{
list: []apiservertesting.Simple{
{
ObjectMeta: api.ObjectMeta{Namespace: "other"},
Other: "foo",
@ -986,7 +951,7 @@ func TestSelfLinkSkipsEmptyName(t *testing.T) {
}
t.Logf("Data: %s", string(body))
}
var listOut SimpleList
var listOut apiservertesting.SimpleList
body, err := extractBody(resp, &listOut)
if err != nil {
t.Fatalf("unexpected error: %v", err)
@ -1031,7 +996,7 @@ func TestMetadata(t *testing.T) {
func TestGet(t *testing.T) {
storage := map[string]rest.Storage{}
simpleStorage := SimpleRESTStorage{
item: Simple{
item: apiservertesting.Simple{
Other: "foo",
},
}
@ -1053,7 +1018,7 @@ func TestGet(t *testing.T) {
if resp.StatusCode != http.StatusOK {
t.Fatalf("unexpected response: %#v", resp)
}
var itemOut Simple
var itemOut apiservertesting.Simple
body, err := extractBody(resp, &itemOut)
if err != nil {
t.Errorf("unexpected error: %v", err)
@ -1145,7 +1110,7 @@ func TestGetWithOptions(t *testing.T) {
storage := map[string]rest.Storage{}
simpleStorage := GetWithOptionsRESTStorage{
SimpleRESTStorage: &SimpleRESTStorage{
item: Simple{
item: apiservertesting.Simple{
Other: "foo",
},
},
@ -1162,7 +1127,7 @@ func TestGetWithOptions(t *testing.T) {
if resp.StatusCode != http.StatusOK {
t.Fatalf("unexpected response: %#v", resp)
}
var itemOut Simple
var itemOut apiservertesting.Simple
body, err := extractBody(resp, &itemOut)
if err != nil {
t.Errorf("unexpected error: %v", err)
@ -1172,7 +1137,7 @@ func TestGetWithOptions(t *testing.T) {
t.Errorf("Unexpected data: %#v, expected %#v (%s)", itemOut, simpleStorage.item, string(body))
}
opts, ok := simpleStorage.optionsReceived.(*SimpleGetOptions)
opts, ok := simpleStorage.optionsReceived.(*apiservertesting.SimpleGetOptions)
if !ok {
t.Errorf("Unexpected options object received: %#v", simpleStorage.optionsReceived)
return
@ -1186,7 +1151,7 @@ func TestGetWithOptionsAndPath(t *testing.T) {
storage := map[string]rest.Storage{}
simpleStorage := GetWithOptionsRESTStorage{
SimpleRESTStorage: &SimpleRESTStorage{
item: Simple{
item: apiservertesting.Simple{
Other: "foo",
},
},
@ -1204,7 +1169,7 @@ func TestGetWithOptionsAndPath(t *testing.T) {
if resp.StatusCode != http.StatusOK {
t.Fatalf("unexpected response: %#v", resp)
}
var itemOut Simple
var itemOut apiservertesting.Simple
body, err := extractBody(resp, &itemOut)
if err != nil {
t.Errorf("unexpected error: %v", err)
@ -1214,7 +1179,7 @@ func TestGetWithOptionsAndPath(t *testing.T) {
t.Errorf("Unexpected data: %#v, expected %#v (%s)", itemOut, simpleStorage.item, string(body))
}
opts, ok := simpleStorage.optionsReceived.(*SimpleGetOptions)
opts, ok := simpleStorage.optionsReceived.(*apiservertesting.SimpleGetOptions)
if !ok {
t.Errorf("Unexpected options object received: %#v", simpleStorage.optionsReceived)
return
@ -1226,7 +1191,7 @@ func TestGetWithOptionsAndPath(t *testing.T) {
func TestGetAlternateSelfLink(t *testing.T) {
storage := map[string]rest.Storage{}
simpleStorage := SimpleRESTStorage{
item: Simple{
item: apiservertesting.Simple{
Other: "foo",
},
}
@ -1248,7 +1213,7 @@ func TestGetAlternateSelfLink(t *testing.T) {
if resp.StatusCode != http.StatusOK {
t.Fatalf("unexpected response: %#v", resp)
}
var itemOut Simple
var itemOut apiservertesting.Simple
body, err := extractBody(resp, &itemOut)
if err != nil {
t.Fatalf("unexpected error: %v", err)
@ -1264,7 +1229,7 @@ func TestGetAlternateSelfLink(t *testing.T) {
func TestGetNamespaceSelfLink(t *testing.T) {
storage := map[string]rest.Storage{}
simpleStorage := SimpleRESTStorage{
item: Simple{
item: apiservertesting.Simple{
Other: "foo",
},
}
@ -1286,7 +1251,7 @@ func TestGetNamespaceSelfLink(t *testing.T) {
if resp.StatusCode != http.StatusOK {
t.Fatalf("unexpected response: %#v", resp)
}
var itemOut Simple
var itemOut apiservertesting.Simple
body, err := extractBody(resp, &itemOut)
if err != nil {
t.Fatalf("unexpected error: %v", err)
@ -1358,7 +1323,7 @@ func TestConnect(t *testing.T) {
func TestConnectWithOptionsRouteParams(t *testing.T) {
connectStorage := &ConnecterRESTStorage{
connectHandler: &SimpleConnectHandler{},
emptyConnectOptions: &SimpleGetOptions{},
emptyConnectOptions: &apiservertesting.SimpleGetOptions{},
}
storage := map[string]rest.Storage{
"simple": &SimpleRESTStorage{},
@ -1389,7 +1354,7 @@ func TestConnectWithOptions(t *testing.T) {
connectHandler: &SimpleConnectHandler{
response: responseText,
},
emptyConnectOptions: &SimpleGetOptions{},
emptyConnectOptions: &apiservertesting.SimpleGetOptions{},
}
storage := map[string]rest.Storage{
"simple": &SimpleRESTStorage{},
@ -1418,7 +1383,7 @@ func TestConnectWithOptions(t *testing.T) {
if string(body) != responseText {
t.Errorf("Unexpected response. Expected: %s. Actual: %s.", responseText, string(body))
}
opts, ok := connectStorage.receivedConnectOptions.(*SimpleGetOptions)
opts, ok := connectStorage.receivedConnectOptions.(*apiservertesting.SimpleGetOptions)
if !ok {
t.Errorf("Unexpected options type: %#v", connectStorage.receivedConnectOptions)
}
@ -1435,7 +1400,7 @@ func TestConnectWithOptionsAndPath(t *testing.T) {
connectHandler: &SimpleConnectHandler{
response: responseText,
},
emptyConnectOptions: &SimpleGetOptions{},
emptyConnectOptions: &apiservertesting.SimpleGetOptions{},
takesPath: "atAPath",
}
storage := map[string]rest.Storage{
@ -1465,7 +1430,7 @@ func TestConnectWithOptionsAndPath(t *testing.T) {
if string(body) != responseText {
t.Errorf("Unexpected response. Expected: %s. Actual: %s.", responseText, string(body))
}
opts, ok := connectStorage.receivedConnectOptions.(*SimpleGetOptions)
opts, ok := connectStorage.receivedConnectOptions.(*apiservertesting.SimpleGetOptions)
if !ok {
t.Errorf("Unexpected options type: %#v", connectStorage.receivedConnectOptions)
}
@ -1645,7 +1610,7 @@ func TestDeleteMissing(t *testing.T) {
func TestPatch(t *testing.T) {
storage := map[string]rest.Storage{}
ID := "id"
item := &Simple{
item := &apiservertesting.Simple{
ObjectMeta: api.ObjectMeta{
Name: ID,
Namespace: "", // update should allow the client to send an empty namespace
@ -1683,7 +1648,7 @@ func TestPatch(t *testing.T) {
func TestPatchRequiresMatchingName(t *testing.T) {
storage := map[string]rest.Storage{}
ID := "id"
item := &Simple{
item := &apiservertesting.Simple{
ObjectMeta: api.ObjectMeta{
Name: ID,
Namespace: "", // update should allow the client to send an empty namespace
@ -1723,7 +1688,7 @@ func TestUpdate(t *testing.T) {
server := httptest.NewServer(handler)
defer server.Close()
item := &Simple{
item := &apiservertesting.Simple{
ObjectMeta: api.ObjectMeta{
Name: ID,
Namespace: "", // update should allow the client to send an empty namespace
@ -1760,7 +1725,7 @@ func TestUpdateInvokesAdmissionControl(t *testing.T) {
server := httptest.NewServer(handler)
defer server.Close()
item := &Simple{
item := &apiservertesting.Simple{
ObjectMeta: api.ObjectMeta{
Name: ID,
Namespace: api.NamespaceDefault,
@ -1793,7 +1758,7 @@ func TestUpdateRequiresMatchingName(t *testing.T) {
server := httptest.NewServer(handler)
defer server.Close()
item := &Simple{
item := &apiservertesting.Simple{
Other: "bar",
}
body, err := codec.Encode(item)
@ -1822,7 +1787,7 @@ func TestUpdateAllowsMissingNamespace(t *testing.T) {
server := httptest.NewServer(handler)
defer server.Close()
item := &Simple{
item := &apiservertesting.Simple{
ObjectMeta: api.ObjectMeta{
Name: ID,
},
@ -1859,7 +1824,7 @@ func TestUpdateAllowsMismatchedNamespaceOnError(t *testing.T) {
server := httptest.NewServer(handler)
defer server.Close()
item := &Simple{
item := &apiservertesting.Simple{
ObjectMeta: api.ObjectMeta{
Name: ID,
Namespace: "other", // does not match request
@ -1896,7 +1861,7 @@ func TestUpdatePreventsMismatchedNamespace(t *testing.T) {
server := httptest.NewServer(handler)
defer server.Close()
item := &Simple{
item := &apiservertesting.Simple{
ObjectMeta: api.ObjectMeta{
Name: ID,
Namespace: "other",
@ -1931,7 +1896,7 @@ func TestUpdateMissing(t *testing.T) {
server := httptest.NewServer(handler)
defer server.Close()
item := &Simple{
item := &apiservertesting.Simple{
ObjectMeta: api.ObjectMeta{
Name: ID,
Namespace: api.NamespaceDefault,
@ -1966,7 +1931,7 @@ func TestCreateNotFound(t *testing.T) {
defer server.Close()
client := http.Client{}
simple := &Simple{Other: "foo"}
simple := &apiservertesting.Simple{Other: "foo"}
data, err := codec.Encode(simple)
if err != nil {
t.Errorf("unexpected error: %v", err)
@ -2097,8 +2062,8 @@ func TestUpdateREST(t *testing.T) {
func TestParentResourceIsRequired(t *testing.T) {
storage := &SimpleTypedStorage{
baseType: &SimpleRoot{}, // a root scoped type
item: &SimpleRoot{},
baseType: &apiservertesting.SimpleRoot{}, // a root scoped type
item: &apiservertesting.SimpleRoot{},
}
group := &APIGroupVersion{
Storage: map[string]rest.Storage{
@ -2125,8 +2090,8 @@ func TestParentResourceIsRequired(t *testing.T) {
}
storage = &SimpleTypedStorage{
baseType: &SimpleRoot{}, // a root scoped type
item: &SimpleRoot{},
baseType: &apiservertesting.SimpleRoot{}, // a root scoped type
item: &apiservertesting.SimpleRoot{},
}
group = &APIGroupVersion{
Storage: map[string]rest.Storage{
@ -2182,7 +2147,7 @@ func TestCreateWithName(t *testing.T) {
defer server.Close()
client := http.Client{}
simple := &Simple{Other: "foo"}
simple := &apiservertesting.Simple{Other: "foo"}
data, err := codec.Encode(simple)
if err != nil {
t.Errorf("unexpected error: %v", err)
@ -2283,7 +2248,7 @@ func TestCreate(t *testing.T) {
defer server.Close()
client := http.Client{}
simple := &Simple{
simple := &apiservertesting.Simple{
Other: "bar",
}
data, err := codec.Encode(simple)
@ -2307,7 +2272,7 @@ func TestCreate(t *testing.T) {
t.Errorf("unexpected error: %v", err)
}
var itemOut Simple
var itemOut apiservertesting.Simple
body, err := extractBody(response, &itemOut)
if err != nil {
t.Errorf("unexpected error: %v", err)
@ -2342,7 +2307,7 @@ func TestCreateInNamespace(t *testing.T) {
defer server.Close()
client := http.Client{}
simple := &Simple{
simple := &apiservertesting.Simple{
Other: "bar",
}
data, err := codec.Encode(simple)
@ -2366,7 +2331,7 @@ func TestCreateInNamespace(t *testing.T) {
t.Errorf("unexpected error: %v", err)
}
var itemOut Simple
var itemOut apiservertesting.Simple
body, err := extractBody(response, &itemOut)
if err != nil {
t.Errorf("unexpected error: %v", err)
@ -2401,7 +2366,7 @@ func TestCreateInvokesAdmissionControl(t *testing.T) {
defer server.Close()
client := http.Client{}
simple := &Simple{
simple := &apiservertesting.Simple{
Other: "bar",
}
data, err := codec.Encode(simple)
@ -2531,7 +2496,7 @@ func TestCreateTimeout(t *testing.T) {
server := httptest.NewServer(handler)
defer server.Close()
simple := &Simple{Other: "foo"}
simple := &apiservertesting.Simple{Other: "foo"}
data, err := codec.Encode(simple)
if err != nil {
t.Errorf("unexpected error: %v", err)
@ -2622,7 +2587,7 @@ func TestCreateChecksAPIVersion(t *testing.T) {
defer server.Close()
client := http.Client{}
simple := &Simple{}
simple := &apiservertesting.Simple{}
//using newCodec and send the request to testVersion URL shall cause a discrepancy in apiVersion
data, err := newCodec.Encode(simple)
if err != nil {
@ -2653,7 +2618,7 @@ func TestCreateDefaultsAPIVersion(t *testing.T) {
defer server.Close()
client := http.Client{}
simple := &Simple{}
simple := &apiservertesting.Simple{}
data, err := codec.Encode(simple)
if err != nil {
t.Errorf("unexpected error: %v", err)
@ -2688,7 +2653,7 @@ func TestUpdateChecksAPIVersion(t *testing.T) {
defer server.Close()
client := http.Client{}
simple := &Simple{ObjectMeta: api.ObjectMeta{Name: "bar"}}
simple := &apiservertesting.Simple{ObjectMeta: api.ObjectMeta{Name: "bar"}}
data, err := newCodec.Encode(simple)
if err != nil {
t.Errorf("unexpected error: %v", err)

View File

@ -0,0 +1,64 @@
/*
Copyright 2015 The Kubernetes Authors 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.
*/
package testing
import (
"k8s.io/kubernetes/pkg/api"
"k8s.io/kubernetes/pkg/api/unversioned"
)
type Simple struct {
unversioned.TypeMeta `json:",inline"`
api.ObjectMeta `json:"metadata"`
Other string `json:"other,omitempty"`
Labels map[string]string `json:"labels,omitempty"`
}
func (*Simple) IsAnAPIObject() {}
type SimpleRoot struct {
unversioned.TypeMeta `json:",inline"`
api.ObjectMeta `json:"metadata"`
Other string `json:"other,omitempty"`
Labels map[string]string `json:"labels,omitempty"`
}
func (*SimpleRoot) IsAnAPIObject() {}
type SimpleGetOptions struct {
unversioned.TypeMeta `json:",inline"`
Param1 string `json:"param1"`
Param2 string `json:"param2"`
Path string `json:"atAPath"`
}
func (SimpleGetOptions) SwaggerDoc() map[string]string {
return map[string]string{
"param1": "description for param1",
"param2": "description for param2",
}
}
func (*SimpleGetOptions) IsAnAPIObject() {}
type SimpleList struct {
unversioned.TypeMeta `json:",inline"`
unversioned.ListMeta `json:"metadata,inline"`
Items []Simple `json:"items,omitempty"`
}
func (*SimpleList) IsAnAPIObject() {}

View File

@ -30,6 +30,7 @@ import (
"k8s.io/kubernetes/pkg/api"
"k8s.io/kubernetes/pkg/api/rest"
"k8s.io/kubernetes/pkg/api/unversioned"
apiservertesting "k8s.io/kubernetes/pkg/apiserver/testing"
"k8s.io/kubernetes/pkg/fields"
"k8s.io/kubernetes/pkg/labels"
"k8s.io/kubernetes/pkg/runtime"
@ -47,9 +48,9 @@ var watchTestTable = []struct {
t watch.EventType
obj runtime.Object
}{
{watch.Added, &Simple{ObjectMeta: api.ObjectMeta{Name: "foo"}}},
{watch.Modified, &Simple{ObjectMeta: api.ObjectMeta{Name: "bar"}}},
{watch.Deleted, &Simple{ObjectMeta: api.ObjectMeta{Name: "bar"}}},
{watch.Added, &apiservertesting.Simple{ObjectMeta: api.ObjectMeta{Name: "foo"}}},
{watch.Modified, &apiservertesting.Simple{ObjectMeta: api.ObjectMeta{Name: "bar"}}},
{watch.Deleted, &apiservertesting.Simple{ObjectMeta: api.ObjectMeta{Name: "bar"}}},
}
func TestWatchWebsocket(t *testing.T) {
@ -363,7 +364,7 @@ func TestWatchHTTPTimeout(t *testing.T) {
req, _ := http.NewRequest("GET", dest.String(), nil)
client := http.Client{}
resp, err := client.Do(req)
watcher.Add(&Simple{TypeMeta: unversioned.TypeMeta{APIVersion: newVersion}})
watcher.Add(&apiservertesting.Simple{TypeMeta: unversioned.TypeMeta{APIVersion: newVersion}})
// Make sure we can actually watch an endpoint
decoder := json.NewDecoder(resp.Body)

View File

@ -31,6 +31,7 @@ import (
"k8s.io/kubernetes/pkg/api/unversioned"
"k8s.io/kubernetes/pkg/api/v1"
"k8s.io/kubernetes/pkg/apis/extensions"
kubectltesting "k8s.io/kubernetes/pkg/kubectl/testing"
"k8s.io/kubernetes/pkg/runtime"
"k8s.io/kubernetes/pkg/util"
"k8s.io/kubernetes/pkg/util/sets"
@ -38,23 +39,12 @@ import (
"github.com/ghodss/yaml"
)
type testStruct struct {
unversioned.TypeMeta `json:",inline"`
api.ObjectMeta `json:"metadata,omitempty"`
Key string `json:"Key"`
Map map[string]int `json:"Map"`
StringList []string `json:"StringList"`
IntList []int `json:"IntList"`
}
func (ts *testStruct) IsAnAPIObject() {}
func init() {
api.Scheme.AddKnownTypes("", &testStruct{})
api.Scheme.AddKnownTypes(testapi.Default.Version(), &testStruct{})
api.Scheme.AddKnownTypes("", &kubectltesting.TestStruct{})
api.Scheme.AddKnownTypes(testapi.Default.Version(), &kubectltesting.TestStruct{})
}
var testData = testStruct{
var testData = kubectltesting.TestStruct{
Key: "testValue",
Map: map[string]int{"TestSubkey": 1},
StringList: []string{"a", "b", "c"},
@ -62,13 +52,13 @@ var testData = testStruct{
}
func TestVersionedPrinter(t *testing.T) {
original := &testStruct{Key: "value"}
original := &kubectltesting.TestStruct{Key: "value"}
p := NewVersionedPrinter(
ResourcePrinterFunc(func(obj runtime.Object, w io.Writer) error {
if obj == original {
t.Fatalf("object should not be identical: %#v", obj)
}
if obj.(*testStruct).Key != "value" {
if obj.(*kubectltesting.TestStruct).Key != "value" {
t.Fatalf("object was not converted: %#v", obj)
}
return nil
@ -177,14 +167,14 @@ func testPrinter(t *testing.T, printer ResourcePrinter, unmarshalFunc func(data
if err != nil {
t.Fatal(err)
}
var poutput testStruct
var poutput kubectltesting.TestStruct
// Verify that given function runs without error.
err = unmarshalFunc(buf.Bytes(), &poutput)
if err != nil {
t.Fatal(err)
}
// Use real decode function to undo the versioning process.
poutput = testStruct{}
poutput = kubectltesting.TestStruct{}
err = runtime.YAMLDecoder(testapi.Default.Codec()).DecodeInto(buf.Bytes(), &poutput)
if err != nil {
t.Fatal(err)

View File

@ -0,0 +1,33 @@
/*
Copyright 2015 The Kubernetes Authors 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.
*/
package testing
import (
"k8s.io/kubernetes/pkg/api"
"k8s.io/kubernetes/pkg/api/unversioned"
)
type TestStruct struct {
unversioned.TypeMeta `json:",inline"`
api.ObjectMeta `json:"metadata,omitempty"`
Key string `json:"Key"`
Map map[string]int `json:"Map"`
StringList []string `json:"StringList"`
IntList []int `json:"IntList"`
}
func (ts *TestStruct) IsAnAPIObject() {}

View File

@ -22,17 +22,18 @@ import (
"k8s.io/kubernetes/pkg/api"
"k8s.io/kubernetes/pkg/api/unversioned"
storagetesting "k8s.io/kubernetes/pkg/storage/testing"
)
func TestObjectVersioner(t *testing.T) {
v := APIObjectVersioner{}
if ver, err := v.ObjectResourceVersion(&TestResource{ObjectMeta: api.ObjectMeta{ResourceVersion: "5"}}); err != nil || ver != 5 {
if ver, err := v.ObjectResourceVersion(&storagetesting.TestResource{ObjectMeta: api.ObjectMeta{ResourceVersion: "5"}}); err != nil || ver != 5 {
t.Errorf("unexpected version: %d %v", ver, err)
}
if ver, err := v.ObjectResourceVersion(&TestResource{ObjectMeta: api.ObjectMeta{ResourceVersion: "a"}}); err == nil || ver != 0 {
if ver, err := v.ObjectResourceVersion(&storagetesting.TestResource{ObjectMeta: api.ObjectMeta{ResourceVersion: "a"}}); err == nil || ver != 0 {
t.Errorf("unexpected version: %d %v", ver, err)
}
obj := &TestResource{ObjectMeta: api.ObjectMeta{ResourceVersion: "a"}}
obj := &storagetesting.TestResource{ObjectMeta: api.ObjectMeta{ResourceVersion: "a"}}
if err := v.UpdateObject(obj, nil, 5); err != nil {
t.Fatalf("unexpected error: %v", err)
}
@ -40,7 +41,7 @@ func TestObjectVersioner(t *testing.T) {
t.Errorf("unexpected resource version: %#v", obj)
}
now := unversioned.Time{Time: time.Now()}
obj = &TestResource{ObjectMeta: api.ObjectMeta{ResourceVersion: "a"}}
obj = &storagetesting.TestResource{ObjectMeta: api.ObjectMeta{ResourceVersion: "a"}}
if err := v.UpdateObject(obj, &now.Time, 5); err != nil {
t.Fatalf("unexpected error: %v", err)
}

View File

@ -40,30 +40,23 @@ import (
"k8s.io/kubernetes/pkg/conversion"
"k8s.io/kubernetes/pkg/runtime"
"k8s.io/kubernetes/pkg/storage"
storagetesting "k8s.io/kubernetes/pkg/storage/testing"
"k8s.io/kubernetes/pkg/tools"
"k8s.io/kubernetes/pkg/tools/etcdtest"
)
const validEtcdVersion = "etcd 2.0.9"
type TestResource struct {
unversioned.TypeMeta `json:",inline"`
api.ObjectMeta `json:"metadata"`
Value int `json:"value"`
}
func (*TestResource) IsAnAPIObject() {}
var scheme *runtime.Scheme
var codec runtime.Codec
func init() {
scheme = runtime.NewScheme()
scheme.AddKnownTypes("", &TestResource{})
scheme.AddKnownTypes(testapi.Default.Version(), &TestResource{})
scheme.AddKnownTypes("", &storagetesting.TestResource{})
scheme.AddKnownTypes(testapi.Default.Version(), &storagetesting.TestResource{})
codec = runtime.CodecFor(scheme, testapi.Default.Version())
scheme.AddConversionFuncs(
func(in *TestResource, out *TestResource, s conversion.Scope) error {
func(in *storagetesting.TestResource, out *storagetesting.TestResource, s conversion.Scope) error {
*out = *in
return nil
},
@ -568,8 +561,8 @@ func TestGuaranteedUpdate(t *testing.T) {
// Create a new node.
fakeClient.ExpectNotFoundGet(key)
obj := &TestResource{ObjectMeta: api.ObjectMeta{Name: "foo"}, Value: 1}
err := helper.GuaranteedUpdate(context.TODO(), "/some/key", &TestResource{}, true, storage.SimpleUpdate(func(in runtime.Object) (runtime.Object, error) {
obj := &storagetesting.TestResource{ObjectMeta: api.ObjectMeta{Name: "foo"}, Value: 1}
err := helper.GuaranteedUpdate(context.TODO(), "/some/key", &storagetesting.TestResource{}, true, storage.SimpleUpdate(func(in runtime.Object) (runtime.Object, error) {
return obj, nil
}))
if err != nil {
@ -587,11 +580,11 @@ func TestGuaranteedUpdate(t *testing.T) {
// Update an existing node.
callbackCalled := false
objUpdate := &TestResource{ObjectMeta: api.ObjectMeta{Name: "foo"}, Value: 2}
err = helper.GuaranteedUpdate(context.TODO(), "/some/key", &TestResource{}, true, storage.SimpleUpdate(func(in runtime.Object) (runtime.Object, error) {
objUpdate := &storagetesting.TestResource{ObjectMeta: api.ObjectMeta{Name: "foo"}, Value: 2}
err = helper.GuaranteedUpdate(context.TODO(), "/some/key", &storagetesting.TestResource{}, true, storage.SimpleUpdate(func(in runtime.Object) (runtime.Object, error) {
callbackCalled = true
if in.(*TestResource).Value != 1 {
if in.(*storagetesting.TestResource).Value != 1 {
t.Errorf("Callback input was not current set value")
}
@ -623,8 +616,8 @@ func TestGuaranteedUpdateTTL(t *testing.T) {
// Create a new node.
fakeClient.ExpectNotFoundGet(key)
obj := &TestResource{ObjectMeta: api.ObjectMeta{Name: "foo"}, Value: 1}
err := helper.GuaranteedUpdate(context.TODO(), "/some/key", &TestResource{}, true, func(in runtime.Object, res storage.ResponseMeta) (runtime.Object, *uint64, error) {
obj := &storagetesting.TestResource{ObjectMeta: api.ObjectMeta{Name: "foo"}, Value: 1}
err := helper.GuaranteedUpdate(context.TODO(), "/some/key", &storagetesting.TestResource{}, true, func(in runtime.Object, res storage.ResponseMeta) (runtime.Object, *uint64, error) {
if res.TTL != 0 {
t.Fatalf("unexpected response meta: %#v", res)
}
@ -649,14 +642,14 @@ func TestGuaranteedUpdateTTL(t *testing.T) {
// Update an existing node.
callbackCalled := false
objUpdate := &TestResource{ObjectMeta: api.ObjectMeta{Name: "foo"}, Value: 2}
err = helper.GuaranteedUpdate(context.TODO(), "/some/key", &TestResource{}, true, func(in runtime.Object, res storage.ResponseMeta) (runtime.Object, *uint64, error) {
objUpdate := &storagetesting.TestResource{ObjectMeta: api.ObjectMeta{Name: "foo"}, Value: 2}
err = helper.GuaranteedUpdate(context.TODO(), "/some/key", &storagetesting.TestResource{}, true, func(in runtime.Object, res storage.ResponseMeta) (runtime.Object, *uint64, error) {
if res.TTL != 10 {
t.Fatalf("unexpected response meta: %#v", res)
}
callbackCalled = true
if in.(*TestResource).Value != 1 {
if in.(*storagetesting.TestResource).Value != 1 {
t.Errorf("Callback input was not current set value")
}
@ -681,14 +674,14 @@ func TestGuaranteedUpdateTTL(t *testing.T) {
// Update an existing node and change ttl
callbackCalled = false
objUpdate = &TestResource{ObjectMeta: api.ObjectMeta{Name: "foo"}, Value: 3}
err = helper.GuaranteedUpdate(context.TODO(), "/some/key", &TestResource{}, true, func(in runtime.Object, res storage.ResponseMeta) (runtime.Object, *uint64, error) {
objUpdate = &storagetesting.TestResource{ObjectMeta: api.ObjectMeta{Name: "foo"}, Value: 3}
err = helper.GuaranteedUpdate(context.TODO(), "/some/key", &storagetesting.TestResource{}, true, func(in runtime.Object, res storage.ResponseMeta) (runtime.Object, *uint64, error) {
if res.TTL != 10 {
t.Fatalf("unexpected response meta: %#v", res)
}
callbackCalled = true
if in.(*TestResource).Value != 2 {
if in.(*storagetesting.TestResource).Value != 2 {
t.Errorf("Callback input was not current set value")
}
@ -724,8 +717,8 @@ func TestGuaranteedUpdateNoChange(t *testing.T) {
// Create a new node.
fakeClient.ExpectNotFoundGet(key)
obj := &TestResource{ObjectMeta: api.ObjectMeta{Name: "foo"}, Value: 1}
err := helper.GuaranteedUpdate(context.TODO(), "/some/key", &TestResource{}, true, storage.SimpleUpdate(func(in runtime.Object) (runtime.Object, error) {
obj := &storagetesting.TestResource{ObjectMeta: api.ObjectMeta{Name: "foo"}, Value: 1}
err := helper.GuaranteedUpdate(context.TODO(), "/some/key", &storagetesting.TestResource{}, true, storage.SimpleUpdate(func(in runtime.Object) (runtime.Object, error) {
return obj, nil
}))
if err != nil {
@ -734,8 +727,8 @@ func TestGuaranteedUpdateNoChange(t *testing.T) {
// Update an existing node with the same data
callbackCalled := false
objUpdate := &TestResource{ObjectMeta: api.ObjectMeta{Name: "foo"}, Value: 1}
err = helper.GuaranteedUpdate(context.TODO(), "/some/key", &TestResource{}, true, storage.SimpleUpdate(func(in runtime.Object) (runtime.Object, error) {
objUpdate := &storagetesting.TestResource{ObjectMeta: api.ObjectMeta{Name: "foo"}, Value: 1}
err = helper.GuaranteedUpdate(context.TODO(), "/some/key", &storagetesting.TestResource{}, true, storage.SimpleUpdate(func(in runtime.Object) (runtime.Object, error) {
fakeClient.Err = errors.New("should not be called")
callbackCalled = true
return objUpdate, nil
@ -756,20 +749,20 @@ func TestGuaranteedUpdateKeyNotFound(t *testing.T) {
// Create a new node.
fakeClient.ExpectNotFoundGet(key)
obj := &TestResource{ObjectMeta: api.ObjectMeta{Name: "foo"}, Value: 1}
obj := &storagetesting.TestResource{ObjectMeta: api.ObjectMeta{Name: "foo"}, Value: 1}
f := storage.SimpleUpdate(func(in runtime.Object) (runtime.Object, error) {
return obj, nil
})
ignoreNotFound := false
err := helper.GuaranteedUpdate(context.TODO(), "/some/key", &TestResource{}, ignoreNotFound, f)
err := helper.GuaranteedUpdate(context.TODO(), "/some/key", &storagetesting.TestResource{}, ignoreNotFound, f)
if err == nil {
t.Errorf("Expected error for key not found.")
}
ignoreNotFound = true
err = helper.GuaranteedUpdate(context.TODO(), "/some/key", &TestResource{}, ignoreNotFound, f)
err = helper.GuaranteedUpdate(context.TODO(), "/some/key", &storagetesting.TestResource{}, ignoreNotFound, f)
if err != nil {
t.Errorf("Unexpected error %v.", err)
}
@ -790,12 +783,12 @@ func TestGuaranteedUpdate_CreateCollision(t *testing.T) {
wgForceCollision.Add(concurrency)
for i := 0; i < concurrency; i++ {
// Increment TestResource.Value by 1
// Increment storagetesting.TestResource.Value by 1
go func() {
defer wgDone.Done()
firstCall := true
err := helper.GuaranteedUpdate(context.TODO(), "/some/key", &TestResource{}, true, storage.SimpleUpdate(func(in runtime.Object) (runtime.Object, error) {
err := helper.GuaranteedUpdate(context.TODO(), "/some/key", &storagetesting.TestResource{}, true, storage.SimpleUpdate(func(in runtime.Object) (runtime.Object, error) {
defer func() { firstCall = false }()
if firstCall {
@ -804,8 +797,8 @@ func TestGuaranteedUpdate_CreateCollision(t *testing.T) {
wgForceCollision.Wait()
}
currValue := in.(*TestResource).Value
obj := &TestResource{ObjectMeta: api.ObjectMeta{Name: "foo"}, Value: currValue + 1}
currValue := in.(*storagetesting.TestResource).Value
obj := &storagetesting.TestResource{ObjectMeta: api.ObjectMeta{Name: "foo"}, Value: currValue + 1}
return obj, nil
}))
if err != nil {
@ -815,9 +808,9 @@ func TestGuaranteedUpdate_CreateCollision(t *testing.T) {
}
wgDone.Wait()
// Check that stored TestResource has received all updates.
// Check that stored storagetesting.TestResource has received all updates.
body := fakeClient.Data[key].R.Node.Value
stored := &TestResource{}
stored := &storagetesting.TestResource{}
if err := codec.DecodeInto([]byte(body), stored); err != nil {
t.Errorf("Error decoding stored value: %v", body)
}

View File

@ -0,0 +1,30 @@
/*
Copyright 2015 The Kubernetes Authors 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.
*/
package testing
import (
"k8s.io/kubernetes/pkg/api"
"k8s.io/kubernetes/pkg/api/unversioned"
)
type TestResource struct {
unversioned.TypeMeta `json:",inline"`
api.ObjectMeta `json:"metadata"`
Value int `json:"value"`
}
func (*TestResource) IsAnAPIObject() {}