|
| 1 | +package homer |
| 2 | + |
| 3 | +import ( |
| 4 | + "testing" |
| 5 | + |
| 6 | + networkingv1 "k8s.io/api/networking/v1" |
| 7 | + metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" |
| 8 | + gatewayv1 "sigs.k8s.io/gateway-api/apis/v1" |
| 9 | +) |
| 10 | + |
| 11 | +// TestIngressClusterAutoTag verifies that Ingresses from remote clusters get auto-tagged |
| 12 | +func TestIngressClusterAutoTag(t *testing.T) { |
| 13 | + tests := []struct { |
| 14 | + name string |
| 15 | + clusterAnnot string |
| 16 | + tagStyleLabel string |
| 17 | + expectTag bool |
| 18 | + expectedTagName string |
| 19 | + expectedTagStyle string |
| 20 | + }{ |
| 21 | + { |
| 22 | + name: "Remote cluster ottawa - default blue", |
| 23 | + clusterAnnot: "ottawa", |
| 24 | + expectTag: true, |
| 25 | + expectedTagName: "ottawa", |
| 26 | + expectedTagStyle: "is-info", // Default blue |
| 27 | + }, |
| 28 | + { |
| 29 | + name: "Remote cluster robbinsdale - default blue", |
| 30 | + clusterAnnot: "robbinsdale", |
| 31 | + expectTag: true, |
| 32 | + expectedTagName: "robbinsdale", |
| 33 | + expectedTagStyle: "is-info", // Default blue |
| 34 | + }, |
| 35 | + { |
| 36 | + name: "Remote cluster with custom red color", |
| 37 | + clusterAnnot: "ottawa", |
| 38 | + tagStyleLabel: "is-danger", |
| 39 | + expectTag: true, |
| 40 | + expectedTagName: "ottawa", |
| 41 | + expectedTagStyle: "is-danger", // Custom red |
| 42 | + }, |
| 43 | + { |
| 44 | + name: "Remote cluster with custom yellow color", |
| 45 | + clusterAnnot: "staging", |
| 46 | + tagStyleLabel: "is-warning", |
| 47 | + expectTag: true, |
| 48 | + expectedTagName: "staging", |
| 49 | + expectedTagStyle: "is-warning", // Custom yellow |
| 50 | + }, |
| 51 | + { |
| 52 | + name: "Local cluster", |
| 53 | + clusterAnnot: "local", |
| 54 | + expectTag: false, |
| 55 | + }, |
| 56 | + { |
| 57 | + name: "No cluster annotation", |
| 58 | + expectTag: false, |
| 59 | + }, |
| 60 | + } |
| 61 | + |
| 62 | + for _, tt := range tests { |
| 63 | + t.Run(tt.name, func(t *testing.T) { |
| 64 | + ingress := &networkingv1.Ingress{ |
| 65 | + ObjectMeta: metav1.ObjectMeta{ |
| 66 | + Name: "test-ingress", |
| 67 | + Namespace: "default", |
| 68 | + Annotations: map[string]string{}, |
| 69 | + Labels: map[string]string{}, |
| 70 | + }, |
| 71 | + Spec: networkingv1.IngressSpec{ |
| 72 | + Rules: []networkingv1.IngressRule{ |
| 73 | + {Host: "test.example.com"}, |
| 74 | + }, |
| 75 | + }, |
| 76 | + } |
| 77 | + |
| 78 | + if tt.clusterAnnot != "" { |
| 79 | + ingress.ObjectMeta.Annotations["homer.rajsingh.info/cluster"] = tt.clusterAnnot |
| 80 | + } |
| 81 | + |
| 82 | + if tt.tagStyleLabel != "" { |
| 83 | + ingress.ObjectMeta.Labels["cluster-tagstyle"] = tt.tagStyleLabel |
| 84 | + } |
| 85 | + |
| 86 | + config := &HomerConfig{ |
| 87 | + Title: "Test Dashboard", |
| 88 | + Header: true, |
| 89 | + } |
| 90 | + |
| 91 | + UpdateHomerConfigIngress(config, *ingress, nil) |
| 92 | + |
| 93 | + if len(config.Services) == 0 { |
| 94 | + t.Fatal("Expected at least one service") |
| 95 | + } |
| 96 | + |
| 97 | + service := config.Services[0] |
| 98 | + if len(service.Items) == 0 { |
| 99 | + t.Fatal("Expected at least one item") |
| 100 | + } |
| 101 | + |
| 102 | + item := service.Items[0] |
| 103 | + |
| 104 | + if tt.expectTag { |
| 105 | + tag, hasTag := item.Parameters["tag"] |
| 106 | + if !hasTag { |
| 107 | + t.Errorf("Expected tag to be set for cluster %s", tt.clusterAnnot) |
| 108 | + } |
| 109 | + if tag != tt.expectedTagName { |
| 110 | + t.Errorf("Expected tag %q, got %q", tt.expectedTagName, tag) |
| 111 | + } |
| 112 | + |
| 113 | + tagstyle, hasTagStyle := item.Parameters["tagstyle"] |
| 114 | + if !hasTagStyle { |
| 115 | + t.Error("Expected tagstyle to be set") |
| 116 | + } |
| 117 | + if tagstyle != tt.expectedTagStyle { |
| 118 | + t.Errorf("Expected tagstyle %q, got %q", tt.expectedTagStyle, tagstyle) |
| 119 | + } |
| 120 | + } else { |
| 121 | + if tag, hasTag := item.Parameters["tag"]; hasTag { |
| 122 | + t.Errorf("Expected no tag for %s, but got %q", tt.name, tag) |
| 123 | + } |
| 124 | + } |
| 125 | + }) |
| 126 | + } |
| 127 | +} |
| 128 | + |
| 129 | +// TestHTTPRouteClusterAutoTag verifies that HTTPRoutes from remote clusters get auto-tagged |
| 130 | +func TestHTTPRouteClusterAutoTag(t *testing.T) { |
| 131 | + tests := []struct { |
| 132 | + name string |
| 133 | + clusterAnnot string |
| 134 | + tagStyleLabel string |
| 135 | + expectTag bool |
| 136 | + expectedTagName string |
| 137 | + expectedTagStyle string |
| 138 | + }{ |
| 139 | + { |
| 140 | + name: "Remote cluster ottawa - default blue", |
| 141 | + clusterAnnot: "ottawa", |
| 142 | + expectTag: true, |
| 143 | + expectedTagName: "ottawa", |
| 144 | + expectedTagStyle: "is-info", // Default blue |
| 145 | + }, |
| 146 | + { |
| 147 | + name: "Remote cluster robbinsdale - default blue", |
| 148 | + clusterAnnot: "robbinsdale", |
| 149 | + expectTag: true, |
| 150 | + expectedTagName: "robbinsdale", |
| 151 | + expectedTagStyle: "is-info", // Default blue |
| 152 | + }, |
| 153 | + { |
| 154 | + name: "Remote cluster with custom red color", |
| 155 | + clusterAnnot: "production", |
| 156 | + tagStyleLabel: "is-danger", |
| 157 | + expectTag: true, |
| 158 | + expectedTagName: "production", |
| 159 | + expectedTagStyle: "is-danger", // Custom red |
| 160 | + }, |
| 161 | + { |
| 162 | + name: "Local cluster", |
| 163 | + clusterAnnot: "local", |
| 164 | + expectTag: false, |
| 165 | + }, |
| 166 | + { |
| 167 | + name: "No cluster annotation", |
| 168 | + expectTag: false, |
| 169 | + }, |
| 170 | + } |
| 171 | + |
| 172 | + for _, tt := range tests { |
| 173 | + t.Run(tt.name, func(t *testing.T) { |
| 174 | + hostname := gatewayv1.Hostname("test.example.com") |
| 175 | + httproute := &gatewayv1.HTTPRoute{ |
| 176 | + ObjectMeta: metav1.ObjectMeta{ |
| 177 | + Name: "test-httproute", |
| 178 | + Namespace: "default", |
| 179 | + Annotations: map[string]string{}, |
| 180 | + Labels: map[string]string{}, |
| 181 | + }, |
| 182 | + Spec: gatewayv1.HTTPRouteSpec{ |
| 183 | + Hostnames: []gatewayv1.Hostname{hostname}, |
| 184 | + }, |
| 185 | + } |
| 186 | + |
| 187 | + if tt.clusterAnnot != "" { |
| 188 | + httproute.ObjectMeta.Annotations["homer.rajsingh.info/cluster"] = tt.clusterAnnot |
| 189 | + } |
| 190 | + |
| 191 | + if tt.tagStyleLabel != "" { |
| 192 | + httproute.ObjectMeta.Labels["cluster-tagstyle"] = tt.tagStyleLabel |
| 193 | + } |
| 194 | + |
| 195 | + config := &HomerConfig{ |
| 196 | + Title: "Test Dashboard", |
| 197 | + Header: true, |
| 198 | + } |
| 199 | + |
| 200 | + UpdateHomerConfigHTTPRoute(config, httproute, nil) |
| 201 | + |
| 202 | + if len(config.Services) == 0 { |
| 203 | + t.Fatal("Expected at least one service") |
| 204 | + } |
| 205 | + |
| 206 | + service := config.Services[0] |
| 207 | + if len(service.Items) == 0 { |
| 208 | + t.Fatal("Expected at least one item") |
| 209 | + } |
| 210 | + |
| 211 | + item := service.Items[0] |
| 212 | + |
| 213 | + if tt.expectTag { |
| 214 | + tag, hasTag := item.Parameters["tag"] |
| 215 | + if !hasTag { |
| 216 | + t.Errorf("Expected tag to be set for cluster %s", tt.clusterAnnot) |
| 217 | + } |
| 218 | + if tag != tt.expectedTagName { |
| 219 | + t.Errorf("Expected tag %q, got %q", tt.expectedTagName, tag) |
| 220 | + } |
| 221 | + |
| 222 | + tagstyle, hasTagStyle := item.Parameters["tagstyle"] |
| 223 | + if !hasTagStyle { |
| 224 | + t.Error("Expected tagstyle to be set") |
| 225 | + } |
| 226 | + if tagstyle != tt.expectedTagStyle { |
| 227 | + t.Errorf("Expected tagstyle %q, got %q", tt.expectedTagStyle, tagstyle) |
| 228 | + } |
| 229 | + } else { |
| 230 | + if tag, hasTag := item.Parameters["tag"]; hasTag { |
| 231 | + t.Errorf("Expected no tag for %s, but got %q", tt.name, tag) |
| 232 | + } |
| 233 | + } |
| 234 | + }) |
| 235 | + } |
| 236 | +} |
| 237 | + |
| 238 | +// TestClusterTagNotOverriddenByAnnotations verifies that manual annotations take precedence |
| 239 | +func TestClusterTagNotOverriddenByAnnotations(t *testing.T) { |
| 240 | + ingress := &networkingv1.Ingress{ |
| 241 | + ObjectMeta: metav1.ObjectMeta{ |
| 242 | + Name: "test-ingress", |
| 243 | + Namespace: "default", |
| 244 | + Annotations: map[string]string{ |
| 245 | + "homer.rajsingh.info/cluster": "ottawa", |
| 246 | + "item.homer.rajsingh.info/tag": "production", |
| 247 | + "item.homer.rajsingh.info/tagstyle": "is-danger", |
| 248 | + }, |
| 249 | + }, |
| 250 | + Spec: networkingv1.IngressSpec{ |
| 251 | + Rules: []networkingv1.IngressRule{ |
| 252 | + {Host: "test.example.com"}, |
| 253 | + }, |
| 254 | + }, |
| 255 | + } |
| 256 | + |
| 257 | + config := &HomerConfig{ |
| 258 | + Title: "Test Dashboard", |
| 259 | + Header: true, |
| 260 | + } |
| 261 | + |
| 262 | + UpdateHomerConfigIngress(config, *ingress, nil) |
| 263 | + |
| 264 | + if len(config.Services) == 0 { |
| 265 | + t.Fatal("Expected at least one service") |
| 266 | + } |
| 267 | + |
| 268 | + service := config.Services[0] |
| 269 | + if len(service.Items) == 0 { |
| 270 | + t.Fatal("Expected at least one item") |
| 271 | + } |
| 272 | + |
| 273 | + item := service.Items[0] |
| 274 | + |
| 275 | + // Manual annotation should take precedence over auto-tag |
| 276 | + tag := item.Parameters["tag"] |
| 277 | + if tag != "production" { |
| 278 | + t.Errorf("Expected manual tag 'production', got %q", tag) |
| 279 | + } |
| 280 | + |
| 281 | + tagstyle := item.Parameters["tagstyle"] |
| 282 | + if tagstyle != "is-danger" { |
| 283 | + t.Errorf("Expected manual tagstyle 'is-danger', got %q", tagstyle) |
| 284 | + } |
| 285 | +} |
0 commit comments