VOL-1838 Unit test cases for golang openolt adapter device_handler.go
1.Mocked AdapterproxyIntf and coreproxyIntf Interfaces for unit test cases.
2. Added few unit testcases for the device_handler.go functions
Change-Id: I793d94055d2c0480e614e918c064df51cdf0b0ae
diff --git a/adaptercore/device_handler.go b/adaptercore/device_handler.go
index 52e4afd..95bc170 100644
--- a/adaptercore/device_handler.go
+++ b/adaptercore/device_handler.go
@@ -33,7 +33,7 @@
"github.com/gogo/protobuf/proto"
"github.com/golang/protobuf/ptypes"
- com "github.com/opencord/voltha-go/adapters/common"
+ "github.com/opencord/voltha-go/adapters/adapterif"
"github.com/opencord/voltha-go/common/log"
rsrcMgr "github.com/opencord/voltha-openolt-adapter/adaptercore/resourcemanager"
"github.com/opencord/voltha-protos/go/common"
@@ -51,15 +51,19 @@
MaxTimeOutInMs = 500
)
+func init() {
+ _, _ = log.AddPackage(log.JSON, log.DebugLevel, nil)
+}
+
//DeviceHandler will interact with the OLT device.
type DeviceHandler struct {
deviceID string
deviceType string
adminState string
device *voltha.Device
- coreProxy *com.CoreProxy
- AdapterProxy *com.AdapterProxy
- EventProxy *com.EventProxy
+ coreProxy adapterif.CoreProxy
+ AdapterProxy adapterif.AdapterProxy
+ EventProxy adapterif.EventProxy
openOLT *OpenOLT
exitChannel chan int
lockDevice sync.RWMutex
@@ -97,7 +101,7 @@
}
//NewDeviceHandler creates a new device handler
-func NewDeviceHandler(cp *com.CoreProxy, ap *com.AdapterProxy, ep *com.EventProxy, device *voltha.Device, adapter *OpenOLT) *DeviceHandler {
+func NewDeviceHandler(cp adapterif.CoreProxy, ap adapterif.AdapterProxy, ep adapterif.EventProxy, device *voltha.Device, adapter *OpenOLT) *DeviceHandler {
var dh DeviceHandler
dh.coreProxy = cp
dh.AdapterProxy = ap
diff --git a/adaptercore/device_handler_test.go b/adaptercore/device_handler_test.go
new file mode 100644
index 0000000..07d8704
--- /dev/null
+++ b/adaptercore/device_handler_test.go
@@ -0,0 +1,144 @@
+/*
+ * Copyright 2018-present Open Networking Foundation
+
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+
+ * http://www.apache.org/licenses/LICENSE-2.0
+
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+//Package adaptercore provides the utility for olt devices, flows and statistics
+package adaptercore
+
+import (
+ "net"
+ "testing"
+
+ "github.com/opencord/voltha-openolt-adapter/mocks"
+ "github.com/opencord/voltha-protos/go/voltha"
+)
+
+func newMockDeviceDeviceHandler() *DeviceHandler {
+ device := &voltha.Device{
+ Id: "olt",
+ Root: true,
+ ParentId: "logical_device",
+ Ports: []*voltha.Port{
+ {PortNo: 1, Label: "pon"},
+ {PortNo: 2, Label: "nni"},
+ },
+ }
+ return &DeviceHandler{
+ deviceID: device.GetId(),
+
+ device: device,
+ coreProxy: &mocks.MockCoreProxy{},
+ AdapterProxy: &mocks.MockAdapterProxy{},
+ }
+}
+
+func Test_generateMacFromHost(t *testing.T) {
+ type args struct {
+ host string
+ }
+ tests := []struct {
+ name string
+ args args
+ want string
+ wantErr bool
+ }{
+ {"test1", args{host: "localhost"}, "00:00:7f:00:00:01", false},
+ {"test2", args{host: "10.10.10.10"}, "00:00:0a:0a:0a:0a", false},
+ }
+ for _, tt := range tests {
+ t.Run(tt.name, func(t *testing.T) {
+ got, err := generateMacFromHost(tt.args.host)
+ if (err != nil) != tt.wantErr {
+ t.Errorf("generateMacFromHost() error = %v, wantErr %v", err, tt.wantErr)
+ return
+ }
+ if got != tt.want {
+ t.Errorf("generateMacFromHost() = %v, want %v", got, tt.want)
+ }
+ })
+ }
+}
+func Test_macifyIP(t *testing.T) {
+ type args struct {
+ ip net.IP
+ }
+ tests := []struct {
+ name string
+ args args
+ want string
+ }{{
+ "test1",
+ args{ip: net.ParseIP("10.10.10.10")},
+ "00:00:0a:0a:0a:0a",
+ },
+ {
+ "test3",
+ args{ip: net.ParseIP("127.0.0.1")},
+ "00:00:7f:00:00:01",
+ }}
+ for _, tt := range tests {
+ t.Run(tt.name, func(t *testing.T) {
+ if got := macifyIP(tt.args.ip); got != tt.want {
+ t.Errorf("macifyIP() = %v, want %v", got, tt.want)
+ }
+ })
+ }
+}
+
+func TestDeviceHandler_GetOfpDeviceInfo(t *testing.T) {
+ dh := newMockDeviceDeviceHandler()
+ device := &voltha.Device{}
+ got, err := dh.GetOfpDeviceInfo(device)
+ if err != nil {
+ t.Errorf("DeviceHandler.GetOfpDeviceInfo() error = %v", err)
+ return
+ }
+ t.Logf("ofpDeviceInfo %v", got)
+}
+
+func TestDeviceHandler_GetOfpPortInfo(t *testing.T) {
+ dh := newMockDeviceDeviceHandler()
+ device := &voltha.Device{}
+ got, err := dh.GetOfpPortInfo(device, 1)
+ if err != nil {
+ t.Errorf("DeviceHandler.GetOfpPortInfo() error = %v", err)
+ return
+ }
+ t.Logf("ofpDeviceInfo %v", got)
+}
+func TestDeviceHandler_GetChildDevice(t *testing.T) {
+ dh := newMockDeviceDeviceHandler()
+ type args struct {
+ parentPort uint32
+ onuID uint32
+ }
+ tests := []struct {
+ name string
+ args args
+ want *voltha.Device
+ }{
+ {"test1",
+ args{parentPort: 1,
+ onuID: 1},
+ &voltha.Device{},
+ },
+ }
+ for _, tt := range tests {
+ t.Run(tt.name, func(t *testing.T) {
+ got := dh.GetChildDevice(tt.args.parentPort, tt.args.onuID)
+ t.Log("onu device id", got)
+ })
+ }
+}
diff --git a/adaptercore/openolt.go b/adaptercore/openolt.go
index 0da66ff..33ac5d5 100644
--- a/adaptercore/openolt.go
+++ b/adaptercore/openolt.go
@@ -23,7 +23,7 @@
"fmt"
"sync"
- com "github.com/opencord/voltha-go/adapters/common"
+ "github.com/opencord/voltha-go/adapters/adapterif"
"github.com/opencord/voltha-go/common/log"
"github.com/opencord/voltha-go/kafka"
ic "github.com/opencord/voltha-protos/go/inter_container"
@@ -34,9 +34,9 @@
//OpenOLT structure holds the OLT information
type OpenOLT struct {
deviceHandlers map[string]*DeviceHandler
- coreProxy *com.CoreProxy
- adapterProxy *com.AdapterProxy
- eventProxy *com.EventProxy
+ coreProxy adapterif.CoreProxy
+ adapterProxy adapterif.AdapterProxy
+ eventProxy adapterif.EventProxy
kafkaICProxy *kafka.InterContainerProxy
numOnus int
KVStoreHost string
@@ -47,7 +47,10 @@
}
//NewOpenOLT returns a new instance of OpenOLT
-func NewOpenOLT(ctx context.Context, kafkaICProxy *kafka.InterContainerProxy, coreProxy *com.CoreProxy, adapterProxy *com.AdapterProxy, eventProxy *com.EventProxy, onuNumber int, kvStoreHost string, kvStorePort int, KVStoreType string) *OpenOLT {
+func NewOpenOLT(ctx context.Context, kafkaICProxy *kafka.InterContainerProxy,
+ coreProxy adapterif.CoreProxy, adapterProxy adapterif.AdapterProxy,
+ eventProxy adapterif.EventProxy, onuNumber int, kvStoreHost string,
+ kvStorePort int, KVStoreType string) *OpenOLT {
var openOLT OpenOLT
openOLT.exitChannel = make(chan int, 1)
openOLT.deviceHandlers = make(map[string]*DeviceHandler)
diff --git a/adaptercore/openolt_eventmgr.go b/adaptercore/openolt_eventmgr.go
index e1cb576..ba74f18 100644
--- a/adaptercore/openolt_eventmgr.go
+++ b/adaptercore/openolt_eventmgr.go
@@ -19,7 +19,8 @@
import (
"fmt"
- com "github.com/opencord/voltha-go/adapters/common"
+
+ "github.com/opencord/voltha-go/adapters/adapterif"
"github.com/opencord/voltha-go/common/log"
oop "github.com/opencord/voltha-protos/go/openolt"
"github.com/opencord/voltha-protos/go/voltha"
@@ -60,11 +61,11 @@
// OpenOltEventMgr struct contains
type OpenOltEventMgr struct {
- eventProxy *com.EventProxy
+ eventProxy adapterif.EventProxy
}
// NewEventMgr is a Function to get a new event manager struct for the OpenOLT to process and publish OpenOLT event
-func NewEventMgr(eventProxy *com.EventProxy) *OpenOltEventMgr {
+func NewEventMgr(eventProxy adapterif.EventProxy) *OpenOltEventMgr {
var em OpenOltEventMgr
em.eventProxy = eventProxy
return &em