|
| 1 | +// Copyright 2025 HAProxy Technologies LLC |
| 2 | +// |
| 3 | +// Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +// you may not use this file except in compliance with the License. |
| 5 | +// You may obtain a copy of the License at |
| 6 | +// |
| 7 | +// http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +// |
| 9 | +// Unless required by applicable law or agreed to in writing, software |
| 10 | +// distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +// See the License for the specific language governing permissions and |
| 13 | +// limitations under the License. |
| 14 | +package startup |
| 15 | + |
| 16 | +import ( |
| 17 | + "testing" |
| 18 | + |
| 19 | + "github.com/haproxytech/client-native/v6/models" |
| 20 | + md "github.com/haproxytech/haproxy-unified-gateway/k8s/gate/haproxy/metadata" |
| 21 | +) |
| 22 | + |
| 23 | +// hugMeta builds a metadata map matching the structure produced by the metadata |
| 24 | +// package after its internal JSON round-trip: |
| 25 | +// { "hug": { kind: { "ns/name": { "LinkID": linkID, "Generation": 1 } } } } |
| 26 | +func hugMeta(kind, objKey, linkID string) map[string]any { |
| 27 | + return map[string]any{ |
| 28 | + md.UnifiedGatewayMetaDataKey: map[string]any{ |
| 29 | + kind: map[string]any{ |
| 30 | + objKey: map[string]any{ |
| 31 | + "LinkID": linkID, |
| 32 | + "Generation": float64(1), |
| 33 | + }, |
| 34 | + }, |
| 35 | + }, |
| 36 | + } |
| 37 | +} |
| 38 | + |
| 39 | +func newFrontend(meta map[string]any) *models.Frontend { |
| 40 | + fe := &models.Frontend{} |
| 41 | + fe.Metadata = meta |
| 42 | + return fe |
| 43 | +} |
| 44 | + |
| 45 | +func newBackend(meta map[string]any) *models.Backend { |
| 46 | + be := &models.Backend{} |
| 47 | + be.Metadata = meta |
| 48 | + return be |
| 49 | +} |
| 50 | + |
| 51 | +func TestIsUnifiedGatewayManaged_Frontend(t *testing.T) { |
| 52 | + tests := []struct { |
| 53 | + name string |
| 54 | + frontend *models.Frontend |
| 55 | + linkID string |
| 56 | + want bool |
| 57 | + }{ |
| 58 | + { |
| 59 | + name: "matching linkID", |
| 60 | + frontend: newFrontend(hugMeta("Gateway", "namespace/name", "hug")), |
| 61 | + linkID: "hug", |
| 62 | + want: true, |
| 63 | + }, |
| 64 | + { |
| 65 | + name: "wrong linkID", |
| 66 | + frontend: newFrontend(hugMeta("Gateway", "namespace/name", "hug")), |
| 67 | + linkID: "other", |
| 68 | + want: false, |
| 69 | + }, |
| 70 | + { |
| 71 | + name: "no hug key", |
| 72 | + frontend: newFrontend(map[string]any{"unrelated": "value"}), |
| 73 | + linkID: "hug", |
| 74 | + want: false, |
| 75 | + }, |
| 76 | + { |
| 77 | + name: "nil metadata", |
| 78 | + frontend: newFrontend(nil), |
| 79 | + linkID: "hug", |
| 80 | + want: false, |
| 81 | + }, |
| 82 | + { |
| 83 | + name: "empty metadata", |
| 84 | + frontend: newFrontend(map[string]any{}), |
| 85 | + linkID: "hug", |
| 86 | + want: false, |
| 87 | + }, |
| 88 | + } |
| 89 | + for _, tt := range tests { |
| 90 | + t.Run(tt.name, func(t *testing.T) { |
| 91 | + got := isUnifiedGatewayManaged(tt.frontend, tt.linkID) |
| 92 | + if got != tt.want { |
| 93 | + t.Errorf("isUnifiedGatewayManaged() = %v, want %v", got, tt.want) |
| 94 | + } |
| 95 | + }) |
| 96 | + } |
| 97 | +} |
| 98 | + |
| 99 | +func TestIsUnifiedGatewayManaged_Backend(t *testing.T) { |
| 100 | + tests := []struct { |
| 101 | + name string |
| 102 | + backend *models.Backend |
| 103 | + linkID string |
| 104 | + want bool |
| 105 | + }{ |
| 106 | + { |
| 107 | + name: "matching linkID", |
| 108 | + backend: newBackend(hugMeta("HTTPRoute", "namespace/name", "hug")), |
| 109 | + linkID: "hug", |
| 110 | + want: true, |
| 111 | + }, |
| 112 | + { |
| 113 | + name: "wrong linkID", |
| 114 | + backend: newBackend(hugMeta("HTTPRoute", "namespace/name", "hug")), |
| 115 | + linkID: "other", |
| 116 | + want: false, |
| 117 | + }, |
| 118 | + { |
| 119 | + name: "nil metadata", |
| 120 | + backend: newBackend(nil), |
| 121 | + linkID: "hug", |
| 122 | + want: false, |
| 123 | + }, |
| 124 | + } |
| 125 | + for _, tt := range tests { |
| 126 | + t.Run(tt.name, func(t *testing.T) { |
| 127 | + got := isUnifiedGatewayManaged(tt.backend, tt.linkID) |
| 128 | + if got != tt.want { |
| 129 | + t.Errorf("isUnifiedGatewayManaged() = %v, want %v", got, tt.want) |
| 130 | + } |
| 131 | + }) |
| 132 | + } |
| 133 | +} |
| 134 | + |
| 135 | +func TestIsUnifiedGatewayManaged_MultipleObjects(t *testing.T) { |
| 136 | + // Two gateways under different object keys, both with the same linkID. |
| 137 | + meta := map[string]any{ |
| 138 | + md.UnifiedGatewayMetaDataKey: map[string]any{ |
| 139 | + "Gateway": map[string]any{ |
| 140 | + "ns/gw-a": map[string]any{"LinkID": "hug", "Generation": float64(1)}, |
| 141 | + "ns/gw-b": map[string]any{"LinkID": "hug", "Generation": float64(2)}, |
| 142 | + }, |
| 143 | + }, |
| 144 | + } |
| 145 | + fe := newFrontend(meta) |
| 146 | + |
| 147 | + if !isUnifiedGatewayManaged(fe, "hug") { |
| 148 | + t.Error("expected true for frontend with two gateways and matching linkID") |
| 149 | + } |
| 150 | + if isUnifiedGatewayManaged(fe, "other") { |
| 151 | + t.Error("expected false for frontend with two gateways and non-matching linkID") |
| 152 | + } |
| 153 | +} |
| 154 | + |
| 155 | +func TestIsUnifiedGatewayManaged_MixedLinkIDs(t *testing.T) { |
| 156 | + // Two objects under different kinds, each with a different linkID. |
| 157 | + meta := map[string]any{ |
| 158 | + md.UnifiedGatewayMetaDataKey: map[string]any{ |
| 159 | + "Gateway": map[string]any{ |
| 160 | + "ns/gw": map[string]any{"LinkID": "hug", "Generation": float64(1)}, |
| 161 | + }, |
| 162 | + "HTTPRoute": map[string]any{ |
| 163 | + "ns/route": map[string]any{"LinkID": "other", "Generation": float64(1)}, |
| 164 | + }, |
| 165 | + }, |
| 166 | + } |
| 167 | + fe := newFrontend(meta) |
| 168 | + |
| 169 | + if !isUnifiedGatewayManaged(fe, "hug") { |
| 170 | + t.Error("expected true: Gateway entry has linkID 'hug'") |
| 171 | + } |
| 172 | + if !isUnifiedGatewayManaged(fe, "other") { |
| 173 | + t.Error("expected true: HTTPRoute entry has linkID 'other'") |
| 174 | + } |
| 175 | + if isUnifiedGatewayManaged(fe, "unknown") { |
| 176 | + t.Error("expected false: no entry has linkID 'unknown'") |
| 177 | + } |
| 178 | +} |
0 commit comments