Skip to content

Commit dc72a78

Browse files
committed
chore: unit-test stub for dataplane cfg
Signed-off-by: Emanuele Di Pascale <[email protected]>
1 parent 1b07941 commit dc72a78

File tree

1 file changed

+246
-0
lines changed

1 file changed

+246
-0
lines changed

pkg/agent/dataplane_test.go

Lines changed: 246 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,246 @@
1+
// Copyright 2025 Hedgehog
2+
// SPDX-License-Identifier: Apache-2.0
3+
4+
package agent
5+
6+
import (
7+
"testing"
8+
9+
"github.com/stretchr/testify/require"
10+
"go.githedgehog.com/gateway-proto/pkg/dataplane"
11+
"go.githedgehog.com/gateway/api/gateway/v1alpha1"
12+
gwintapi "go.githedgehog.com/gateway/api/gwint/v1alpha1"
13+
)
14+
15+
func agb(name string, f ...func(ag *gwintapi.GatewayAgent)) *gwintapi.GatewayAgent {
16+
agentBase := &gwintapi.GatewayAgent{
17+
Spec: gwintapi.GatewayAgentSpec{
18+
Gateway: v1alpha1.GatewaySpec{
19+
ProtocolIP: "192.0.2.1/32",
20+
VTEPIP: "192.0.2.2",
21+
VTEPMAC: "aa:bb:cc:dd:ee:ff",
22+
VTEPMTU: 1500,
23+
ASN: 65001,
24+
Interfaces: map[string]v1alpha1.GatewayInterface{
25+
"eth0": {IPs: []string{"10.0.0.1"}, MTU: 1500},
26+
},
27+
Neighbors: []v1alpha1.GatewayBGPNeighbor{
28+
{IP: "192.0.2.3", ASN: 65002, Source: "eth0"},
29+
},
30+
},
31+
VPCs: map[string]gwintapi.VPCInfoData{
32+
"vpc-01": {
33+
VPCInfoSpec: v1alpha1.VPCInfoSpec{
34+
VNI: 100,
35+
Subnets: map[string]*v1alpha1.VPCInfoSubnet{
36+
"subnet1": {CIDR: "10.0.1.0/24"},
37+
},
38+
},
39+
},
40+
"vpc-02": {
41+
VPCInfoSpec: v1alpha1.VPCInfoSpec{
42+
VNI: 200,
43+
Subnets: map[string]*v1alpha1.VPCInfoSubnet{
44+
"subnet1": {CIDR: "10.0.2.0/24"},
45+
},
46+
},
47+
},
48+
},
49+
Peerings: map[string]v1alpha1.PeeringSpec{},
50+
},
51+
}
52+
agentBase.Name = name
53+
agentBase.Namespace = "fab"
54+
55+
for _, fn := range f {
56+
fn(agentBase)
57+
}
58+
59+
return agentBase
60+
}
61+
62+
func dpb(name string, f ...func(dp *dataplane.GatewayConfig)) *dataplane.GatewayConfig {
63+
dpCfgBase := &dataplane.GatewayConfig{
64+
Generation: 0,
65+
Device: &dataplane.Device{
66+
Driver: dataplane.PacketDriver_KERNEL,
67+
Hostname: name,
68+
Loglevel: dataplane.LogLevel_DEBUG,
69+
},
70+
Underlay: &dataplane.Underlay{
71+
Vrfs: []*dataplane.VRF{
72+
{
73+
Name: "default",
74+
Interfaces: []*dataplane.Interface{
75+
{
76+
Name: "lo",
77+
Ipaddrs: []string{"192.0.2.2"},
78+
Type: dataplane.IfType_IF_TYPE_LOOPBACK,
79+
Role: dataplane.IfRole_IF_ROLE_FABRIC,
80+
},
81+
{
82+
Name: "vtep",
83+
Ipaddrs: []string{"192.0.2.2"},
84+
Type: dataplane.IfType_IF_TYPE_VTEP,
85+
Role: dataplane.IfRole_IF_ROLE_FABRIC,
86+
Macaddr: ptr("aa:bb:cc:dd:ee:ff"),
87+
Mtu: ptr(uint32(1500)),
88+
},
89+
{
90+
Name: "eth0",
91+
Ipaddrs: []string{"10.0.0.1"},
92+
Type: dataplane.IfType_IF_TYPE_ETHERNET,
93+
Role: dataplane.IfRole_IF_ROLE_FABRIC,
94+
Mtu: ptr(uint32(1500)),
95+
},
96+
},
97+
Router: &dataplane.RouterConfig{
98+
Asn: "65001",
99+
RouterId: "192.0.2.1",
100+
Neighbors: []*dataplane.BgpNeighbor{
101+
{
102+
Address: "192.0.2.3",
103+
RemoteAsn: "65002",
104+
AfActivate: []dataplane.BgpAF{
105+
dataplane.BgpAF_IPV4_UNICAST,
106+
dataplane.BgpAF_L2VPN_EVPN,
107+
},
108+
UpdateSource: &dataplane.BgpNeighborUpdateSource{
109+
Source: &dataplane.BgpNeighborUpdateSource_Interface{
110+
Interface: "eth0",
111+
},
112+
},
113+
},
114+
},
115+
Ipv4Unicast: &dataplane.BgpAddressFamilyIPv4{
116+
Networks: []string{"192.0.2.2"},
117+
RedistributeConnected: false,
118+
RedistributeStatic: false,
119+
},
120+
L2VpnEvpn: &dataplane.BgpAddressFamilyL2VpnEvpn{
121+
AdvertiseAllVni: true,
122+
},
123+
},
124+
},
125+
},
126+
},
127+
Overlay: &dataplane.Overlay{
128+
Vpcs: []*dataplane.VPC{
129+
{
130+
Name: "vpc-01",
131+
Id: "",
132+
Vni: 100,
133+
},
134+
{
135+
Name: "vpc-02",
136+
Id: "",
137+
Vni: 200,
138+
},
139+
},
140+
},
141+
}
142+
143+
for _, fn := range f {
144+
fn(dpCfgBase)
145+
}
146+
147+
return dpCfgBase
148+
}
149+
150+
func TestBuildDataplaneConfig(t *testing.T) {
151+
tests := []struct {
152+
name string
153+
agent *gwintapi.GatewayAgent
154+
outputCfg *dataplane.GatewayConfig
155+
wantErr bool
156+
}{
157+
{
158+
name: "single expose with CIDR",
159+
agent: agb("agent1", func(ag *gwintapi.GatewayAgent) {
160+
ag.Spec.Peerings = map[string]v1alpha1.PeeringSpec{
161+
"peering1": {
162+
Peering: map[string]*v1alpha1.PeeringEntry{
163+
"vpc-01": {
164+
Expose: []v1alpha1.PeeringEntryExpose{
165+
{
166+
IPs: []v1alpha1.PeeringEntryIP{
167+
{CIDR: "10.0.1.0/24"},
168+
},
169+
As: []v1alpha1.PeeringEntryAs{
170+
{CIDR: "172.96.1.0/24"},
171+
},
172+
},
173+
},
174+
},
175+
"vpc-02": {
176+
Expose: []v1alpha1.PeeringEntryExpose{
177+
{
178+
IPs: []v1alpha1.PeeringEntryIP{
179+
{VPCSubnet: "subnet1"},
180+
},
181+
As: []v1alpha1.PeeringEntryAs{
182+
{CIDR: "172.96.2.0/24"},
183+
},
184+
},
185+
},
186+
},
187+
},
188+
},
189+
}
190+
}),
191+
outputCfg: dpb("agent1", func(dp *dataplane.GatewayConfig) {
192+
dp.Overlay.Peerings = []*dataplane.VpcPeering{
193+
{
194+
Name: "peering1",
195+
For: []*dataplane.PeeringEntryFor{
196+
{
197+
Vpc: "vpc-01",
198+
Expose: []*dataplane.Expose{
199+
{
200+
Ips: []*dataplane.PeeringIPs{
201+
{Rule: &dataplane.PeeringIPs_Cidr{Cidr: "10.0.1.0/24"}},
202+
},
203+
As: []*dataplane.PeeringAs{
204+
{Rule: &dataplane.PeeringAs_Cidr{Cidr: "172.96.1.0/24"}},
205+
},
206+
},
207+
},
208+
},
209+
{
210+
Vpc: "vpc-02",
211+
Expose: []*dataplane.Expose{
212+
{
213+
Ips: []*dataplane.PeeringIPs{
214+
{Rule: &dataplane.PeeringIPs_Cidr{Cidr: "10.0.2.0/24"}},
215+
},
216+
As: []*dataplane.PeeringAs{
217+
{Rule: &dataplane.PeeringAs_Cidr{Cidr: "172.96.2.0/24"}},
218+
},
219+
},
220+
},
221+
},
222+
},
223+
},
224+
}
225+
}),
226+
wantErr: false,
227+
},
228+
// Add more cases for multiple exposes, Not, empty exposes, etc.
229+
}
230+
231+
for _, tt := range tests {
232+
t.Run(tt.name, func(t *testing.T) {
233+
got, err := buildDataplaneConfig(tt.agent)
234+
if tt.wantErr {
235+
require.Error(t, err)
236+
return
237+
}
238+
require.NoError(t, err)
239+
require.NotNil(t, got)
240+
require.Equal(t, tt.outputCfg, got)
241+
})
242+
}
243+
}
244+
245+
// Helper for pointer values
246+
func ptr[T any](v T) *T { return &v }

0 commit comments

Comments
 (0)