go.ligato.io/vpp-agent/v3@v3.5.0/proto/ligato/vpp/abf/models_test.go (about) 1 // Copyright (c) 2019 Cisco and/or its affiliates. 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 vpp_abf_test 16 17 import ( 18 "testing" 19 20 vpp_abf "go.ligato.io/vpp-agent/v3/proto/ligato/vpp/abf" 21 ) 22 23 func TestABFKey(t *testing.T) { 24 tests := []struct { 25 name string 26 abfIndex uint32 27 expectedKey string 28 }{ 29 { 30 name: "valid ABF index", 31 abfIndex: 0, 32 expectedKey: "config/vpp/abfs/v2/abf/0", 33 }, 34 { 35 name: "valid ABF index", 36 abfIndex: 1, 37 expectedKey: "config/vpp/abfs/v2/abf/1", 38 }, 39 } 40 for _, test := range tests { 41 t.Run(test.name, func(t *testing.T) { 42 key := vpp_abf.Key(test.abfIndex) 43 if key != test.expectedKey { 44 t.Errorf("failed for: abfIndex=%d\n"+ 45 "expected key:\n\t%q\ngot key:\n\t%q", 46 test.abfIndex, test.expectedKey, key) 47 } 48 }) 49 } 50 } 51 52 func TestParseNameFromKey(t *testing.T) { 53 tests := []struct { 54 name string 55 key string 56 expectedABFIndex string 57 expectedIsABFKey bool 58 }{ 59 { 60 name: "valid ABF index", 61 key: "config/vpp/abfs/v2/abf/1", 62 expectedABFIndex: "1", 63 expectedIsABFKey: true, 64 }, 65 { 66 name: "invalid ABF index", 67 key: "config/vpp/abfs/v2/abf/<invalid>", 68 expectedABFIndex: "<invalid>", 69 expectedIsABFKey: true, 70 }, 71 } 72 for _, test := range tests { 73 t.Run(test.name, func(t *testing.T) { 74 abfIndex, isABFKey := vpp_abf.ModelABF.ParseKey(test.key) 75 if isABFKey != test.expectedIsABFKey { 76 t.Errorf("expected isABFKey: %v\tgot: %v", test.expectedIsABFKey, isABFKey) 77 } 78 if abfIndex != test.expectedABFIndex { 79 t.Errorf("expected abfIndex: %s\tgot: %s", test.expectedABFIndex, abfIndex) 80 } 81 }) 82 } 83 } 84 85 func TestABFToInterfaceKey(t *testing.T) { 86 tests := []struct { 87 name string 88 abfIndex uint32 89 iface string 90 expectedKey string 91 }{ 92 { 93 name: "interface", 94 abfIndex: 1, 95 iface: "tap0", 96 expectedKey: "vpp/abf/1/interface/tap0", 97 }, 98 { 99 name: "empty interface", 100 abfIndex: 2, 101 iface: "", 102 expectedKey: "vpp/abf/2/interface/<invalid>", 103 }, 104 } 105 for _, test := range tests { 106 t.Run(test.name, func(t *testing.T) { 107 key := vpp_abf.ToInterfaceKey(test.abfIndex, test.iface) 108 if key != test.expectedKey { 109 t.Errorf("failed for: abfIndex=%d iface=%s\n"+ 110 "expected key:\n\t%q\ngot key:\n\t%q", 111 test.abfIndex, test.iface, test.expectedKey, key) 112 } 113 }) 114 } 115 } 116 117 func TestParseACLToInterfaceKey(t *testing.T) { 118 tests := []struct { 119 name string 120 key string 121 expectedABFIndex string 122 expectedIface string 123 expectedIsABFIfaceKey bool 124 }{ 125 { 126 name: "interface", 127 key: "vpp/abf/1/interface/tap0", 128 expectedABFIndex: "1", 129 expectedIface: "tap0", 130 expectedIsABFIfaceKey: true, 131 }, 132 { 133 name: "invalid abf index", 134 key: "vpp/abf/<invalid>/interface/tap0", 135 expectedABFIndex: "<invalid>", 136 expectedIface: "tap0", 137 expectedIsABFIfaceKey: true, 138 }, 139 { 140 name: "invalid interface", 141 key: "vpp/abf/1/interface/<invalid>", 142 expectedABFIndex: "1", 143 expectedIface: "<invalid>", 144 expectedIsABFIfaceKey: true, 145 }, 146 { 147 name: "all parameters invalid", 148 key: "vpp/abf/<invalid>/interface/<invalid>", 149 expectedABFIndex: "<invalid>", 150 expectedIface: "<invalid>", 151 expectedIsABFIfaceKey: true, 152 }, 153 { 154 name: "not ABFToInterface key", 155 key: "vpp/acl/acl1/interface/ingress/tap0", 156 expectedABFIndex: "", 157 expectedIface: "", 158 expectedIsABFIfaceKey: false, 159 }, 160 { 161 name: "not ABFToInterface key (cut after interface)", 162 key: "vpp/abf/<invalid>/interface/", 163 expectedABFIndex: "", 164 expectedIface: "", 165 expectedIsABFIfaceKey: false, 166 }, 167 { 168 name: "empty key", 169 key: "", 170 expectedABFIndex: "", 171 expectedIface: "", 172 expectedIsABFIfaceKey: false, 173 }, 174 } 175 for _, test := range tests { 176 t.Run(test.name, func(t *testing.T) { 177 abfIndex, iface, isABFIfaceKey := vpp_abf.ParseToInterfaceKey(test.key) 178 if isABFIfaceKey != test.expectedIsABFIfaceKey { 179 t.Errorf("expected isABFKey: %v\tgot: %v", test.expectedIsABFIfaceKey, isABFIfaceKey) 180 } 181 if abfIndex != test.expectedABFIndex { 182 t.Errorf("expected abfIndex: %s\tgot: %s", test.expectedABFIndex, abfIndex) 183 } 184 if iface != test.expectedIface { 185 t.Errorf("expected iface: %s\tgot: %s", test.expectedIface, iface) 186 } 187 }) 188 } 189 }