go.ligato.io/vpp-agent/v3@v3.5.0/plugins/linux/linuxmock/namespace_mock.go (about) 1 package linuxmock 2 3 /* 4 // NamespacePluginMock allows to mock namespace plugin methods to manage namespaces and microservices 5 type NamespacePluginMock struct { 6 responses []*WhenNsResp 7 respCurr int 8 respMax int 9 } 10 11 // NewNamespacePluginMock creates new instance of the mock and initializes response list 12 func NewNamespacePluginMock() *NamespacePluginMock { 13 return &NamespacePluginMock{ 14 responses: make([]*WhenNsResp, 0), 15 } 16 } 17 18 // WhenNsResp is helper struct with single method call and desired response items 19 type WhenNsResp struct { 20 methodName string 21 items []interface{} 22 } 23 24 // When defines name of the related method. It creates a new instance of WhenNsResp with provided method name and 25 // stores it to the mock. 26 func (mock *NamespacePluginMock) When(methodName string) *WhenNsResp { 27 resp := &WhenNsResp{ 28 methodName: methodName, 29 } 30 mock.responses = append(mock.responses, resp) 31 return resp 32 } 33 34 // ThenReturn receives array of items, which are desired to be returned in mocked method defined in "When". The full 35 // logic is: 36 // - When('someMethod').ThenReturn('values') 37 // 38 // Provided values should match return types of method. If method returns multiple values and only one is provided, 39 // mock tries to parse the value and returns it, while others will be nil or empty. 40 // 41 // If method is called several times, all cases must be defined separately, even if the return value is the same: 42 // - When('method1').ThenReturn('val1') 43 // - When('method1').ThenReturn('val1') 44 // 45 // All mocked methods are evaluated in same order they were assigned. 46 func (when *WhenNsResp) ThenReturn(item ...interface{}) { 47 when.items = item 48 } 49 50 // Auxiliary method returns next return value for provided method as generic type 51 func (mock *NamespacePluginMock) getReturnValues(name string) (response []interface{}) { 52 for i, resp := range mock.responses { 53 if resp.methodName == name { 54 // Remove used response but retain order 55 mock.responses = append(mock.responses[:i], mock.responses[i+1:]...) 56 return resp.items 57 } 58 } 59 // Return empty response 60 return 61 } 62 63 // Mocked netlink handler methods //todo define other 64 65 // GetOrCreateNamespace implements NsManagement. 66 func (mock *NamespacePluginMock) GetOrCreateNamespace(ns *nsplugin.Namespace) (netns.NsHandle, error) { 67 items := mock.getReturnValues("GetOrCreateNamespace") 68 if len(items) == 1 { 69 switch typed := items[0].(type) { 70 case netns.NsHandle: 71 return typed, nil 72 case error: 73 return 0, typed 74 } 75 } else if len(items) == 2 { 76 return items[0].(netns.NsHandle), items[1].(error) 77 } 78 return 0, nil 79 } 80 81 // IsNamespaceAvailable implements NsManagement. 82 func (mock *NamespacePluginMock) IsNamespaceAvailable(ns *interfaces.LinuxInterfaces_Interface_Namespace) bool { 83 items := mock.getReturnValues("IsNamespaceAvailable") 84 return items[0].(bool) 85 } 86 87 // SwitchNamespace implements NsManagement. 88 func (mock *NamespacePluginMock) SwitchNamespace(ns *nsplugin.Namespace, ctx *nsplugin.NamespaceMgmtCtx) (func(), error) { 89 items := mock.getReturnValues("SwitchNamespace") 90 if len(items) == 1 { 91 switch typed := items[0].(type) { 92 case func(): 93 return typed, nil 94 case error: 95 return func() {}, typed 96 } 97 } else if len(items) == 2 { 98 return items[0].(func()), items[1].(error) 99 } 100 return func() {}, nil 101 } 102 103 // SwitchToNamespace implements NsManagement. 104 func (mock *NamespacePluginMock) SwitchToNamespace(nsMgmtCtx *nsplugin.NamespaceMgmtCtx, ns *interfaces.LinuxInterfaces_Interface_Namespace) (func(), error) { 105 items := mock.getReturnValues("SwitchToNamespace") 106 if len(items) == 1 { 107 switch typed := items[0].(type) { 108 case func(): 109 return typed, nil 110 case error: 111 return func() {}, typed 112 } 113 } else if len(items) == 2 { 114 return items[0].(func()), items[1].(error) 115 } 116 return func() {}, nil 117 } 118 119 // GetConfigNamespace implements NsManagement. 120 func (mock *NamespacePluginMock) GetConfigNamespace() *interfaces.LinuxInterfaces_Interface_Namespace { 121 items := mock.getReturnValues("GetConfigNamespace") 122 if len(items) >= 1 { 123 return items[0].(*interfaces.LinuxInterfaces_Interface_Namespace) 124 } 125 return nil 126 } 127 128 // ConvertMicroserviceNsToPidNs implements NsManagement 129 func (mock *NamespacePluginMock) ConvertMicroserviceNsToPidNs(msLabel string) (pidNs *nsplugin.Namespace) { 130 items := mock.getReturnValues("ConvertMicroserviceNsToPidNs") 131 if len(items) >= 1 { 132 return items[0].(*nsplugin.Namespace) 133 } 134 return nil 135 } 136 137 // IfaceNsToString implements NsConvertor. 138 func (mock *NamespacePluginMock) IfaceNsToString(namespace *interfaces.LinuxInterfaces_Interface_Namespace) string { 139 items := mock.getReturnValues("IfaceNsToString") 140 if len(items) >= 1 { 141 return items[0].(string) 142 } 143 return "" 144 } 145 146 // IfNsToGeneric implements NsConvertor. 147 func (mock *NamespacePluginMock) IfNsToGeneric(ns *interfaces.LinuxInterfaces_Interface_Namespace) *nsplugin.Namespace { 148 items := mock.getReturnValues("IfNsToGeneric") 149 if len(items) >= 1 { 150 return items[0].(*nsplugin.Namespace) 151 } 152 return nil 153 } 154 155 // ArpNsToGeneric implements NsConvertor. 156 func (mock *NamespacePluginMock) ArpNsToGeneric(ns *l3.LinuxStaticArpEntries_ArpEntry_Namespace) *nsplugin.Namespace { 157 items := mock.getReturnValues("ArpNsToGeneric") 158 if len(items) >= 1 { 159 return items[0].(*nsplugin.Namespace) 160 } 161 return nil 162 } 163 164 // GenericToArpNs implements NsConvertor. 165 func (mock *NamespacePluginMock) GenericToArpNs(ns *nsplugin.Namespace) (*l3.LinuxStaticArpEntries_ArpEntry_Namespace, error) { 166 items := mock.getReturnValues("GenericToArpNs") 167 if len(items) == 1 { 168 switch typed := items[0].(type) { 169 case *l3.LinuxStaticArpEntries_ArpEntry_Namespace: 170 return typed, nil 171 case error: 172 return nil, typed 173 } 174 } else if len(items) == 2 { 175 return items[0].(*l3.LinuxStaticArpEntries_ArpEntry_Namespace), items[1].(error) 176 } 177 return nil, nil 178 } 179 180 // RouteNsToGeneric implements NsConvertor. 181 func (mock *NamespacePluginMock) RouteNsToGeneric(ns *l3.LinuxStaticRoutes_Route_Namespace) *nsplugin.Namespace { 182 items := mock.getReturnValues("RouteNsToGeneric") 183 if len(items) >= 1 { 184 return items[0].(*nsplugin.Namespace) 185 } 186 return nil 187 } 188 189 // HandleMicroservices implements Microservices. 190 func (mock *NamespacePluginMock) HandleMicroservices(ctx *nsplugin.MicroserviceCtx) {} 191 */