| 1 | // SPDX-License-Identifier: GPL-3.0-or-later |
| 2 | |
| 3 | package parity |
| 4 | |
| 5 | import ( |
| 6 | "testing" |
| 7 | |
| 8 | "github.com/stretchr/testify/require" |
| 9 | ) |
| 10 | |
| 11 | func TestGoldenYAMLValidation_RejectsBidirectionalPairMismatch(t *testing.T) { |
| 12 | doc := GoldenDocument{ |
| 13 | Version: GoldenVersion, |
| 14 | ScenarioID: "pair-mismatch", |
| 15 | Devices: []GoldenDevice{ |
| 16 | {ID: "a", Hostname: "a"}, |
| 17 | {ID: "b", Hostname: "b"}, |
| 18 | }, |
| 19 | Adjacencies: []GoldenAdjacency{ |
| 20 | {Protocol: "lldp", SourceDevice: "a", SourcePort: "Gi0/1", TargetDevice: "b", TargetPort: "Gi0/2"}, |
| 21 | {Protocol: "lldp", SourceDevice: "b", SourcePort: "Gi0/2", TargetDevice: "a", TargetPort: "Gi0/1"}, |
| 22 | }, |
| 23 | Expectations: GoldenCounts{ |
| 24 | DirectionalAdjacencies: 2, |
| 25 | BidirectionalPairs: 0, |
| 26 | Devices: 2, |
| 27 | }, |
| 28 | } |
| 29 | |
| 30 | err := doc.validate() |
| 31 | require.Error(t, err) |
| 32 | require.Contains(t, err.Error(), "expectations.bidirectional_pairs") |
| 33 | } |
| 34 | |
| 35 | func TestGoldenYAMLValidation_AcceptsBidirectionalDevicePairsWithPortAliases(t *testing.T) { |
| 36 | doc := GoldenDocument{ |
| 37 | Version: GoldenVersion, |
| 38 | ScenarioID: "pair-port-aliases", |
| 39 | Devices: []GoldenDevice{ |
| 40 | {ID: "a", Hostname: "a"}, |
| 41 | {ID: "b", Hostname: "b"}, |
| 42 | }, |
| 43 | Adjacencies: []GoldenAdjacency{ |
| 44 | {Protocol: "cdp", SourceDevice: "a", SourcePort: "Gi0/0", TargetDevice: "b", TargetPort: "GigabitEthernet0/1"}, |
| 45 | {Protocol: "cdp", SourceDevice: "b", SourcePort: "Gi0/1", TargetDevice: "a", TargetPort: "GigabitEthernet0/0"}, |
| 46 | }, |
| 47 | Expectations: GoldenCounts{ |
| 48 | DirectionalAdjacencies: 2, |
| 49 | BidirectionalPairs: 1, |
| 50 | Devices: 2, |
| 51 | }, |
| 52 | } |
| 53 | |
| 54 | require.NoError(t, doc.validate()) |
| 55 | } |
| 56 | |
| 57 | func TestGoldenYAMLValidation_SelfLoopsDoNotCountAsBidirectionalPairs(t *testing.T) { |
| 58 | doc := GoldenDocument{ |
| 59 | Version: GoldenVersion, |
| 60 | ScenarioID: "self-loop", |
| 61 | Devices: []GoldenDevice{ |
| 62 | {ID: "a", Hostname: "a"}, |
| 63 | }, |
| 64 | Adjacencies: []GoldenAdjacency{ |
| 65 | {Protocol: "lldp", SourceDevice: "a", SourcePort: "Gi0/1", TargetDevice: "a", TargetPort: "Gi0/1"}, |
| 66 | }, |
| 67 | Expectations: GoldenCounts{ |
| 68 | DirectionalAdjacencies: 1, |
| 69 | BidirectionalPairs: 1, |
| 70 | Devices: 1, |
| 71 | }, |
| 72 | } |
| 73 | |
| 74 | err := doc.validate() |
| 75 | require.Error(t, err) |
| 76 | require.Contains(t, err.Error(), "expectations.bidirectional_pairs") |
| 77 | } |
| 78 | |
| 79 | func TestGoldenCanonicalJSON_UsesEmptyArrayForEmptyAdjacencies(t *testing.T) { |
| 80 | doc := GoldenDocument{ |
| 81 | Version: GoldenVersion, |
| 82 | ScenarioID: "empty-adjacencies", |
| 83 | Devices: []GoldenDevice{{ID: "a", Hostname: "a"}}, |
| 84 | Adjacencies: nil, |
| 85 | Expectations: GoldenCounts{ |
| 86 | DirectionalAdjacencies: 0, |
| 87 | BidirectionalPairs: 0, |
| 88 | Devices: 1, |
| 89 | }, |
| 90 | } |
| 91 | |
| 92 | payload, err := doc.CanonicalJSON() |
| 93 | require.NoError(t, err) |
| 94 | require.Contains(t, string(payload), "\"adjacencies\": []") |
| 95 | } |