2014-06-20 01:03: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-20 21:01:16 +00:00
|
|
|
|
2014-08-11 07:34:59 +00:00
|
|
|
package minion
|
2014-06-20 01:03:48 +00:00
|
|
|
|
|
|
|
import (
|
|
|
|
"testing"
|
|
|
|
|
2014-06-20 21:01:16 +00:00
|
|
|
"github.com/GoogleCloudPlatform/kubernetes/pkg/api"
|
2014-06-20 01:03:48 +00:00
|
|
|
"github.com/GoogleCloudPlatform/kubernetes/pkg/labels"
|
2014-09-09 03:02:21 +00:00
|
|
|
"github.com/GoogleCloudPlatform/kubernetes/pkg/registry/registrytest"
|
2014-06-20 01:03:48 +00:00
|
|
|
)
|
|
|
|
|
2014-09-08 21:40:56 +00:00
|
|
|
func TestMinionREST(t *testing.T) {
|
2014-09-09 03:02:21 +00:00
|
|
|
ms := NewREST(registrytest.NewMinionRegistry([]string{"foo", "bar"}, api.NodeResources{}))
|
2014-09-25 18:35:10 +00:00
|
|
|
ctx := api.NewContext()
|
2014-10-22 17:02:02 +00:00
|
|
|
if obj, err := ms.Get(ctx, "foo"); err != nil || obj.(*api.Minion).Name != "foo" {
|
2014-06-20 01:03:48 +00:00
|
|
|
t.Errorf("missing expected object")
|
|
|
|
}
|
2014-10-22 17:02:02 +00:00
|
|
|
if obj, err := ms.Get(ctx, "bar"); err != nil || obj.(*api.Minion).Name != "bar" {
|
2014-06-20 01:03:48 +00:00
|
|
|
t.Errorf("missing expected object")
|
|
|
|
}
|
2014-09-25 18:35:10 +00:00
|
|
|
if _, err := ms.Get(ctx, "baz"); err != ErrDoesNotExist {
|
2014-06-20 01:03:48 +00:00
|
|
|
t.Errorf("has unexpected object")
|
|
|
|
}
|
|
|
|
|
2014-10-23 20:51:34 +00:00
|
|
|
c, err := ms.Create(ctx, &api.Minion{ObjectMeta: api.ObjectMeta{Name: "baz"}})
|
2014-06-25 23:23:15 +00:00
|
|
|
if err != nil {
|
2014-06-20 01:03:48 +00:00
|
|
|
t.Errorf("insert failed")
|
|
|
|
}
|
2014-06-25 23:23:15 +00:00
|
|
|
obj := <-c
|
2014-10-24 17:16:02 +00:00
|
|
|
if m, ok := obj.Object.(*api.Minion); !ok || m.Name != "baz" {
|
2014-06-25 23:23:15 +00:00
|
|
|
t.Errorf("insert return value was weird: %#v", obj)
|
|
|
|
}
|
2014-10-22 17:02:02 +00:00
|
|
|
if obj, err := ms.Get(ctx, "baz"); err != nil || obj.(*api.Minion).Name != "baz" {
|
2014-06-20 01:03:48 +00:00
|
|
|
t.Errorf("insert didn't actually insert")
|
|
|
|
}
|
|
|
|
|
2014-09-25 18:35:10 +00:00
|
|
|
c, err = ms.Delete(ctx, "bar")
|
2014-06-25 23:23:15 +00:00
|
|
|
if err != nil {
|
2014-06-20 01:03:48 +00:00
|
|
|
t.Errorf("delete failed")
|
|
|
|
}
|
2014-06-25 23:23:15 +00:00
|
|
|
obj = <-c
|
2014-10-24 17:16:02 +00:00
|
|
|
if s, ok := obj.Object.(*api.Status); !ok || s.Status != api.StatusSuccess {
|
2014-06-25 23:23:15 +00:00
|
|
|
t.Errorf("delete return value was weird: %#v", obj)
|
|
|
|
}
|
2014-09-25 18:35:10 +00:00
|
|
|
if _, err := ms.Get(ctx, "bar"); err != ErrDoesNotExist {
|
2014-06-20 01:03:48 +00:00
|
|
|
t.Errorf("delete didn't actually delete")
|
|
|
|
}
|
2014-06-25 23:23:15 +00:00
|
|
|
|
2014-09-25 18:35:10 +00:00
|
|
|
_, err = ms.Delete(ctx, "bar")
|
2014-06-25 23:23:15 +00:00
|
|
|
if err != ErrDoesNotExist {
|
2014-06-20 01:03:48 +00:00
|
|
|
t.Errorf("delete returned wrong error")
|
|
|
|
}
|
|
|
|
|
2014-09-25 18:35:10 +00:00
|
|
|
list, err := ms.List(ctx, labels.Everything(), labels.Everything())
|
2014-06-20 01:03:48 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Errorf("got error calling List")
|
|
|
|
}
|
2014-06-20 21:01:16 +00:00
|
|
|
expect := []api.Minion{
|
|
|
|
{
|
2014-10-23 20:51:34 +00:00
|
|
|
ObjectMeta: api.ObjectMeta{Name: "foo"},
|
2014-09-09 03:02:21 +00:00
|
|
|
}, {
|
2014-10-23 20:51:34 +00:00
|
|
|
ObjectMeta: api.ObjectMeta{Name: "baz"},
|
2014-06-20 21:01:16 +00:00
|
|
|
},
|
|
|
|
}
|
2014-09-26 23:28:30 +00:00
|
|
|
nodeList := list.(*api.MinionList)
|
|
|
|
if len(expect) != len(nodeList.Items) || !contains(nodeList, "foo") || !contains(nodeList, "baz") {
|
2014-06-20 01:03:48 +00:00
|
|
|
t.Errorf("Unexpected list value: %#v", list)
|
|
|
|
}
|
|
|
|
}
|
2014-09-09 03:02:21 +00:00
|
|
|
|
|
|
|
func contains(nodes *api.MinionList, nodeID string) bool {
|
|
|
|
for _, node := range nodes.Items {
|
2014-10-22 17:02:02 +00:00
|
|
|
if node.Name == nodeID {
|
2014-09-09 03:02:21 +00:00
|
|
|
return true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return false
|
|
|
|
}
|