...

Source file src/k8s.io/kubernetes/pkg/registry/core/namespace/strategy_test.go

Documentation: k8s.io/kubernetes/pkg/registry/core/namespace

     1  /*
     2  Copyright 2014 The Kubernetes Authors.
     3  
     4  Licensed under the Apache License, Version 2.0 (the "License");
     5  you may not use this file except in compliance with the License.
     6  You may obtain a copy of the License at
     7  
     8      http://www.apache.org/licenses/LICENSE-2.0
     9  
    10  Unless required by applicable law or agreed to in writing, software
    11  distributed under the License is distributed on an "AS IS" BASIS,
    12  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    13  See the License for the specific language governing permissions and
    14  limitations under the License.
    15  */
    16  
    17  package namespace
    18  
    19  import (
    20  	"testing"
    21  
    22  	v1 "k8s.io/api/core/v1"
    23  	metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
    24  	genericapirequest "k8s.io/apiserver/pkg/endpoints/request"
    25  	apitesting "k8s.io/kubernetes/pkg/api/testing"
    26  	api "k8s.io/kubernetes/pkg/apis/core"
    27  
    28  	// ensure types are installed
    29  	_ "k8s.io/kubernetes/pkg/apis/core/install"
    30  )
    31  
    32  func TestNamespaceStrategy(t *testing.T) {
    33  	ctx := genericapirequest.NewDefaultContext()
    34  	if Strategy.NamespaceScoped() {
    35  		t.Errorf("Namespaces should not be namespace scoped")
    36  	}
    37  	if Strategy.AllowCreateOnUpdate() {
    38  		t.Errorf("Namespaces should not allow create on update")
    39  	}
    40  	namespace := &api.Namespace{
    41  		ObjectMeta: metav1.ObjectMeta{Name: "foo", ResourceVersion: "10", Labels: map[string]string{v1.LabelMetadataName: "foo"}},
    42  		Status:     api.NamespaceStatus{Phase: api.NamespaceTerminating},
    43  	}
    44  	Strategy.PrepareForCreate(ctx, namespace)
    45  	if namespace.Status.Phase != api.NamespaceActive {
    46  		t.Errorf("Namespaces do not allow setting phase on create")
    47  	}
    48  	if len(namespace.Spec.Finalizers) != 1 || namespace.Spec.Finalizers[0] != api.FinalizerKubernetes {
    49  		t.Errorf("Prepare For Create should have added kubernetes finalizer")
    50  	}
    51  	errs := Strategy.Validate(ctx, namespace)
    52  	if len(errs) != 0 {
    53  		t.Errorf("Unexpected error validating %v", errs)
    54  	}
    55  	invalidNamespace := &api.Namespace{
    56  		ObjectMeta: metav1.ObjectMeta{Name: "bar", ResourceVersion: "4"},
    57  	}
    58  	// ensure we copy spec.finalizers from old to new
    59  	Strategy.PrepareForUpdate(ctx, invalidNamespace, namespace)
    60  	if len(invalidNamespace.Spec.Finalizers) != 1 || invalidNamespace.Spec.Finalizers[0] != api.FinalizerKubernetes {
    61  		t.Errorf("PrepareForUpdate should have preserved old.spec.finalizers")
    62  	}
    63  	errs = Strategy.ValidateUpdate(ctx, invalidNamespace, namespace)
    64  	if len(errs) == 0 {
    65  		t.Errorf("Expected a validation error")
    66  	}
    67  	if invalidNamespace.ResourceVersion != "4" {
    68  		t.Errorf("Incoming resource version on update should not be mutated")
    69  	}
    70  }
    71  
    72  func TestNamespaceDefaultLabelCanonicalize(t *testing.T) {
    73  	namespace := &api.Namespace{
    74  		ObjectMeta: metav1.ObjectMeta{Name: "foo"},
    75  	}
    76  
    77  	Strategy.Canonicalize(namespace)
    78  	if namespace.Labels[v1.LabelMetadataName] != namespace.Name {
    79  		t.Errorf("Invalid namespace, default label was not added")
    80  	}
    81  }
    82  
    83  func TestNamespaceStatusStrategy(t *testing.T) {
    84  	ctx := genericapirequest.NewDefaultContext()
    85  	if StatusStrategy.NamespaceScoped() {
    86  		t.Errorf("Namespaces should not be namespace scoped")
    87  	}
    88  	if StatusStrategy.AllowCreateOnUpdate() {
    89  		t.Errorf("Namespaces should not allow create on update")
    90  	}
    91  	now := metav1.Now()
    92  	oldNamespace := &api.Namespace{
    93  		ObjectMeta: metav1.ObjectMeta{Name: "foo", ResourceVersion: "10", DeletionTimestamp: &now,
    94  			Labels: map[string]string{v1.LabelMetadataName: "foo"}},
    95  		Spec:   api.NamespaceSpec{Finalizers: []api.FinalizerName{"kubernetes"}},
    96  		Status: api.NamespaceStatus{Phase: api.NamespaceActive},
    97  	}
    98  	namespace := &api.Namespace{
    99  		ObjectMeta: metav1.ObjectMeta{Name: "foo", ResourceVersion: "9", DeletionTimestamp: &now,
   100  			Labels: map[string]string{v1.LabelMetadataName: "foo"}},
   101  		Status: api.NamespaceStatus{Phase: api.NamespaceTerminating},
   102  	}
   103  	StatusStrategy.PrepareForUpdate(ctx, namespace, oldNamespace)
   104  	if namespace.Status.Phase != api.NamespaceTerminating {
   105  		t.Errorf("Namespace status updates should allow change of phase: %v", namespace.Status.Phase)
   106  	}
   107  	if len(namespace.Spec.Finalizers) != 1 || namespace.Spec.Finalizers[0] != api.FinalizerKubernetes {
   108  		t.Errorf("PrepareForUpdate should have preserved old finalizers")
   109  	}
   110  	errs := StatusStrategy.ValidateUpdate(ctx, namespace, oldNamespace)
   111  	if len(errs) != 0 {
   112  		t.Errorf("Unexpected error %v", errs)
   113  	}
   114  	if namespace.ResourceVersion != "9" {
   115  		t.Errorf("Incoming resource version on update should not be mutated")
   116  	}
   117  }
   118  
   119  func TestNamespaceFinalizeStrategy(t *testing.T) {
   120  	ctx := genericapirequest.NewDefaultContext()
   121  	if FinalizeStrategy.NamespaceScoped() {
   122  		t.Errorf("Namespaces should not be namespace scoped")
   123  	}
   124  	if FinalizeStrategy.AllowCreateOnUpdate() {
   125  		t.Errorf("Namespaces should not allow create on update")
   126  	}
   127  	oldNamespace := &api.Namespace{
   128  		ObjectMeta: metav1.ObjectMeta{Name: "foo", ResourceVersion: "10",
   129  			Labels: map[string]string{v1.LabelMetadataName: "foo"}},
   130  		Spec:   api.NamespaceSpec{Finalizers: []api.FinalizerName{"kubernetes", "example.com/org"}},
   131  		Status: api.NamespaceStatus{Phase: api.NamespaceActive},
   132  	}
   133  	namespace := &api.Namespace{
   134  		ObjectMeta: metav1.ObjectMeta{Name: "foo", ResourceVersion: "9",
   135  			Labels: map[string]string{v1.LabelMetadataName: "foo"}},
   136  		Spec:   api.NamespaceSpec{Finalizers: []api.FinalizerName{"example.com/foo"}},
   137  		Status: api.NamespaceStatus{Phase: api.NamespaceTerminating},
   138  	}
   139  	FinalizeStrategy.PrepareForUpdate(ctx, namespace, oldNamespace)
   140  	if namespace.Status.Phase != api.NamespaceActive {
   141  		t.Errorf("finalize updates should not allow change of phase: %v", namespace.Status.Phase)
   142  	}
   143  	if len(namespace.Spec.Finalizers) != 1 || string(namespace.Spec.Finalizers[0]) != "example.com/foo" {
   144  		t.Errorf("PrepareForUpdate should have modified finalizers")
   145  	}
   146  	errs := StatusStrategy.ValidateUpdate(ctx, namespace, oldNamespace)
   147  	if len(errs) != 0 {
   148  		t.Errorf("Unexpected error %v", errs)
   149  	}
   150  	if namespace.ResourceVersion != "9" {
   151  		t.Errorf("Incoming resource version on update should not be mutated")
   152  	}
   153  }
   154  
   155  func TestSelectableFieldLabelConversions(t *testing.T) {
   156  	apitesting.TestSelectableFieldLabelConversionsOfKind(t,
   157  		"v1",
   158  		"Namespace",
   159  		NamespaceToSelectableFields(&api.Namespace{}),
   160  		map[string]string{"name": "metadata.name"},
   161  	)
   162  }
   163  

View as plain text