|
| 1 | +// Copyright 2023 Blink Labs, LLC. |
| 2 | +// |
| 3 | +// Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +// you may not use this file except in compliance with the License. |
| 5 | +// You may obtain a copy of the License at |
| 6 | +// |
| 7 | +// http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +// |
| 9 | +// Unless required by applicable law or agreed to in writing, software |
| 10 | +// distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +// See the License for the specific language governing permissions and |
| 13 | +// limitations under the License. |
| 14 | + |
| 15 | +package ouroboros_test |
| 16 | + |
| 17 | +import ( |
| 18 | + "reflect" |
| 19 | + "strings" |
| 20 | + "testing" |
| 21 | + |
| 22 | + ouroboros "github.com/blinklabs-io/gouroboros" |
| 23 | +) |
| 24 | + |
| 25 | +type topologyTestDefinition struct { |
| 26 | + jsonData string |
| 27 | + expectedObject *ouroboros.TopologyConfig |
| 28 | +} |
| 29 | + |
| 30 | +var topologyTests = []topologyTestDefinition{ |
| 31 | + { |
| 32 | + jsonData: ` |
| 33 | +{ |
| 34 | + "Producers": [ |
| 35 | + { |
| 36 | + "addr": "relays-new.cardano-mainnet.iohk.io", |
| 37 | + "port": 3001, |
| 38 | + "valency": 2 |
| 39 | + } |
| 40 | + ] |
| 41 | +} |
| 42 | +`, |
| 43 | + expectedObject: &ouroboros.TopologyConfig{ |
| 44 | + Producers: []ouroboros.TopologyConfigLegacyProducer{ |
| 45 | + { |
| 46 | + Address: "relays-new.cardano-mainnet.iohk.io", |
| 47 | + Port: 3001, |
| 48 | + Valency: 2, |
| 49 | + }, |
| 50 | + }, |
| 51 | + }, |
| 52 | + }, |
| 53 | + { |
| 54 | + jsonData: ` |
| 55 | +{ |
| 56 | + "localRoots": [ |
| 57 | + { |
| 58 | + "accessPoints": [], |
| 59 | + "advertise": false, |
| 60 | + "valency": 1 |
| 61 | + } |
| 62 | + ], |
| 63 | + "publicRoots": [ |
| 64 | + { |
| 65 | + "accessPoints": [ |
| 66 | + { |
| 67 | + "address": "backbone.cardano-mainnet.iohk.io", |
| 68 | + "port": 3001 |
| 69 | + } |
| 70 | + ], |
| 71 | + "advertise": false |
| 72 | + }, |
| 73 | + { |
| 74 | + "accessPoints": [ |
| 75 | + { |
| 76 | + "address": "backbone.mainnet.emurgornd.com", |
| 77 | + "port": 3001 |
| 78 | + } |
| 79 | + ], |
| 80 | + "advertise": false |
| 81 | + } |
| 82 | + ], |
| 83 | + "useLedgerAfterSlot": 99532743 |
| 84 | +} |
| 85 | +`, |
| 86 | + expectedObject: &ouroboros.TopologyConfig{ |
| 87 | + LocalRoots: []ouroboros.TopologyConfigP2PLocalRoot{ |
| 88 | + { |
| 89 | + AccessPoints: []ouroboros.TopologyConfigP2PAccessPoint{}, |
| 90 | + Advertise: false, |
| 91 | + Valency: 1, |
| 92 | + }, |
| 93 | + }, |
| 94 | + PublicRoots: []ouroboros.TopologyConfigP2PPublicRoot{ |
| 95 | + { |
| 96 | + AccessPoints: []ouroboros.TopologyConfigP2PAccessPoint{ |
| 97 | + { |
| 98 | + Address: "backbone.cardano-mainnet.iohk.io", |
| 99 | + Port: 3001, |
| 100 | + }, |
| 101 | + }, |
| 102 | + Advertise: false, |
| 103 | + }, |
| 104 | + { |
| 105 | + AccessPoints: []ouroboros.TopologyConfigP2PAccessPoint{ |
| 106 | + { |
| 107 | + Address: "backbone.mainnet.emurgornd.com", |
| 108 | + Port: 3001, |
| 109 | + }, |
| 110 | + }, |
| 111 | + Advertise: false, |
| 112 | + }, |
| 113 | + }, |
| 114 | + UseLedgerAfterSlot: 99532743, |
| 115 | + }, |
| 116 | + }, |
| 117 | +} |
| 118 | + |
| 119 | +func TestParseTopologyConfig(t *testing.T) { |
| 120 | + for _, test := range topologyTests { |
| 121 | + topology, err := ouroboros.NewTopologyConfigFromReader( |
| 122 | + strings.NewReader(test.jsonData), |
| 123 | + ) |
| 124 | + if err != nil { |
| 125 | + t.Fatalf("failed to load TopologyConfig from JSON data: %s", err) |
| 126 | + } |
| 127 | + if !reflect.DeepEqual(topology, test.expectedObject) { |
| 128 | + t.Fatalf( |
| 129 | + "did not get expected object\n got:\n %#v\n wanted:\n %#v", |
| 130 | + topology, |
| 131 | + test.expectedObject, |
| 132 | + ) |
| 133 | + } |
| 134 | + } |
| 135 | +} |
0 commit comments