github.com/operator-framework/operator-lifecycle-manager@v0.30.0/pkg/lib/operatorclient/service_test.go (about) 1 package operatorclient 2 3 import ( 4 "testing" 5 6 corev1 "k8s.io/api/core/v1" 7 metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" 8 "k8s.io/apimachinery/pkg/runtime/schema" 9 "k8s.io/apimachinery/pkg/types" 10 "k8s.io/client-go/kubernetes/fake" 11 clienttesting "k8s.io/client-go/testing" 12 13 "github.com/stretchr/testify/require" 14 ) 15 16 func TestUpdateService(t *testing.T) { 17 gvr := schema.GroupVersionResource{Group: "", Version: "v1", Resource: "services"} 18 19 // In a test expectation, matches any single fake client action. 20 var wildcard clienttesting.Action = clienttesting.ActionImpl{Verb: "wildcard!"} 21 22 for _, tc := range []struct { 23 Name string 24 Old *corev1.Service 25 New *corev1.Service 26 Expected []clienttesting.Action 27 }{ 28 { 29 Name: "no changes", 30 Old: &corev1.Service{ 31 ObjectMeta: metav1.ObjectMeta{ 32 Name: "name", 33 Namespace: "namespace", 34 }, 35 }, 36 New: &corev1.Service{ 37 ObjectMeta: metav1.ObjectMeta{ 38 Name: "name", 39 Namespace: "namespace", 40 }, 41 }, 42 Expected: []clienttesting.Action{ 43 wildcard, 44 clienttesting.NewPatchAction(gvr, "namespace", "name", types.StrategicMergePatchType, []byte(`{}`)), 45 }, 46 }, 47 { 48 Name: "resourceversion not patched", 49 Old: &corev1.Service{ 50 ObjectMeta: metav1.ObjectMeta{ 51 Name: "name", 52 Namespace: "namespace", 53 ResourceVersion: "42", 54 }, 55 }, 56 New: &corev1.Service{ 57 ObjectMeta: metav1.ObjectMeta{ 58 Name: "name", 59 Namespace: "namespace", 60 }, 61 }, 62 Expected: []clienttesting.Action{ 63 wildcard, 64 clienttesting.NewPatchAction(gvr, "namespace", "name", types.StrategicMergePatchType, []byte(`{}`)), 65 }, 66 }, 67 { 68 Name: "clusterip not patched if omitted", 69 Old: &corev1.Service{ 70 ObjectMeta: metav1.ObjectMeta{ 71 Name: "name", 72 Namespace: "namespace", 73 }, 74 Spec: corev1.ServiceSpec{ 75 ClusterIP: "1.2.3.4", 76 }, 77 }, 78 New: &corev1.Service{ 79 ObjectMeta: metav1.ObjectMeta{ 80 Name: "name", 81 Namespace: "namespace", 82 }, 83 }, 84 Expected: []clienttesting.Action{ 85 wildcard, 86 clienttesting.NewPatchAction(gvr, "namespace", "name", types.StrategicMergePatchType, []byte(`{}`)), 87 }, 88 }, 89 { 90 Name: "clusterip not patched if unchanged", 91 Old: &corev1.Service{ 92 ObjectMeta: metav1.ObjectMeta{ 93 Name: "name", 94 Namespace: "namespace", 95 }, 96 Spec: corev1.ServiceSpec{ 97 ClusterIP: "1.2.3.4", 98 }, 99 }, 100 New: &corev1.Service{ 101 ObjectMeta: metav1.ObjectMeta{ 102 Name: "name", 103 Namespace: "namespace", 104 }, 105 Spec: corev1.ServiceSpec{ 106 ClusterIP: "1.2.3.4", 107 }, 108 }, 109 Expected: []clienttesting.Action{ 110 wildcard, 111 clienttesting.NewPatchAction(gvr, "namespace", "name", types.StrategicMergePatchType, []byte(`{}`)), 112 }, 113 }, 114 { 115 Name: "clusterip patched if changed", // even though the patch will be rejected due to field immutability 116 Old: &corev1.Service{ 117 ObjectMeta: metav1.ObjectMeta{ 118 Name: "name", 119 Namespace: "namespace", 120 }, 121 Spec: corev1.ServiceSpec{ 122 ClusterIP: "1.2.3.4", 123 }, 124 }, 125 New: &corev1.Service{ 126 ObjectMeta: metav1.ObjectMeta{ 127 Name: "name", 128 Namespace: "namespace", 129 }, 130 Spec: corev1.ServiceSpec{ 131 ClusterIP: "4.3.2.1", 132 }, 133 }, 134 Expected: []clienttesting.Action{ 135 wildcard, 136 clienttesting.NewPatchAction(gvr, "namespace", "name", types.StrategicMergePatchType, []byte(`{"spec":{"clusterIP":"4.3.2.1"}}`)), 137 }, 138 }, 139 { 140 Name: "spec modified", 141 Old: &corev1.Service{ 142 ObjectMeta: metav1.ObjectMeta{ 143 Name: "name", 144 Namespace: "namespace", 145 }, 146 Spec: corev1.ServiceSpec{ 147 SessionAffinity: "None", 148 }, 149 }, 150 New: &corev1.Service{ 151 ObjectMeta: metav1.ObjectMeta{ 152 Name: "name", 153 Namespace: "namespace", 154 }, 155 Spec: corev1.ServiceSpec{ 156 SessionAffinity: "ClientIP", 157 }, 158 }, 159 Expected: []clienttesting.Action{ 160 wildcard, 161 clienttesting.NewPatchAction(gvr, "namespace", "name", types.StrategicMergePatchType, []byte(`{"spec":{"sessionAffinity":"ClientIP"}}`)), 162 }, 163 }, 164 } { 165 t.Run(tc.Name, func(t *testing.T) { 166 require := require.New(t) 167 168 kube := fake.NewSimpleClientset(tc.Old) 169 c := &Client{ 170 Interface: kube, 171 } 172 173 _, err := c.UpdateService(tc.New) 174 require.NoError(err) 175 176 actual := kube.Actions() 177 require.Len(actual, len(tc.Expected)) 178 179 for i, action := range kube.Actions() { 180 if tc.Expected[i] == wildcard { 181 continue 182 } 183 require.Equal(tc.Expected[i], action) 184 } 185 }) 186 } 187 }