vinokuma | f7605fc | 2023-06-02 18:08:01 +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 ( |
vinokuma | 04dc9f8 | 2023-07-31 15:47:49 +0530 | [diff] [blame] | 19 | "context" |
| 20 | "reflect" |
vinokuma | f7605fc | 2023-06-02 18:08:01 +0530 | [diff] [blame] | 21 | "testing" |
| 22 | "voltha-go-controller/internal/pkg/of" |
vinokuma | 04dc9f8 | 2023-07-31 15:47:49 +0530 | [diff] [blame] | 23 | "voltha-go-controller/internal/test/mocks" |
| 24 | |
| 25 | "github.com/golang/mock/gomock" |
vinokuma | f7605fc | 2023-06-02 18:08:01 +0530 | [diff] [blame] | 26 | ) |
| 27 | |
| 28 | func Test_isFlowOperSuccess(t *testing.T) { |
| 29 | type args struct { |
| 30 | statusCode uint32 |
| 31 | oper of.Command |
| 32 | } |
| 33 | tests := []struct { |
| 34 | name string |
| 35 | args args |
| 36 | want bool |
| 37 | }{ |
| 38 | { |
| 39 | name: "test", |
| 40 | args: args{ |
| 41 | statusCode: uint32(1004), |
| 42 | oper: of.CommandAdd, |
| 43 | }, |
| 44 | want: true, |
| 45 | }, |
| 46 | } |
| 47 | for _, tt := range tests { |
| 48 | t.Run(tt.name, func(t *testing.T) { |
| 49 | if got := isFlowOperSuccess(tt.args.statusCode, tt.args.oper); got != tt.want { |
| 50 | t.Errorf("isFlowOperSuccess() = %v, want %v", got, tt.want) |
| 51 | } |
| 52 | }) |
| 53 | } |
| 54 | } |
vinokuma | 04dc9f8 | 2023-07-31 15:47:49 +0530 | [diff] [blame] | 55 | |
| 56 | func TestAddFlowsTask_Start(t *testing.T) { |
| 57 | type args struct { |
| 58 | ctx context.Context |
| 59 | taskID uint8 |
| 60 | } |
| 61 | tests := []struct { |
| 62 | name string |
| 63 | args args |
| 64 | wantErr bool |
| 65 | }{ |
| 66 | { |
| 67 | name: "AddFlowsTask_Start", |
| 68 | args: args{ |
| 69 | ctx: context.Background(), |
| 70 | taskID: 0, |
| 71 | }, |
| 72 | wantErr: false, |
| 73 | }, |
| 74 | { |
| 75 | name: "DeleteFlowsTask_Start", |
| 76 | args: args{ |
| 77 | ctx: context.Background(), |
| 78 | taskID: 0, |
| 79 | }, |
| 80 | wantErr: false, |
| 81 | }, |
| 82 | } |
| 83 | for _, tt := range tests { |
| 84 | t.Run(tt.name, func(t *testing.T) { |
| 85 | switch tt.name { |
| 86 | case "AddFlowsTask_Start": |
| 87 | subFlows := map[uint64]*of.VoltSubFlow{} |
| 88 | vltSubFlow := &of.VoltSubFlow{ |
| 89 | Priority: 100, |
| 90 | Cookie: 103112802816, |
| 91 | State: of.FlowAddSuccess, |
| 92 | Match: of.Match{ |
| 93 | InPort: 1573376, |
| 94 | MatchVlan: 4096, |
| 95 | L4Protocol: 255, |
| 96 | }, |
| 97 | Action: of.Action{ |
| 98 | Metadata: 279189651712, |
| 99 | GoToTableID: 1, |
| 100 | MeterID: 1, |
| 101 | SetVlan: 4097, |
| 102 | Pcp: 8, |
| 103 | Output: 4, |
| 104 | }, |
| 105 | } |
| 106 | subFlows[0] = vltSubFlow |
| 107 | portsByID := map[uint32]*DevicePort{} |
| 108 | portsByID[256] = &DevicePort{ |
| 109 | Name: "SDX6320031", |
| 110 | ID: 256, |
| 111 | } |
| 112 | device := &Device{ |
| 113 | flows: subFlows, |
| 114 | PortsByID: portsByID, |
| 115 | } |
| 116 | flow := &of.VoltFlow{ |
| 117 | SubFlows: subFlows, |
| 118 | PortName: "SDX6320031-1", |
| 119 | PortID: 256, |
| 120 | Command: 0, |
| 121 | } |
| 122 | aft := &AddFlowsTask{ |
| 123 | flow: flow, |
| 124 | device: device, |
| 125 | } |
| 126 | dbintf := mocks.NewMockDBIntf(gomock.NewController(t)) |
| 127 | db = dbintf |
| 128 | dbintf.EXPECT().PutFlow(gomock.Any(), gomock.Any(), gomock.Any(), gomock.Any()).Return(nil).AnyTimes() |
| 129 | if err := aft.Start(tt.args.ctx, tt.args.taskID); (err != nil) != tt.wantErr { |
| 130 | t.Errorf("AddFlowsTask.Start() error = %v, wantErr %v", err, tt.wantErr) |
| 131 | } |
| 132 | case "DeleteFlowsTask_Start": |
| 133 | subFlows := map[uint64]*of.VoltSubFlow{} |
| 134 | vltSubFlow := &of.VoltSubFlow{ |
| 135 | Priority: 100, |
| 136 | Cookie: 103112802816, |
| 137 | State: of.FlowAddSuccess, |
| 138 | Match: of.Match{ |
| 139 | InPort: 1573376, |
| 140 | MatchVlan: 4096, |
| 141 | L4Protocol: 255, |
| 142 | }, |
| 143 | Action: of.Action{ |
| 144 | Metadata: 279189651712, |
| 145 | GoToTableID: 1, |
| 146 | MeterID: 1, |
| 147 | SetVlan: 4097, |
| 148 | Pcp: 8, |
| 149 | Output: 4, |
| 150 | }, |
| 151 | } |
| 152 | subFlows[0] = vltSubFlow |
| 153 | portsByID := map[uint32]*DevicePort{} |
| 154 | portsByID[256] = &DevicePort{ |
| 155 | Name: "SDX6320031", |
| 156 | ID: 256, |
| 157 | } |
| 158 | device := &Device{ |
| 159 | flows: subFlows, |
| 160 | PortsByID: portsByID, |
| 161 | } |
| 162 | flow := &of.VoltFlow{ |
| 163 | SubFlows: subFlows, |
| 164 | PortName: "SDX6320031-1", |
| 165 | PortID: 256, |
| 166 | Command: 1, |
| 167 | } |
| 168 | aft := &AddFlowsTask{ |
| 169 | flow: flow, |
| 170 | device: device, |
| 171 | } |
| 172 | appMock := mocks.NewMockApp(gomock.NewController(t)) |
| 173 | NewController(ctx, appMock) |
| 174 | appMock.EXPECT().ProcessFlowModResultIndication(gomock.Any(), gomock.Any()).AnyTimes() |
| 175 | dbintf := mocks.NewMockDBIntf(gomock.NewController(t)) |
| 176 | db = dbintf |
| 177 | dbintf.EXPECT().DelFlow(gomock.Any(), gomock.Any(), gomock.Any()).Return(nil).AnyTimes() |
| 178 | if err := aft.Start(tt.args.ctx, tt.args.taskID); (err != nil) != tt.wantErr { |
| 179 | t.Errorf("AddFlowsTask.Start() error = %v, wantErr %v", err, tt.wantErr) |
| 180 | } |
| 181 | } |
| 182 | }) |
| 183 | } |
| 184 | } |
| 185 | |
| 186 | func TestNewAddFlowsTask(t *testing.T) { |
| 187 | type args struct { |
| 188 | ctx context.Context |
| 189 | flow *of.VoltFlow |
| 190 | device *Device |
| 191 | } |
| 192 | flow := &of.VoltFlow{ |
| 193 | PortName: "SDX6320031-1", |
| 194 | PortID: 256, |
| 195 | Command: 0, |
| 196 | } |
| 197 | portsByID := map[uint32]*DevicePort{} |
| 198 | portsByID[256] = &DevicePort{ |
| 199 | Name: "SDX6320031", |
| 200 | ID: 256, |
| 201 | } |
| 202 | device := &Device{ |
| 203 | PortsByID: portsByID, |
| 204 | } |
| 205 | |
| 206 | tests := []struct { |
| 207 | name string |
| 208 | args args |
| 209 | want *AddFlowsTask |
| 210 | }{ |
| 211 | { |
| 212 | name: "NewAddFlowsTask", |
| 213 | args: args{ |
| 214 | ctx: context.Background(), |
| 215 | flow: flow, |
| 216 | device: device, |
| 217 | }, |
| 218 | want: &AddFlowsTask{ |
| 219 | ctx: context.Background(), |
| 220 | flow: flow, |
| 221 | device: device, |
| 222 | }, |
| 223 | }, |
| 224 | } |
| 225 | for _, tt := range tests { |
| 226 | t.Run(tt.name, func(t *testing.T) { |
| 227 | if got := NewAddFlowsTask(tt.args.ctx, tt.args.flow, tt.args.device); reflect.DeepEqual(got, tt.want) { |
| 228 | t.Errorf("NewAddFlowsTask() = %v, want %v", got, tt.want) |
| 229 | } |
| 230 | }) |
| 231 | } |
| 232 | } |