|
| 1 | +/* |
| 2 | +Copyright 2023 NVIDIA CORPORATION |
| 3 | +SPDX-License-Identifier: Apache-2.0 |
| 4 | +*/ |
| 5 | + |
| 6 | +package framework |
| 7 | + |
| 8 | +import ( |
| 9 | + "testing" |
| 10 | + |
| 11 | + "github.com/stretchr/testify/assert" |
| 12 | + |
| 13 | + "github.com/NVIDIA/KAI-scheduler/pkg/scheduler/api" |
| 14 | + "github.com/NVIDIA/KAI-scheduler/pkg/scheduler/api/pod_info" |
| 15 | +) |
| 16 | + |
| 17 | +func TestMutateBindRequestAnnotations(t *testing.T) { |
| 18 | + tests := []struct { |
| 19 | + name string |
| 20 | + mutateFns []api.BindRequestMutateFn |
| 21 | + expectedAnnotations map[string]string |
| 22 | + }{ |
| 23 | + { |
| 24 | + name: "no mutate functions", |
| 25 | + mutateFns: []api.BindRequestMutateFn{}, |
| 26 | + expectedAnnotations: map[string]string{}, |
| 27 | + }, |
| 28 | + { |
| 29 | + name: "single mutate function", |
| 30 | + mutateFns: []api.BindRequestMutateFn{ |
| 31 | + func(pod *pod_info.PodInfo, nodeName string) map[string]string { |
| 32 | + return map[string]string{"key1": "value1"} |
| 33 | + }, |
| 34 | + }, |
| 35 | + expectedAnnotations: map[string]string{"key1": "value1"}, |
| 36 | + }, |
| 37 | + { |
| 38 | + name: "multiple mutate functions with different keys", |
| 39 | + mutateFns: []api.BindRequestMutateFn{ |
| 40 | + func(pod *pod_info.PodInfo, nodeName string) map[string]string { |
| 41 | + return map[string]string{"key1": "value1"} |
| 42 | + }, |
| 43 | + func(pod *pod_info.PodInfo, nodeName string) map[string]string { |
| 44 | + return map[string]string{"key2": "value2"} |
| 45 | + }, |
| 46 | + }, |
| 47 | + expectedAnnotations: map[string]string{"key1": "value1", "key2": "value2"}, |
| 48 | + }, |
| 49 | + { |
| 50 | + name: "multiple mutate functions with overlapping keys - later should override", |
| 51 | + mutateFns: []api.BindRequestMutateFn{ |
| 52 | + func(pod *pod_info.PodInfo, nodeName string) map[string]string { |
| 53 | + return map[string]string{"key1": "value1", "common": "first"} |
| 54 | + }, |
| 55 | + func(pod *pod_info.PodInfo, nodeName string) map[string]string { |
| 56 | + return map[string]string{"key2": "value2", "common": "second"} |
| 57 | + }, |
| 58 | + }, |
| 59 | + expectedAnnotations: map[string]string{"key1": "value1", "key2": "value2", "common": "second"}, |
| 60 | + }, |
| 61 | + { |
| 62 | + name: "mutate function returns nil map", |
| 63 | + mutateFns: []api.BindRequestMutateFn{ |
| 64 | + func(pod *pod_info.PodInfo, nodeName string) map[string]string { |
| 65 | + return map[string]string{"key1": "value1"} |
| 66 | + }, |
| 67 | + func(pod *pod_info.PodInfo, nodeName string) map[string]string { |
| 68 | + return nil |
| 69 | + }, |
| 70 | + }, |
| 71 | + expectedAnnotations: map[string]string{"key1": "value1"}, |
| 72 | + }, |
| 73 | + } |
| 74 | + |
| 75 | + for _, tt := range tests { |
| 76 | + t.Run(tt.name, func(t *testing.T) { |
| 77 | + ssn := &Session{ |
| 78 | + BindRequestMutateFns: tt.mutateFns, |
| 79 | + } |
| 80 | + pod := &pod_info.PodInfo{ |
| 81 | + Name: "test-pod", |
| 82 | + } |
| 83 | + nodeName := "test-node" |
| 84 | + annotations := ssn.MutateBindRequestAnnotations(pod, nodeName) |
| 85 | + assert.Equal(t, tt.expectedAnnotations, annotations) |
| 86 | + }) |
| 87 | + } |
| 88 | +} |
0 commit comments