Mahir Gunyel | 03de0d3 | 2020-06-03 01:36:59 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2020-present Open Networking Foundation |
| 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | */ |
| 16 | |
| 17 | // Package core Common Logger initialization |
| 18 | package test |
| 19 | |
| 20 | import ( |
Mahir Gunyel | 03de0d3 | 2020-06-03 01:36:59 -0700 | [diff] [blame] | 21 | "testing" |
| 22 | |
| 23 | "github.com/opencord/voltha-go/rw_core/config" |
| 24 | "github.com/opencord/voltha-go/rw_core/core/adapter" |
| 25 | cm "github.com/opencord/voltha-go/rw_core/mocks" |
| 26 | "github.com/opencord/voltha-lib-go/v3/pkg/adapters" |
| 27 | com "github.com/opencord/voltha-lib-go/v3/pkg/adapters/common" |
| 28 | "github.com/opencord/voltha-lib-go/v3/pkg/db/kvstore" |
| 29 | "github.com/opencord/voltha-lib-go/v3/pkg/kafka" |
| 30 | "github.com/opencord/voltha-lib-go/v3/pkg/log" |
| 31 | mock_etcd "github.com/opencord/voltha-lib-go/v3/pkg/mocks/etcd" |
| 32 | "github.com/opencord/voltha-lib-go/v3/pkg/version" |
| 33 | "github.com/opencord/voltha-protos/v3/go/voltha" |
| 34 | "github.com/phayes/freeport" |
| 35 | "github.com/stretchr/testify/assert" |
| 36 | "google.golang.org/grpc/codes" |
| 37 | "google.golang.org/grpc/status" |
| 38 | ) |
| 39 | |
| 40 | const ( |
| 41 | OltAdapter = iota |
| 42 | OnuAdapter |
| 43 | ) |
| 44 | |
| 45 | //CreateMockAdapter creates mock OLT and ONU adapters |
| 46 | func CreateMockAdapter(adapterType int, kafkaClient kafka.Client, coreInstanceID string, coreName string, adapterName string) (adapters.IAdapter, error) { |
| 47 | var err error |
| 48 | var adapter adapters.IAdapter |
| 49 | adapterKafkaICProxy := kafka.NewInterContainerProxy( |
| 50 | kafka.MsgClient(kafkaClient), |
| 51 | kafka.DefaultTopic(&kafka.Topic{Name: adapterName})) |
| 52 | adapterCoreProxy := com.NewCoreProxy(adapterKafkaICProxy, adapterName, coreName) |
| 53 | var adapterReqHandler *com.RequestHandlerProxy |
| 54 | switch adapterType { |
| 55 | case OltAdapter: |
| 56 | adapter = cm.NewOLTAdapter(adapterCoreProxy) |
| 57 | case OnuAdapter: |
| 58 | adapter = cm.NewONUAdapter(adapterCoreProxy) |
| 59 | default: |
| 60 | logger.Fatalf("invalid-adapter-type-%d", adapterType) |
| 61 | } |
| 62 | adapterReqHandler = com.NewRequestHandlerProxy(coreInstanceID, adapter, adapterCoreProxy) |
| 63 | |
| 64 | if err = adapterKafkaICProxy.Start(); err != nil { |
| 65 | logger.Errorw("Failure-starting-adapter-intercontainerProxy", log.Fields{"error": err}) |
| 66 | return nil, err |
| 67 | } |
| 68 | if err = adapterKafkaICProxy.SubscribeWithRequestHandlerInterface(kafka.Topic{Name: adapterName}, adapterReqHandler); err != nil { |
| 69 | logger.Errorw("Failure-to-subscribe-onu-request-handler", log.Fields{"error": err}) |
| 70 | return nil, err |
| 71 | } |
| 72 | return adapter, nil |
| 73 | } |
| 74 | |
| 75 | //CreateAndregisterAdapters creates mock ONU and OLT adapters and egisters them to rw-core |
| 76 | func CreateAndregisterAdapters(t *testing.T, kClient kafka.Client, coreInstanceID string, oltAdapterName string, onuAdapterName string, adapterMgr *adapter.Manager) (*cm.OLTAdapter, *cm.ONUAdapter) { |
| 77 | // Setup the mock OLT adapter |
| 78 | oltAdapter, err := CreateMockAdapter(OltAdapter, kClient, coreInstanceID, "rw_core", oltAdapterName) |
| 79 | assert.Nil(t, err) |
| 80 | assert.NotNil(t, oltAdapter) |
| 81 | |
| 82 | // Register the adapter |
| 83 | registrationData := &voltha.Adapter{ |
| 84 | Id: oltAdapterName, |
| 85 | Vendor: "Voltha-olt", |
| 86 | Version: version.VersionInfo.Version, |
| 87 | Type: oltAdapterName, |
| 88 | CurrentReplica: 1, |
| 89 | TotalReplicas: 1, |
| 90 | Endpoint: oltAdapterName, |
| 91 | } |
| 92 | types := []*voltha.DeviceType{{Id: oltAdapterName, Adapter: oltAdapterName, AcceptsAddRemoveFlowUpdates: true}} |
| 93 | deviceTypes := &voltha.DeviceTypes{Items: types} |
| 94 | if _, err := adapterMgr.RegisterAdapter(registrationData, deviceTypes); err != nil { |
| 95 | logger.Errorw("failed-to-register-adapter", log.Fields{"error": err}) |
| 96 | assert.NotNil(t, err) |
| 97 | } |
| 98 | |
| 99 | // Setup the mock ONU adapter |
| 100 | onuAdapter, err := CreateMockAdapter(OnuAdapter, kClient, coreInstanceID, "rw_core", onuAdapterName) |
| 101 | |
| 102 | assert.Nil(t, err) |
| 103 | assert.NotNil(t, onuAdapter) |
| 104 | // Register the adapter |
| 105 | registrationData = &voltha.Adapter{ |
| 106 | Id: onuAdapterName, |
| 107 | Vendor: "Voltha-onu", |
| 108 | Version: version.VersionInfo.Version, |
| 109 | Type: onuAdapterName, |
| 110 | CurrentReplica: 1, |
| 111 | TotalReplicas: 1, |
| 112 | Endpoint: onuAdapterName, |
| 113 | } |
| 114 | types = []*voltha.DeviceType{{Id: onuAdapterName, Adapter: onuAdapterName, AcceptsAddRemoveFlowUpdates: true}} |
| 115 | deviceTypes = &voltha.DeviceTypes{Items: types} |
| 116 | if _, err := adapterMgr.RegisterAdapter(registrationData, deviceTypes); err != nil { |
| 117 | logger.Errorw("failed-to-register-adapter", log.Fields{"error": err}) |
| 118 | assert.NotNil(t, err) |
| 119 | } |
| 120 | return oltAdapter.(*cm.OLTAdapter), onuAdapter.(*cm.ONUAdapter) |
| 121 | } |
| 122 | |
| 123 | //StartEmbeddedEtcdServer creates and starts an Embedded etcd server locally. |
| 124 | func StartEmbeddedEtcdServer(configName, storageDir, logLevel string) (*mock_etcd.EtcdServer, int, error) { |
| 125 | kvClientPort, err := freeport.GetFreePort() |
| 126 | if err != nil { |
| 127 | return nil, 0, err |
| 128 | } |
| 129 | peerPort, err := freeport.GetFreePort() |
| 130 | if err != nil { |
| 131 | return nil, 0, err |
| 132 | } |
| 133 | etcdServer := mock_etcd.StartEtcdServer(mock_etcd.MKConfig(configName, kvClientPort, peerPort, storageDir, logLevel)) |
| 134 | if etcdServer == nil { |
| 135 | return nil, 0, status.Error(codes.Internal, "Embedded server failed to start") |
| 136 | } |
| 137 | return etcdServer, kvClientPort, nil |
| 138 | } |
| 139 | |
| 140 | //StopEmbeddedEtcdServer stops the embedded etcd server |
| 141 | func StopEmbeddedEtcdServer(server *mock_etcd.EtcdServer) { |
| 142 | if server != nil { |
| 143 | server.Stop() |
| 144 | } |
| 145 | } |
| 146 | |
| 147 | //SetupKVClient creates a new etcd client |
| 148 | func SetupKVClient(cf *config.RWCoreFlags, coreInstanceID string) kvstore.Client { |
Neha Sharma | d1387da | 2020-05-07 20:07:28 +0000 | [diff] [blame] | 149 | client, err := kvstore.NewEtcdClient(cf.KVStoreAddress, cf.KVStoreTimeout, log.FatalLevel) |
Mahir Gunyel | 03de0d3 | 2020-06-03 01:36:59 -0700 | [diff] [blame] | 150 | if err != nil { |
| 151 | panic("no kv client") |
| 152 | } |
| 153 | return client |
| 154 | } |