kdarapu | 381c690 | 2019-07-31 18:23:16 +0530 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2018-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 adaptercore provides the utility for olt devices, flows and statistics |
| 18 | package adaptercore |
| 19 | |
| 20 | import ( |
| 21 | "net" |
| 22 | "testing" |
| 23 | |
| 24 | "github.com/opencord/voltha-openolt-adapter/mocks" |
| 25 | "github.com/opencord/voltha-protos/go/voltha" |
| 26 | ) |
| 27 | |
| 28 | func newMockDeviceDeviceHandler() *DeviceHandler { |
| 29 | device := &voltha.Device{ |
| 30 | Id: "olt", |
| 31 | Root: true, |
| 32 | ParentId: "logical_device", |
| 33 | Ports: []*voltha.Port{ |
| 34 | {PortNo: 1, Label: "pon"}, |
| 35 | {PortNo: 2, Label: "nni"}, |
| 36 | }, |
| 37 | } |
| 38 | return &DeviceHandler{ |
| 39 | deviceID: device.GetId(), |
| 40 | |
| 41 | device: device, |
| 42 | coreProxy: &mocks.MockCoreProxy{}, |
| 43 | AdapterProxy: &mocks.MockAdapterProxy{}, |
| 44 | } |
| 45 | } |
| 46 | |
| 47 | func Test_generateMacFromHost(t *testing.T) { |
| 48 | type args struct { |
| 49 | host string |
| 50 | } |
| 51 | tests := []struct { |
| 52 | name string |
| 53 | args args |
| 54 | want string |
| 55 | wantErr bool |
| 56 | }{ |
| 57 | {"test1", args{host: "localhost"}, "00:00:7f:00:00:01", false}, |
| 58 | {"test2", args{host: "10.10.10.10"}, "00:00:0a:0a:0a:0a", false}, |
| 59 | } |
| 60 | for _, tt := range tests { |
| 61 | t.Run(tt.name, func(t *testing.T) { |
| 62 | got, err := generateMacFromHost(tt.args.host) |
| 63 | if (err != nil) != tt.wantErr { |
| 64 | t.Errorf("generateMacFromHost() error = %v, wantErr %v", err, tt.wantErr) |
| 65 | return |
| 66 | } |
| 67 | if got != tt.want { |
| 68 | t.Errorf("generateMacFromHost() = %v, want %v", got, tt.want) |
| 69 | } |
| 70 | }) |
| 71 | } |
| 72 | } |
| 73 | func Test_macifyIP(t *testing.T) { |
| 74 | type args struct { |
| 75 | ip net.IP |
| 76 | } |
| 77 | tests := []struct { |
| 78 | name string |
| 79 | args args |
| 80 | want string |
| 81 | }{{ |
| 82 | "test1", |
| 83 | args{ip: net.ParseIP("10.10.10.10")}, |
| 84 | "00:00:0a:0a:0a:0a", |
| 85 | }, |
| 86 | { |
| 87 | "test3", |
| 88 | args{ip: net.ParseIP("127.0.0.1")}, |
| 89 | "00:00:7f:00:00:01", |
| 90 | }} |
| 91 | for _, tt := range tests { |
| 92 | t.Run(tt.name, func(t *testing.T) { |
| 93 | if got := macifyIP(tt.args.ip); got != tt.want { |
| 94 | t.Errorf("macifyIP() = %v, want %v", got, tt.want) |
| 95 | } |
| 96 | }) |
| 97 | } |
| 98 | } |
| 99 | |
| 100 | func TestDeviceHandler_GetOfpDeviceInfo(t *testing.T) { |
| 101 | dh := newMockDeviceDeviceHandler() |
| 102 | device := &voltha.Device{} |
| 103 | got, err := dh.GetOfpDeviceInfo(device) |
| 104 | if err != nil { |
| 105 | t.Errorf("DeviceHandler.GetOfpDeviceInfo() error = %v", err) |
| 106 | return |
| 107 | } |
| 108 | t.Logf("ofpDeviceInfo %v", got) |
| 109 | } |
| 110 | |
| 111 | func TestDeviceHandler_GetOfpPortInfo(t *testing.T) { |
| 112 | dh := newMockDeviceDeviceHandler() |
| 113 | device := &voltha.Device{} |
| 114 | got, err := dh.GetOfpPortInfo(device, 1) |
| 115 | if err != nil { |
| 116 | t.Errorf("DeviceHandler.GetOfpPortInfo() error = %v", err) |
| 117 | return |
| 118 | } |
| 119 | t.Logf("ofpDeviceInfo %v", got) |
| 120 | } |
| 121 | func TestDeviceHandler_GetChildDevice(t *testing.T) { |
| 122 | dh := newMockDeviceDeviceHandler() |
| 123 | type args struct { |
| 124 | parentPort uint32 |
| 125 | onuID uint32 |
| 126 | } |
| 127 | tests := []struct { |
| 128 | name string |
| 129 | args args |
| 130 | want *voltha.Device |
| 131 | }{ |
| 132 | {"test1", |
| 133 | args{parentPort: 1, |
| 134 | onuID: 1}, |
| 135 | &voltha.Device{}, |
| 136 | }, |
| 137 | } |
| 138 | for _, tt := range tests { |
| 139 | t.Run(tt.name, func(t *testing.T) { |
| 140 | got := dh.GetChildDevice(tt.args.parentPort, tt.args.onuID) |
| 141 | t.Log("onu device id", got) |
| 142 | }) |
| 143 | } |
| 144 | } |