|
| 1 | +/* |
| 2 | +Copyright 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 webhooks |
| 18 | + |
| 19 | +import ( |
| 20 | + "testing" |
| 21 | + |
| 22 | + "github.com/google/go-cmp/cmp" |
| 23 | + appsv1 "k8s.io/api/apps/v1" |
| 24 | + corev1 "k8s.io/api/core/v1" |
| 25 | + metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" |
| 26 | + |
| 27 | + slice "tpu-slice-controller/api/v1beta1" |
| 28 | + "tpu-slice-controller/internal/core" |
| 29 | + utiltesting "tpu-slice-controller/internal/util/testing" |
| 30 | +) |
| 31 | + |
| 32 | +func TestStatefulSetDefault(t *testing.T) { |
| 33 | + const ( |
| 34 | + baseName = "sts" |
| 35 | + baseNamespace = "default" |
| 36 | + ) |
| 37 | + |
| 38 | + testCases := map[string]struct { |
| 39 | + sts *appsv1.StatefulSet |
| 40 | + wantSts *appsv1.StatefulSet |
| 41 | + wantErr error |
| 42 | + }{ |
| 43 | + "not a relevant statefulset (missing annotation and node selector)": { |
| 44 | + sts: &appsv1.StatefulSet{ |
| 45 | + ObjectMeta: metav1.ObjectMeta{Name: baseName, Namespace: baseNamespace}, |
| 46 | + Spec: appsv1.StatefulSetSpec{}, |
| 47 | + }, |
| 48 | + wantSts: &appsv1.StatefulSet{ |
| 49 | + ObjectMeta: metav1.ObjectMeta{Name: baseName, Namespace: baseNamespace}, |
| 50 | + Spec: appsv1.StatefulSetSpec{}, |
| 51 | + }, |
| 52 | + }, |
| 53 | + "not a relevant statefulset (missing node selector)": { |
| 54 | + sts: &appsv1.StatefulSet{ |
| 55 | + ObjectMeta: metav1.ObjectMeta{ |
| 56 | + Name: baseName, |
| 57 | + Namespace: baseNamespace, |
| 58 | + }, |
| 59 | + Spec: appsv1.StatefulSetSpec{ |
| 60 | + Template: corev1.PodTemplateSpec{ |
| 61 | + ObjectMeta: metav1.ObjectMeta{ |
| 62 | + Annotations: map[string]string{core.TPUSliceTopologyAnnotation: "4x4x12"}, |
| 63 | + }, |
| 64 | + Spec: corev1.PodSpec{}, |
| 65 | + }, |
| 66 | + }, |
| 67 | + }, |
| 68 | + wantSts: &appsv1.StatefulSet{ |
| 69 | + ObjectMeta: metav1.ObjectMeta{ |
| 70 | + Name: baseName, |
| 71 | + Namespace: baseNamespace, |
| 72 | + }, |
| 73 | + Spec: appsv1.StatefulSetSpec{ |
| 74 | + Template: corev1.PodTemplateSpec{ |
| 75 | + ObjectMeta: metav1.ObjectMeta{ |
| 76 | + Annotations: map[string]string{core.TPUSliceTopologyAnnotation: "4x4x12"}, |
| 77 | + }, |
| 78 | + Spec: corev1.PodSpec{}, |
| 79 | + }, |
| 80 | + }, |
| 81 | + }, |
| 82 | + }, |
| 83 | + "relevant statefulset, missing tpu topology node selector": { |
| 84 | + sts: &appsv1.StatefulSet{ |
| 85 | + ObjectMeta: metav1.ObjectMeta{ |
| 86 | + Name: baseName, |
| 87 | + Namespace: baseNamespace, |
| 88 | + }, |
| 89 | + Spec: appsv1.StatefulSetSpec{ |
| 90 | + Template: corev1.PodTemplateSpec{ |
| 91 | + ObjectMeta: metav1.ObjectMeta{ |
| 92 | + Annotations: map[string]string{core.TPUSliceTopologyAnnotation: "4x4x12"}, |
| 93 | + }, |
| 94 | + Spec: corev1.PodSpec{ |
| 95 | + NodeSelector: map[string]string{ |
| 96 | + core.TPUAcceleratorLabel: string(slice.TypeTpu7x), |
| 97 | + }, |
| 98 | + }, |
| 99 | + }, |
| 100 | + }, |
| 101 | + }, |
| 102 | + wantSts: &appsv1.StatefulSet{ |
| 103 | + ObjectMeta: metav1.ObjectMeta{ |
| 104 | + Name: baseName, |
| 105 | + Namespace: baseNamespace, |
| 106 | + }, |
| 107 | + Spec: appsv1.StatefulSetSpec{ |
| 108 | + Template: corev1.PodTemplateSpec{ |
| 109 | + ObjectMeta: metav1.ObjectMeta{ |
| 110 | + Annotations: map[string]string{core.TPUSliceTopologyAnnotation: "4x4x12"}, |
| 111 | + }, |
| 112 | + Spec: corev1.PodSpec{ |
| 113 | + NodeSelector: map[string]string{ |
| 114 | + core.TPUAcceleratorLabel: string(slice.TypeTpu7x), |
| 115 | + core.TPUTopologyAnnotation: "4x4x12", |
| 116 | + }, |
| 117 | + }, |
| 118 | + }, |
| 119 | + }, |
| 120 | + }, |
| 121 | + }, |
| 122 | + "relevant statefulset, with tpu topology node selector already present": { |
| 123 | + sts: &appsv1.StatefulSet{ |
| 124 | + ObjectMeta: metav1.ObjectMeta{ |
| 125 | + Name: baseName, |
| 126 | + Namespace: baseNamespace, |
| 127 | + }, |
| 128 | + Spec: appsv1.StatefulSetSpec{ |
| 129 | + Template: corev1.PodTemplateSpec{ |
| 130 | + ObjectMeta: metav1.ObjectMeta{ |
| 131 | + Annotations: map[string]string{core.TPUSliceTopologyAnnotation: "4x4x12"}, |
| 132 | + }, |
| 133 | + Spec: corev1.PodSpec{ |
| 134 | + NodeSelector: map[string]string{ |
| 135 | + core.TPUAcceleratorLabel: string(slice.TypeTpu7x), |
| 136 | + core.TPUTopologyAnnotation: "4x4x12", |
| 137 | + }, |
| 138 | + }, |
| 139 | + }, |
| 140 | + }, |
| 141 | + }, |
| 142 | + wantSts: &appsv1.StatefulSet{ |
| 143 | + ObjectMeta: metav1.ObjectMeta{ |
| 144 | + Name: baseName, |
| 145 | + Namespace: baseNamespace, |
| 146 | + }, |
| 147 | + Spec: appsv1.StatefulSetSpec{ |
| 148 | + Template: corev1.PodTemplateSpec{ |
| 149 | + ObjectMeta: metav1.ObjectMeta{ |
| 150 | + Annotations: map[string]string{core.TPUSliceTopologyAnnotation: "4x4x12"}, |
| 151 | + }, |
| 152 | + Spec: corev1.PodSpec{ |
| 153 | + NodeSelector: map[string]string{ |
| 154 | + core.TPUAcceleratorLabel: string(slice.TypeTpu7x), |
| 155 | + core.TPUTopologyAnnotation: "4x4x12", |
| 156 | + }, |
| 157 | + }, |
| 158 | + }, |
| 159 | + }, |
| 160 | + }, |
| 161 | + }, |
| 162 | + } |
| 163 | + |
| 164 | + for name, tc := range testCases { |
| 165 | + t.Run(name, func(t *testing.T) { |
| 166 | + ctx := t.Context() |
| 167 | + webhook := &StatefulSetWebhook{} |
| 168 | + |
| 169 | + gotErr := webhook.Default(ctx, tc.sts) |
| 170 | + if diff := cmp.Diff(tc.wantErr, gotErr, utiltesting.EquateErrors); diff != "" { |
| 171 | + t.Errorf("Default() error mismatch (-want +got):\n%s", diff) |
| 172 | + } |
| 173 | + if tc.wantSts != nil { |
| 174 | + if diff := cmp.Diff(tc.wantSts, tc.sts); diff != "" { |
| 175 | + t.Errorf("Default() mismatch (-want,+got):\n%s", diff) |
| 176 | + } |
| 177 | + } |
| 178 | + }) |
| 179 | + } |
| 180 | +} |
0 commit comments