Daniele Rossi | 566c75c | 2019-11-06 16:55:56 +0100 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2019-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 | package core |
| 17 | |
| 18 | import ( |
Thomas Lee S | e5a4401 | 2019-11-07 20:32:24 +0530 | [diff] [blame] | 19 | "context" |
Daniele Rossi | 566c75c | 2019-11-06 16:55:56 +0100 | [diff] [blame] | 20 | "github.com/opencord/voltha-go/ro_core/config" |
serkant.uluderya | 2ae470f | 2020-01-21 11:13:09 -0800 | [diff] [blame] | 21 | "github.com/opencord/voltha-lib-go/v3/pkg/log" |
Daniele Rossi | 566c75c | 2019-11-06 16:55:56 +0100 | [diff] [blame] | 22 | "github.com/phayes/freeport" |
| 23 | "github.com/stretchr/testify/assert" |
| 24 | "strconv" |
| 25 | "testing" |
| 26 | ) |
| 27 | |
| 28 | func MakeTestLogDevAgConfig() (*Core, error) { |
Thomas Lee S | e5a4401 | 2019-11-07 20:32:24 +0530 | [diff] [blame] | 29 | var ctx context.Context |
Daniele Rossi | 566c75c | 2019-11-06 16:55:56 +0100 | [diff] [blame] | 30 | var core *Core |
| 31 | var roCoreFlgs *config.ROCoreFlags |
| 32 | var roC *roCore |
| 33 | |
| 34 | freePort, errP := freeport.GetFreePort() |
| 35 | if errP == nil { |
| 36 | freePortStr := strconv.Itoa(freePort) |
| 37 | |
| 38 | roCoreFlgs = config.NewROCoreFlags() |
| 39 | roC = newROCore(roCoreFlgs) |
Thomas Lee S | e5a4401 | 2019-11-07 20:32:24 +0530 | [diff] [blame] | 40 | |
Daniele Rossi | 566c75c | 2019-11-06 16:55:56 +0100 | [diff] [blame] | 41 | if (roC != nil) && (roCoreFlgs != nil) { |
| 42 | addr := "127.0.0.1" + ":" + freePortStr |
| 43 | cli, err := newKVClient("etcd", addr, 5) |
| 44 | if err == nil { |
| 45 | roC.kvClient = cli |
| 46 | //return roCoreFlgs, roC |
| 47 | } |
| 48 | } |
| 49 | } |
| 50 | |
Thomas Lee S | e5a4401 | 2019-11-07 20:32:24 +0530 | [diff] [blame] | 51 | core = NewCore(ctx, "ro_core", roCoreFlgs, roC.kvClient) |
Daniele Rossi | 566c75c | 2019-11-06 16:55:56 +0100 | [diff] [blame] | 52 | return core, nil |
| 53 | } |
| 54 | |
| 55 | // newLogicalDeviceAgent test |
| 56 | func TestNewLogicalDeviceAgent(t *testing.T) { |
| 57 | core, _ := MakeTestLogDevAgConfig() |
| 58 | assert.NotNil(t, core) |
| 59 | logAgent := newLogicalDeviceAgent("log-dev", "", core.logicalDeviceMgr, core.deviceMgr, core.clusterDataProxy) |
| 60 | assert.NotNil(t, logAgent) |
| 61 | } |
| 62 | |
| 63 | // GetLogicalDevice, Flows, Ports test |
| 64 | func TestGetLogicalDevice(t *testing.T) { |
| 65 | core, _ := MakeTestLogDevAgConfig() |
| 66 | assert.NotNil(t, core) |
| 67 | |
Thomas Lee S | e5a4401 | 2019-11-07 20:32:24 +0530 | [diff] [blame] | 68 | var err1 error |
| 69 | if core.clusterDataProxy, err1 = core.localDataRoot.CreateProxy(context.Background(), "/", false); err1 != nil { |
| 70 | log.Errorw("failed-to-create-cluster-proxy", log.Fields{"error": err1}) |
| 71 | assert.NotNil(t, err1) |
| 72 | } |
| 73 | |
Daniele Rossi | 566c75c | 2019-11-06 16:55:56 +0100 | [diff] [blame] | 74 | logDevMgr := newLogicalDeviceManager(core.deviceMgr, core.clusterDataProxy) |
| 75 | assert.NotNil(t, logDevMgr) |
| 76 | |
| 77 | logAgent := newLogicalDeviceAgent("log-dev-ag", "", logDevMgr, core.deviceMgr, logDevMgr.clusterDataProxy) |
| 78 | assert.NotNil(t, logAgent) |
| 79 | |
| 80 | logDevMgr.addLogicalDeviceAgentToMap(logAgent) |
| 81 | |
| 82 | logDev, err := logAgent.GetLogicalDevice() |
| 83 | assert.Nil(t, logDev) |
| 84 | assert.NotNil(t, err) |
| 85 | |
| 86 | Flws, err := logAgent.ListLogicalDeviceFlows() |
| 87 | assert.Nil(t, Flws) |
| 88 | assert.NotNil(t, err) |
| 89 | FlwsGrp, err := logAgent.ListLogicalDeviceFlowGroups() |
| 90 | assert.Nil(t, FlwsGrp) |
| 91 | assert.NotNil(t, err) |
| 92 | |
| 93 | logDevPorts, err := logAgent.ListLogicalDevicePorts() |
| 94 | assert.Nil(t, logDevPorts) |
| 95 | assert.NotNil(t, err) |
| 96 | |
| 97 | } |