| 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 | "sync" |
| 21 | "testing" |
| 22 | "voltha-go-controller/internal/pkg/holder" |
| 23 | "voltha-go-controller/internal/pkg/of" |
| 24 | "voltha-go-controller/internal/test/mocks" |
| 25 | |
| bseeniva | dd66c36 | 2026-02-12 19:13:26 +0530 | [diff] [blame^] | 26 | "go.uber.org/mock/gomock" |
| 27 | |
| Akash Soni | d36d23b | 2023-08-18 12:51:40 +0530 | [diff] [blame] | 28 | "github.com/stretchr/testify/assert" |
| 29 | ) |
| 30 | |
| 31 | func TestModMeterTask_Start(t *testing.T) { |
| 32 | type args struct { |
| 33 | ctx context.Context |
| 34 | taskID uint8 |
| 35 | } |
| 36 | tests := []struct { |
| 37 | name string |
| 38 | args args |
| 39 | wantErr bool |
| 40 | }{ |
| 41 | { |
| 42 | name: "mmt.command == of.MeterCommandAdd", |
| 43 | args: args{ |
| 44 | ctx: context.Background(), |
| 45 | taskID: uint8(1), |
| 46 | }, |
| 47 | }, |
| 48 | } |
| 49 | for _, tt := range tests { |
| 50 | t.Run(tt.name, func(t *testing.T) { |
| 51 | meters := make(map[uint32]*of.Meter) |
| 52 | meters[uint32(1)] = &of.Meter{ |
| 53 | ID: uint32(1), |
| 54 | } |
| 55 | volthaClientMock := mocks.NewMockVolthaServiceClient(gomock.NewController(t)) |
| 56 | mmt := &ModMeterTask{ |
| 57 | meter: &of.Meter{ |
| 58 | ID: uint32(1), |
| 59 | }, |
| 60 | device: &Device{ |
| 61 | meterLock: sync.RWMutex{}, |
| 62 | meters: meters, |
| 63 | State: DeviceStateUP, |
| 64 | vclientHolder: &holder.VolthaServiceClientHolder{ |
| 65 | VolthaSvcClient: volthaClientMock, |
| 66 | }, |
| 67 | }, |
| 68 | } |
| 69 | mmt.meter.ID = uint32(2) |
| bseeniva | dd66c36 | 2026-02-12 19:13:26 +0530 | [diff] [blame^] | 70 | // Avoid assigning a package-level mock DB here; it can be |
| 71 | // invoked by background goroutines from other tests after this |
| 72 | // test completes and cause a panic. The ModMeterTask path under |
| 73 | // test does not require DB interactions. |
| Akash Soni | d36d23b | 2023-08-18 12:51:40 +0530 | [diff] [blame] | 74 | volthaClientMock.EXPECT().UpdateLogicalDeviceMeterTable(gomock.Any(), gomock.Any()).Return(nil, nil).AnyTimes() |
| 75 | err := mmt.Start(tt.args.ctx, tt.args.taskID) |
| 76 | assert.Nil(t, err) |
| 77 | }) |
| 78 | } |
| 79 | } |
| 80 | |
| 81 | func TestNewModMeterTask(t *testing.T) { |
| 82 | type args struct { |
| 83 | ctx context.Context |
| 84 | command of.MeterCommand |
| 85 | meter *of.Meter |
| 86 | device *Device |
| 87 | } |
| 88 | tests := []struct { |
| 89 | name string |
| 90 | args args |
| 91 | want *ModMeterTask |
| 92 | }{ |
| 93 | { |
| 94 | name: "NewModMeterTask", |
| 95 | args: args{ |
| 96 | ctx: context.Background(), |
| 97 | command: of.MeterCommandAdd, |
| 98 | meter: &of.Meter{ |
| 99 | ID: uint32(1), |
| 100 | }, |
| 101 | }, |
| 102 | }, |
| 103 | } |
| 104 | for _, tt := range tests { |
| 105 | t.Run(tt.name, func(t *testing.T) { |
| 106 | got := NewModMeterTask(tt.args.ctx, tt.args.command, tt.args.meter, tt.args.device) |
| 107 | assert.NotNil(t, got) |
| 108 | }) |
| 109 | } |
| 110 | } |
| 111 | |
| 112 | func TestModMeterTask_Name(t *testing.T) { |
| 113 | tests := []struct { |
| 114 | name string |
| 115 | want string |
| 116 | }{ |
| 117 | { |
| 118 | name: "ModMeterTask_Name", |
| 119 | want: "Add Flows Task", |
| 120 | }, |
| 121 | } |
| 122 | for _, tt := range tests { |
| 123 | t.Run(tt.name, func(t *testing.T) { |
| 124 | mmt := &ModMeterTask{} |
| 125 | if got := mmt.Name(); got != tt.want { |
| 126 | t.Errorf("ModMeterTask.Name() = %v, want %v", got, tt.want) |
| 127 | } |
| 128 | }) |
| 129 | } |
| 130 | } |
| 131 | |
| 132 | func TestModMeterTask_TaskID(t *testing.T) { |
| 133 | tests := []struct { |
| 134 | name string |
| 135 | want uint8 |
| 136 | }{ |
| 137 | { |
| 138 | name: "ModMeterTask_TaskID", |
| 139 | }, |
| 140 | } |
| 141 | for _, tt := range tests { |
| 142 | t.Run(tt.name, func(t *testing.T) { |
| 143 | mmt := &ModMeterTask{} |
| 144 | if got := mmt.TaskID(); got != tt.want { |
| 145 | t.Errorf("ModMeterTask.TaskID() = %v, want %v", got, tt.want) |
| 146 | } |
| 147 | }) |
| 148 | } |
| 149 | } |
| 150 | |
| 151 | func TestModMeterTask_Timestamp(t *testing.T) { |
| 152 | tests := []struct { |
| 153 | name string |
| 154 | want string |
| 155 | }{ |
| 156 | { |
| 157 | name: "ModMeterTask_Timestamp", |
| 158 | }, |
| 159 | } |
| 160 | for _, tt := range tests { |
| 161 | t.Run(tt.name, func(t *testing.T) { |
| 162 | mmt := &ModMeterTask{} |
| 163 | if got := mmt.Timestamp(); got != tt.want { |
| 164 | t.Errorf("ModMeterTask.Timestamp() = %v, want %v", got, tt.want) |
| 165 | } |
| 166 | }) |
| 167 | } |
| 168 | } |
| 169 | |
| 170 | func TestModMeterTask_Stop(t *testing.T) { |
| 171 | tests := []struct { |
| 172 | name string |
| 173 | }{ |
| 174 | { |
| 175 | name: "ModMeterTask_Stop", |
| 176 | }, |
| 177 | } |
| 178 | for _, tt := range tests { |
| 179 | t.Run(tt.name, func(t *testing.T) { |
| 180 | mmt := &ModMeterTask{} |
| 181 | mmt.Stop() |
| 182 | }) |
| 183 | } |
| 184 | } |