sbarbari | 17d7e22 | 2019-11-05 10:02:29 -0500 | [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 | package model |
| 17 | |
| 18 | import ( |
| 19 | "context" |
| 20 | "encoding/hex" |
| 21 | "encoding/json" |
Kent Hagerman | f5a6735 | 2020-04-30 15:15:26 -0400 | [diff] [blame] | 22 | "strconv" |
| 23 | "strings" |
| 24 | "sync" |
| 25 | "testing" |
| 26 | |
sbarbari | 17d7e22 | 2019-11-05 10:02:29 -0500 | [diff] [blame] | 27 | "github.com/google/uuid" |
Kent Hagerman | 4f355f5 | 2020-03-30 16:01:33 -0400 | [diff] [blame] | 28 | "github.com/opencord/voltha-lib-go/v3/pkg/db" |
| 29 | "github.com/opencord/voltha-lib-go/v3/pkg/db/kvstore" |
serkant.uluderya | 2ae470f | 2020-01-21 11:13:09 -0800 | [diff] [blame] | 30 | "github.com/opencord/voltha-lib-go/v3/pkg/log" |
serkant.uluderya | 2ae470f | 2020-01-21 11:13:09 -0800 | [diff] [blame] | 31 | "github.com/opencord/voltha-protos/v3/go/openflow_13" |
| 32 | "github.com/opencord/voltha-protos/v3/go/voltha" |
Thomas Lee S | e5a4401 | 2019-11-07 20:32:24 +0530 | [diff] [blame] | 33 | "github.com/stretchr/testify/assert" |
sbarbari | 17d7e22 | 2019-11-05 10:02:29 -0500 | [diff] [blame] | 34 | ) |
| 35 | |
| 36 | var ( |
Kent Hagerman | 4f355f5 | 2020-03-30 16:01:33 -0400 | [diff] [blame] | 37 | BenchmarkProxyLogger log.Logger |
npujar | 9a30c70 | 2019-11-14 17:06:39 +0530 | [diff] [blame] | 38 | TestProxyRootLogicalDevice *Proxy |
| 39 | TestProxyRootDevice *Proxy |
| 40 | TestProxyRootAdapter *Proxy |
| 41 | TestProxyDeviceID string |
| 42 | TestProxyAdapterID string |
| 43 | TestProxyLogicalDeviceID string |
| 44 | TestProxyTargetDeviceID string |
| 45 | TestProxyTargetLogicalDeviceID string |
npujar | 9a30c70 | 2019-11-14 17:06:39 +0530 | [diff] [blame] | 46 | TestProxyStats *openflow_13.OfpFlowStats |
npujar | 9a30c70 | 2019-11-14 17:06:39 +0530 | [diff] [blame] | 47 | TestProxyDevice *voltha.Device |
| 48 | TestProxyLogicalDevice *voltha.LogicalDevice |
| 49 | TestProxyAdapter *voltha.Adapter |
sbarbari | 17d7e22 | 2019-11-05 10:02:29 -0500 | [diff] [blame] | 50 | ) |
| 51 | |
| 52 | func init() { |
Kent Hagerman | 4f355f5 | 2020-03-30 16:01:33 -0400 | [diff] [blame] | 53 | BenchmarkProxyLogger, _ = log.AddPackage(log.JSON, log.DebugLevel, log.Fields{"instanceId": "PLT"}) |
| 54 | //log.UpdateAllLoggers(log.Fields{"instanceId": "PROXY_LOAD_TEST"}) |
| 55 | //Setup default logger - applies for packages that do not have specific logger set |
| 56 | if _, err := log.SetDefaultLogger(log.JSON, log.DebugLevel, log.Fields{"instanceId": "PLT"}); err != nil { |
| 57 | log.With(log.Fields{"error": err}).Fatal("Cannot setup logging") |
Thomas Lee S | e5a4401 | 2019-11-07 20:32:24 +0530 | [diff] [blame] | 58 | } |
Kent Hagerman | 4f355f5 | 2020-03-30 16:01:33 -0400 | [diff] [blame] | 59 | |
| 60 | // Update all loggers (provisioned via init) with a common field |
| 61 | if err := log.UpdateAllLoggers(log.Fields{"instanceId": "PLT"}); err != nil { |
| 62 | log.With(log.Fields{"error": err}).Fatal("Cannot setup logging") |
Thomas Lee S | e5a4401 | 2019-11-07 20:32:24 +0530 | [diff] [blame] | 63 | } |
Kent Hagerman | 4f355f5 | 2020-03-30 16:01:33 -0400 | [diff] [blame] | 64 | log.SetPackageLogLevel("github.com/opencord/voltha-go/db/model", log.DebugLevel) |
| 65 | |
Kent Hagerman | f5a6735 | 2020-04-30 15:15:26 -0400 | [diff] [blame] | 66 | proxy := NewDBPath(mockBackend) |
| 67 | TestProxyRootLogicalDevice = proxy.Proxy("logical_devices") |
| 68 | TestProxyRootDevice = proxy.Proxy("device") |
| 69 | TestProxyRootAdapter = proxy.Proxy("adapters") |
sbarbari | 17d7e22 | 2019-11-05 10:02:29 -0500 | [diff] [blame] | 70 | |
npujar | 9a30c70 | 2019-11-14 17:06:39 +0530 | [diff] [blame] | 71 | TestProxyStats = &openflow_13.OfpFlowStats{ |
sbarbari | 17d7e22 | 2019-11-05 10:02:29 -0500 | [diff] [blame] | 72 | Id: 1111, |
| 73 | } |
npujar | 9a30c70 | 2019-11-14 17:06:39 +0530 | [diff] [blame] | 74 | TestProxyDevice = &voltha.Device{ |
| 75 | Id: TestProxyDeviceID, |
sbarbari | 17d7e22 | 2019-11-05 10:02:29 -0500 | [diff] [blame] | 76 | Type: "simulated_olt", |
| 77 | Address: &voltha.Device_HostAndPort{HostAndPort: "1.2.3.4:5555"}, |
| 78 | AdminState: voltha.AdminState_PREPROVISIONED, |
sbarbari | 17d7e22 | 2019-11-05 10:02:29 -0500 | [diff] [blame] | 79 | } |
| 80 | |
npujar | 9a30c70 | 2019-11-14 17:06:39 +0530 | [diff] [blame] | 81 | TestProxyLogicalDevice = &voltha.LogicalDevice{ |
| 82 | Id: TestProxyDeviceID, |
sbarbari | 17d7e22 | 2019-11-05 10:02:29 -0500 | [diff] [blame] | 83 | DatapathId: 0, |
sbarbari | 17d7e22 | 2019-11-05 10:02:29 -0500 | [diff] [blame] | 84 | } |
| 85 | |
npujar | 9a30c70 | 2019-11-14 17:06:39 +0530 | [diff] [blame] | 86 | TestProxyAdapter = &voltha.Adapter{ |
| 87 | Id: TestProxyAdapterID, |
sbarbari | 17d7e22 | 2019-11-05 10:02:29 -0500 | [diff] [blame] | 88 | Vendor: "test-adapter-vendor", |
| 89 | Version: "test-adapter-version", |
| 90 | } |
| 91 | } |
| 92 | |
Kent Hagerman | 4f355f5 | 2020-03-30 16:01:33 -0400 | [diff] [blame] | 93 | type mockKV struct { |
| 94 | kvstore.Client // pretend we implement everything |
| 95 | |
| 96 | mutex sync.RWMutex |
| 97 | data map[string]interface{} |
| 98 | } |
| 99 | |
| 100 | func (kv *mockKV) List(_ context.Context, key string) (map[string]*kvstore.KVPair, error) { |
| 101 | kv.mutex.RLock() |
| 102 | defer kv.mutex.RUnlock() |
| 103 | |
| 104 | ret := make(map[string]*kvstore.KVPair, len(kv.data)) |
| 105 | for k, v := range kv.data { |
| 106 | if strings.HasPrefix(k, key) { |
| 107 | ret[k] = &kvstore.KVPair{Key: k, Value: v} |
| 108 | } |
| 109 | } |
| 110 | return ret, nil |
| 111 | } |
| 112 | func (kv *mockKV) Get(_ context.Context, key string) (*kvstore.KVPair, error) { |
| 113 | kv.mutex.RLock() |
| 114 | defer kv.mutex.RUnlock() |
| 115 | |
| 116 | if val, have := kv.data[key]; have { |
| 117 | return &kvstore.KVPair{Key: key, Value: val}, nil |
| 118 | } |
| 119 | return nil, nil |
| 120 | } |
| 121 | func (kv *mockKV) Put(_ context.Context, key string, value interface{}) error { |
| 122 | kv.mutex.Lock() |
| 123 | defer kv.mutex.Unlock() |
| 124 | |
| 125 | kv.data[key] = value |
| 126 | return nil |
| 127 | } |
| 128 | func (kv *mockKV) Delete(_ context.Context, key string) error { |
| 129 | kv.mutex.Lock() |
| 130 | defer kv.mutex.Unlock() |
| 131 | |
| 132 | delete(kv.data, key) |
| 133 | return nil |
| 134 | } |
| 135 | |
| 136 | var mockBackend = &db.Backend{Client: &mockKV{data: make(map[string]interface{})}} |
| 137 | |
sbarbari | 17d7e22 | 2019-11-05 10:02:29 -0500 | [diff] [blame] | 138 | func TestProxy_1_1_1_Add_NewDevice(t *testing.T) { |
| 139 | devIDBin, _ := uuid.New().MarshalBinary() |
npujar | 9a30c70 | 2019-11-14 17:06:39 +0530 | [diff] [blame] | 140 | TestProxyDeviceID = "0001" + hex.EncodeToString(devIDBin)[:12] |
| 141 | TestProxyDevice.Id = TestProxyDeviceID |
sbarbari | 17d7e22 | 2019-11-05 10:02:29 -0500 | [diff] [blame] | 142 | |
Kent Hagerman | f5a6735 | 2020-04-30 15:15:26 -0400 | [diff] [blame] | 143 | if err := TestProxyRootDevice.Set(context.Background(), TestProxyDeviceID, TestProxyDevice); err != nil { |
npujar | 9a30c70 | 2019-11-14 17:06:39 +0530 | [diff] [blame] | 144 | BenchmarkProxyLogger.Errorf("Failed to add test proxy device due to error: %v", err) |
Kent Hagerman | 4f355f5 | 2020-03-30 16:01:33 -0400 | [diff] [blame] | 145 | t.Errorf("failed to add device: %s", err) |
Thomas Lee S | e5a4401 | 2019-11-07 20:32:24 +0530 | [diff] [blame] | 146 | } |
Kent Hagerman | 4f355f5 | 2020-03-30 16:01:33 -0400 | [diff] [blame] | 147 | t.Logf("Added device : %+v", TestProxyDevice.Id) |
sbarbari | 17d7e22 | 2019-11-05 10:02:29 -0500 | [diff] [blame] | 148 | |
| 149 | // Verify that the added device can now be retrieved |
Kent Hagerman | 4f355f5 | 2020-03-30 16:01:33 -0400 | [diff] [blame] | 150 | d := &voltha.Device{} |
Kent Hagerman | f5a6735 | 2020-04-30 15:15:26 -0400 | [diff] [blame] | 151 | if have, err := TestProxyRootDevice.Get(context.Background(), TestProxyDeviceID, d); err != nil { |
npujar | 9a30c70 | 2019-11-14 17:06:39 +0530 | [diff] [blame] | 152 | BenchmarkProxyLogger.Errorf("Failed get device info from test proxy due to error: %v", err) |
Thomas Lee S | e5a4401 | 2019-11-07 20:32:24 +0530 | [diff] [blame] | 153 | assert.NotNil(t, err) |
Kent Hagerman | 4f355f5 | 2020-03-30 16:01:33 -0400 | [diff] [blame] | 154 | } else if !have { |
sbarbari | 17d7e22 | 2019-11-05 10:02:29 -0500 | [diff] [blame] | 155 | t.Error("Failed to find added device") |
| 156 | } else { |
| 157 | djson, _ := json.Marshal(d) |
| 158 | t.Logf("Found device: %s", string(djson)) |
| 159 | } |
| 160 | } |
| 161 | |
| 162 | func TestProxy_1_1_2_Add_ExistingDevice(t *testing.T) { |
npujar | 9a30c70 | 2019-11-14 17:06:39 +0530 | [diff] [blame] | 163 | TestProxyDevice.Id = TestProxyDeviceID |
sbarbari | 17d7e22 | 2019-11-05 10:02:29 -0500 | [diff] [blame] | 164 | |
Kent Hagerman | f5a6735 | 2020-04-30 15:15:26 -0400 | [diff] [blame] | 165 | if err := TestProxyRootDevice.Set(context.Background(), "devices", TestProxyDevice); err != nil { |
npujar | 9a30c70 | 2019-11-14 17:06:39 +0530 | [diff] [blame] | 166 | BenchmarkProxyLogger.Errorf("Failed to add device to test proxy due to error: %v", err) |
Thomas Lee S | e5a4401 | 2019-11-07 20:32:24 +0530 | [diff] [blame] | 167 | assert.NotNil(t, err) |
| 168 | } |
sbarbari | 17d7e22 | 2019-11-05 10:02:29 -0500 | [diff] [blame] | 169 | |
Kent Hagerman | 4f355f5 | 2020-03-30 16:01:33 -0400 | [diff] [blame] | 170 | d := &voltha.Device{} |
Kent Hagerman | f5a6735 | 2020-04-30 15:15:26 -0400 | [diff] [blame] | 171 | if have, err := TestProxyRootDevice.Get(context.Background(), TestProxyDeviceID, d); err != nil { |
Kent Hagerman | 4f355f5 | 2020-03-30 16:01:33 -0400 | [diff] [blame] | 172 | BenchmarkProxyLogger.Errorf("Failed get device info from test proxy due to error: %v", err) |
| 173 | assert.NotNil(t, err) |
| 174 | } else if !have { |
| 175 | t.Error("Failed to find added device") |
| 176 | } else { |
| 177 | if d.String() != TestProxyDevice.String() { |
| 178 | t.Errorf("Devices don't match - existing: %+v returned: %+v", TestProxyLogicalDevice, d) |
| 179 | } |
| 180 | djson, _ := json.Marshal(d) |
| 181 | t.Logf("Found device: %s", string(djson)) |
sbarbari | 17d7e22 | 2019-11-05 10:02:29 -0500 | [diff] [blame] | 182 | } |
Kent Hagerman | 4f355f5 | 2020-03-30 16:01:33 -0400 | [diff] [blame] | 183 | |
sbarbari | 17d7e22 | 2019-11-05 10:02:29 -0500 | [diff] [blame] | 184 | } |
| 185 | |
| 186 | func TestProxy_1_1_3_Add_NewAdapter(t *testing.T) { |
npujar | 9a30c70 | 2019-11-14 17:06:39 +0530 | [diff] [blame] | 187 | TestProxyAdapterID = "test-adapter" |
| 188 | TestProxyAdapter.Id = TestProxyAdapterID |
sbarbari | 17d7e22 | 2019-11-05 10:02:29 -0500 | [diff] [blame] | 189 | |
| 190 | // Add the adapter |
Kent Hagerman | f5a6735 | 2020-04-30 15:15:26 -0400 | [diff] [blame] | 191 | if err := TestProxyRootAdapter.Set(context.Background(), TestProxyAdapterID, TestProxyAdapter); err != nil { |
npujar | 9a30c70 | 2019-11-14 17:06:39 +0530 | [diff] [blame] | 192 | BenchmarkProxyLogger.Errorf("Failed to add adapter to test proxy due to error: %v", err) |
Thomas Lee S | e5a4401 | 2019-11-07 20:32:24 +0530 | [diff] [blame] | 193 | assert.NotNil(t, err) |
sbarbari | 17d7e22 | 2019-11-05 10:02:29 -0500 | [diff] [blame] | 194 | } else { |
Kent Hagerman | 4f355f5 | 2020-03-30 16:01:33 -0400 | [diff] [blame] | 195 | t.Logf("Added adapter : %+v", TestProxyAdapter.Id) |
sbarbari | 17d7e22 | 2019-11-05 10:02:29 -0500 | [diff] [blame] | 196 | } |
| 197 | |
sbarbari | 17d7e22 | 2019-11-05 10:02:29 -0500 | [diff] [blame] | 198 | // Verify that the added device can now be retrieved |
Kent Hagerman | 4f355f5 | 2020-03-30 16:01:33 -0400 | [diff] [blame] | 199 | d := &voltha.Device{} |
Kent Hagerman | f5a6735 | 2020-04-30 15:15:26 -0400 | [diff] [blame] | 200 | if have, err := TestProxyRootAdapter.Get(context.Background(), TestProxyAdapterID, d); err != nil { |
npujar | 9a30c70 | 2019-11-14 17:06:39 +0530 | [diff] [blame] | 201 | BenchmarkProxyLogger.Errorf("Failed to retrieve device info from test proxy due to error: %v", err) |
Thomas Lee S | e5a4401 | 2019-11-07 20:32:24 +0530 | [diff] [blame] | 202 | assert.NotNil(t, err) |
Kent Hagerman | 4f355f5 | 2020-03-30 16:01:33 -0400 | [diff] [blame] | 203 | } else if !have { |
sbarbari | 17d7e22 | 2019-11-05 10:02:29 -0500 | [diff] [blame] | 204 | t.Error("Failed to find added adapter") |
| 205 | } else { |
| 206 | djson, _ := json.Marshal(d) |
| 207 | t.Logf("Found adapter: %s", string(djson)) |
| 208 | } |
| 209 | |
sbarbari | 17d7e22 | 2019-11-05 10:02:29 -0500 | [diff] [blame] | 210 | } |
| 211 | |
| 212 | func TestProxy_1_2_1_Get_AllDevices(t *testing.T) { |
Kent Hagerman | 4f355f5 | 2020-03-30 16:01:33 -0400 | [diff] [blame] | 213 | var devices []*voltha.Device |
Kent Hagerman | f5a6735 | 2020-04-30 15:15:26 -0400 | [diff] [blame] | 214 | if err := TestProxyRootDevice.List(context.Background(), &devices); err != nil { |
npujar | 9a30c70 | 2019-11-14 17:06:39 +0530 | [diff] [blame] | 215 | BenchmarkProxyLogger.Errorf("Failed to get all devices info from test proxy due to error: %v", err) |
Thomas Lee S | e5a4401 | 2019-11-07 20:32:24 +0530 | [diff] [blame] | 216 | assert.NotNil(t, err) |
| 217 | } |
Kent Hagerman | 4f355f5 | 2020-03-30 16:01:33 -0400 | [diff] [blame] | 218 | if len(devices) == 0 { |
sbarbari | 17d7e22 | 2019-11-05 10:02:29 -0500 | [diff] [blame] | 219 | t.Error("there are no available devices to retrieve") |
| 220 | } else { |
| 221 | // Save the target device id for later tests |
Kent Hagerman | 4f355f5 | 2020-03-30 16:01:33 -0400 | [diff] [blame] | 222 | TestProxyTargetDeviceID = devices[0].Id |
sbarbari | 17d7e22 | 2019-11-05 10:02:29 -0500 | [diff] [blame] | 223 | t.Logf("retrieved all devices: %+v", devices) |
| 224 | } |
| 225 | } |
| 226 | |
| 227 | func TestProxy_1_2_2_Get_SingleDevice(t *testing.T) { |
Kent Hagerman | 4f355f5 | 2020-03-30 16:01:33 -0400 | [diff] [blame] | 228 | d := &voltha.Device{} |
Kent Hagerman | f5a6735 | 2020-04-30 15:15:26 -0400 | [diff] [blame] | 229 | if have, err := TestProxyRootDevice.Get(context.Background(), TestProxyTargetDeviceID, d); err != nil { |
npujar | 9a30c70 | 2019-11-14 17:06:39 +0530 | [diff] [blame] | 230 | BenchmarkProxyLogger.Errorf("Failed to get single device info from test proxy due to error: %v", err) |
Thomas Lee S | e5a4401 | 2019-11-07 20:32:24 +0530 | [diff] [blame] | 231 | assert.NotNil(t, err) |
Kent Hagerman | 4f355f5 | 2020-03-30 16:01:33 -0400 | [diff] [blame] | 232 | } else if !have { |
npujar | 9a30c70 | 2019-11-14 17:06:39 +0530 | [diff] [blame] | 233 | t.Errorf("Failed to find device : %s", TestProxyTargetDeviceID) |
sbarbari | 17d7e22 | 2019-11-05 10:02:29 -0500 | [diff] [blame] | 234 | } else { |
| 235 | djson, _ := json.Marshal(d) |
| 236 | t.Logf("Found device: %s", string(djson)) |
| 237 | } |
| 238 | } |
| 239 | |
| 240 | func TestProxy_1_3_1_Update_Device(t *testing.T) { |
| 241 | var fwVersion int |
| 242 | |
Kent Hagerman | 4f355f5 | 2020-03-30 16:01:33 -0400 | [diff] [blame] | 243 | retrieved := &voltha.Device{} |
Kent Hagerman | f5a6735 | 2020-04-30 15:15:26 -0400 | [diff] [blame] | 244 | if have, err := TestProxyRootDevice.Get(context.Background(), TestProxyTargetDeviceID, retrieved); err != nil { |
npujar | 9a30c70 | 2019-11-14 17:06:39 +0530 | [diff] [blame] | 245 | BenchmarkProxyLogger.Errorf("Failed to get device info from test proxy due to error: %v", err) |
Thomas Lee S | e5a4401 | 2019-11-07 20:32:24 +0530 | [diff] [blame] | 246 | assert.NotNil(t, err) |
Kent Hagerman | 4f355f5 | 2020-03-30 16:01:33 -0400 | [diff] [blame] | 247 | } else if !have { |
sbarbari | 17d7e22 | 2019-11-05 10:02:29 -0500 | [diff] [blame] | 248 | t.Error("Failed to get device") |
| 249 | } else { |
| 250 | t.Logf("Found raw device (root proxy): %+v", retrieved) |
| 251 | |
Kent Hagerman | 4f355f5 | 2020-03-30 16:01:33 -0400 | [diff] [blame] | 252 | if retrieved.FirmwareVersion == "n/a" { |
sbarbari | 17d7e22 | 2019-11-05 10:02:29 -0500 | [diff] [blame] | 253 | fwVersion = 0 |
| 254 | } else { |
Kent Hagerman | 4f355f5 | 2020-03-30 16:01:33 -0400 | [diff] [blame] | 255 | fwVersion, _ = strconv.Atoi(retrieved.FirmwareVersion) |
sbarbari | 17d7e22 | 2019-11-05 10:02:29 -0500 | [diff] [blame] | 256 | fwVersion++ |
| 257 | } |
| 258 | |
Kent Hagerman | 4f355f5 | 2020-03-30 16:01:33 -0400 | [diff] [blame] | 259 | retrieved.FirmwareVersion = strconv.Itoa(fwVersion) |
sbarbari | 17d7e22 | 2019-11-05 10:02:29 -0500 | [diff] [blame] | 260 | |
Kent Hagerman | f5a6735 | 2020-04-30 15:15:26 -0400 | [diff] [blame] | 261 | if err := TestProxyRootDevice.Set(context.Background(), TestProxyTargetDeviceID, retrieved); err != nil { |
npujar | 9a30c70 | 2019-11-14 17:06:39 +0530 | [diff] [blame] | 262 | BenchmarkProxyLogger.Errorf("Failed to update device info test proxy due to error: %v", err) |
Thomas Lee S | e5a4401 | 2019-11-07 20:32:24 +0530 | [diff] [blame] | 263 | assert.NotNil(t, err) |
| 264 | } |
Kent Hagerman | 4f355f5 | 2020-03-30 16:01:33 -0400 | [diff] [blame] | 265 | afterUpdate := &voltha.Device{} |
Kent Hagerman | f5a6735 | 2020-04-30 15:15:26 -0400 | [diff] [blame] | 266 | if have, err := TestProxyRootDevice.Get(context.Background(), TestProxyTargetDeviceID, afterUpdate); err != nil { |
Kent Hagerman | 4f355f5 | 2020-03-30 16:01:33 -0400 | [diff] [blame] | 267 | BenchmarkProxyLogger.Errorf("Failed to get device info from test proxy due to error: %v", err) |
| 268 | } else if !have { |
sbarbari | 17d7e22 | 2019-11-05 10:02:29 -0500 | [diff] [blame] | 269 | t.Error("Failed to update device") |
| 270 | } else { |
| 271 | t.Logf("Updated device : %+v", afterUpdate) |
| 272 | } |
| 273 | |
Kent Hagerman | 4f355f5 | 2020-03-30 16:01:33 -0400 | [diff] [blame] | 274 | d := &voltha.Device{} |
Kent Hagerman | f5a6735 | 2020-04-30 15:15:26 -0400 | [diff] [blame] | 275 | if have, err := TestProxyRootDevice.Get(context.Background(), TestProxyTargetDeviceID, d); err != nil { |
npujar | 9a30c70 | 2019-11-14 17:06:39 +0530 | [diff] [blame] | 276 | BenchmarkProxyLogger.Errorf("Failed to get device info from test proxy due to error: %v", err) |
Thomas Lee S | e5a4401 | 2019-11-07 20:32:24 +0530 | [diff] [blame] | 277 | assert.NotNil(t, err) |
Kent Hagerman | 4f355f5 | 2020-03-30 16:01:33 -0400 | [diff] [blame] | 278 | } else if !have { |
sbarbari | 17d7e22 | 2019-11-05 10:02:29 -0500 | [diff] [blame] | 279 | t.Error("Failed to find updated device (root proxy)") |
| 280 | } else { |
| 281 | djson, _ := json.Marshal(d) |
| 282 | t.Logf("Found device (root proxy): %s raw: %+v", string(djson), d) |
| 283 | } |
| 284 | } |
| 285 | } |
| 286 | |
sbarbari | 17d7e22 | 2019-11-05 10:02:29 -0500 | [diff] [blame] | 287 | func TestProxy_1_3_3_Update_Adapter(t *testing.T) { |
sbarbari | 17d7e22 | 2019-11-05 10:02:29 -0500 | [diff] [blame] | 288 | |
Kent Hagerman | f5a6735 | 2020-04-30 15:15:26 -0400 | [diff] [blame] | 289 | adaptersProxy := NewDBPath(mockBackend).Proxy("adapters") |
Kent Hagerman | 4f355f5 | 2020-03-30 16:01:33 -0400 | [diff] [blame] | 290 | |
| 291 | retrieved := &voltha.Adapter{} |
Kent Hagerman | f5a6735 | 2020-04-30 15:15:26 -0400 | [diff] [blame] | 292 | if have, err := TestProxyRootAdapter.Get(context.Background(), TestProxyAdapterID, retrieved); err != nil { |
npujar | 9a30c70 | 2019-11-14 17:06:39 +0530 | [diff] [blame] | 293 | BenchmarkProxyLogger.Errorf("Failed to retrieve adapter info from adapters proxy due to error: %v", err) |
Thomas Lee S | e5a4401 | 2019-11-07 20:32:24 +0530 | [diff] [blame] | 294 | assert.NotNil(t, err) |
Kent Hagerman | 4f355f5 | 2020-03-30 16:01:33 -0400 | [diff] [blame] | 295 | } else if !have { |
sbarbari | 17d7e22 | 2019-11-05 10:02:29 -0500 | [diff] [blame] | 296 | t.Error("Failed to get adapter") |
| 297 | } else { |
| 298 | t.Logf("Found raw adapter (root proxy): %+v", retrieved) |
| 299 | |
Kent Hagerman | 4f355f5 | 2020-03-30 16:01:33 -0400 | [diff] [blame] | 300 | retrieved.Version = "test-adapter-version-2" |
sbarbari | 17d7e22 | 2019-11-05 10:02:29 -0500 | [diff] [blame] | 301 | |
Kent Hagerman | f5a6735 | 2020-04-30 15:15:26 -0400 | [diff] [blame] | 302 | if err := adaptersProxy.Set(context.Background(), TestProxyAdapterID, retrieved); err != nil { |
npujar | 9a30c70 | 2019-11-14 17:06:39 +0530 | [diff] [blame] | 303 | BenchmarkProxyLogger.Errorf("Failed to update adapter info in adapters proxy due to error: %v", err) |
Thomas Lee S | e5a4401 | 2019-11-07 20:32:24 +0530 | [diff] [blame] | 304 | assert.NotNil(t, err) |
sbarbari | 17d7e22 | 2019-11-05 10:02:29 -0500 | [diff] [blame] | 305 | } else { |
Kent Hagerman | 4f355f5 | 2020-03-30 16:01:33 -0400 | [diff] [blame] | 306 | t.Logf("Updated adapter : %s", retrieved.Id) |
sbarbari | 17d7e22 | 2019-11-05 10:02:29 -0500 | [diff] [blame] | 307 | } |
| 308 | |
Kent Hagerman | 4f355f5 | 2020-03-30 16:01:33 -0400 | [diff] [blame] | 309 | d := &voltha.Adapter{} |
Kent Hagerman | f5a6735 | 2020-04-30 15:15:26 -0400 | [diff] [blame] | 310 | if have, err := TestProxyRootAdapter.Get(context.Background(), TestProxyAdapterID, d); err != nil { |
npujar | 9a30c70 | 2019-11-14 17:06:39 +0530 | [diff] [blame] | 311 | BenchmarkProxyLogger.Errorf("Failed to get updated adapter info from adapters proxy due to error: %v", err) |
Thomas Lee S | e5a4401 | 2019-11-07 20:32:24 +0530 | [diff] [blame] | 312 | assert.NotNil(t, err) |
Kent Hagerman | 4f355f5 | 2020-03-30 16:01:33 -0400 | [diff] [blame] | 313 | } else if !have { |
sbarbari | 17d7e22 | 2019-11-05 10:02:29 -0500 | [diff] [blame] | 314 | t.Error("Failed to find updated adapter (root proxy)") |
| 315 | } else { |
| 316 | djson, _ := json.Marshal(d) |
| 317 | t.Logf("Found adapter (root proxy): %s raw: %+v", string(djson), d) |
| 318 | } |
| 319 | } |
| 320 | } |
| 321 | |
| 322 | func TestProxy_1_4_1_Remove_Device(t *testing.T) { |
Kent Hagerman | f5a6735 | 2020-04-30 15:15:26 -0400 | [diff] [blame] | 323 | if err := TestProxyRootDevice.Remove(context.Background(), TestProxyDeviceID); err != nil { |
npujar | 9a30c70 | 2019-11-14 17:06:39 +0530 | [diff] [blame] | 324 | BenchmarkProxyLogger.Errorf("Failed to remove device from devices proxy due to error: %v", err) |
Kent Hagerman | 4f355f5 | 2020-03-30 16:01:33 -0400 | [diff] [blame] | 325 | t.Errorf("failed to remove device: %s", err) |
sbarbari | 17d7e22 | 2019-11-05 10:02:29 -0500 | [diff] [blame] | 326 | } else { |
Kent Hagerman | 4f355f5 | 2020-03-30 16:01:33 -0400 | [diff] [blame] | 327 | t.Logf("Removed device : %+v", TestProxyDeviceID) |
sbarbari | 17d7e22 | 2019-11-05 10:02:29 -0500 | [diff] [blame] | 328 | } |
| 329 | |
Kent Hagerman | 4f355f5 | 2020-03-30 16:01:33 -0400 | [diff] [blame] | 330 | d := &voltha.Device{} |
Kent Hagerman | f5a6735 | 2020-04-30 15:15:26 -0400 | [diff] [blame] | 331 | have, err := TestProxyRootDevice.Get(context.Background(), TestProxyDeviceID, d) |
Thomas Lee S | e5a4401 | 2019-11-07 20:32:24 +0530 | [diff] [blame] | 332 | if err != nil { |
npujar | 9a30c70 | 2019-11-14 17:06:39 +0530 | [diff] [blame] | 333 | BenchmarkProxyLogger.Errorf("Failed to get device info from devices proxy due to error: %v", err) |
Thomas Lee S | e5a4401 | 2019-11-07 20:32:24 +0530 | [diff] [blame] | 334 | assert.NotNil(t, err) |
Kent Hagerman | 4f355f5 | 2020-03-30 16:01:33 -0400 | [diff] [blame] | 335 | } else if have { |
sbarbari | 17d7e22 | 2019-11-05 10:02:29 -0500 | [diff] [blame] | 336 | djson, _ := json.Marshal(d) |
| 337 | t.Errorf("Device was not removed - %s", djson) |
| 338 | } else { |
npujar | 9a30c70 | 2019-11-14 17:06:39 +0530 | [diff] [blame] | 339 | t.Logf("Device was removed: %s", TestProxyDeviceID) |
sbarbari | 17d7e22 | 2019-11-05 10:02:29 -0500 | [diff] [blame] | 340 | } |
| 341 | } |
| 342 | |
| 343 | func TestProxy_2_1_1_Add_NewLogicalDevice(t *testing.T) { |
| 344 | |
| 345 | ldIDBin, _ := uuid.New().MarshalBinary() |
npujar | 9a30c70 | 2019-11-14 17:06:39 +0530 | [diff] [blame] | 346 | TestProxyLogicalDeviceID = "0001" + hex.EncodeToString(ldIDBin)[:12] |
| 347 | TestProxyLogicalDevice.Id = TestProxyLogicalDeviceID |
sbarbari | 17d7e22 | 2019-11-05 10:02:29 -0500 | [diff] [blame] | 348 | |
Kent Hagerman | f5a6735 | 2020-04-30 15:15:26 -0400 | [diff] [blame] | 349 | if err := TestProxyRootLogicalDevice.Set(context.Background(), TestProxyLogicalDeviceID, TestProxyLogicalDevice); err != nil { |
npujar | 9a30c70 | 2019-11-14 17:06:39 +0530 | [diff] [blame] | 350 | BenchmarkProxyLogger.Errorf("Failed to add new logical device into proxy due to error: %v", err) |
Thomas Lee S | e5a4401 | 2019-11-07 20:32:24 +0530 | [diff] [blame] | 351 | assert.NotNil(t, err) |
sbarbari | 17d7e22 | 2019-11-05 10:02:29 -0500 | [diff] [blame] | 352 | } else { |
Kent Hagerman | 4f355f5 | 2020-03-30 16:01:33 -0400 | [diff] [blame] | 353 | t.Logf("Added logical device : %s", TestProxyLogicalDevice.Id) |
sbarbari | 17d7e22 | 2019-11-05 10:02:29 -0500 | [diff] [blame] | 354 | } |
| 355 | |
Kent Hagerman | 4f355f5 | 2020-03-30 16:01:33 -0400 | [diff] [blame] | 356 | ld := &voltha.LogicalDevice{} |
Kent Hagerman | f5a6735 | 2020-04-30 15:15:26 -0400 | [diff] [blame] | 357 | if have, err := TestProxyRootLogicalDevice.Get(context.Background(), TestProxyLogicalDeviceID, ld); err != nil { |
npujar | 9a30c70 | 2019-11-14 17:06:39 +0530 | [diff] [blame] | 358 | BenchmarkProxyLogger.Errorf("Failed to get logical device info from logical device proxy due to error: %v", err) |
Thomas Lee S | e5a4401 | 2019-11-07 20:32:24 +0530 | [diff] [blame] | 359 | assert.NotNil(t, err) |
Kent Hagerman | 4f355f5 | 2020-03-30 16:01:33 -0400 | [diff] [blame] | 360 | } else if !have { |
sbarbari | 17d7e22 | 2019-11-05 10:02:29 -0500 | [diff] [blame] | 361 | t.Error("Failed to find added logical device") |
| 362 | } else { |
| 363 | ldJSON, _ := json.Marshal(ld) |
| 364 | t.Logf("Found logical device: %s", string(ldJSON)) |
| 365 | } |
sbarbari | 17d7e22 | 2019-11-05 10:02:29 -0500 | [diff] [blame] | 366 | } |
| 367 | |
| 368 | func TestProxy_2_1_2_Add_ExistingLogicalDevice(t *testing.T) { |
npujar | 9a30c70 | 2019-11-14 17:06:39 +0530 | [diff] [blame] | 369 | TestProxyLogicalDevice.Id = TestProxyLogicalDeviceID |
sbarbari | 17d7e22 | 2019-11-05 10:02:29 -0500 | [diff] [blame] | 370 | |
Kent Hagerman | f5a6735 | 2020-04-30 15:15:26 -0400 | [diff] [blame] | 371 | if err := TestProxyRootLogicalDevice.Set(context.Background(), "logical_devices", TestProxyLogicalDevice); err != nil { |
npujar | 9a30c70 | 2019-11-14 17:06:39 +0530 | [diff] [blame] | 372 | BenchmarkProxyLogger.Errorf("Failed to add logical device due to error: %v", err) |
Thomas Lee S | e5a4401 | 2019-11-07 20:32:24 +0530 | [diff] [blame] | 373 | assert.NotNil(t, err) |
| 374 | } |
Kent Hagerman | 4f355f5 | 2020-03-30 16:01:33 -0400 | [diff] [blame] | 375 | |
| 376 | device := &voltha.LogicalDevice{} |
| 377 | if have, err := TestProxyRootLogicalDevice.Get(context.Background(), "logical_devices", device); err != nil { |
| 378 | BenchmarkProxyLogger.Errorf("Failed to get logical device info from logical device proxy due to error: %v", err) |
| 379 | assert.NotNil(t, err) |
| 380 | } else if !have { |
| 381 | t.Error("Failed to find added logical device") |
| 382 | } else { |
| 383 | if device.String() != TestProxyLogicalDevice.String() { |
| 384 | t.Errorf("Logical devices don't match - existing: %+v returned: %+v", TestProxyLogicalDevice, device) |
| 385 | } |
sbarbari | 17d7e22 | 2019-11-05 10:02:29 -0500 | [diff] [blame] | 386 | } |
| 387 | } |
| 388 | |
| 389 | func TestProxy_2_2_1_Get_AllLogicalDevices(t *testing.T) { |
Kent Hagerman | 4f355f5 | 2020-03-30 16:01:33 -0400 | [diff] [blame] | 390 | var logicalDevices []*voltha.LogicalDevice |
Kent Hagerman | f5a6735 | 2020-04-30 15:15:26 -0400 | [diff] [blame] | 391 | if err := TestProxyRootLogicalDevice.List(context.Background(), &logicalDevices); err != nil { |
npujar | 9a30c70 | 2019-11-14 17:06:39 +0530 | [diff] [blame] | 392 | BenchmarkProxyLogger.Errorf("Failed to get all logical devices from proxy due to error: %v", err) |
Thomas Lee S | e5a4401 | 2019-11-07 20:32:24 +0530 | [diff] [blame] | 393 | assert.NotNil(t, err) |
| 394 | } |
Kent Hagerman | 4f355f5 | 2020-03-30 16:01:33 -0400 | [diff] [blame] | 395 | if len(logicalDevices) == 0 { |
sbarbari | 17d7e22 | 2019-11-05 10:02:29 -0500 | [diff] [blame] | 396 | t.Error("there are no available logical devices to retrieve") |
| 397 | } else { |
| 398 | // Save the target device id for later tests |
Kent Hagerman | 4f355f5 | 2020-03-30 16:01:33 -0400 | [diff] [blame] | 399 | TestProxyTargetLogicalDeviceID = logicalDevices[0].Id |
sbarbari | 17d7e22 | 2019-11-05 10:02:29 -0500 | [diff] [blame] | 400 | t.Logf("retrieved all logical devices: %+v", logicalDevices) |
| 401 | } |
| 402 | } |
| 403 | |
| 404 | func TestProxy_2_2_2_Get_SingleLogicalDevice(t *testing.T) { |
Kent Hagerman | 4f355f5 | 2020-03-30 16:01:33 -0400 | [diff] [blame] | 405 | ld := &voltha.LogicalDevice{} |
Kent Hagerman | f5a6735 | 2020-04-30 15:15:26 -0400 | [diff] [blame] | 406 | if have, err := TestProxyRootLogicalDevice.Get(context.Background(), TestProxyTargetLogicalDeviceID, ld); err != nil { |
npujar | 9a30c70 | 2019-11-14 17:06:39 +0530 | [diff] [blame] | 407 | BenchmarkProxyLogger.Errorf("Failed to get single logical device from proxy due to error: %v", err) |
Thomas Lee S | e5a4401 | 2019-11-07 20:32:24 +0530 | [diff] [blame] | 408 | assert.NotNil(t, err) |
Kent Hagerman | 4f355f5 | 2020-03-30 16:01:33 -0400 | [diff] [blame] | 409 | } else if !have { |
npujar | 9a30c70 | 2019-11-14 17:06:39 +0530 | [diff] [blame] | 410 | t.Errorf("Failed to find logical device : %s", TestProxyTargetLogicalDeviceID) |
sbarbari | 17d7e22 | 2019-11-05 10:02:29 -0500 | [diff] [blame] | 411 | } else { |
| 412 | ldJSON, _ := json.Marshal(ld) |
| 413 | t.Logf("Found logical device: %s", string(ldJSON)) |
| 414 | } |
| 415 | |
| 416 | } |
| 417 | |
| 418 | func TestProxy_2_3_1_Update_LogicalDevice(t *testing.T) { |
| 419 | var fwVersion int |
sbarbari | 17d7e22 | 2019-11-05 10:02:29 -0500 | [diff] [blame] | 420 | |
Kent Hagerman | 4f355f5 | 2020-03-30 16:01:33 -0400 | [diff] [blame] | 421 | retrieved := &voltha.LogicalDevice{} |
Kent Hagerman | f5a6735 | 2020-04-30 15:15:26 -0400 | [diff] [blame] | 422 | if have, err := TestProxyRootLogicalDevice.Get(context.Background(), TestProxyTargetLogicalDeviceID, retrieved); err != nil { |
npujar | 9a30c70 | 2019-11-14 17:06:39 +0530 | [diff] [blame] | 423 | BenchmarkProxyLogger.Errorf("Failed to get logical devices due to error: %v", err) |
Thomas Lee S | e5a4401 | 2019-11-07 20:32:24 +0530 | [diff] [blame] | 424 | assert.NotNil(t, err) |
Kent Hagerman | 4f355f5 | 2020-03-30 16:01:33 -0400 | [diff] [blame] | 425 | } else if !have { |
sbarbari | 17d7e22 | 2019-11-05 10:02:29 -0500 | [diff] [blame] | 426 | t.Error("Failed to get logical device") |
| 427 | } else { |
| 428 | t.Logf("Found raw logical device (root proxy): %+v", retrieved) |
| 429 | |
Kent Hagerman | 4f355f5 | 2020-03-30 16:01:33 -0400 | [diff] [blame] | 430 | if retrieved.RootDeviceId == "" { |
sbarbari | 17d7e22 | 2019-11-05 10:02:29 -0500 | [diff] [blame] | 431 | fwVersion = 0 |
| 432 | } else { |
Kent Hagerman | 4f355f5 | 2020-03-30 16:01:33 -0400 | [diff] [blame] | 433 | fwVersion, _ = strconv.Atoi(retrieved.RootDeviceId) |
sbarbari | 17d7e22 | 2019-11-05 10:02:29 -0500 | [diff] [blame] | 434 | fwVersion++ |
| 435 | } |
| 436 | |
Kent Hagerman | 4f355f5 | 2020-03-30 16:01:33 -0400 | [diff] [blame] | 437 | retrieved.RootDeviceId = strconv.Itoa(fwVersion) |
sbarbari | 17d7e22 | 2019-11-05 10:02:29 -0500 | [diff] [blame] | 438 | |
Kent Hagerman | f5a6735 | 2020-04-30 15:15:26 -0400 | [diff] [blame] | 439 | if err := TestProxyRootLogicalDevice.Set(context.Background(), TestProxyTargetLogicalDeviceID, retrieved); err != nil { |
npujar | 9a30c70 | 2019-11-14 17:06:39 +0530 | [diff] [blame] | 440 | BenchmarkProxyLogger.Errorf("Faield to update logical device info due to error: %v", err) |
Thomas Lee S | e5a4401 | 2019-11-07 20:32:24 +0530 | [diff] [blame] | 441 | assert.NotNil(t, err) |
sbarbari | 17d7e22 | 2019-11-05 10:02:29 -0500 | [diff] [blame] | 442 | } else { |
Kent Hagerman | 4f355f5 | 2020-03-30 16:01:33 -0400 | [diff] [blame] | 443 | t.Log("Updated logical device") |
sbarbari | 17d7e22 | 2019-11-05 10:02:29 -0500 | [diff] [blame] | 444 | } |
| 445 | |
Kent Hagerman | 4f355f5 | 2020-03-30 16:01:33 -0400 | [diff] [blame] | 446 | d := &voltha.LogicalDevice{} |
Kent Hagerman | f5a6735 | 2020-04-30 15:15:26 -0400 | [diff] [blame] | 447 | if have, err := TestProxyRootLogicalDevice.Get(context.Background(), TestProxyTargetLogicalDeviceID, d); err != nil { |
npujar | 9a30c70 | 2019-11-14 17:06:39 +0530 | [diff] [blame] | 448 | BenchmarkProxyLogger.Errorf("Failed to get logical device info due to error: %v", err) |
Thomas Lee S | e5a4401 | 2019-11-07 20:32:24 +0530 | [diff] [blame] | 449 | assert.NotNil(t, err) |
Kent Hagerman | 4f355f5 | 2020-03-30 16:01:33 -0400 | [diff] [blame] | 450 | } else if !have { |
sbarbari | 17d7e22 | 2019-11-05 10:02:29 -0500 | [diff] [blame] | 451 | t.Error("Failed to find updated logical device (root proxy)") |
| 452 | } else { |
| 453 | djson, _ := json.Marshal(d) |
sbarbari | 17d7e22 | 2019-11-05 10:02:29 -0500 | [diff] [blame] | 454 | t.Logf("Found logical device (root proxy): %s raw: %+v", string(djson), d) |
| 455 | } |
| 456 | } |
| 457 | } |
| 458 | |
sbarbari | 17d7e22 | 2019-11-05 10:02:29 -0500 | [diff] [blame] | 459 | func TestProxy_2_4_1_Remove_Device(t *testing.T) { |
Kent Hagerman | 4f355f5 | 2020-03-30 16:01:33 -0400 | [diff] [blame] | 460 | if err := TestProxyRootLogicalDevice.Remove(context.Background(), "logical_devices/"+TestProxyLogicalDeviceID); err != nil { |
npujar | 9a30c70 | 2019-11-14 17:06:39 +0530 | [diff] [blame] | 461 | BenchmarkProxyLogger.Errorf("Failed to remove device from logical devices proxy due to error: %v", err) |
Kent Hagerman | 4f355f5 | 2020-03-30 16:01:33 -0400 | [diff] [blame] | 462 | t.Errorf("Failed to remove logical device: %s", err) |
sbarbari | 17d7e22 | 2019-11-05 10:02:29 -0500 | [diff] [blame] | 463 | } else { |
Kent Hagerman | 4f355f5 | 2020-03-30 16:01:33 -0400 | [diff] [blame] | 464 | t.Logf("Removed device : %+v", TestProxyLogicalDeviceID) |
sbarbari | 17d7e22 | 2019-11-05 10:02:29 -0500 | [diff] [blame] | 465 | } |
| 466 | |
Kent Hagerman | 4f355f5 | 2020-03-30 16:01:33 -0400 | [diff] [blame] | 467 | d := &voltha.LogicalDevice{} |
| 468 | if have, err := TestProxyRootLogicalDevice.Get(context.Background(), "logical_devices/"+TestProxyLogicalDeviceID, d); err != nil { |
npujar | 9a30c70 | 2019-11-14 17:06:39 +0530 | [diff] [blame] | 469 | BenchmarkProxyLogger.Errorf("Failed to get logical device info due to error: %v", err) |
Thomas Lee S | e5a4401 | 2019-11-07 20:32:24 +0530 | [diff] [blame] | 470 | assert.NotNil(t, err) |
Kent Hagerman | 4f355f5 | 2020-03-30 16:01:33 -0400 | [diff] [blame] | 471 | } else if have { |
sbarbari | 17d7e22 | 2019-11-05 10:02:29 -0500 | [diff] [blame] | 472 | djson, _ := json.Marshal(d) |
| 473 | t.Errorf("Device was not removed - %s", djson) |
| 474 | } else { |
npujar | 9a30c70 | 2019-11-14 17:06:39 +0530 | [diff] [blame] | 475 | t.Logf("Device was removed: %s", TestProxyLogicalDeviceID) |
sbarbari | 17d7e22 | 2019-11-05 10:02:29 -0500 | [diff] [blame] | 476 | } |
| 477 | } |