github.com/kubewharf/katalyst-core@v0.5.3/pkg/util/native/pods_test.go (about) 1 /* 2 Copyright 2022 The Katalyst 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 native 18 19 import ( 20 "fmt" 21 "testing" 22 23 "github.com/stretchr/testify/assert" 24 v1 "k8s.io/api/core/v1" 25 metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" 26 ) 27 28 func TestFilterPodAnnotations(t *testing.T) { 29 t.Parallel() 30 31 for _, tc := range []struct { 32 name string 33 pod *v1.Pod 34 filterKeys []string 35 expected map[string]string 36 }{ 37 { 38 name: "test case 1", 39 pod: &v1.Pod{ 40 ObjectMeta: metav1.ObjectMeta{ 41 Name: "test-pod", 42 Annotations: map[string]string{ 43 "key-1": "val-1", 44 "key-2": "val-2", 45 "key-3": "val-3", 46 }, 47 }, 48 }, 49 filterKeys: []string{ 50 "key-1", 51 "key-2", 52 "key-4", 53 }, 54 expected: map[string]string{ 55 "key-1": "val-1", 56 "key-2": "val-2", 57 }, 58 }, 59 } { 60 tc := tc 61 t.Run(tc.name, func(t *testing.T) { 62 t.Parallel() 63 64 t.Logf("case: %v", tc.name) 65 got := FilterPodAnnotations(tc.filterKeys, tc.pod) 66 assert.Equal(t, tc.expected, got) 67 }) 68 } 69 } 70 71 func TestGetContainerID(t *testing.T) { 72 t.Parallel() 73 74 type args struct { 75 pod *v1.Pod 76 name string 77 } 78 tests := []struct { 79 name string 80 args args 81 want string 82 wantErr assert.ErrorAssertionFunc 83 }{ 84 { 85 name: "test-1", 86 args: args{ 87 pod: &v1.Pod{ 88 Status: v1.PodStatus{ 89 ContainerStatuses: []v1.ContainerStatus{ 90 { 91 Name: "test-container-1", 92 State: v1.ContainerState{}, 93 ContainerID: "docker://test-container-id-1", 94 }, 95 }, 96 }, 97 }, 98 }, 99 want: "test-container-id-1", 100 wantErr: func(t assert.TestingT, err error, i ...interface{}) bool { 101 return false 102 }, 103 }, 104 } 105 for _, tt := range tests { 106 tt := tt 107 t.Run(tt.name, func(t *testing.T) { 108 t.Parallel() 109 110 got, err := GetContainerID(tt.args.pod, tt.args.name) 111 if !tt.wantErr(t, err, fmt.Sprintf("GetContainerID(%v, %v)", tt.args.pod, tt.args.name)) { 112 return 113 } 114 assert.Equalf(t, tt.want, got, "GetContainerID(%v, %v)", tt.args.pod, tt.args.name) 115 }) 116 } 117 } 118 119 func TestGetContainerEnvs(t *testing.T) { 120 t.Parallel() 121 122 type args struct { 123 pod *v1.Pod 124 containerName string 125 envs []string 126 } 127 tests := []struct { 128 name string 129 args args 130 want map[string]string 131 }{ 132 { 133 name: "test-1", 134 args: args{ 135 pod: &v1.Pod{ 136 Spec: v1.PodSpec{ 137 Containers: []v1.Container{ 138 { 139 Name: "test-container-1", 140 Env: []v1.EnvVar{ 141 { 142 Name: "test-env-1", 143 Value: "test-value-1", 144 }, 145 { 146 Name: "test-env-2", 147 Value: "test-value-2", 148 }, 149 { 150 Name: "test-env-3", 151 Value: "test-value-3", 152 }, 153 }, 154 }, 155 }, 156 }, 157 }, 158 containerName: "test-container-1", 159 envs: []string{"test-env-1", "test-env-2"}, 160 }, 161 want: map[string]string{ 162 "test-env-1": "test-value-1", 163 "test-env-2": "test-value-2", 164 }, 165 }, 166 } 167 for _, tt := range tests { 168 tt := tt 169 t.Run(tt.name, func(t *testing.T) { 170 t.Parallel() 171 172 assert.Equalf(t, tt.want, GetContainerEnvs(tt.args.pod, tt.args.containerName, tt.args.envs...), "GetContainerEnvs(%v, %v, %v)", tt.args.pod, tt.args.containerName, tt.args.envs) 173 }) 174 } 175 } 176 177 func TestGetPodHostIPs(t *testing.T) { 178 t.Parallel() 179 180 type args struct { 181 pod *v1.Pod 182 } 183 tests := []struct { 184 name string 185 args args 186 want []string 187 }{ 188 { 189 name: "test-1", 190 args: args{ 191 pod: &v1.Pod{ 192 ObjectMeta: metav1.ObjectMeta{ 193 Name: "test-pod-2", 194 }, 195 }, 196 }, 197 want: []string{}, 198 }, 199 { 200 name: "test-2", 201 args: args{ 202 pod: &v1.Pod{ 203 ObjectMeta: metav1.ObjectMeta{ 204 Name: "test-pod-2", 205 }, 206 Status: v1.PodStatus{ 207 HostIP: "xx:xx:xx:xx", 208 }, 209 }, 210 }, 211 want: []string{"xx:xx:xx:xx"}, 212 }, 213 } 214 for _, tt := range tests { 215 tt := tt 216 t.Run(tt.name, func(t *testing.T) { 217 t.Parallel() 218 219 got, _ := GetPodHostIPs(tt.args.pod) 220 assert.Equalf(t, tt.want, got, "GetPodHostIP(%v)", tt.args.pod) 221 }) 222 } 223 } 224 225 func TestParseHostPortForPod(t *testing.T) { 226 t.Parallel() 227 228 type args struct { 229 pod *v1.Pod 230 } 231 tests := []struct { 232 name string 233 args args 234 want int32 235 }{ 236 { 237 name: "test-1", 238 args: args{ 239 pod: &v1.Pod{ 240 ObjectMeta: metav1.ObjectMeta{ 241 Name: "test-pod-2", 242 }, 243 Spec: v1.PodSpec{ 244 Containers: []v1.Container{ 245 { 246 Ports: []v1.ContainerPort{ 247 { 248 Name: "test", 249 HostPort: 11, 250 }, 251 }, 252 }, 253 }, 254 }, 255 }, 256 }, 257 want: 11, 258 }, 259 { 260 name: "test-1", 261 args: args{ 262 pod: &v1.Pod{ 263 ObjectMeta: metav1.ObjectMeta{ 264 Name: "test-pod-2", 265 }, 266 }, 267 }, 268 want: 0, 269 }, 270 } 271 for _, tt := range tests { 272 tt := tt 273 t.Run(tt.name, func(t *testing.T) { 274 t.Parallel() 275 276 got, _ := ParseHostPortForPod(tt.args.pod, "test") 277 assert.Equalf(t, tt.want, got, "GetPodHostIP(%v)", tt.args.pod) 278 }) 279 } 280 } 281 282 func TestPodIsPending(t *testing.T) { 283 t.Parallel() 284 285 type args struct { 286 pod *v1.Pod 287 } 288 tests := []struct { 289 name string 290 args args 291 want bool 292 }{ 293 { 294 name: "pod is pending", 295 args: args{ 296 pod: &v1.Pod{ 297 Status: v1.PodStatus{ 298 Phase: v1.PodPending, 299 }, 300 }, 301 }, 302 want: true, 303 }, 304 { 305 name: "pod isn't pending", 306 args: args{ 307 pod: &v1.Pod{ 308 Status: v1.PodStatus{ 309 Phase: v1.PodRunning, 310 }, 311 }, 312 }, 313 want: false, 314 }, 315 { 316 name: "nil pod", 317 args: args{}, 318 want: false, 319 }, 320 } 321 for _, tt := range tests { 322 tt := tt 323 t.Run(tt.name, func(t *testing.T) { 324 t.Parallel() 325 326 if got := PodIsPending(tt.args.pod); got != tt.want { 327 t.Errorf("PodIsPending() = %v, want %v", got, tt.want) 328 } 329 }) 330 } 331 }