Minions should have common logic with create

TODO: disable / document generate names for cluster scoped resources.
This commit is contained in:
Clayton Coleman
2015-01-28 21:56:57 -05:00
parent 0a87f0332b
commit e485dc93ca
5 changed files with 112 additions and 12 deletions

View File

@@ -29,7 +29,8 @@ import (
type Tester struct {
*testing.T
storage apiserver.RESTStorage
storage apiserver.RESTStorage
clusterScope bool
}
func New(t *testing.T, storage apiserver.RESTStorage) *Tester {
@@ -39,6 +40,11 @@ func New(t *testing.T, storage apiserver.RESTStorage) *Tester {
}
}
func (t *Tester) ClusterScope() *Tester {
t.clusterScope = true
return t
}
func copyOrDie(obj runtime.Object) runtime.Object {
out, err := api.Scheme.Copy(obj)
if err != nil {
@@ -50,7 +56,11 @@ func copyOrDie(obj runtime.Object) runtime.Object {
func (t *Tester) TestCreate(valid runtime.Object, invalid ...runtime.Object) {
t.TestCreateHasMetadata(copyOrDie(valid))
t.TestCreateGeneratesName(copyOrDie(valid))
t.TestCreateRejectsMismatchedNamespace(copyOrDie(valid))
if t.clusterScope {
t.TestCreateRejectsNamespace(copyOrDie(valid))
} else {
t.TestCreateRejectsMismatchedNamespace(copyOrDie(valid))
}
t.TestCreateInvokesValidation(invalid...)
}
@@ -84,8 +94,13 @@ func (t *Tester) TestCreateHasMetadata(valid runtime.Object) {
objectMeta.Name = "test"
objectMeta.Namespace = api.NamespaceDefault
context := api.NewDefaultContext()
if t.clusterScope {
objectMeta.Namespace = api.NamespaceNone
context = api.NewContext()
}
channel, err := t.storage.(apiserver.RESTCreater).Create(api.NewDefaultContext(), valid)
channel, err := t.storage.(apiserver.RESTCreater).Create(context, valid)
if err != nil {
t.Fatalf("Unexpected error: %v", err)
}
@@ -139,3 +154,19 @@ func (t *Tester) TestCreateRejectsMismatchedNamespace(valid runtime.Object) {
t.Errorf("Expected 'Controller.Namespace does not match the provided context' error, got '%v'", err.Error())
}
}
func (t *Tester) TestCreateRejectsNamespace(valid runtime.Object) {
objectMeta, err := api.ObjectMetaFor(valid)
if err != nil {
t.Fatalf("object does not have ObjectMeta: %v\n%#v", err, valid)
}
objectMeta.Namespace = "not-default"
_, err = t.storage.(apiserver.RESTCreater).Create(api.NewDefaultContext(), valid)
if err == nil {
t.Errorf("Expected an error, but we didn't get one")
} else if strings.Contains(err.Error(), "Controller.Namespace does not match the provided context") {
t.Errorf("Expected 'Controller.Namespace does not match the provided context' error, got '%v'", err.Error())
}
}