|
| 1 | +// nolint:dupl |
| 2 | +package convertpolicy |
| 3 | + |
| 4 | +import ( |
| 5 | + "testing" |
| 6 | + |
| 7 | + metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" |
| 8 | + |
| 9 | + kosmosv1alpha1 "github.com/kosmos.io/kosmos/pkg/apis/kosmos/v1alpha1" |
| 10 | +) |
| 11 | + |
| 12 | +func TestGetMatchPodConvertPolicy(t *testing.T) { |
| 13 | + t.Run("Test with empty policies", func(t *testing.T) { |
| 14 | + policies := kosmosv1alpha1.PodConvertPolicyList{} |
| 15 | + podLabels := map[string]string{ |
| 16 | + "app": "test", |
| 17 | + } |
| 18 | + nodeLabels := map[string]string{ |
| 19 | + "node": "test", |
| 20 | + } |
| 21 | + |
| 22 | + matched, err := GetMatchPodConvertPolicy(policies, podLabels, nodeLabels) |
| 23 | + if err != nil { |
| 24 | + t.Errorf("Expected no error, got %v", err) |
| 25 | + } |
| 26 | + if len(matched) != 0 { |
| 27 | + t.Errorf("Expected no matched policies, got %v", matched) |
| 28 | + } |
| 29 | + }) |
| 30 | + |
| 31 | + t.Run("Test with policies that do not match", func(t *testing.T) { |
| 32 | + policies := kosmosv1alpha1.PodConvertPolicyList{ |
| 33 | + Items: []kosmosv1alpha1.PodConvertPolicy{ |
| 34 | + { |
| 35 | + Spec: kosmosv1alpha1.PodConvertPolicySpec{ |
| 36 | + LabelSelector: metav1.LabelSelector{ |
| 37 | + MatchLabels: map[string]string{ |
| 38 | + "app": "not-test", |
| 39 | + }, |
| 40 | + }, |
| 41 | + }, |
| 42 | + }, |
| 43 | + }, |
| 44 | + } |
| 45 | + |
| 46 | + podLabels := map[string]string{ |
| 47 | + "app": "test", |
| 48 | + } |
| 49 | + nodeLabels := map[string]string{ |
| 50 | + "node": "test", |
| 51 | + } |
| 52 | + matched, err := GetMatchPodConvertPolicy(policies, podLabels, nodeLabels) |
| 53 | + |
| 54 | + if err != nil { |
| 55 | + t.Errorf("Expected no error, got %v", err) |
| 56 | + } |
| 57 | + if len(matched) != 0 { |
| 58 | + t.Errorf("Expected no matched policies, got %v", matched) |
| 59 | + } |
| 60 | + }) |
| 61 | + |
| 62 | + t.Run("Test with policies that match", func(t *testing.T) { |
| 63 | + policies := kosmosv1alpha1.PodConvertPolicyList{ |
| 64 | + Items: []kosmosv1alpha1.PodConvertPolicy{ |
| 65 | + { |
| 66 | + Spec: kosmosv1alpha1.PodConvertPolicySpec{ |
| 67 | + LabelSelector: metav1.LabelSelector{ |
| 68 | + MatchLabels: map[string]string{ |
| 69 | + "app": "test", |
| 70 | + }, |
| 71 | + }, |
| 72 | + }, |
| 73 | + }, |
| 74 | + }, |
| 75 | + } |
| 76 | + |
| 77 | + podLabels := map[string]string{ |
| 78 | + "app": "test", |
| 79 | + } |
| 80 | + nodeLabels := map[string]string{ |
| 81 | + "node": "test", |
| 82 | + } |
| 83 | + matched, err := GetMatchPodConvertPolicy(policies, podLabels, nodeLabels) |
| 84 | + |
| 85 | + if err != nil { |
| 86 | + t.Errorf("Expected no error, got %v", err) |
| 87 | + } |
| 88 | + if len(matched) != 1 { |
| 89 | + t.Errorf("Expected 1 matched policy, got %v", len(matched)) |
| 90 | + } |
| 91 | + }) |
| 92 | +} |
| 93 | + |
| 94 | +func TestGetMatchClusterPodConvertPolicy(t *testing.T) { |
| 95 | + t.Run("Test with empty policies", func(t *testing.T) { |
| 96 | + policies := kosmosv1alpha1.ClusterPodConvertPolicyList{} |
| 97 | + podLabels := map[string]string{ |
| 98 | + "app": "test", |
| 99 | + } |
| 100 | + nodeLabels := map[string]string{ |
| 101 | + "node": "test", |
| 102 | + } |
| 103 | + |
| 104 | + matched, err := GetMatchClusterPodConvertPolicy(policies, podLabels, nodeLabels) |
| 105 | + if err != nil { |
| 106 | + t.Errorf("Expected no error, got %v", err) |
| 107 | + } |
| 108 | + if len(matched) != 0 { |
| 109 | + t.Errorf("Expected no matched policies, got %v", matched) |
| 110 | + } |
| 111 | + }) |
| 112 | + |
| 113 | + t.Run("Test with policies that do not match", func(t *testing.T) { |
| 114 | + policies := kosmosv1alpha1.ClusterPodConvertPolicyList{ |
| 115 | + Items: []kosmosv1alpha1.ClusterPodConvertPolicy{ |
| 116 | + { |
| 117 | + Spec: kosmosv1alpha1.ClusterPodConvertPolicySpec{ |
| 118 | + LabelSelector: metav1.LabelSelector{ |
| 119 | + MatchLabels: map[string]string{"app": "not-test"}, |
| 120 | + }, |
| 121 | + }, |
| 122 | + }, |
| 123 | + }, |
| 124 | + } |
| 125 | + |
| 126 | + podLabels := map[string]string{ |
| 127 | + "app": "test", |
| 128 | + } |
| 129 | + nodeLabels := map[string]string{ |
| 130 | + "node": "test", |
| 131 | + } |
| 132 | + matched, err := GetMatchClusterPodConvertPolicy(policies, podLabels, nodeLabels) |
| 133 | + |
| 134 | + if err != nil { |
| 135 | + t.Errorf("Expected no error, got %v", err) |
| 136 | + } |
| 137 | + if len(matched) != 0 { |
| 138 | + t.Errorf("Expected no matched policies, got %v", matched) |
| 139 | + } |
| 140 | + }) |
| 141 | + |
| 142 | + t.Run("Test with policies that match", func(t *testing.T) { |
| 143 | + policies := kosmosv1alpha1.ClusterPodConvertPolicyList{ |
| 144 | + Items: []kosmosv1alpha1.ClusterPodConvertPolicy{ |
| 145 | + { |
| 146 | + Spec: kosmosv1alpha1.ClusterPodConvertPolicySpec{ |
| 147 | + LabelSelector: metav1.LabelSelector{ |
| 148 | + MatchLabels: map[string]string{"app": "test"}, |
| 149 | + }, |
| 150 | + }, |
| 151 | + }, |
| 152 | + }, |
| 153 | + } |
| 154 | + |
| 155 | + podLabels := map[string]string{ |
| 156 | + "app": "test", |
| 157 | + } |
| 158 | + nodeLabels := map[string]string{ |
| 159 | + "node": "test", |
| 160 | + } |
| 161 | + matched, err := GetMatchClusterPodConvertPolicy(policies, podLabels, nodeLabels) |
| 162 | + |
| 163 | + if err != nil { |
| 164 | + t.Errorf("Expected no error, got %v", err) |
| 165 | + } |
| 166 | + if len(matched) != 1 { |
| 167 | + t.Errorf("more than one matched policies, got %v", matched) |
| 168 | + } |
| 169 | + }) |
| 170 | +} |
0 commit comments