Improvements to namespace registry to align with pod model

This commit is contained in:
derekwaynecarr
2015-03-12 11:08:06 -04:00
parent 972a3b1998
commit 2d13dfaf13
9 changed files with 588 additions and 299 deletions

View File

@@ -17,169 +17,24 @@ limitations under the License.
package namespace
import (
"reflect"
"testing"
"github.com/GoogleCloudPlatform/kubernetes/pkg/api"
"github.com/GoogleCloudPlatform/kubernetes/pkg/apiserver"
"github.com/GoogleCloudPlatform/kubernetes/pkg/fields"
"github.com/GoogleCloudPlatform/kubernetes/pkg/labels"
"github.com/GoogleCloudPlatform/kubernetes/pkg/registry/registrytest"
"github.com/GoogleCloudPlatform/kubernetes/pkg/util"
"github.com/GoogleCloudPlatform/kubernetes/pkg/watch"
)
type testRegistry struct {
*registrytest.GenericRegistry
}
func NewTestREST() (testRegistry, *REST) {
reg := testRegistry{registrytest.NewGeneric(nil)}
return reg, NewREST(reg)
}
func testNamespace(name string) *api.Namespace {
return &api.Namespace{
ObjectMeta: api.ObjectMeta{
Name: name,
},
}
}
func TestRESTCreate(t *testing.T) {
table := []struct {
ctx api.Context
namespace *api.Namespace
valid bool
}{
{
ctx: api.NewContext(),
namespace: testNamespace("foo"),
valid: true,
}, {
ctx: api.NewContext(),
namespace: testNamespace("bar"),
valid: true,
},
}
for _, item := range table {
_, rest := NewTestREST()
c, err := rest.Create(item.ctx, item.namespace)
if !item.valid {
if err == nil {
t.Errorf("unexpected non-error for %v", item.namespace.Name)
}
continue
}
if err != nil {
t.Errorf("%v: Unexpected error %v", item.namespace.Name, err)
continue
}
if !api.HasObjectMetaSystemFieldValues(&item.namespace.ObjectMeta) {
t.Errorf("storage did not populate object meta field values")
}
if e, a := item.namespace, c; !reflect.DeepEqual(e, a) {
t.Errorf("diff: %s", util.ObjectDiff(e, a))
}
// Ensure we implement the interface
_ = apiserver.ResourceWatcher(rest)
}
}
func TestRESTUpdate(t *testing.T) {
_, rest := NewTestREST()
namespaceA := testNamespace("foo")
_, err := rest.Create(api.NewDefaultContext(), namespaceA)
if err != nil {
t.Fatalf("Unexpected error %v", err)
}
got, err := rest.Get(api.NewDefaultContext(), namespaceA.Name)
if err != nil {
t.Fatalf("Unexpected error %v", err)
}
if e, a := namespaceA, got; !reflect.DeepEqual(e, a) {
t.Errorf("diff: %s", util.ObjectDiff(e, a))
}
namespaceB := testNamespace("foo")
_, _, err = rest.Update(api.NewDefaultContext(), namespaceB)
if err != nil {
t.Fatalf("Unexpected error %v", err)
}
got2, err := rest.Get(api.NewDefaultContext(), namespaceB.Name)
if err != nil {
t.Fatalf("Unexpected error %v", err)
}
if e, a := namespaceB, got2; !reflect.DeepEqual(e, a) {
t.Errorf("diff: %s", util.ObjectDiff(e, a))
}
}
func TestRESTDelete(t *testing.T) {
_, rest := NewTestREST()
namespaceA := testNamespace("foo")
_, err := rest.Create(api.NewContext(), namespaceA)
if err != nil {
t.Fatalf("Unexpected error %v", err)
}
c, err := rest.Delete(api.NewContext(), namespaceA.Name)
if err != nil {
t.Fatalf("Unexpected error %v", err)
}
if stat := c.(*api.Status); stat.Status != api.StatusSuccess {
t.Errorf("unexpected status: %v", stat)
}
}
func TestRESTGet(t *testing.T) {
_, rest := NewTestREST()
namespaceA := testNamespace("foo")
_, err := rest.Create(api.NewContext(), namespaceA)
if err != nil {
t.Fatalf("Unexpected error %v", err)
}
got, err := rest.Get(api.NewContext(), namespaceA.Name)
if err != nil {
t.Fatalf("Unexpected error %v", err)
}
if e, a := namespaceA, got; !reflect.DeepEqual(e, a) {
t.Errorf("diff: %s", util.ObjectDiff(e, a))
}
}
func TestRESTList(t *testing.T) {
reg, rest := NewTestREST()
namespaceA := testNamespace("foo")
namespaceB := testNamespace("bar")
namespaceC := testNamespace("baz")
reg.ObjectList = &api.NamespaceList{
Items: []api.Namespace{*namespaceA, *namespaceB, *namespaceC},
}
got, err := rest.List(api.NewContext(), labels.Everything(), fields.Everything())
if err != nil {
t.Fatalf("Unexpected error %v", err)
}
expect := &api.NamespaceList{
Items: []api.Namespace{*namespaceA, *namespaceB, *namespaceC},
}
if e, a := expect, got; !reflect.DeepEqual(e, a) {
t.Errorf("diff: %s", util.ObjectDiff(e, a))
}
}
func TestRESTWatch(t *testing.T) {
namespaceA := testNamespace("foo")
reg, rest := NewTestREST()
wi, err := rest.Watch(api.NewContext(), labels.Everything(), fields.Everything(), "0")
if err != nil {
t.Fatalf("Unexpected error %v", err)
}
go func() {
reg.Broadcaster.Action(watch.Added, namespaceA)
}()
got := <-wi.ResultChan()
if e, a := namespaceA, got.Object; !reflect.DeepEqual(e, a) {
t.Errorf("diff: %s", util.ObjectDiff(e, a))
func TestNamespaceStrategy(t *testing.T) {
if Strategy.NamespaceScoped() {
t.Errorf("Namespaces should not be namespace scoped")
}
if Strategy.AllowCreateOnUpdate() {
t.Errorf("Namespaces should not allow create on update")
}
namespace := &api.Namespace{
ObjectMeta: api.ObjectMeta{Name: "foo"},
Status: api.NamespaceStatus{Phase: api.NamespaceTerminating},
}
Strategy.ResetBeforeCreate(namespace)
if namespace.Status.Phase != api.NamespaceActive {
t.Errorf("Namespaces do not allow setting phase on create")
}
}