Akash Soni | 9fad736 | 2023-10-03 12:19:37 +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 application |
| 17 | |
| 18 | import ( |
| 19 | "context" |
| 20 | "net" |
| 21 | "reflect" |
| 22 | "testing" |
| 23 | "time" |
| 24 | "voltha-go-controller/internal/pkg/of" |
| 25 | "voltha-go-controller/internal/test/mocks" |
| 26 | |
| 27 | "github.com/golang/mock/gomock" |
| 28 | "github.com/stretchr/testify/assert" |
| 29 | ) |
| 30 | |
| 31 | func TestNewIgmpGroup(t *testing.T) { |
| 32 | type args struct { |
| 33 | name string |
| 34 | vlan of.VlanType |
| 35 | } |
| 36 | group := &IgmpGroup{ |
| 37 | GroupName: "test_key", |
| 38 | } |
| 39 | tests := []struct { |
| 40 | name string |
| 41 | args args |
| 42 | want *IgmpGroup |
| 43 | }{ |
| 44 | { |
| 45 | name: "NewIgmpGroup", |
| 46 | args: args{ |
| 47 | name: "test_key", |
| 48 | }, |
| 49 | want: group, |
| 50 | }, |
| 51 | } |
| 52 | for _, tt := range tests { |
| 53 | t.Run(tt.name, func(t *testing.T) { |
| 54 | got := NewIgmpGroup(tt.args.name, tt.args.vlan) |
| 55 | assert.NotNil(t, got) |
| 56 | }) |
| 57 | } |
| 58 | } |
| 59 | |
| 60 | func TestIgmpGroup_IgmpGroupInit(t *testing.T) { |
| 61 | type args struct { |
| 62 | name string |
| 63 | gip net.IP |
| 64 | mvp *MvlanProfile |
| 65 | } |
| 66 | grp := make(map[string]*MvlanGroup) |
| 67 | grp["test_key"] = &MvlanGroup{ |
| 68 | Name: "test_key", |
| 69 | IsStatic: true, |
| 70 | } |
| 71 | tests := []struct { |
| 72 | name string |
| 73 | args args |
| 74 | }{ |
| 75 | { |
| 76 | name: "IgmpGroupInit", |
| 77 | args: args{ |
| 78 | name: "test_key", |
| 79 | gip: AllSystemsMulticastGroupIP, |
| 80 | mvp: &MvlanProfile{ |
| 81 | Version: "test_version", |
| 82 | Name: "test_key", |
| 83 | Groups: grp, |
| 84 | }, |
| 85 | }, |
| 86 | }, |
| 87 | } |
| 88 | for _, tt := range tests { |
| 89 | t.Run(tt.name, func(t *testing.T) { |
| 90 | ig := &IgmpGroup{} |
| 91 | ig.IgmpGroupInit(tt.args.name, tt.args.gip, tt.args.mvp) |
| 92 | }) |
| 93 | } |
| 94 | } |
| 95 | |
| 96 | func TestIgmpGroup_IgmpGroupReInit(t *testing.T) { |
| 97 | type args struct { |
| 98 | cntx context.Context |
| 99 | name string |
| 100 | gip net.IP |
| 101 | } |
| 102 | tests := []struct { |
| 103 | name string |
| 104 | args args |
| 105 | }{ |
| 106 | { |
| 107 | name: "IgmpGroupInit", |
| 108 | args: args{ |
| 109 | cntx: context.Background(), |
| 110 | name: "test_key", |
| 111 | gip: AllSystemsMulticastGroupIP, |
| 112 | }, |
| 113 | }, |
| 114 | } |
| 115 | for _, tt := range tests { |
| 116 | t.Run(tt.name, func(t *testing.T) { |
| 117 | ig := &IgmpGroup{} |
| 118 | ig.IgmpGroupReInit(tt.args.cntx, tt.args.name, tt.args.gip) |
| 119 | }) |
| 120 | } |
| 121 | } |
| 122 | |
| 123 | func TestIgmpGroup_DeleteIgmpGroupDevice(t *testing.T) { |
| 124 | type args struct { |
| 125 | cntx context.Context |
| 126 | device string |
| 127 | } |
| 128 | devices := map[string]*IgmpGroupDevice{} |
| 129 | igmpDevice := &IgmpGroupDevice{ |
| 130 | Device: "SDX6320031", |
| 131 | SerialNo: "SDX6320031", |
| 132 | GroupName: "group1", |
| 133 | Mvlan: of.VlanAny, |
| 134 | } |
| 135 | devices["SDX6320031"] = igmpDevice |
| 136 | tests := []struct { |
| 137 | name string |
| 138 | args args |
| 139 | }{ |
| 140 | { |
| 141 | name: "DeleteIgmpGroupDevice", |
| 142 | args: args{ |
| 143 | cntx: context.Background(), |
| 144 | device: "SDX6320031", |
| 145 | }, |
| 146 | }, |
| 147 | } |
| 148 | for _, tt := range tests { |
| 149 | t.Run(tt.name, func(t *testing.T) { |
| 150 | ig := &IgmpGroup{ |
| 151 | Devices: devices, |
| 152 | } |
| 153 | dbintf := mocks.NewMockDBIntf(gomock.NewController(t)) |
| 154 | db = dbintf |
| 155 | dbintf.EXPECT().DelIgmpDevice(gomock.Any(), gomock.Any(), gomock.Any(), gomock.Any(), gomock.Any()).AnyTimes() |
| 156 | ig.DeleteIgmpGroupDevice(tt.args.cntx, tt.args.device) |
| 157 | }) |
| 158 | } |
| 159 | } |
| 160 | |
| 161 | func TestIgmpGroup_removeExpiredGroupFromDevice(t *testing.T) { |
| 162 | type args struct { |
| 163 | cntx context.Context |
| 164 | } |
| 165 | PendingGroupForDevice := make(map[string]time.Time) |
| 166 | PendingGroupForDevice["SDX6320031"] = time.Now().Add(time.Duration(GroupExpiryTime) * time.Minute) |
| 167 | tests := []struct { |
| 168 | name string |
| 169 | args args |
| 170 | }{ |
| 171 | { |
| 172 | name: "DeleteIgmpGroupDevice", |
| 173 | args: args{ |
| 174 | cntx: context.Background(), |
| 175 | }, |
| 176 | }, |
| 177 | } |
| 178 | for _, tt := range tests { |
| 179 | t.Run(tt.name, func(t *testing.T) { |
| 180 | ig := &IgmpGroup{ |
| 181 | PendingGroupForDevice: PendingGroupForDevice, |
| 182 | } |
| 183 | ig.removeExpiredGroupFromDevice(tt.args.cntx) |
| 184 | }) |
| 185 | } |
| 186 | } |
| 187 | |
| 188 | func TestIgmpGroup_GetAllIgmpChannel(t *testing.T) { |
| 189 | devices := map[string]*IgmpGroupDevice{} |
| 190 | igmpDevice := &IgmpGroupDevice{ |
| 191 | Device: "SDX6320031", |
| 192 | SerialNo: "SDX6320031", |
| 193 | GroupName: "group1", |
| 194 | Mvlan: of.VlanAny, |
| 195 | } |
| 196 | devices["SDX6320031"] = igmpDevice |
| 197 | tests := []struct { |
| 198 | name string |
| 199 | want map[string]string |
| 200 | }{ |
| 201 | { |
| 202 | name: "GetAllIgmpChannel", |
| 203 | want: make(map[string]string), |
| 204 | }, |
| 205 | } |
| 206 | for _, tt := range tests { |
| 207 | t.Run(tt.name, func(t *testing.T) { |
| 208 | ig := &IgmpGroup{ |
| 209 | Devices: devices, |
| 210 | } |
| 211 | if got := ig.GetAllIgmpChannel(); !reflect.DeepEqual(got, tt.want) { |
| 212 | t.Errorf("IgmpGroup.GetAllIgmpChannel() = %v, want %v", got, tt.want) |
| 213 | } |
| 214 | }) |
| 215 | } |
| 216 | } |
| 217 | |
| 218 | func TestIgmpGroup_GetAllIgmpChannelForDevice(t *testing.T) { |
| 219 | type args struct { |
| 220 | deviceID string |
| 221 | } |
| 222 | devices := map[string]*IgmpGroupDevice{} |
| 223 | igmpDevice := &IgmpGroupDevice{ |
| 224 | Device: "SDX6320031", |
| 225 | SerialNo: "SDX6320031", |
| 226 | GroupName: "group1", |
| 227 | Mvlan: of.VlanAny, |
| 228 | } |
| 229 | devices["SDX6320031"] = igmpDevice |
| 230 | tests := []struct { |
| 231 | name string |
| 232 | args args |
| 233 | want map[string]string |
| 234 | }{ |
| 235 | { |
| 236 | name: "GetAllIgmpChannelForDevice", |
| 237 | args: args{ |
| 238 | deviceID: "SDX6320031", |
| 239 | }, |
| 240 | want: make(map[string]string), |
| 241 | }, |
| 242 | } |
| 243 | for _, tt := range tests { |
| 244 | t.Run(tt.name, func(t *testing.T) { |
| 245 | ig := &IgmpGroup{ |
| 246 | Devices: devices, |
| 247 | } |
| 248 | if got := ig.GetAllIgmpChannelForDevice(tt.args.deviceID); !reflect.DeepEqual(got, tt.want) { |
| 249 | t.Errorf("IgmpGroup.GetAllIgmpChannelForDevice() = %v, want %v", got, tt.want) |
| 250 | } |
| 251 | }) |
| 252 | } |
| 253 | } |