...

Source file src/k8s.io/apimachinery/pkg/util/managedfields/internal/skipnonapplied_test.go

Documentation: k8s.io/apimachinery/pkg/util/managedfields/internal

     1  /*
     2  Copyright 2019 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 internal_test
    18  
    19  import (
    20  	"encoding/json"
    21  	"strings"
    22  	"testing"
    23  
    24  	apierrors "k8s.io/apimachinery/pkg/api/errors"
    25  	"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
    26  	"k8s.io/apimachinery/pkg/runtime/schema"
    27  	"k8s.io/apimachinery/pkg/util/managedfields/internal"
    28  	internaltesting "k8s.io/apimachinery/pkg/util/managedfields/internal/testing"
    29  	"sigs.k8s.io/yaml"
    30  )
    31  
    32  func TestNoUpdateBeforeFirstApply(t *testing.T) {
    33  	f := internaltesting.NewTestFieldManagerImpl(fakeTypeConverter, schema.FromAPIVersionAndKind("v1", "Pod"), "", func(m internal.Manager) internal.Manager {
    34  		return internal.NewSkipNonAppliedManager(m, &internaltesting.FakeObjectCreater{})
    35  	})
    36  
    37  	appliedObj := &unstructured.Unstructured{Object: map[string]interface{}{}}
    38  	if err := yaml.Unmarshal([]byte(`{
    39  		"apiVersion": "v1",
    40  		"kind": "Pod",
    41  		"metadata": {
    42  			"name": "pod",
    43  			"labels": {"app": "nginx"}
    44  		},
    45  		"spec": {
    46  			"containers": [{
    47  				"name":  "nginx",
    48  				"image": "nginx:latest"
    49  			}]
    50          }
    51  	}`), &appliedObj.Object); err != nil {
    52  		t.Fatalf("error decoding YAML: %v", err)
    53  	}
    54  
    55  	if err := f.Apply(appliedObj, "fieldmanager_test_apply", false); err != nil {
    56  		t.Fatalf("failed to update object: %v", err)
    57  	}
    58  
    59  	if e, a := 1, len(f.ManagedFields()); e != a {
    60  		t.Fatalf("exected %v entries in managedFields, but got %v: %#v", e, a, f.ManagedFields())
    61  	}
    62  
    63  	if e, a := "fieldmanager_test_apply", f.ManagedFields()[0].Manager; e != a {
    64  		t.Fatalf("exected manager name to be %v, but got %v: %#v", e, a, f.ManagedFields())
    65  	}
    66  }
    67  
    68  func TestUpdateBeforeFirstApply(t *testing.T) {
    69  	f := internaltesting.NewTestFieldManagerImpl(fakeTypeConverter, schema.FromAPIVersionAndKind("v1", "Pod"), "", func(m internal.Manager) internal.Manager {
    70  		return internal.NewSkipNonAppliedManager(m, &internaltesting.FakeObjectCreater{})
    71  	})
    72  
    73  	updatedObj := &unstructured.Unstructured{}
    74  	if err := json.Unmarshal([]byte(`{"kind": "Pod", "apiVersion": "v1", "metadata": {"labels": {"app": "my-nginx"}}}`), updatedObj); err != nil {
    75  		t.Fatalf("Failed to unmarshal object: %v", err)
    76  	}
    77  
    78  	if err := f.Update(updatedObj, "fieldmanager_test_update"); err != nil {
    79  		t.Fatalf("failed to update object: %v", err)
    80  	}
    81  
    82  	if m := f.ManagedFields(); len(m) != 0 {
    83  		t.Fatalf("managedFields were tracked on update only: %v", m)
    84  	}
    85  
    86  	appliedObj := &unstructured.Unstructured{Object: map[string]interface{}{}}
    87  	if err := yaml.Unmarshal([]byte(`{
    88  		"apiVersion": "v1",
    89  		"kind": "Pod",
    90  		"metadata": {
    91  			"name": "pod",
    92  			"labels": {"app": "nginx"}
    93  		},
    94  		"spec": {
    95  			"containers": [{
    96  				"name":  "nginx",
    97  				"image": "nginx:latest"
    98  			}]
    99          }
   100  	}`), &appliedObj.Object); err != nil {
   101  		t.Fatalf("error decoding YAML: %v", err)
   102  	}
   103  
   104  	err := f.Apply(appliedObj, "fieldmanager_test_apply", false)
   105  	apiStatus, _ := err.(apierrors.APIStatus)
   106  	if err == nil || !apierrors.IsConflict(err) || len(apiStatus.Status().Details.Causes) != 1 {
   107  		t.Fatalf("Expecting to get one conflict but got %v", err)
   108  	}
   109  
   110  	if e, a := ".metadata.labels.app", apiStatus.Status().Details.Causes[0].Field; e != a {
   111  		t.Fatalf("Expecting to conflict on field %q but conflicted on field %q: %v", e, a, err)
   112  	}
   113  
   114  	if e, a := "before-first-apply", apiStatus.Status().Details.Causes[0].Message; !strings.Contains(a, e) {
   115  		t.Fatalf("Expecting conflict message to contain %q but got %q: %v", e, a, err)
   116  	}
   117  
   118  	if err := f.Apply(appliedObj, "fieldmanager_test_apply", true); err != nil {
   119  		t.Fatalf("failed to update object: %v", err)
   120  	}
   121  
   122  	if e, a := 2, len(f.ManagedFields()); e != a {
   123  		t.Fatalf("exected %v entries in managedFields, but got %v: %#v", e, a, f.ManagedFields())
   124  	}
   125  
   126  	if e, a := "fieldmanager_test_apply", f.ManagedFields()[0].Manager; e != a {
   127  		t.Fatalf("exected first manager name to be %v, but got %v: %#v", e, a, f.ManagedFields())
   128  	}
   129  
   130  	if e, a := "before-first-apply", f.ManagedFields()[1].Manager; e != a {
   131  		t.Fatalf("exected second manager name to be %v, but got %v: %#v", e, a, f.ManagedFields())
   132  	}
   133  }
   134  

View as plain text