k8s.io/client-go@v0.31.1/kubernetes/fake/clientset_generated_test.go (about) 1 /* 2 Copyright 2022 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 fake 18 19 import ( 20 "context" 21 "github.com/stretchr/testify/assert" 22 "testing" 23 24 v1 "k8s.io/api/core/v1" 25 policy "k8s.io/api/policy/v1" 26 meta_v1 "k8s.io/apimachinery/pkg/apis/meta/v1" 27 v1ac "k8s.io/client-go/applyconfigurations/core/v1" 28 ) 29 30 func TestNewSimpleClientset(t *testing.T) { 31 client := NewSimpleClientset() 32 client.CoreV1().Pods("default").Create(context.Background(), &v1.Pod{ 33 ObjectMeta: meta_v1.ObjectMeta{ 34 Name: "pod-1", 35 Namespace: "default", 36 }, 37 }, meta_v1.CreateOptions{}) 38 client.CoreV1().Pods("default").Create(context.Background(), &v1.Pod{ 39 ObjectMeta: meta_v1.ObjectMeta{ 40 Name: "pod-2", 41 Namespace: "default", 42 }, 43 }, meta_v1.CreateOptions{}) 44 err := client.CoreV1().Pods("default").EvictV1(context.Background(), &policy.Eviction{ 45 ObjectMeta: meta_v1.ObjectMeta{ 46 Name: "pod-2", 47 }, 48 }) 49 50 if err != nil { 51 t.Errorf("TestNewSimpleClientset() res = %v", err.Error()) 52 } 53 54 pods, err := client.CoreV1().Pods("default").List(context.Background(), meta_v1.ListOptions{}) 55 // err: item[0]: can't assign or convert v1beta1.Eviction into v1.Pod 56 if err != nil { 57 t.Errorf("TestNewSimpleClientset() res = %v", err.Error()) 58 } else { 59 t.Logf("TestNewSimpleClientset() res = %v", pods) 60 } 61 } 62 63 func TestManagedFieldClientset(t *testing.T) { 64 client := NewClientset() 65 name := "pod-1" 66 namespace := "default" 67 cm, err := client.CoreV1().ConfigMaps("default").Create(context.Background(), 68 &v1.ConfigMap{ 69 ObjectMeta: meta_v1.ObjectMeta{Name: name, Namespace: namespace}, 70 Data: map[string]string{"k0": "v0"}, 71 }, meta_v1.CreateOptions{FieldManager: "test-manager-0"}) 72 if err != nil { 73 t.Errorf("Failed to create pod: %v", err) 74 } 75 assert.Equal(t, map[string]string{"k0": "v0"}, cm.Data) 76 77 // Apply with test-manager-1 78 // Expect data to be shared with initial create 79 cm, err = client.CoreV1().ConfigMaps("default").Apply(context.Background(), 80 v1ac.ConfigMap(name, namespace).WithData(map[string]string{"k1": "v1"}), 81 meta_v1.ApplyOptions{FieldManager: "test-manager-1"}) 82 if err != nil { 83 t.Errorf("Failed to create pod: %v", err) 84 } 85 assert.Equal(t, map[string]string{"k0": "v0", "k1": "v1"}, cm.Data) 86 87 // Apply conflicting with test-manager-2, expect apply to fail 88 _, err = client.CoreV1().ConfigMaps("default").Apply(context.Background(), 89 v1ac.ConfigMap(name, namespace).WithData(map[string]string{"k1": "xyz"}), 90 meta_v1.ApplyOptions{FieldManager: "test-manager-2"}) 91 if assert.Error(t, err) { 92 assert.Equal(t, "Apply failed with 1 conflict: conflict with \"test-manager-1\": .data.k1", err.Error()) 93 } 94 95 // Apply with test-manager-2 96 // Expect data to be shared with initial create and test-manager-1 97 cm, err = client.CoreV1().ConfigMaps("default").Apply(context.Background(), 98 v1ac.ConfigMap(name, namespace).WithData(map[string]string{"k2": "v2"}), 99 meta_v1.ApplyOptions{FieldManager: "test-manager-2"}) 100 if err != nil { 101 t.Errorf("Failed to create pod: %v", err) 102 } 103 assert.Equal(t, map[string]string{"k0": "v0", "k1": "v1", "k2": "v2"}, cm.Data) 104 105 // Apply with test-manager-1 106 // Expect owned data to be updated 107 cm, err = client.CoreV1().ConfigMaps("default").Apply(context.Background(), 108 v1ac.ConfigMap(name, namespace).WithData(map[string]string{"k1": "v101"}), 109 meta_v1.ApplyOptions{FieldManager: "test-manager-1"}) 110 if err != nil { 111 t.Errorf("Failed to create pod: %v", err) 112 } 113 assert.Equal(t, map[string]string{"k0": "v0", "k1": "v101", "k2": "v2"}, cm.Data) 114 115 // Force apply with test-manager-2 116 // Expect data owned by test-manager-1 to be updated, expect data already owned but not in apply configuration to be removed 117 cm, err = client.CoreV1().ConfigMaps("default").Apply(context.Background(), 118 v1ac.ConfigMap(name, namespace).WithData(map[string]string{"k1": "v202"}), 119 meta_v1.ApplyOptions{FieldManager: "test-manager-2", Force: true}) 120 if err != nil { 121 t.Errorf("Failed to create pod: %v", err) 122 } 123 assert.Equal(t, map[string]string{"k0": "v0", "k1": "v202"}, cm.Data) 124 125 // Update with test-manager-1 to perform a force update of the entire resource 126 cm, err = client.CoreV1().ConfigMaps("default").Update(context.Background(), 127 &v1.ConfigMap{ 128 TypeMeta: meta_v1.TypeMeta{ 129 APIVersion: "v1", 130 Kind: "ConfigMap", 131 }, 132 ObjectMeta: meta_v1.ObjectMeta{ 133 Name: name, 134 Namespace: namespace, 135 }, 136 Data: map[string]string{ 137 "k99": "v99", 138 }, 139 }, meta_v1.UpdateOptions{FieldManager: "test-manager-0"}) 140 if err != nil { 141 t.Errorf("Failed to update pod: %v", err) 142 } 143 assert.Equal(t, map[string]string{"k99": "v99"}, cm.Data) 144 }