|
| 1 | +// SPDX-FileCopyrightText: SAP SE or an SAP affiliate company and IronCore contributors |
| 2 | +// SPDX-License-Identifier: Apache-2.0 |
| 3 | + |
| 4 | +package srlinux |
| 5 | + |
| 6 | +import ( |
| 7 | + "context" |
| 8 | + "encoding/json" |
| 9 | + "strings" |
| 10 | + "testing" |
| 11 | + |
| 12 | + corev1alpha1 "github.com/ironcore-dev/network-operator/api/core/v1alpha1" |
| 13 | + "github.com/ironcore-dev/network-operator/internal/provider" |
| 14 | + "github.com/ironcore-dev/network-operator/internal/transport/gnmiext" |
| 15 | +) |
| 16 | + |
| 17 | +func newProviderWithClient(client gnmiext.Client) *Provider { |
| 18 | + return &Provider{client: client} |
| 19 | +} |
| 20 | + |
| 21 | +// captureRelays returns a mock whose Do records every DHCPRelay element queued. |
| 22 | +func captureRelays(relays *[]*DHCPRelay) *gnmiext.ClientMock { |
| 23 | + return &gnmiext.ClientMock{ |
| 24 | + DoFunc: func(_ context.Context, b *gnmiext.SetBuilder) error { |
| 25 | + b.Split(func(el gnmiext.DataElement) bool { |
| 26 | + if r, ok := el.(*DHCPRelay); ok { |
| 27 | + *relays = append(*relays, r) |
| 28 | + } |
| 29 | + return false |
| 30 | + }) |
| 31 | + return nil |
| 32 | + }, |
| 33 | + } |
| 34 | +} |
| 35 | + |
| 36 | +func TestEnsureDHCPRelayNetworkInstance(t *testing.T) { |
| 37 | + tests := []struct { |
| 38 | + name string |
| 39 | + vrf *corev1alpha1.VRF |
| 40 | + want string |
| 41 | + }{ |
| 42 | + { |
| 43 | + name: "no vrf leaves network-instance empty", |
| 44 | + vrf: nil, |
| 45 | + want: "", |
| 46 | + }, |
| 47 | + { |
| 48 | + name: "vrf sets network-instance to the device vrf name", |
| 49 | + vrf: &corev1alpha1.VRF{Spec: corev1alpha1.VRFSpec{Name: "shared-services"}}, |
| 50 | + want: "shared-services", |
| 51 | + }, |
| 52 | + } |
| 53 | + for _, test := range tests { |
| 54 | + t.Run(test.name, func(t *testing.T) { |
| 55 | + var relays []*DHCPRelay |
| 56 | + p := newProviderWithClient(captureRelays(&relays)) |
| 57 | + |
| 58 | + err := p.EnsureDHCPRelay(t.Context(), &provider.DHCPRelayRequest{ |
| 59 | + DHCPRelay: &corev1alpha1.DHCPRelay{ |
| 60 | + Spec: corev1alpha1.DHCPRelaySpec{Servers: []string{"192.0.2.1"}}, |
| 61 | + }, |
| 62 | + Interface: &corev1alpha1.Interface{ |
| 63 | + Spec: corev1alpha1.InterfaceSpec{Name: "ethernet-1/1"}, |
| 64 | + }, |
| 65 | + VRF: test.vrf, |
| 66 | + }) |
| 67 | + if err != nil { |
| 68 | + t.Fatalf("EnsureDHCPRelay() error = %v", err) |
| 69 | + } |
| 70 | + if len(relays) != 1 { |
| 71 | + t.Fatalf("queued %d relays, want 1", len(relays)) |
| 72 | + } |
| 73 | + if got := relays[0].NetworkInstance; got != test.want { |
| 74 | + t.Errorf("NetworkInstance = %q, want %q", got, test.want) |
| 75 | + } |
| 76 | + }) |
| 77 | + } |
| 78 | +} |
| 79 | + |
| 80 | +// TestDHCPRelayJSONNetworkInstance guards the omitempty contract: an unset |
| 81 | +// network-instance must be absent from the payload so periodic reconciles do |
| 82 | +// not diff against the device default and trigger a spurious Set. |
| 83 | +func TestDHCPRelayJSONNetworkInstance(t *testing.T) { |
| 84 | + tests := []struct { |
| 85 | + name string |
| 86 | + relay *DHCPRelay |
| 87 | + wantKey bool |
| 88 | + }{ |
| 89 | + { |
| 90 | + name: "empty network-instance is omitted", |
| 91 | + relay: &DHCPRelay{AdminState: dhcpRelayAdminStateEnable, Server: []string{"192.0.2.1"}}, |
| 92 | + wantKey: false, |
| 93 | + }, |
| 94 | + { |
| 95 | + name: "set network-instance is present", |
| 96 | + relay: &DHCPRelay{AdminState: dhcpRelayAdminStateEnable, Server: []string{"192.0.2.1"}, NetworkInstance: "shared-services"}, |
| 97 | + wantKey: true, |
| 98 | + }, |
| 99 | + } |
| 100 | + for _, test := range tests { |
| 101 | + t.Run(test.name, func(t *testing.T) { |
| 102 | + got, err := json.Marshal(test.relay) |
| 103 | + if err != nil { |
| 104 | + t.Fatalf("json.Marshal() error = %v", err) |
| 105 | + } |
| 106 | + if hasKey := strings.Contains(string(got), `"network-instance"`); hasKey != test.wantKey { |
| 107 | + t.Errorf("json.Marshal() = %s, network-instance present = %v, want %v", got, hasKey, test.wantKey) |
| 108 | + } |
| 109 | + }) |
| 110 | + } |
| 111 | +} |
| 112 | + |
| 113 | +func TestDHCPRelayXPath(t *testing.T) { |
| 114 | + tests := []struct { |
| 115 | + name string |
| 116 | + relay *DHCPRelay |
| 117 | + want string |
| 118 | + }{ |
| 119 | + { |
| 120 | + name: "base subinterface", |
| 121 | + relay: &DHCPRelay{Interface: "ethernet-1/1"}, |
| 122 | + want: "srl_nokia-interfaces:interface[name=ethernet-1/1]/subinterface[index=0]/ipv4/srl_nokia-interfaces-ip-dhcp-relay:dhcp-relay", |
| 123 | + }, |
| 124 | + { |
| 125 | + name: "explicit subinterface index", |
| 126 | + relay: &DHCPRelay{Interface: "ethernet-1/1", SubinterfaceIndex: 100}, |
| 127 | + want: "srl_nokia-interfaces:interface[name=ethernet-1/1]/subinterface[index=100]/ipv4/srl_nokia-interfaces-ip-dhcp-relay:dhcp-relay", |
| 128 | + }, |
| 129 | + } |
| 130 | + for _, test := range tests { |
| 131 | + t.Run(test.name, func(t *testing.T) { |
| 132 | + if got := test.relay.XPath(); got != test.want { |
| 133 | + t.Errorf("XPath() = %q, want %q", got, test.want) |
| 134 | + } |
| 135 | + }) |
| 136 | + } |
| 137 | +} |
| 138 | + |
| 139 | +func TestSplitSubinterface(t *testing.T) { |
| 140 | + tests := []struct { |
| 141 | + name string |
| 142 | + in string |
| 143 | + wantIface string |
| 144 | + wantIndex int |
| 145 | + wantErr bool |
| 146 | + }{ |
| 147 | + {name: "no suffix defaults to zero", in: "ethernet-1/1", wantIface: "ethernet-1/1", wantIndex: 0}, |
| 148 | + {name: "explicit subinterface", in: "ethernet-1/1.100", wantIface: "ethernet-1/1", wantIndex: 100}, |
| 149 | + {name: "loopback", in: "lo0", wantIface: "lo0", wantIndex: 0}, |
| 150 | + {name: "non-numeric suffix errors", in: "ethernet-1/1.abc", wantErr: true}, |
| 151 | + } |
| 152 | + for _, test := range tests { |
| 153 | + t.Run(test.name, func(t *testing.T) { |
| 154 | + iface, index, err := splitSubinterface(test.in) |
| 155 | + if test.wantErr { |
| 156 | + if err == nil { |
| 157 | + t.Fatal("expected error, got nil") |
| 158 | + } |
| 159 | + return |
| 160 | + } |
| 161 | + if err != nil { |
| 162 | + t.Fatalf("splitSubinterface(%q) error = %v", test.in, err) |
| 163 | + } |
| 164 | + if iface != test.wantIface || index != test.wantIndex { |
| 165 | + t.Errorf("splitSubinterface(%q) = (%q, %d), want (%q, %d)", test.in, iface, index, test.wantIface, test.wantIndex) |
| 166 | + } |
| 167 | + }) |
| 168 | + } |
| 169 | +} |
| 170 | + |
| 171 | +func TestEnsureDHCPRelaySubinterface(t *testing.T) { |
| 172 | + var relays []*DHCPRelay |
| 173 | + p := newProviderWithClient(captureRelays(&relays)) |
| 174 | + |
| 175 | + err := p.EnsureDHCPRelay(t.Context(), &provider.DHCPRelayRequest{ |
| 176 | + DHCPRelay: &corev1alpha1.DHCPRelay{ |
| 177 | + Spec: corev1alpha1.DHCPRelaySpec{Servers: []string{"192.0.2.1"}}, |
| 178 | + }, |
| 179 | + Interface: &corev1alpha1.Interface{ |
| 180 | + Spec: corev1alpha1.InterfaceSpec{Name: "ethernet-1/1.100"}, |
| 181 | + }, |
| 182 | + }) |
| 183 | + if err != nil { |
| 184 | + t.Fatalf("EnsureDHCPRelay() error = %v", err) |
| 185 | + } |
| 186 | + if len(relays) != 1 { |
| 187 | + t.Fatalf("queued %d relays, want 1", len(relays)) |
| 188 | + } |
| 189 | + if relays[0].Interface != "ethernet-1/1" || relays[0].SubinterfaceIndex != 100 { |
| 190 | + t.Errorf("relay = (%q, %d), want (ethernet-1/1, 100)", relays[0].Interface, relays[0].SubinterfaceIndex) |
| 191 | + } |
| 192 | +} |
| 193 | + |
| 194 | +func TestDeleteDHCPRelay(t *testing.T) { |
| 195 | + t.Run("normal path deletes the interface relay", func(t *testing.T) { |
| 196 | + var deleted []*DHCPRelay |
| 197 | + client := &gnmiext.ClientMock{ |
| 198 | + DeleteFunc: func(_ context.Context, els ...gnmiext.DataElement) error { |
| 199 | + for _, el := range els { |
| 200 | + if r, ok := el.(*DHCPRelay); ok { |
| 201 | + deleted = append(deleted, r) |
| 202 | + } |
| 203 | + } |
| 204 | + return nil |
| 205 | + }, |
| 206 | + } |
| 207 | + p := newProviderWithClient(client) |
| 208 | + |
| 209 | + err := p.DeleteDHCPRelay(t.Context(), &provider.DeleteDHCPRelayRequest{ |
| 210 | + DHCPRelay: &corev1alpha1.DHCPRelay{}, |
| 211 | + Interface: &corev1alpha1.Interface{Spec: corev1alpha1.InterfaceSpec{Name: "ethernet-1/1.100"}}, |
| 212 | + }) |
| 213 | + if err != nil { |
| 214 | + t.Fatalf("DeleteDHCPRelay() error = %v", err) |
| 215 | + } |
| 216 | + if len(deleted) != 1 || deleted[0].Interface != "ethernet-1/1" || deleted[0].SubinterfaceIndex != 100 { |
| 217 | + t.Fatalf("deleted = %+v, want one relay for ethernet-1/1 subinterface 100", deleted) |
| 218 | + } |
| 219 | + }) |
| 220 | + |
| 221 | + t.Run("deprecated InterfaceRefs path is a no-op and does not panic", func(t *testing.T) { |
| 222 | + // Mock with no funcs: any device call panics, and req.Interface is nil. |
| 223 | + p := newProviderWithClient(&gnmiext.ClientMock{}) |
| 224 | + |
| 225 | + err := p.DeleteDHCPRelay(t.Context(), &provider.DeleteDHCPRelayRequest{ |
| 226 | + DHCPRelay: &corev1alpha1.DHCPRelay{ |
| 227 | + Spec: corev1alpha1.DHCPRelaySpec{ |
| 228 | + InterfaceRefs: []corev1alpha1.LocalObjectReference{{Name: "eth1"}}, |
| 229 | + }, |
| 230 | + }, |
| 231 | + }) |
| 232 | + if err != nil { |
| 233 | + t.Fatalf("DeleteDHCPRelay() error = %v", err) |
| 234 | + } |
| 235 | + }) |
| 236 | +} |
0 commit comments