blob: a6477e3e6c0e5b353d487e8f1fd70164dee3b0b6 [file] [log] [blame]
vinokuma04dc9f82023-07-31 15:47:49 +05301/*
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
16package controller
17
18import (
19 "context"
20 "testing"
21 "voltha-go-controller/internal/pkg/holder"
22 "voltha-go-controller/internal/pkg/of"
23 "voltha-go-controller/internal/test/mocks"
24
25 "github.com/golang/mock/gomock"
26 "github.com/opencord/voltha-protos/v5/go/openflow_13"
27 "github.com/opencord/voltha-protos/v5/go/voltha"
28)
29
30func TestAuditDevice_DelExcessPorts(t *testing.T) {
31 type args struct {
32 cntx context.Context
Sridhar Ravindra0bc5dc52023-12-13 19:03:30 +053033 eps map[uint32]*DevicePort
vinokuma04dc9f82023-07-31 15:47:49 +053034 }
35 subFlows := map[uint64]*of.VoltSubFlow{}
36 vltSubFlow := &of.VoltSubFlow{
37 Priority: 100,
38 Cookie: 103112802816,
39 State: of.FlowAddSuccess,
40 Match: of.Match{
41 InPort: 1573376,
42 MatchVlan: 4096,
43 L4Protocol: 255,
44 },
45 Action: of.Action{
46 Metadata: 279189651712,
47 GoToTableID: 1,
48 MeterID: 1,
49 SetVlan: 4097,
50 Pcp: 8,
51 Output: 4,
52 },
53 }
54 subFlows[0] = vltSubFlow
55 portsByID := map[uint32]*DevicePort{}
56 portsByID[256] = &DevicePort{
57 Name: "SDX6320031",
58 ID: 256,
59 State: PortStateUp,
60 }
Sridhar Ravindra0bc5dc52023-12-13 19:03:30 +053061 eps := make(map[uint32]*DevicePort)
vinokuma04dc9f82023-07-31 15:47:49 +053062 device := &Device{
63 flows: subFlows,
64 PortsByID: portsByID,
65 ID: "SDX6320031",
66 }
67 tests := []struct {
68 name string
69 args args
70 }{
71 {
72 name: "AddFlowsTask_Start",
73 args: args{
74 cntx: context.Background(),
75 eps: eps,
76 },
77 },
78 }
79 for _, tt := range tests {
80 t.Run(tt.name, func(t *testing.T) {
81 ad := &AuditDevice{
82 device: device,
83 }
84 appMock := mocks.NewMockApp(gomock.NewController(t))
85 NewController(ctx, appMock)
86 appMock.EXPECT().PortDownInd(gomock.Any(), gomock.Any(), gomock.Any()).AnyTimes()
87 appMock.EXPECT().PortDelInd(gomock.Any(), gomock.Any(), gomock.Any()).AnyTimes()
88 dbintf := mocks.NewMockDBIntf(gomock.NewController(t))
89 db = dbintf
90 dbintf.EXPECT().DelPort(gomock.Any(), gomock.Any(), gomock.Any()).Return(nil).AnyTimes()
91 ad.DelExcessPorts(tt.args.cntx, tt.args.eps)
92 })
93 }
94}
95
96func TestAuditDevice_Start(t *testing.T) {
97 type args struct {
98 ctx context.Context
99 taskID uint8
100 }
101 volthaClientMock := mocks.NewMockVolthaServiceClient(gomock.NewController(t))
102 volthaServiceClientHolder := &holder.VolthaServiceClientHolder{
103 VolthaSvcClient: volthaClientMock,
104 }
105 portsByID := map[uint32]*DevicePort{}
106 portsByID[16777216] = &DevicePort{
107 Name: "SDX6320031",
108 ID: 16777216,
109 State: PortStateUp,
110 }
111 device := &Device{
112 ID: "SDX6320031",
113 vclientHolder: volthaServiceClientHolder,
114 PortsByID: portsByID,
115 }
116 items := []*voltha.LogicalPort{}
117 item := &voltha.LogicalPort{
118 Id: "SDX6320031-1",
119 DeviceId: "SDX6320031",
120 DevicePortNo: 16777216,
121 OfpPort: &openflow_13.OfpPort{
122 PortNo: 16777216,
123 Name: "SDX6320031-1",
124 },
125 }
126 items = append(items, item)
127
128 ofpps := &voltha.LogicalPorts{
129 Items: items,
130 }
131 tests := []struct {
132 name string
133 args args
134 wantErr bool
135 }{
136 {
137 name: "AddFlowsTask_Start",
138 args: args{
139 ctx: context.Background(),
140 taskID: 0,
141 },
142 wantErr: false,
143 },
144 }
145 for _, tt := range tests {
146 t.Run(tt.name, func(t *testing.T) {
147 ad := &AuditDevice{
148 device: device,
149 }
150 volthaClientMock.EXPECT().ListLogicalDevicePorts(gomock.Any(), gomock.Any(), gomock.Any()).Return(ofpps, nil).AnyTimes()
151 appMock := mocks.NewMockApp(gomock.NewController(t))
152 NewController(ctx, appMock)
153 appMock.EXPECT().SetRebootFlag(gomock.Any()).AnyTimes()
154 appMock.EXPECT().PortDownInd(gomock.Any(), gomock.Any(), gomock.Any()).AnyTimes()
155 dbintf := mocks.NewMockDBIntf(gomock.NewController(t))
156 db = dbintf
157 dbintf.EXPECT().PutPort(gomock.Any(), gomock.Any(), gomock.Any(), gomock.Any()).Return(nil).AnyTimes()
158 if err := ad.Start(tt.args.ctx, tt.args.taskID); (err != nil) != tt.wantErr {
159 t.Errorf("AuditDevice.Start() error = %v, wantErr %v", err, tt.wantErr)
160 }
161 })
162 }
163}