|
| 1 | +/* |
| 2 | +Copyright 2018 The Volcano 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 mutate |
| 18 | + |
| 19 | +import ( |
| 20 | + "encoding/json" |
| 21 | + "fmt" |
| 22 | + "reflect" |
| 23 | + "testing" |
| 24 | + |
| 25 | + schedulingv1alpha2 "volcano.sh/volcano/pkg/apis/scheduling/v1alpha2" |
| 26 | + "volcano.sh/volcano/pkg/webhooks/util" |
| 27 | + |
| 28 | + "k8s.io/api/admission/v1beta1" |
| 29 | + metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" |
| 30 | + "k8s.io/apimachinery/pkg/runtime" |
| 31 | +) |
| 32 | + |
| 33 | +func TestMutateQueues(t *testing.T) { |
| 34 | + trueValue := true |
| 35 | + stateNotSetReclaimableNotSet := schedulingv1alpha2.Queue{ |
| 36 | + ObjectMeta: metav1.ObjectMeta{ |
| 37 | + Name: "normal-case-refresh-default-state", |
| 38 | + }, |
| 39 | + Spec: schedulingv1alpha2.QueueSpec{ |
| 40 | + Weight: 1, |
| 41 | + }, |
| 42 | + } |
| 43 | + |
| 44 | + stateNotSetJSON, err := json.Marshal(stateNotSetReclaimableNotSet) |
| 45 | + if err != nil { |
| 46 | + t.Errorf("Marshal queue without state set failed for %v.", err) |
| 47 | + } |
| 48 | + |
| 49 | + openStateReclaimableSet := schedulingv1alpha2.Queue{ |
| 50 | + ObjectMeta: metav1.ObjectMeta{ |
| 51 | + Name: "normal-case-set-open", |
| 52 | + }, |
| 53 | + Spec: schedulingv1alpha2.QueueSpec{ |
| 54 | + Weight: 1, |
| 55 | + State: schedulingv1alpha2.QueueStateOpen, |
| 56 | + Reclaimable: &trueValue, |
| 57 | + }, |
| 58 | + } |
| 59 | + |
| 60 | + openStateJSON, err := json.Marshal(openStateReclaimableSet) |
| 61 | + if err != nil { |
| 62 | + t.Errorf("Marshal queue with open state failed for %v.", err) |
| 63 | + } |
| 64 | + |
| 65 | + pt := v1beta1.PatchTypeJSONPatch |
| 66 | + |
| 67 | + var refreshPatch []patchOperation |
| 68 | + refreshPatch = append(refreshPatch, patchOperation{ |
| 69 | + Op: "add", |
| 70 | + Path: "/spec/state", |
| 71 | + Value: schedulingv1alpha2.QueueStateOpen, |
| 72 | + }, patchOperation{ |
| 73 | + Op: "add", |
| 74 | + Path: "/spec/reclaimable", |
| 75 | + Value: &trueValue, |
| 76 | + }) |
| 77 | + |
| 78 | + refreshPatchJSON, err := json.Marshal(refreshPatch) |
| 79 | + if err != nil { |
| 80 | + t.Errorf("Marshal queue patch failed for %v.", err) |
| 81 | + } |
| 82 | + |
| 83 | + var openStatePatch []patchOperation |
| 84 | + openStatePatchJSON, err := json.Marshal(openStatePatch) |
| 85 | + if err != nil { |
| 86 | + t.Errorf("Marshal null patch failed for %v.", err) |
| 87 | + } |
| 88 | + |
| 89 | + testCases := []struct { |
| 90 | + Name string |
| 91 | + AR v1beta1.AdmissionReview |
| 92 | + reviewResponse *v1beta1.AdmissionResponse |
| 93 | + }{ |
| 94 | + { |
| 95 | + Name: "Normal Case Refresh Default Open State and Reclaimable For Queue", |
| 96 | + AR: v1beta1.AdmissionReview{ |
| 97 | + TypeMeta: metav1.TypeMeta{ |
| 98 | + Kind: "AdmissionReview", |
| 99 | + APIVersion: "admission.k8s.io/v1beta1", |
| 100 | + }, |
| 101 | + Request: &v1beta1.AdmissionRequest{ |
| 102 | + Kind: metav1.GroupVersionKind{ |
| 103 | + Group: "scheduling.sigs.dev", |
| 104 | + Version: "v1alpha2", |
| 105 | + Kind: "Queue", |
| 106 | + }, |
| 107 | + Resource: metav1.GroupVersionResource{ |
| 108 | + Group: "scheduling.sigs.dev", |
| 109 | + Version: "v1alpha2", |
| 110 | + Resource: "queues", |
| 111 | + }, |
| 112 | + Name: "normal-case-refresh-default-state", |
| 113 | + Operation: "CREATE", |
| 114 | + Object: runtime.RawExtension{ |
| 115 | + Raw: stateNotSetJSON, |
| 116 | + }, |
| 117 | + }, |
| 118 | + }, |
| 119 | + reviewResponse: &v1beta1.AdmissionResponse{ |
| 120 | + Allowed: true, |
| 121 | + PatchType: &pt, |
| 122 | + Patch: refreshPatchJSON, |
| 123 | + }, |
| 124 | + }, |
| 125 | + { |
| 126 | + Name: "Normal Case Without Queue State or Reclaimable Patch", |
| 127 | + AR: v1beta1.AdmissionReview{ |
| 128 | + TypeMeta: metav1.TypeMeta{ |
| 129 | + Kind: "AdmissionReview", |
| 130 | + APIVersion: "admission.k8s.io/v1beta1", |
| 131 | + }, |
| 132 | + Request: &v1beta1.AdmissionRequest{ |
| 133 | + Kind: metav1.GroupVersionKind{ |
| 134 | + Group: "scheduling.sigs.dev", |
| 135 | + Version: "v1alpha2", |
| 136 | + Kind: "Queue", |
| 137 | + }, |
| 138 | + Resource: metav1.GroupVersionResource{ |
| 139 | + Group: "scheduling.sigs.dev", |
| 140 | + Version: "v1alpha2", |
| 141 | + Resource: "queues", |
| 142 | + }, |
| 143 | + Name: "normal-case-set-open", |
| 144 | + Operation: "CREATE", |
| 145 | + Object: runtime.RawExtension{ |
| 146 | + Raw: openStateJSON, |
| 147 | + }, |
| 148 | + }, |
| 149 | + }, |
| 150 | + reviewResponse: &v1beta1.AdmissionResponse{ |
| 151 | + Allowed: true, |
| 152 | + PatchType: &pt, |
| 153 | + Patch: openStatePatchJSON, |
| 154 | + }, |
| 155 | + }, |
| 156 | + { |
| 157 | + Name: "Invalid Action", |
| 158 | + AR: v1beta1.AdmissionReview{ |
| 159 | + TypeMeta: metav1.TypeMeta{ |
| 160 | + Kind: "AdmissionReview", |
| 161 | + APIVersion: "admission.k8s.io/v1beta1", |
| 162 | + }, |
| 163 | + Request: &v1beta1.AdmissionRequest{ |
| 164 | + Kind: metav1.GroupVersionKind{ |
| 165 | + Group: "scheduling.sigs.dev", |
| 166 | + Version: "v1alpha2", |
| 167 | + Kind: "Queue", |
| 168 | + }, |
| 169 | + Resource: metav1.GroupVersionResource{ |
| 170 | + Group: "scheduling.sigs.dev", |
| 171 | + Version: "v1alpha2", |
| 172 | + Resource: "queues", |
| 173 | + }, |
| 174 | + Name: "normal-case-set-open", |
| 175 | + Operation: "Invalid", |
| 176 | + Object: runtime.RawExtension{ |
| 177 | + Raw: openStateJSON, |
| 178 | + }, |
| 179 | + }, |
| 180 | + }, |
| 181 | + reviewResponse: util.ToAdmissionResponse(fmt.Errorf("invalid operation `%s`, "+ |
| 182 | + "expect operation to be `CREATE`", "Invalid")), |
| 183 | + }, |
| 184 | + } |
| 185 | + |
| 186 | + for _, testCase := range testCases { |
| 187 | + t.Run(testCase.Name, func(t *testing.T) { |
| 188 | + reviewResponse := MutateQueues(testCase.AR) |
| 189 | + if !reflect.DeepEqual(reviewResponse, testCase.reviewResponse) { |
| 190 | + t.Errorf("Test case %s failed, expect %v, got %v", testCase.Name, |
| 191 | + reviewResponse, testCase.reviewResponse) |
| 192 | + } |
| 193 | + }) |
| 194 | + } |
| 195 | +} |
0 commit comments