Skip to content

Commit

Permalink
Make tests pass again
Browse files Browse the repository at this point in the history
  • Loading branch information
lavalamp committed Sep 8, 2014
1 parent 48ce23a commit fc09f98
Show file tree
Hide file tree
Showing 30 changed files with 270 additions and 228 deletions.
6 changes: 3 additions & 3 deletions cmd/integration/integration.go
Original file line number Diff line number Diff line change
Expand Up @@ -179,7 +179,7 @@ func runReplicationControllerTest(c *client.Client) {
}

glog.Infof("Creating replication controllers")
if _, err := c.CreateReplicationController(controllerRequest); err != nil {
if _, err := c.CreateReplicationController(&controllerRequest); err != nil {
glog.Fatalf("Unexpected error: %#v", err)
}
glog.Infof("Done creating replication controllers")
Expand All @@ -194,7 +194,7 @@ func runReplicationControllerTest(c *client.Client) {
if err != nil {
glog.Fatalf("FAILED: unable to get pods to list: %v", err)
}
if err := wait.Poll(time.Second, time.Second*10, podsOnMinions(c, pods)); err != nil {
if err := wait.Poll(time.Second, time.Second*10, podsOnMinions(c, *pods)); err != nil {
glog.Fatalf("FAILED: pods never started running %v", err)
}

Expand All @@ -204,7 +204,7 @@ func runReplicationControllerTest(c *client.Client) {
func runAtomicPutTest(c *client.Client) {
var svc api.Service
err := c.Post().Path("services").Body(
api.Service{
&api.Service{
JSONBase: api.JSONBase{ID: "atomicservice", APIVersion: "v1beta1"},
Port: 12345,
Labels: map[string]string{
Expand Down
10 changes: 5 additions & 5 deletions cmd/kubecfg/kubecfg.go
Original file line number Diff line number Diff line change
Expand Up @@ -58,11 +58,11 @@ var (
imageName = flag.String("image", "", "Image used when updating a replicationController. Will apply to the first container in the pod template.")
)

var parser = kubecfg.NewParser(map[string]interface{}{
"pods": api.Pod{},
"services": api.Service{},
"replicationControllers": api.ReplicationController{},
"minions": api.Minion{},
var parser = kubecfg.NewParser(map[string]runtime.Object{
"pods": &api.Pod{},
"services": &api.Service{},
"replicationControllers": &api.ReplicationController{},
"minions": &api.Minion{},
})

func usage() {
Expand Down
73 changes: 40 additions & 33 deletions pkg/apiserver/apiserver_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,27 +38,31 @@ import (
"github.com/GoogleCloudPlatform/kubernetes/pkg/watch"
)

func convert(obj interface{}) (interface{}, error) {
func convert(obj runtime.Object) (runtime.Object, error) {
return obj, nil
}

var codec = runtime.DefaultCodec

func init() {
runtime.AddKnownTypes("", Simple{}, SimpleList{})
runtime.AddKnownTypes("v1beta1", Simple{}, SimpleList{})
runtime.DefaultScheme.AddKnownTypes("", &Simple{}, &SimpleList{})
runtime.DefaultScheme.AddKnownTypes("v1beta1", &Simple{}, &SimpleList{})
}

type Simple struct {
api.JSONBase `yaml:",inline" json:",inline"`
Name string `yaml:"name,omitempty" json:"name,omitempty"`
}

func (*Simple) IsAnAPIObject() {}

type SimpleList struct {
api.JSONBase `yaml:",inline" json:",inline"`
Items []Simple `yaml:"items,omitempty" json:"items,omitempty"`
}

func (*SimpleList) IsAnAPIObject() {}

type SimpleRESTStorage struct {
errors map[string]error
list []Simple
Expand All @@ -78,56 +82,56 @@ type SimpleRESTStorage struct {

// If non-nil, called inside the WorkFunc when answering update, delete, create.
// obj receives the original input to the update, delete, or create call.
injectedFunction func(obj interface{}) (returnObj interface{}, err error)
injectedFunction func(obj runtime.Object) (returnObj runtime.Object, err error)
}

func (storage *SimpleRESTStorage) List(labels.Selector) (interface{}, error) {
func (storage *SimpleRESTStorage) List(labels.Selector) (runtime.Object, error) {
result := &SimpleList{
Items: storage.list,
}
return result, storage.errors["list"]
}

func (storage *SimpleRESTStorage) Get(id string) (interface{}, error) {
return storage.item, storage.errors["get"]
func (storage *SimpleRESTStorage) Get(id string) (runtime.Object, error) {
return runtime.DefaultScheme.CopyOrDie(&storage.item), storage.errors["get"]
}

func (storage *SimpleRESTStorage) Delete(id string) (<-chan interface{}, error) {
func (storage *SimpleRESTStorage) Delete(id string) (<-chan runtime.Object, error) {
storage.deleted = id
if err := storage.errors["delete"]; err != nil {
return nil, err
}
return MakeAsync(func() (interface{}, error) {
return MakeAsync(func() (runtime.Object, error) {
if storage.injectedFunction != nil {
return storage.injectedFunction(id)
return storage.injectedFunction(&Simple{JSONBase: api.JSONBase{ID: id}})
}
return &api.Status{Status: api.StatusSuccess}, nil
}), nil
}

func (storage *SimpleRESTStorage) New() interface{} {
func (storage *SimpleRESTStorage) New() runtime.Object {
return &Simple{}
}

func (storage *SimpleRESTStorage) Create(obj interface{}) (<-chan interface{}, error) {
func (storage *SimpleRESTStorage) Create(obj runtime.Object) (<-chan runtime.Object, error) {
storage.created = obj.(*Simple)
if err := storage.errors["create"]; err != nil {
return nil, err
}
return MakeAsync(func() (interface{}, error) {
return MakeAsync(func() (runtime.Object, error) {
if storage.injectedFunction != nil {
return storage.injectedFunction(obj)
}
return obj, nil
}), nil
}

func (storage *SimpleRESTStorage) Update(obj interface{}) (<-chan interface{}, error) {
func (storage *SimpleRESTStorage) Update(obj runtime.Object) (<-chan runtime.Object, error) {
storage.updated = obj.(*Simple)
if err := storage.errors["update"]; err != nil {
return nil, err
}
return MakeAsync(func() (interface{}, error) {
return MakeAsync(func() (runtime.Object, error) {
if storage.injectedFunction != nil {
return storage.injectedFunction(obj)
}
Expand Down Expand Up @@ -156,7 +160,7 @@ func (storage *SimpleRESTStorage) ResourceLocation(id string) (string, error) {
return id, nil
}

func extractBody(response *http.Response, object interface{}) (string, error) {
func extractBody(response *http.Response, object runtime.Object) (string, error) {
defer response.Body.Close()
body, err := ioutil.ReadAll(response.Body)
if err != nil {
Expand Down Expand Up @@ -398,7 +402,7 @@ func TestUpdate(t *testing.T) {
handler := Handle(storage, codec, "/prefix/version")
server := httptest.NewServer(handler)

item := Simple{
item := &Simple{
Name: "bar",
}
body, err := codec.Encode(item)
Expand Down Expand Up @@ -428,7 +432,7 @@ func TestUpdateMissing(t *testing.T) {
handler := Handle(storage, codec, "/prefix/version")
server := httptest.NewServer(handler)

item := Simple{
item := &Simple{
Name: "bar",
}
body, err := codec.Encode(item)
Expand Down Expand Up @@ -457,7 +461,7 @@ func TestCreate(t *testing.T) {
server := httptest.NewServer(handler)
client := http.Client{}

simple := Simple{
simple := &Simple{
Name: "foo",
}
data, _ := codec.Encode(simple)
Expand Down Expand Up @@ -497,7 +501,7 @@ func TestCreateNotFound(t *testing.T) {
server := httptest.NewServer(handler)
client := http.Client{}

simple := Simple{Name: "foo"}
simple := &Simple{Name: "foo"}
data, _ := codec.Encode(simple)
request, err := http.NewRequest("POST", server.URL+"/prefix/version/simple", bytes.NewBuffer(data))
if err != nil {
Expand Down Expand Up @@ -528,7 +532,7 @@ func TestParseTimeout(t *testing.T) {

func TestSyncCreate(t *testing.T) {
storage := SimpleRESTStorage{
injectedFunction: func(obj interface{}) (interface{}, error) {
injectedFunction: func(obj runtime.Object) (runtime.Object, error) {
time.Sleep(5 * time.Millisecond)
return obj, nil
},
Expand All @@ -539,7 +543,7 @@ func TestSyncCreate(t *testing.T) {
server := httptest.NewServer(handler)
client := http.Client{}

simple := Simple{
simple := &Simple{
Name: "foo",
}
data, _ := codec.Encode(simple)
Expand All @@ -566,7 +570,7 @@ func TestSyncCreate(t *testing.T) {
t.Errorf("unexpected error: %v", err)
}

if !reflect.DeepEqual(itemOut, simple) {
if !reflect.DeepEqual(&itemOut, simple) {
t.Errorf("Unexpected data: %#v, expected %#v (%s)", itemOut, simple, string(body))
}
if response.StatusCode != http.StatusOK {
Expand Down Expand Up @@ -600,7 +604,7 @@ func expectApiStatus(t *testing.T, method, url string, data []byte, code int) *a

func TestAsyncDelayReturnsError(t *testing.T) {
storage := SimpleRESTStorage{
injectedFunction: func(obj interface{}) (interface{}, error) {
injectedFunction: func(obj runtime.Object) (runtime.Object, error) {
return nil, apierrs.NewAlreadyExists("foo", "bar")
},
}
Expand All @@ -617,7 +621,7 @@ func TestAsyncDelayReturnsError(t *testing.T) {
func TestAsyncCreateError(t *testing.T) {
ch := make(chan struct{})
storage := SimpleRESTStorage{
injectedFunction: func(obj interface{}) (interface{}, error) {
injectedFunction: func(obj runtime.Object) (runtime.Object, error) {
<-ch
return nil, apierrs.NewAlreadyExists("foo", "bar")
},
Expand All @@ -626,7 +630,7 @@ func TestAsyncCreateError(t *testing.T) {
handler.(*defaultAPIServer).group.handler.asyncOpWait = 0
server := httptest.NewServer(handler)

simple := Simple{Name: "foo"}
simple := &Simple{Name: "foo"}
data, _ := codec.Encode(simple)

status := expectApiStatus(t, "POST", fmt.Sprintf("%s/prefix/version/foo", server.URL), data, http.StatusAccepted)
Expand Down Expand Up @@ -662,18 +666,21 @@ func TestAsyncCreateError(t *testing.T) {
}
}

type UnregisteredAPIObject struct {
Value string
}

func (*UnregisteredAPIObject) IsAnAPIObject() {}

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, runtime.DefaultCodec, &T{"Undecodable"}, w)
writeJSON(http.StatusOK, runtime.DefaultCodec, &UnregisteredAPIObject{"Undecodable"}, w)
}))
status := expectApiStatus(t, "GET", server.URL, nil, http.StatusInternalServerError)
if status.Reason != api.StatusReasonUnknown {
t.Errorf("unexpected reason %#v", status)
}
if !strings.Contains(status.Message, "type apiserver.T is not registered") {
if !strings.Contains(status.Message, "type apiserver.UnregisteredAPIObject is not registered") {
t.Errorf("unexpected message %#v", status)
}
}
Expand Down Expand Up @@ -705,7 +712,7 @@ func TestSyncCreateTimeout(t *testing.T) {
testOver := make(chan struct{})
defer close(testOver)
storage := SimpleRESTStorage{
injectedFunction: func(obj interface{}) (interface{}, error) {
injectedFunction: func(obj runtime.Object) (runtime.Object, error) {
// Eliminate flakes by ensuring the create operation takes longer than this test.
<-testOver
return obj, nil
Expand All @@ -716,7 +723,7 @@ func TestSyncCreateTimeout(t *testing.T) {
}, codec, "/prefix/version")
server := httptest.NewServer(handler)

simple := Simple{Name: "foo"}
simple := &Simple{Name: "foo"}
data, _ := codec.Encode(simple)
itemOut := expectApiStatus(t, "POST", server.URL+"/prefix/version/foo?sync=true&timeout=4ms", data, http.StatusAccepted)
if itemOut.Status != api.StatusWorking || itemOut.Details == nil || itemOut.Details.ID == "" {
Expand Down
15 changes: 8 additions & 7 deletions pkg/apiserver/operation_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,20 +28,21 @@ import (
// TODO: remove dependency on api, apiserver should be generic
"github.com/GoogleCloudPlatform/kubernetes/pkg/api"
_ "github.com/GoogleCloudPlatform/kubernetes/pkg/api/v1beta1"
"github.com/GoogleCloudPlatform/kubernetes/pkg/runtime"
)

func TestOperation(t *testing.T) {
ops := NewOperations()

c := make(chan interface{})
c := make(chan runtime.Object)
op := ops.NewOperation(c)
// Allow context switch, so that op's ID can get added to the map and Get will work.
// This is just so we can test Get. Ordinary users have no need to call Get immediately
// after calling NewOperation, because it returns the operation directly.
time.Sleep(time.Millisecond)
go func() {
time.Sleep(500 * time.Millisecond)
c <- "All done"
c <- &Simple{JSONBase: api.JSONBase{ID: "All done"}}
}()

if op.expired(time.Now().Add(-time.Minute)) {
Expand Down Expand Up @@ -89,7 +90,7 @@ func TestOperation(t *testing.T) {
t.Errorf("expire failed to remove the operation %#v", ops)
}

if op.result.(string) != "All done" {
if op.result.(*Simple).ID != "All done" {
t.Errorf("Got unexpected result: %#v", op.result)
}
}
Expand All @@ -98,7 +99,7 @@ func TestOperationsList(t *testing.T) {
testOver := make(chan struct{})
defer close(testOver)
simpleStorage := &SimpleRESTStorage{
injectedFunction: func(obj interface{}) (interface{}, error) {
injectedFunction: func(obj runtime.Object) (runtime.Object, error) {
// Eliminate flakes by ensuring the create operation takes longer than this test.
<-testOver
return obj, nil
Expand All @@ -111,7 +112,7 @@ func TestOperationsList(t *testing.T) {
server := httptest.NewServer(handler)
client := http.Client{}

simple := Simple{
simple := &Simple{
Name: "foo",
}
data, err := codec.Encode(simple)
Expand Down Expand Up @@ -154,7 +155,7 @@ func TestOpGet(t *testing.T) {
testOver := make(chan struct{})
defer close(testOver)
simpleStorage := &SimpleRESTStorage{
injectedFunction: func(obj interface{}) (interface{}, error) {
injectedFunction: func(obj runtime.Object) (runtime.Object, error) {
// Eliminate flakes by ensuring the create operation takes longer than this test.
<-testOver
return obj, nil
Expand All @@ -167,7 +168,7 @@ func TestOpGet(t *testing.T) {
server := httptest.NewServer(handler)
client := http.Client{}

simple := Simple{
simple := &Simple{
Name: "foo",
}
data, err := codec.Encode(simple)
Expand Down
7 changes: 4 additions & 3 deletions pkg/apiserver/watch_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,12 +26,13 @@ import (

"code.google.com/p/go.net/websocket"
"github.com/GoogleCloudPlatform/kubernetes/pkg/api"
"github.com/GoogleCloudPlatform/kubernetes/pkg/runtime"
"github.com/GoogleCloudPlatform/kubernetes/pkg/watch"
)

var watchTestTable = []struct {
t watch.EventType
obj interface{}
obj runtime.Object
}{
{watch.Added, &Simple{Name: "A Name"}},
{watch.Modified, &Simple{Name: "Another Name"}},
Expand All @@ -56,7 +57,7 @@ func TestWatchWebsocket(t *testing.T) {
t.Errorf("unexpected error: %v", err)
}

try := func(action watch.EventType, object interface{}) {
try := func(action watch.EventType, object runtime.Object) {
// Send
simpleStorage.fakeWatch.Action(action, object)
// Test receive
Expand Down Expand Up @@ -113,7 +114,7 @@ func TestWatchHTTP(t *testing.T) {

decoder := json.NewDecoder(response.Body)

try := func(action watch.EventType, object interface{}) {
try := func(action watch.EventType, object runtime.Object) {
// Send
simpleStorage.fakeWatch.Action(action, object)
// Test receive
Expand Down
Loading

0 comments on commit fc09f98

Please sign in to comment.