Verify namespace is not set on root objects

This commit is contained in:
Clayton Coleman
2015-05-07 23:51:36 -04:00
parent 65cf37ab6d
commit 964bc8afda
2 changed files with 37 additions and 0 deletions

View File

@@ -147,6 +147,15 @@ func streamYAMLTestData() (io.Reader, *api.PodList, *api.ServiceList) {
return r, pods, svc
}
func streamTestObject(obj runtime.Object) io.Reader {
r, w := io.Pipe()
go func() {
defer w.Close()
w.Write([]byte(runtime.EncodeOrDie(latest.Codec, obj)))
}()
return r
}
type testVisitor struct {
InjectErr error
Infos []*Info
@@ -617,6 +626,31 @@ func TestSingularObject(t *testing.T) {
}
}
func TestSingularRootScopedObject(t *testing.T) {
node := &api.Node{ObjectMeta: api.ObjectMeta{Name: "test"}, Spec: api.NodeSpec{ExternalID: "test"}}
r := streamTestObject(node)
infos, err := NewBuilder(latest.RESTMapper, api.Scheme, fakeClient()).
NamespaceParam("test").DefaultNamespace().
Stream(r, "STDIN").
Flatten().
Do().Infos()
if err != nil || len(infos) != 1 {
t.Fatalf("unexpected error: %v", err)
}
if infos[0].Namespace != "" {
t.Errorf("namespace should be empty: %#v", infos[0])
}
n, ok := infos[0].Object.(*api.Node)
if !ok {
t.Fatalf("unexpected object: %#v", infos[0].Object)
}
if n.Name != "test" || n.Namespace != "" {
t.Errorf("unexpected object: %#v", n)
}
}
func TestListObject(t *testing.T) {
pods, _ := testData()
labelKey := api.LabelSelectorQueryParam(testapi.Version())

View File

@@ -474,6 +474,9 @@ func FilterNamespace(info *Info) error {
// set. If info.Object is set, it will be mutated as well.
func SetNamespace(namespace string) VisitorFunc {
return func(info *Info) error {
if !info.Namespaced() {
return nil
}
if len(info.Namespace) == 0 {
info.Namespace = namespace
UpdateObjectNamespace(info)