master
go 160 lines 4.07 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 TestCanonicalTopologyKeyHelpers_NormalizeDeterministically(t *testing.T) {
11 require.Equal(t,
12 "00:11:22:33:44:55,0a000001,switch-a",
13 canonicalTopologyHardwareKey([]string{" Switch-A ", "0A000001", "00:11:22:33:44:55", "switch-a"}),
14 )
15 require.Equal(t,
16 "example.net,switch-a",
17 canonicalTopologyStringListKey([]string{" Switch-A ", "", "example.net", "switch-a"}),
18 )
19 }
20
21 func TestAssignTopologyActorIDsAndLinkEndpoints_IsDeterministic(t *testing.T) {
22 actors := []Actor{
23 {
24 ActorType: "device",
25 Layer: "l2",
26 Source: "snmp",
27 Match: Match{
28 MacAddresses: []string{"00:11:22:33:44:55"},
29 Hostnames: []string{"switch-a"},
30 },
31 },
32 {
33 ActorType: "device",
34 Layer: "l2",
35 Source: "snmp",
36 Match: Match{
37 MacAddresses: []string{"00-11-22-33-44-55"},
38 Hostnames: []string{"switch-a-duplicate"},
39 },
40 },
41 {
42 ActorType: "endpoint",
43 Layer: "l2",
44 Source: "derived",
45 Match: Match{
46 IPAddresses: []string{"10.0.0.2"},
47 },
48 },
49 {
50 ActorType: "segment",
51 Layer: "l2",
52 Source: "derived",
53 Match: Match{},
54 },
55 }
56 links := []Link{
57 {
58 Protocol: "lldp",
59 Direction: "outbound",
60 State: "up",
61 Src: LinkEndpoint{
62 Match: Match{MacAddresses: []string{"00:11:22:33:44:55"}},
63 Attributes: map[string]any{
64 "if_name": "eth0",
65 },
66 },
67 Dst: LinkEndpoint{
68 Match: Match{IPAddresses: []string{"10.0.0.2"}},
69 Attributes: map[string]any{
70 "if_name": "eth9",
71 },
72 },
73 },
74 {
75 Protocol: "lldp",
76 Direction: "outbound",
77 State: "up",
78 Src: LinkEndpoint{
79 Match: Match{IPAddresses: []string{"10.0.0.2"}},
80 Attributes: map[string]any{
81 "if_name": "eth9",
82 },
83 },
84 Dst: LinkEndpoint{
85 Match: Match{MacAddresses: []string{"00:11:22:33:44:55"}},
86 Attributes: map[string]any{
87 "if_name": "eth0",
88 },
89 },
90 },
91 }
92
93 assignTopologyActorIDsAndLinkEndpoints(actors, links)
94
95 require.Equal(t, "mac:00:11:22:33:44:55", actors[0].ActorID)
96 require.Equal(t, "mac:00:11:22:33:44:55#2", actors[1].ActorID)
97 require.Equal(t, "ip:10.0.0.2", actors[2].ActorID)
98 require.Equal(t, "generated:segment", actors[3].ActorID)
99 require.Equal(t, "mac:00:11:22:33:44:55", links[0].SrcActorID)
100 require.Equal(t, "ip:10.0.0.2", links[0].DstActorID)
101 require.Equal(t, "ip:10.0.0.2", links[1].SrcActorID)
102 require.Equal(t, "mac:00:11:22:33:44:55", links[1].DstActorID)
103
104 sortTopologyActors(actors)
105 require.Equal(t, []string{
106 "mac:00:11:22:33:44:55",
107 "mac:00:11:22:33:44:55#2",
108 "ip:10.0.0.2",
109 "generated:segment",
110 }, []string{actors[0].ActorID, actors[1].ActorID, actors[2].ActorID, actors[3].ActorID})
111
112 sortTopologyLinks(links)
113 require.Equal(t, "ip:10.0.0.2", links[0].SrcActorID)
114 require.Equal(t, "mac:00:11:22:33:44:55", links[0].DstActorID)
115 require.Equal(t, "mac:00:11:22:33:44:55", links[1].SrcActorID)
116 require.Equal(t, "ip:10.0.0.2", links[1].DstActorID)
117 }
118
119 func TestEnrichTopologyPortTablesWithLinkCounts_AddsCountsToMatchingPorts(t *testing.T) {
120 actors := []Actor{
121 {
122 ActorID: "device-a",
123 Tables: map[string][]map[string]any{
124 "ports": {
125 {"name": "eth0"},
126 {"name": "eth1"},
127 },
128 },
129 },
130 {
131 ActorID: "device-b",
132 Tables: map[string][]map[string]any{
133 "ports": {
134 {"name": "xe-0/0/0"},
135 },
136 },
137 },
138 }
139 links := []Link{
140 {
141 SrcActorID: "device-a",
142 DstActorID: "device-b",
143 Src: LinkEndpoint{Attributes: map[string]any{"if_name": "eth0"}},
144 Dst: LinkEndpoint{Attributes: map[string]any{"if_name": "xe-0/0/0"}},
145 },
146 {
147 SrcActorID: "device-a",
148 DstActorID: "device-b",
149 Src: LinkEndpoint{Attributes: map[string]any{"if_name": "eth0"}},
150 Dst: LinkEndpoint{Attributes: map[string]any{"if_name": "xe-0/0/0"}},
151 },
152 }
153
154 enrichTopologyPortTablesWithLinkCounts(actors, links)
155
156 require.Equal(t, 2, actors[0].Tables["ports"][0]["link_count"])
157 _, exists := actors[0].Tables["ports"][1]["link_count"]
158 require.False(t, exists)
159 require.Equal(t, 2, actors[1].Tables["ports"][0]["link_count"])
160 }