k8s.io/apiserver@v0.31.1/pkg/registry/rest/meta_test.go (about)

     1  /*
     2  Copyright 2017 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 rest
    18  
    19  import (
    20  	"testing"
    21  
    22  	"k8s.io/apimachinery/pkg/api/meta"
    23  	metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
    24  )
    25  
    26  // TestWipeObjectMetaSystemFields validates that system populated fields are set on an object
    27  func TestWipeObjectMetaSystemFields(t *testing.T) {
    28  	resource := metav1.ObjectMeta{}
    29  	WipeObjectMetaSystemFields(&resource)
    30  	if !resource.CreationTimestamp.Time.IsZero() {
    31  		t.Errorf("resource.CreationTimestamp is set")
    32  	}
    33  	if len(resource.UID) != 0 {
    34  		t.Errorf("resource.UID is set")
    35  	}
    36  	if resource.DeletionTimestamp != nil {
    37  		t.Errorf("resource.DeletionTimestamp is set")
    38  	}
    39  	if resource.DeletionGracePeriodSeconds != nil {
    40  		t.Errorf("resource.DeletionGracePeriodSeconds is set")
    41  	}
    42  	if len(resource.SelfLink) != 0 {
    43  		t.Errorf("resource.SelfLink is set")
    44  	}
    45  }
    46  
    47  // TestFillObjectMetaSystemFields validates that system populated fields are set on an object
    48  func TestFillObjectMetaSystemFields(t *testing.T) {
    49  	resource := metav1.ObjectMeta{}
    50  	FillObjectMetaSystemFields(&resource)
    51  	if resource.CreationTimestamp.Time.IsZero() {
    52  		t.Errorf("resource.CreationTimestamp is zero")
    53  	}
    54  	if len(resource.UID) == 0 {
    55  		t.Errorf("resource.UID missing")
    56  	}
    57  }
    58  
    59  // TestHasObjectMetaSystemFieldValues validates that true is returned if and only if all fields are populated
    60  func TestHasObjectMetaSystemFieldValues(t *testing.T) {
    61  	resource := metav1.ObjectMeta{}
    62  	objMeta, err := meta.Accessor(&resource)
    63  	if err != nil {
    64  		t.Fatal(err)
    65  	}
    66  	if metav1.HasObjectMetaSystemFieldValues(objMeta) {
    67  		t.Errorf("the resource does not have all fields yet populated, but incorrectly reports it does")
    68  	}
    69  	FillObjectMetaSystemFields(&resource)
    70  	if !metav1.HasObjectMetaSystemFieldValues(objMeta) {
    71  		t.Errorf("the resource does have all fields populated, but incorrectly reports it does not")
    72  	}
    73  }
    74  
    75  func TestEnsureObjectNamespaceMatchesRequestNamespace(t *testing.T) {
    76  	testcases := []struct {
    77  		name        string
    78  		reqNS       string
    79  		objNS       string
    80  		expectErr   bool
    81  		expectObjNS string
    82  	}{
    83  		{
    84  			name:        "cluster-scoped req, cluster-scoped obj",
    85  			reqNS:       "",
    86  			objNS:       "",
    87  			expectErr:   false,
    88  			expectObjNS: "",
    89  		},
    90  		{
    91  			name:        "cluster-scoped req, namespaced obj",
    92  			reqNS:       "",
    93  			objNS:       "foo",
    94  			expectErr:   false,
    95  			expectObjNS: "", // no error, object is forced to cluster-scoped for backwards compatibility
    96  		},
    97  		{
    98  			name:        "namespaced req, no-namespace obj",
    99  			reqNS:       "foo",
   100  			objNS:       "",
   101  			expectErr:   false,
   102  			expectObjNS: "foo", // no error, object is updated to match request for backwards compatibility
   103  		},
   104  		{
   105  			name:        "namespaced req, matching obj",
   106  			reqNS:       "foo",
   107  			objNS:       "foo",
   108  			expectErr:   false,
   109  			expectObjNS: "foo",
   110  		},
   111  		{
   112  			name:      "namespaced req, mis-matched obj",
   113  			reqNS:     "foo",
   114  			objNS:     "bar",
   115  			expectErr: true,
   116  		},
   117  	}
   118  	for _, tc := range testcases {
   119  		t.Run(tc.name, func(t *testing.T) {
   120  			obj := metav1.ObjectMeta{Namespace: tc.objNS}
   121  			err := EnsureObjectNamespaceMatchesRequestNamespace(tc.reqNS, &obj)
   122  			if tc.expectErr {
   123  				if err == nil {
   124  					t.Fatal("expected err, got none")
   125  				}
   126  				return
   127  			} else if err != nil {
   128  				t.Fatalf("unexpected err: %v", err)
   129  			}
   130  
   131  			if obj.Namespace != tc.expectObjNS {
   132  				t.Fatalf("expected obj ns %q, got %q", tc.expectObjNS, obj.Namespace)
   133  			}
   134  		})
   135  	}
   136  }