master
go 171 lines 4.33 KB
Raw
1 // SPDX-License-Identifier: GPL-3.0-or-later
2
3 package l2topology
4
5 import (
6 "github.com/stretchr/testify/require"
7 "testing"
8 )
9
10 func TestBackfillPairGroupMissingEndpointPortsCopiesPeerInterfaceAttributes(t *testing.T) {
11 entries := []*builtAdjacencyLink{
12 {
13 adj: Adjacency{
14 SourceID: "device-a",
15 TargetID: "device-b",
16 },
17 link: Link{
18 Src: LinkEndpoint{Attributes: map[string]any{}},
19 Dst: LinkEndpoint{Attributes: map[string]any{}},
20 },
21 },
22 {
23 adj: Adjacency{
24 SourceID: "device-b",
25 TargetID: "device-a",
26 },
27 link: Link{
28 Src: LinkEndpoint{Attributes: map[string]any{
29 "if_index": 2,
30 "if_name": "Gi0/2",
31 "port_id": "Gi0/2",
32 }},
33 Dst: LinkEndpoint{Attributes: map[string]any{
34 "if_index": 1,
35 "if_name": "Gi0/1",
36 "port_id": "Gi0/1",
37 }},
38 },
39 },
40 }
41
42 backfillPairGroupMissingEndpointPorts(entries)
43
44 require.Equal(t, 1, topologyAttrInt(entries[0].link.Src.Attributes, "if_index"))
45 require.Equal(t, "Gi0/1", topologyAttrString(entries[0].link.Src.Attributes, "if_name"))
46 require.Equal(t, "Gi0/1", topologyAttrString(entries[0].link.Src.Attributes, "port_id"))
47 require.Equal(t, 2, topologyAttrInt(entries[0].link.Dst.Attributes, "if_index"))
48 require.Equal(t, "Gi0/2", topologyAttrString(entries[0].link.Dst.Attributes, "if_name"))
49 require.Equal(t, "Gi0/2", topologyAttrString(entries[0].link.Dst.Attributes, "port_id"))
50 }
51
52 func TestBackfillPairGroupMissingEndpointPortsSkipsAmbiguousReverseCandidates(t *testing.T) {
53 entries := []*builtAdjacencyLink{
54 {
55 adj: Adjacency{
56 SourceID: "device-a",
57 TargetID: "device-b",
58 },
59 link: Link{
60 Src: LinkEndpoint{Attributes: map[string]any{}},
61 Dst: LinkEndpoint{Attributes: map[string]any{}},
62 },
63 },
64 {
65 adj: Adjacency{
66 SourceID: "device-b",
67 TargetID: "device-a",
68 },
69 link: Link{
70 Src: LinkEndpoint{Attributes: map[string]any{
71 "if_name": "Gi0/2",
72 }},
73 Dst: LinkEndpoint{Attributes: map[string]any{
74 "if_name": "Gi0/1",
75 }},
76 },
77 },
78 {
79 adj: Adjacency{
80 SourceID: "device-b",
81 TargetID: "device-a",
82 },
83 link: Link{
84 Src: LinkEndpoint{Attributes: map[string]any{
85 "if_name": "Gi0/22",
86 }},
87 Dst: LinkEndpoint{Attributes: map[string]any{
88 "if_name": "Gi0/11",
89 }},
90 },
91 },
92 }
93
94 backfillPairGroupMissingEndpointPorts(entries)
95
96 require.Equal(t, "", topologyAttrString(entries[0].link.Src.Attributes, "if_name"))
97 require.Equal(t, "", topologyAttrString(entries[0].link.Dst.Attributes, "if_name"))
98 }
99
100 func TestBackfillEndpointPortFromPeerPreservesExistingCanonicalPort(t *testing.T) {
101 endpoint := LinkEndpoint{
102 Attributes: map[string]any{
103 "if_name": "Gi0/10",
104 },
105 }
106 peer := LinkEndpoint{
107 Attributes: map[string]any{
108 "if_index": 7,
109 "if_name": "Gi0/7",
110 "port_id": "Gi0/7",
111 },
112 }
113
114 backfilled := backfillEndpointPortFromPeer(endpoint, peer)
115
116 require.Equal(t, "Gi0/10", topologyAttrString(backfilled.Attributes, "if_name"))
117 require.Zero(t, topologyAttrInt(backfilled.Attributes, "if_index"))
118 require.Equal(t, "", topologyAttrString(backfilled.Attributes, "port_id"))
119 }
120
121 func TestSegmentProjectionBuilderPruneSegmentsWithoutLinksRemovesEmptySegments(t *testing.T) {
122 builder := &segmentProjectionBuilder{
123 segmentIDs: []string{"segment-a", "segment-b"},
124 out: projectedSegments{
125 actors: []Actor{
126 {
127 ActorID: "segment-a",
128 ActorType: "segment",
129 Attributes: map[string]any{
130 "segment_id": "segment-a",
131 },
132 },
133 {
134 ActorID: "segment-b",
135 ActorType: "segment",
136 Attributes: map[string]any{
137 "segment_id": "segment-b",
138 },
139 },
140 },
141 links: []Link{
142 {
143 Protocol: "fdb",
144 Direction: "bidirectional",
145 Metrics: map[string]any{
146 "bridge_domain": "segment-a",
147 },
148 },
149 {
150 Protocol: "fdb",
151 Direction: "bidirectional",
152 Metrics: map[string]any{
153 "bridge_domain": "segment-b",
154 },
155 },
156 },
157 },
158 }
159
160 builder.pruneSegmentsWithoutLinks(map[string]struct{}{
161 "segment-a": {},
162 })
163
164 require.Len(t, builder.out.actors, 1)
165 require.Equal(t, "segment-a", builder.out.actors[0].ActorID)
166 require.Len(t, builder.out.links, 1)
167 require.Equal(t, "segment-a", topologyMetricString(builder.out.links[0].Metrics, "bridge_domain"))
168 require.Equal(t, 1, builder.out.linksFdb)
169 require.Equal(t, 1, builder.out.bidirectionalCount)
170 require.Equal(t, 1, builder.out.endpointLinksEmitted)
171 }