go.ligato.io/vpp-agent/v3@v3.5.0/plugins/linux/linuxmock/netlink_l3_handler_mock.go (about) 1 // Copyright (c) 2018 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 linuxmock 16 17 /* 18 // L3NetlinkHandlerMock allows to mock netlink-related methods 19 type L3NetlinkHandlerMock struct { 20 responses []*WhenL3Resp 21 respCurr int 22 respMax int 23 } 24 25 // NewL3NetlinkHandlerMock creates new instance of the mock and initializes response list 26 func NewL3NetlinkHandlerMock() *L3NetlinkHandlerMock { 27 return &L3NetlinkHandlerMock{ 28 responses: make([]*WhenL3Resp, 0), 29 } 30 } 31 32 // WhenL3Resp is helper struct with single method call and desired response items 33 type WhenL3Resp struct { 34 methodName string 35 items []interface{} 36 } 37 38 // When defines name of the related method. It creates a new instance of WhenL3Resp with provided method name and 39 // stores it to the mock. 40 func (mock *L3NetlinkHandlerMock) When(name string) *WhenL3Resp { 41 resp := &WhenL3Resp{ 42 methodName: name, 43 } 44 mock.responses = append(mock.responses, resp) 45 return resp 46 } 47 48 // ThenReturn receives array of items, which are desired to be returned in mocked method defined in "When". The full 49 // logic is: 50 // - When('someMethod').ThenReturn('values') 51 // 52 // Provided values should match return types of method. If method returns multiple values and only one is provided, 53 // mock tries to parse the value and returns it, while others will be nil or empty. 54 // 55 // If method is called several times, all cases must be defined separately, even if the return value is the same: 56 // - When('method1').ThenReturn('val1') 57 // - When('method1').ThenReturn('val1') 58 // 59 // All mocked methods are evaluated in same order they were assigned. 60 func (when *WhenL3Resp) ThenReturn(item ...interface{}) { 61 when.items = item 62 } 63 64 // Auxiliary method returns next return value for provided method as generic type 65 func (mock *L3NetlinkHandlerMock) getReturnValues(name string) (response []interface{}) { 66 for i, resp := range mock.responses { 67 if resp.methodName == name { 68 // Remove used response but retain order 69 mock.responses = append(mock.responses[:i], mock.responses[i+1:]...) 70 return resp.items 71 } 72 } 73 // Return empty response 74 return 75 } 76 77 // Mocked netlink handler methods 78 79 // AddArpEntry implements NetlinkAPI. 80 func (mock *L3NetlinkHandlerMock) AddArpEntry(name string, arpEntry *netlink.Neigh) error { 81 items := mock.getReturnValues("AddArpEntry") 82 if len(items) >= 1 { 83 return items[0].(error) 84 } 85 return nil 86 } 87 88 // SetArpEntry implements NetlinkAPI. 89 func (mock *L3NetlinkHandlerMock) SetArpEntry(name string, arpEntry *netlink.Neigh) error { 90 items := mock.getReturnValues("SetArpEntry") 91 if len(items) >= 1 { 92 return items[0].(error) 93 } 94 return nil 95 } 96 97 // DelArpEntry implements NetlinkAPI. 98 func (mock *L3NetlinkHandlerMock) DelArpEntry(name string, arpEntry *netlink.Neigh) error { 99 items := mock.getReturnValues("DelArpEntry") 100 if len(items) >= 1 { 101 return items[0].(error) 102 } 103 return nil 104 } 105 106 // GetArpEntries implements NetlinkAPI. 107 func (mock *L3NetlinkHandlerMock) GetArpEntries(interfaceIdx int, family int) ([]netlink.Neigh, error) { 108 items := mock.getReturnValues("GetArpEntries") 109 if len(items) == 1 { 110 switch typed := items[0].(type) { 111 case []netlink.Neigh: 112 return typed, nil 113 case error: 114 return nil, typed 115 } 116 } else if len(items) == 2 { 117 return items[0].([]netlink.Neigh), items[1].(error) 118 } 119 return nil, nil 120 } 121 122 // AddStaticRoute implements NetlinkAPI. 123 func (mock *L3NetlinkHandlerMock) AddStaticRoute(name string, route *netlink.Route) error { 124 items := mock.getReturnValues("AddStaticRoute") 125 if len(items) >= 1 { 126 return items[0].(error) 127 } 128 return nil 129 } 130 131 // ReplaceStaticRoute implements NetlinkAPI. 132 func (mock *L3NetlinkHandlerMock) ReplaceStaticRoute(name string, route *netlink.Route) error { 133 items := mock.getReturnValues("ReplaceStaticRoute") 134 if len(items) >= 1 { 135 return items[0].(error) 136 } 137 return nil 138 } 139 140 // DelStaticRoute implements NetlinkAPI. 141 func (mock *L3NetlinkHandlerMock) DelStaticRoute(name string, route *netlink.Route) error { 142 items := mock.getReturnValues("DelStaticRoute") 143 if len(items) >= 1 { 144 return items[0].(error) 145 } 146 return nil 147 } 148 149 // GetStaticRoutes implements NetlinkAPI. 150 func (mock *L3NetlinkHandlerMock) GetStaticRoutes(link netlink.Link, family int) ([]netlink.Route, error) { 151 items := mock.getReturnValues("GetStaticRoutes") 152 if len(items) == 1 { 153 switch typed := items[0].(type) { 154 case []netlink.Route: 155 return typed, nil 156 case error: 157 return nil, typed 158 } 159 } else if len(items) == 2 { 160 return items[0].([]netlink.Route), items[1].(error) 161 } 162 return nil, nil 163 } 164 165 // DumpArpEntries does not return a value 166 func (mock *L3NetlinkHandlerMock) DumpArpEntries() ([]*linuxcalls.LinuxArpDetails, error) { 167 return nil, nil 168 } 169 170 // DumpRoutes does not return a value 171 func (mock *L3NetlinkHandlerMock) DumpRoutes() ([]*linuxcalls.LinuxRouteDetails, error) { 172 return nil, nil 173 } 174 */