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/Gopkg.lock b/Gopkg.lock
index 11f90bd..f0bef73 100644
--- a/Gopkg.lock
+++ b/Gopkg.lock
@@ -242,7 +242,7 @@
"rw_core/utils",
]
pruneopts = "UT"
- revision = "84169b5c5b9913aa612f3145b2a39c288ecd53d1"
+ revision = "4c9e559d974d5a8cf30e9ba6425547303b9a5d0d"
[[projects]]
digest = "1:78a853c38b4935f01a99fbb41edbb4382a23586f929ecf02dd80d4452f2e3c0d"
@@ -531,8 +531,10 @@
analyzer-version = 1
input-imports = [
"github.com/gogo/protobuf/proto",
+ "github.com/golang/protobuf/proto",
"github.com/golang/protobuf/ptypes",
"github.com/opencord/voltha-go/adapters",
+ "github.com/opencord/voltha-go/adapters/adapterif",
"github.com/opencord/voltha-go/adapters/common",
"github.com/opencord/voltha-go/common/log",
"github.com/opencord/voltha-go/common/ponresourcemanager",
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
diff --git a/main.go b/main.go
index 30efa84..af7ca2e 100644
--- a/main.go
+++ b/main.go
@@ -21,6 +21,14 @@
"context"
"errors"
"fmt"
+ "os"
+ "os/signal"
+ "strconv"
+ "syscall"
+ "time"
+
+ "github.com/opencord/voltha-go/adapters/adapterif"
+
"github.com/opencord/voltha-go/adapters"
com "github.com/opencord/voltha-go/adapters/common"
"github.com/opencord/voltha-go/common/log"
@@ -31,11 +39,6 @@
"github.com/opencord/voltha-openolt-adapter/config/version"
ic "github.com/opencord/voltha-protos/go/inter_container"
"github.com/opencord/voltha-protos/go/voltha"
- "os"
- "os/signal"
- "strconv"
- "syscall"
- "time"
)
type adapter struct {
@@ -45,9 +48,9 @@
kafkaClient kafka.Client
kvClient kvstore.Client
kip *kafka.InterContainerProxy
- coreProxy *com.CoreProxy
- adapterProxy *com.AdapterProxy
- eventProxy *com.EventProxy
+ coreProxy adapterif.CoreProxy
+ adapterProxy adapterif.AdapterProxy
+ eventProxy adapterif.EventProxy
halted bool
exitChannel chan int
receiverChannels []<-chan *ic.InterContainerMessage
@@ -97,7 +100,8 @@
a.eventProxy = com.NewEventProxy(com.MsgClient(a.kafkaClient), com.MsgTopic(kafka.Topic{Name: a.config.EventTopic}))
// Create the open OLT adapter
- if a.iAdapter, err = a.startOpenOLT(ctx, a.kip, a.coreProxy, a.adapterProxy, a.eventProxy, a.config.OnuNumber,
+ if a.iAdapter, err = a.startOpenOLT(ctx, a.kip, a.coreProxy, a.adapterProxy, a.eventProxy,
+ a.config.OnuNumber,
a.config.KVStoreHost, a.config.KVStorePort, a.config.KVStoreType); err != nil {
log.Fatal("error-starting-inter-container-proxy")
}
@@ -207,7 +211,9 @@
return kip, nil
}
-func (a *adapter) startOpenOLT(ctx context.Context, kip *kafka.InterContainerProxy, cp *com.CoreProxy, ap *com.AdapterProxy, ep *com.EventProxy, onuNumber int, kvStoreHost string, kvStorePort int, KVStoreType string) (*ac.OpenOLT, error) {
+func (a *adapter) startOpenOLT(ctx context.Context, kip *kafka.InterContainerProxy,
+ cp adapterif.CoreProxy, ap adapterif.AdapterProxy, ep adapterif.EventProxy, onuNumber int, kvStoreHost string,
+ kvStorePort int, KVStoreType string) (*ac.OpenOLT, error) {
log.Info("starting-open-olt")
var err error
sOLT := ac.NewOpenOLT(ctx, a.kip, cp, ap, ep, onuNumber, kvStoreHost, kvStorePort, KVStoreType)
diff --git a/mocks/mockAdapterProxy.go b/mocks/mockAdapterProxy.go
new file mode 100644
index 0000000..a91d361
--- /dev/null
+++ b/mocks/mockAdapterProxy.go
@@ -0,0 +1,47 @@
+/*
+ * 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 mocks provides the mocks for openolt-adapter.
+package mocks
+
+import (
+ "context"
+ "errors"
+
+ "github.com/golang/protobuf/proto"
+ "github.com/opencord/voltha-protos/go/inter_container"
+)
+
+// MockAdapterProxy mocks the AdapterProxy interface.
+type MockAdapterProxy struct {
+}
+
+// SendInterAdapterMessage mocks SendInterAdapterMessage function.
+func (ma *MockAdapterProxy) SendInterAdapterMessage(ctx context.Context,
+ msg proto.Message,
+ msgType inter_container.InterAdapterMessageType_Types,
+ fromAdapter string,
+ toAdapter string,
+ toDeviceID string,
+ proxyDeviceID string,
+ messageID string) error {
+ //panic("implement me")
+ if ctx == nil || msg == nil || fromAdapter != "" ||
+ toAdapter != "" || toDeviceID != "" || proxyDeviceID != "" || messageID != "" {
+ return errors.New("sendInterAdapterMessage func parameters cannot be nil")
+ }
+ return nil
+}
diff --git a/mocks/mockCoreProxy.go b/mocks/mockCoreProxy.go
new file mode 100644
index 0000000..6abf628
--- /dev/null
+++ b/mocks/mockCoreProxy.go
@@ -0,0 +1,134 @@
+/*
+ * 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 mocks provides the mocks for openolt-adapter.
+package mocks
+
+import (
+ "context"
+ "errors"
+
+ "github.com/opencord/voltha-go/kafka"
+ "github.com/opencord/voltha-protos/go/voltha"
+)
+
+// MockCoreProxy mocks the CoreProxy interface
+type MockCoreProxy struct {
+ // Values to be used in test can reside inside this structure
+ // TODO store relevant info in this, use this info for negative and positive tests
+}
+
+// UpdateCoreReference mock updatesCoreReference
+func (mp *MockCoreProxy) UpdateCoreReference(deviceID string, coreReference string) {
+ panic("implement me")
+}
+
+// DeleteCoreReference mock DeleteCoreReference function
+func (mp *MockCoreProxy) DeleteCoreReference(deviceID string) {
+ panic("implement me")
+}
+
+// GetCoreTopic implements mock GetCoreTopic
+func (mp *MockCoreProxy) GetCoreTopic(deviceID string) kafka.Topic {
+ panic("implement me")
+}
+
+// GetAdapterTopic implements mock GetAdapterTopic
+func (mp *MockCoreProxy) GetAdapterTopic(args ...string) kafka.Topic {
+ panic("implement me")
+}
+
+// RegisterAdapter implements mock RegisterAdapter
+func (mp *MockCoreProxy) RegisterAdapter(ctx context.Context, adapter *voltha.Adapter,
+ deviceTypes *voltha.DeviceTypes) error {
+ if ctx == nil || adapter == nil || deviceTypes == nil {
+
+ return errors.New("registerAdapter func parameters cannot be nil")
+ }
+ return nil
+
+}
+
+// DeviceUpdate implements mock DeviceUpdate
+func (mp *MockCoreProxy) DeviceUpdate(ctx context.Context, device *voltha.Device) error {
+ panic("implement me")
+}
+
+// PortCreated implements mock PortCreated
+func (mp *MockCoreProxy) PortCreated(ctx context.Context, deviceID string, port *voltha.Port) error {
+ panic("implement me")
+}
+
+// PortsStateUpdate implements mock PortsStateUpdate
+func (mp *MockCoreProxy) PortsStateUpdate(ctx context.Context, deviceID string, operStatus voltha.OperStatus_OperStatus) error {
+ panic("implement me")
+}
+
+// DeleteAllPorts implements mock DeleteAllPorts
+func (mp *MockCoreProxy) DeleteAllPorts(ctx context.Context, deviceID string) error {
+ panic("implement me")
+}
+
+// DeviceStateUpdate implements mock DeviceStateUpdate
+func (mp *MockCoreProxy) DeviceStateUpdate(ctx context.Context, deviceID string,
+ connStatus voltha.ConnectStatus_ConnectStatus, operStatus voltha.OperStatus_OperStatus) error {
+ panic("implement me")
+}
+
+// ChildDeviceDetected implements mock ChildDeviceDetected
+func (mp *MockCoreProxy) ChildDeviceDetected(ctx context.Context, parentDeviceID string, parentPortNo int,
+ childDeviceType string, channelID int, vendorID string, serialNumber string, onuID int64) (*voltha.Device, error) {
+ panic("implement me")
+}
+
+// ChildDevicesLost implements mock ChildDevicesLost
+func (mp *MockCoreProxy) ChildDevicesLost(ctx context.Context, parentDeviceID string) error {
+ panic("implement me")
+}
+
+// ChildDevicesDetected implements mock ChildDevicesDetecte
+func (mp *MockCoreProxy) ChildDevicesDetected(ctx context.Context, parentDeviceID string) error {
+ panic("implement me")
+}
+
+// GetDevice implements mock GetDevice
+func (mp *MockCoreProxy) GetDevice(ctx context.Context, parentDeviceID string, deviceID string) (*voltha.Device, error) {
+ if parentDeviceID != "" {
+ return &voltha.Device{}, nil
+ }
+ return nil, errors.New("device detection failed")
+}
+
+// GetChildDevice implements mock GetChildDevice
+func (mp *MockCoreProxy) GetChildDevice(ctx context.Context, parentDeviceID string, kwargs map[string]interface{}) (*voltha.Device, error) {
+ if parentDeviceID != "" {
+ return &voltha.Device{}, nil
+ }
+ return nil, errors.New("device detection failed")
+}
+
+// GetChildDevices implements mock GetChildDevices
+func (mp *MockCoreProxy) GetChildDevices(ctx context.Context, parentDeviceID string) (*voltha.Devices, error) {
+ if parentDeviceID != "" {
+ return &voltha.Devices{}, nil
+ }
+ return nil, errors.New("device detection failed")
+}
+
+// SendPacketIn implements mock SendPacketIn
+func (mp *MockCoreProxy) SendPacketIn(ctx context.Context, deviceID string, port uint32, pktPayload []byte) error {
+ panic("implement me")
+}