Akash Soni | d36d23b | 2023-08-18 12:51:40 +0530 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2022-present Open Networking Foundation |
| 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 | |
| 16 | package controller |
| 17 | |
| 18 | import ( |
| 19 | "context" |
| 20 | "reflect" |
| 21 | "testing" |
| 22 | "voltha-go-controller/internal/pkg/holder" |
| 23 | "voltha-go-controller/internal/pkg/of" |
| 24 | "voltha-go-controller/internal/test/mocks" |
| 25 | |
| 26 | "github.com/golang/mock/gomock" |
| 27 | ofp "github.com/opencord/voltha-protos/v5/go/openflow_13" |
| 28 | "github.com/stretchr/testify/assert" |
| 29 | ) |
| 30 | |
| 31 | func TestNewDevicePort(t *testing.T) { |
| 32 | type args struct { |
| 33 | mp *ofp.OfpPort |
| 34 | } |
| 35 | tests := []struct { |
| 36 | name string |
| 37 | args args |
| 38 | want *DevicePort |
| 39 | }{ |
| 40 | { |
| 41 | name: "NewDevicePort", |
| 42 | args: args{ |
| 43 | mp: &ofp.OfpPort{ |
| 44 | PortNo: uint32(1), |
| 45 | }, |
| 46 | }, |
| 47 | }, |
| 48 | } |
| 49 | for _, tt := range tests { |
| 50 | t.Run(tt.name, func(t *testing.T) { |
| 51 | got := NewDevicePort(tt.args.mp) |
| 52 | assert.NotNil(t, got) |
| 53 | }) |
| 54 | } |
| 55 | } |
| 56 | |
| 57 | func TestDevice_UpdateFlows(t *testing.T) { |
| 58 | type args struct { |
| 59 | flow *of.VoltFlow |
| 60 | devPort *DevicePort |
| 61 | } |
| 62 | tests := []struct { |
| 63 | name string |
| 64 | args args |
| 65 | }{ |
| 66 | { |
| 67 | name: "Device_UpdateFlows", |
| 68 | args: args{ |
| 69 | flow: &of.VoltFlow{ |
| 70 | PortName: "test_port_name", |
| 71 | }, |
| 72 | devPort: &DevicePort{ |
| 73 | Name: "test_name", |
| 74 | }, |
| 75 | }, |
| 76 | }, |
| 77 | } |
| 78 | for _, tt := range tests { |
| 79 | flushQueue := make(map[uint32]*UniIDFlowQueue) |
| 80 | flushQueue[uint32(1)] = &UniIDFlowQueue{ |
| 81 | ID: uint32(1), |
| 82 | } |
| 83 | t.Run(tt.name, func(t *testing.T) { |
| 84 | d := &Device{ |
| 85 | flowQueue: flushQueue, |
| 86 | flowHash: uint32(1), |
| 87 | } |
| 88 | d.UpdateFlows(tt.args.flow, tt.args.devPort) |
| 89 | }) |
| 90 | } |
| 91 | } |
| 92 | |
| 93 | func TestNewDevice(t *testing.T) { |
| 94 | type args struct { |
| 95 | cntx context.Context |
| 96 | id string |
| 97 | slno string |
| 98 | vclientHldr *holder.VolthaServiceClientHolder |
| 99 | southBoundID string |
| 100 | mfr string |
| 101 | hwDesc string |
| 102 | swDesc string |
| 103 | } |
| 104 | tests := []struct { |
| 105 | name string |
| 106 | args args |
| 107 | want *Device |
| 108 | }{ |
| 109 | { |
| 110 | name: "TestNewDevice", |
| 111 | args: args{ |
| 112 | cntx: context.Background(), |
| 113 | id: "test_id", |
| 114 | slno: "test_sl_no", |
| 115 | }, |
| 116 | }, |
| 117 | } |
| 118 | for _, tt := range tests { |
| 119 | t.Run(tt.name, func(t *testing.T) { |
| 120 | dbintf := mocks.NewMockDBIntf(gomock.NewController(t)) |
| 121 | db = dbintf |
| 122 | dbintf.EXPECT().GetFlowHash(gomock.Any(), gomock.Any()).Return("1", nil).Times(1) |
| 123 | got := NewDevice(tt.args.cntx, tt.args.id, tt.args.slno, tt.args.vclientHldr, tt.args.southBoundID, tt.args.mfr, tt.args.hwDesc, tt.args.swDesc) |
| 124 | assert.NotNil(t, got) |
| 125 | }) |
| 126 | } |
| 127 | } |
| 128 | |
| 129 | func TestDevice_triggerFlowResultNotification(t *testing.T) { |
| 130 | type args struct { |
| 131 | cntx context.Context |
| 132 | cookie uint64 |
| 133 | flow *of.VoltSubFlow |
| 134 | oper of.Command |
| 135 | bwDetails of.BwAvailDetails |
| 136 | err error |
| 137 | } |
| 138 | tests := []struct { |
| 139 | name string |
| 140 | args args |
| 141 | }{ |
| 142 | { |
| 143 | name: "Device_triggerFlowResultNotification", |
| 144 | args: args{ |
| 145 | cntx: context.Background(), |
| 146 | cookie: uint64(1), |
| 147 | flow: &of.VoltSubFlow{ |
| 148 | Cookie: uint64(1), |
| 149 | }, |
| 150 | oper: of.CommandAdd, |
| 151 | bwDetails: of.BwAvailDetails{ |
| 152 | PrevBw: "test_prev_bw", |
| 153 | }, |
| 154 | err: nil, |
| 155 | }, |
| 156 | }, |
| 157 | } |
| 158 | for _, tt := range tests { |
| 159 | t.Run(tt.name, func(t *testing.T) { |
| 160 | flows := make(map[uint64]*of.VoltSubFlow) |
| 161 | flows[uint64(1)] = &of.VoltSubFlow{ |
| 162 | Cookie: uint64(1), |
| 163 | } |
| 164 | d := &Device{ |
| 165 | flows: flows, |
| 166 | } |
| 167 | appMock := mocks.NewMockApp(gomock.NewController(t)) |
| 168 | _ = NewController(context.Background(), appMock) |
| 169 | dbintf := mocks.NewMockDBIntf(gomock.NewController(t)) |
| 170 | db = dbintf |
| 171 | dbintf.EXPECT().PutFlow(gomock.Any(), gomock.Any(), gomock.Any(), gomock.Any()).Return(nil).Times(1) |
| 172 | appMock.EXPECT().ProcessFlowModResultIndication(gomock.Any(), gomock.Any()).Times(1) |
Akash Soni | ef452f1 | 2024-12-12 18:20:28 +0530 | [diff] [blame^] | 173 | d.triggerFlowResultNotification(tt.args.cntx, tt.args.cookie, tt.args.flow, tt.args.oper, tt.args.bwDetails, tt.args.err) |
Akash Soni | d36d23b | 2023-08-18 12:51:40 +0530 | [diff] [blame] | 174 | }) |
| 175 | } |
| 176 | } |
| 177 | |
| 178 | func TestDevice_ResetCache(t *testing.T) { |
| 179 | tests := []struct { |
| 180 | name string |
| 181 | }{ |
| 182 | { |
| 183 | name: "Device_ResetCache", |
| 184 | }, |
| 185 | } |
| 186 | for _, tt := range tests { |
| 187 | t.Run(tt.name, func(t *testing.T) { |
| 188 | d := &Device{} |
| 189 | d.ResetCache() |
| 190 | }) |
| 191 | } |
| 192 | } |
| 193 | |
| 194 | func TestDevice_GetAllFlows(t *testing.T) { |
| 195 | tests := []struct { |
| 196 | name string |
| 197 | want []*of.VoltSubFlow |
| 198 | }{ |
| 199 | { |
| 200 | name: "Device_GetAllFlows", |
| 201 | }, |
| 202 | } |
| 203 | for _, tt := range tests { |
| 204 | t.Run(tt.name, func(t *testing.T) { |
| 205 | d := &Device{} |
| 206 | if got := d.GetAllFlows(); !reflect.DeepEqual(got, tt.want) { |
| 207 | t.Errorf("Device.GetAllFlows() = %v, want %v", got, tt.want) |
| 208 | } |
| 209 | }) |
| 210 | } |
| 211 | } |
Akash Soni | 6f36945 | 2023-09-19 11:18:28 +0530 | [diff] [blame] | 212 | |
| 213 | func TestDevice_PacketOutReq(t *testing.T) { |
| 214 | type args struct { |
| 215 | outport string |
| 216 | inport string |
| 217 | data []byte |
| 218 | isCustomPkt bool |
| 219 | } |
| 220 | tests := []struct { |
| 221 | name string |
| 222 | args args |
| 223 | wantErr bool |
| 224 | }{ |
| 225 | { |
| 226 | name: "Device_PacketOutReq", |
| 227 | args: args{ |
| 228 | outport: "test_out_port", |
| 229 | inport: "test_in_port", |
| 230 | }, |
| 231 | }, |
| 232 | } |
| 233 | for _, tt := range tests { |
| 234 | t.Run(tt.name, func(t *testing.T) { |
| 235 | portByName := make(map[string]*DevicePort) |
| 236 | portByName["test_in_port"] = &DevicePort{ |
| 237 | Name: "test_device", |
| 238 | } |
| 239 | portByName["test_out_port"] = &DevicePort{ |
| 240 | Name: "test_device", |
| 241 | } |
| 242 | packetOutChannel := make(chan *ofp.PacketOut, 2) |
| 243 | |
| 244 | d := &Device{ |
| 245 | packetOutChannel: packetOutChannel, |
| 246 | PortsByName: portByName, |
| 247 | } |
| 248 | if err := d.PacketOutReq(tt.args.outport, tt.args.inport, tt.args.data, tt.args.isCustomPkt); (err != nil) != tt.wantErr { |
| 249 | t.Errorf("Device.PacketOutReq() error = %v, wantErr %v", err, tt.wantErr) |
| 250 | } |
| 251 | }) |
| 252 | } |
| 253 | } |
| 254 | |
| 255 | func TestDevice_SetFlowHash(t *testing.T) { |
| 256 | type args struct { |
| 257 | cntx context.Context |
| 258 | hash uint32 |
| 259 | } |
| 260 | tests := []struct { |
| 261 | name string |
| 262 | args args |
| 263 | }{ |
| 264 | { |
| 265 | name: "Device_SetFlowHash", |
| 266 | args: args{ |
| 267 | cntx: context.Background(), |
| 268 | hash: uint32(2), |
| 269 | }, |
| 270 | }, |
| 271 | } |
| 272 | for _, tt := range tests { |
| 273 | t.Run(tt.name, func(t *testing.T) { |
| 274 | d := &Device{} |
| 275 | dbintf := mocks.NewMockDBIntf(gomock.NewController(t)) |
| 276 | db = dbintf |
| 277 | dbintf.EXPECT().PutFlowHash(gomock.Any(), gomock.Any(), gomock.Any()).Return(nil).Times(1) |
| 278 | d.SetFlowHash(tt.args.cntx, tt.args.hash) |
| 279 | }) |
| 280 | } |
| 281 | } |