Kent Hagerman | 0ab4cb2 | 2019-04-24 13:13:35 -0400 | [diff] [blame] | 1 | // +build integration |
| 2 | |
khenaidoo | 731697e | 2019-01-29 16:03:29 -0500 | [diff] [blame] | 3 | /* |
| 4 | * Copyright 2018-present Open Networking Foundation |
| 5 | |
| 6 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 7 | * you may not use this file except in compliance with the License. |
| 8 | * You may obtain a copy of the License at |
| 9 | |
| 10 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 11 | |
| 12 | * Unless required by applicable law or agreed to in writing, software |
| 13 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 14 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 15 | * See the License for the specific language governing permissions and |
| 16 | * limitations under the License. |
| 17 | */ |
Kent Hagerman | 0ab4cb2 | 2019-04-24 13:13:35 -0400 | [diff] [blame] | 18 | |
khenaidoo | 297cd25 | 2019-02-07 22:10:23 -0500 | [diff] [blame] | 19 | package api |
khenaidoo | 731697e | 2019-01-29 16:03:29 -0500 | [diff] [blame] | 20 | |
| 21 | import ( |
| 22 | "context" |
| 23 | "fmt" |
npujar | 1273234 | 2019-11-14 17:28:40 +0530 | [diff] [blame] | 24 | "os" |
| 25 | "os/exec" |
| 26 | "strings" |
| 27 | "testing" |
| 28 | "time" |
| 29 | |
khenaidoo | 731697e | 2019-01-29 16:03:29 -0500 | [diff] [blame] | 30 | "github.com/golang/protobuf/ptypes/empty" |
serkant.uluderya | 2ae470f | 2020-01-21 11:13:09 -0800 | [diff] [blame] | 31 | com "github.com/opencord/voltha-lib-go/v3/pkg/adapters/common" |
| 32 | "github.com/opencord/voltha-lib-go/v3/pkg/log" |
| 33 | "github.com/opencord/voltha-protos/v3/go/common" |
| 34 | "github.com/opencord/voltha-protos/v3/go/openflow_13" |
| 35 | "github.com/opencord/voltha-protos/v3/go/voltha" |
khenaidoo | 731697e | 2019-01-29 16:03:29 -0500 | [diff] [blame] | 36 | "github.com/stretchr/testify/assert" |
| 37 | "google.golang.org/grpc" |
| 38 | "google.golang.org/grpc/codes" |
| 39 | "google.golang.org/grpc/metadata" |
| 40 | "google.golang.org/grpc/status" |
khenaidoo | 731697e | 2019-01-29 16:03:29 -0500 | [diff] [blame] | 41 | ) |
| 42 | |
| 43 | var conn *grpc.ClientConn |
| 44 | var stub voltha.VolthaServiceClient |
| 45 | var testMode string |
| 46 | |
| 47 | /* |
| 48 | Prerequite: These tests require the rw_core to run prior to executing these test cases. |
| 49 | */ |
| 50 | |
| 51 | var devices map[string]*voltha.Device |
| 52 | |
khenaidoo | 731697e | 2019-01-29 16:03:29 -0500 | [diff] [blame] | 53 | func setup() { |
khenaidoo | 731697e | 2019-01-29 16:03:29 -0500 | [diff] [blame] | 54 | //Start kafka and Etcd |
| 55 | startKafka() |
| 56 | startEtcd() |
| 57 | time.Sleep(10 * time.Second) //TODO: Find a better way to ascertain they are up |
| 58 | |
| 59 | stub = setupGrpcConnection() |
| 60 | testMode = common.TestModeKeys_api_test.String() |
| 61 | devices = make(map[string]*voltha.Device) |
| 62 | } |
| 63 | |
| 64 | func setupGrpcConnection() voltha.VolthaServiceClient { |
| 65 | grpcHostIP := os.Getenv("DOCKER_HOST_IP") |
| 66 | grpcPort := 50057 |
| 67 | grpcHost := fmt.Sprintf("%s:%d", grpcHostIP, grpcPort) |
| 68 | var err error |
| 69 | conn, err = grpc.Dial(grpcHost, grpc.WithInsecure()) |
| 70 | if err != nil { |
Girish Kumar | f56a468 | 2020-03-20 20:07:46 +0000 | [diff] [blame] | 71 | logger.Fatalf("did not connect: %s", err) |
khenaidoo | 731697e | 2019-01-29 16:03:29 -0500 | [diff] [blame] | 72 | } |
| 73 | return voltha.NewVolthaServiceClient(conn) |
| 74 | } |
| 75 | |
| 76 | func clearAllDevices(clearMap bool) { |
| 77 | for key, _ := range devices { |
| 78 | ctx := context.Background() |
| 79 | response, err := stub.DeleteDevice(ctx, &voltha.ID{Id: key}) |
Girish Kumar | f56a468 | 2020-03-20 20:07:46 +0000 | [diff] [blame] | 80 | logger.Infow("response", log.Fields{"res": response, "error": err}) |
khenaidoo | 731697e | 2019-01-29 16:03:29 -0500 | [diff] [blame] | 81 | if clearMap { |
| 82 | delete(devices, key) |
| 83 | } |
| 84 | } |
| 85 | } |
| 86 | |
| 87 | // Verify if all ids are present in the global list of devices |
| 88 | func hasAllIds(ids *voltha.IDs) bool { |
| 89 | if ids == nil && len(devices) == 0 { |
| 90 | return true |
| 91 | } |
| 92 | if ids == nil { |
| 93 | return false |
| 94 | } |
| 95 | for _, id := range ids.Items { |
| 96 | if _, exist := devices[id.Id]; !exist { |
| 97 | return false |
| 98 | } |
| 99 | } |
| 100 | return true |
| 101 | } |
| 102 | |
| 103 | func startKafka() { |
| 104 | fmt.Println("Starting Kafka and Etcd ...") |
| 105 | command := "docker-compose" |
Kent Hagerman | 0ab4cb2 | 2019-04-24 13:13:35 -0400 | [diff] [blame] | 106 | cmd := exec.Command(command, "-f", "../../../compose/docker-compose-zk-kafka-test.yml", "up", "-d") |
khenaidoo | 731697e | 2019-01-29 16:03:29 -0500 | [diff] [blame] | 107 | if err := cmd.Run(); err != nil { |
Girish Kumar | f56a468 | 2020-03-20 20:07:46 +0000 | [diff] [blame] | 108 | logger.Fatal(err) |
khenaidoo | 731697e | 2019-01-29 16:03:29 -0500 | [diff] [blame] | 109 | } |
| 110 | } |
| 111 | |
| 112 | func startEtcd() { |
| 113 | fmt.Println("Starting Etcd ...") |
| 114 | command := "docker-compose" |
Kent Hagerman | 0ab4cb2 | 2019-04-24 13:13:35 -0400 | [diff] [blame] | 115 | cmd := exec.Command(command, "-f", "../../../compose/docker-compose-etcd.yml", "up", "-d") |
khenaidoo | 731697e | 2019-01-29 16:03:29 -0500 | [diff] [blame] | 116 | if err := cmd.Run(); err != nil { |
Girish Kumar | f56a468 | 2020-03-20 20:07:46 +0000 | [diff] [blame] | 117 | logger.Fatal(err) |
khenaidoo | 731697e | 2019-01-29 16:03:29 -0500 | [diff] [blame] | 118 | } |
| 119 | } |
| 120 | |
| 121 | func stopKafka() { |
| 122 | fmt.Println("Stopping Kafka and Etcd ...") |
| 123 | command := "docker-compose" |
Kent Hagerman | 0ab4cb2 | 2019-04-24 13:13:35 -0400 | [diff] [blame] | 124 | cmd := exec.Command(command, "-f", "../../../compose/docker-compose-zk-kafka-test.yml", "down") |
khenaidoo | 731697e | 2019-01-29 16:03:29 -0500 | [diff] [blame] | 125 | if err := cmd.Run(); err != nil { |
| 126 | // ignore error - as this is mostly due network being left behind as its being used by other |
| 127 | // containers |
Girish Kumar | f56a468 | 2020-03-20 20:07:46 +0000 | [diff] [blame] | 128 | logger.Warn(err) |
khenaidoo | 731697e | 2019-01-29 16:03:29 -0500 | [diff] [blame] | 129 | } |
| 130 | } |
| 131 | |
| 132 | func stopEtcd() { |
| 133 | fmt.Println("Stopping Etcd ...") |
| 134 | command := "docker-compose" |
Kent Hagerman | 0ab4cb2 | 2019-04-24 13:13:35 -0400 | [diff] [blame] | 135 | cmd := exec.Command(command, "-f", "../../../compose/docker-compose-etcd.yml", "down") |
khenaidoo | 731697e | 2019-01-29 16:03:29 -0500 | [diff] [blame] | 136 | if err := cmd.Run(); err != nil { |
| 137 | // ignore error - as this is mostly due network being left behind as its being used by other |
| 138 | // containers |
Girish Kumar | f56a468 | 2020-03-20 20:07:46 +0000 | [diff] [blame] | 139 | logger.Warn(err) |
khenaidoo | 731697e | 2019-01-29 16:03:29 -0500 | [diff] [blame] | 140 | } |
| 141 | } |
| 142 | |
| 143 | func startCore() { |
| 144 | fmt.Println("Starting voltha core ...") |
| 145 | command := "docker-compose" |
Kent Hagerman | 0ab4cb2 | 2019-04-24 13:13:35 -0400 | [diff] [blame] | 146 | cmd := exec.Command(command, "-f", "../../../compose/rw_core.yml", "up", "-d") |
khenaidoo | 731697e | 2019-01-29 16:03:29 -0500 | [diff] [blame] | 147 | if err := cmd.Run(); err != nil { |
Girish Kumar | f56a468 | 2020-03-20 20:07:46 +0000 | [diff] [blame] | 148 | logger.Fatal(err) |
khenaidoo | 731697e | 2019-01-29 16:03:29 -0500 | [diff] [blame] | 149 | } |
| 150 | } |
| 151 | |
| 152 | func stopCore() { |
| 153 | fmt.Println("Stopping voltha core ...") |
| 154 | command := "docker-compose" |
Kent Hagerman | 0ab4cb2 | 2019-04-24 13:13:35 -0400 | [diff] [blame] | 155 | cmd := exec.Command(command, "-f", "../../../compose/rw_core.yml", "down") |
khenaidoo | 731697e | 2019-01-29 16:03:29 -0500 | [diff] [blame] | 156 | if err := cmd.Run(); err != nil { |
| 157 | // ignore error - as this is mostly due network being left behind as its being used by other |
| 158 | // containers |
Girish Kumar | f56a468 | 2020-03-20 20:07:46 +0000 | [diff] [blame] | 159 | logger.Warn(err) |
khenaidoo | 731697e | 2019-01-29 16:03:29 -0500 | [diff] [blame] | 160 | } |
| 161 | } |
| 162 | |
| 163 | func startSimulatedOLTAndONUAdapters() { |
| 164 | fmt.Println("Starting simulated OLT and ONU adapters ...") |
| 165 | command := "docker-compose" |
Kent Hagerman | 0ab4cb2 | 2019-04-24 13:13:35 -0400 | [diff] [blame] | 166 | cmd := exec.Command(command, "-f", "../../../compose/adapters-simulated.yml", "up", "-d") |
khenaidoo | 731697e | 2019-01-29 16:03:29 -0500 | [diff] [blame] | 167 | if err := cmd.Run(); err != nil { |
Girish Kumar | f56a468 | 2020-03-20 20:07:46 +0000 | [diff] [blame] | 168 | logger.Fatal(err) |
khenaidoo | 731697e | 2019-01-29 16:03:29 -0500 | [diff] [blame] | 169 | } |
| 170 | } |
| 171 | |
| 172 | func stopSimulatedOLTAndONUAdapters() { |
| 173 | fmt.Println("Stopping simulated OLT and ONU adapters ...") |
| 174 | command := "docker-compose" |
Kent Hagerman | 0ab4cb2 | 2019-04-24 13:13:35 -0400 | [diff] [blame] | 175 | cmd := exec.Command(command, "-f", "../../../compose/adapters-simulated.yml", "down") |
khenaidoo | 731697e | 2019-01-29 16:03:29 -0500 | [diff] [blame] | 176 | if err := cmd.Run(); err != nil { |
| 177 | // ignore error - as this is mostly due network being left behind as its being used by other |
| 178 | // containers |
Girish Kumar | f56a468 | 2020-03-20 20:07:46 +0000 | [diff] [blame] | 179 | logger.Warn(err) |
khenaidoo | 731697e | 2019-01-29 16:03:29 -0500 | [diff] [blame] | 180 | } |
| 181 | } |
| 182 | |
khenaidoo | 731697e | 2019-01-29 16:03:29 -0500 | [diff] [blame] | 183 | func TestListDeviceIds(t *testing.T) { |
| 184 | fmt.Println("Testing list Devices Ids ...") |
| 185 | //0. Start kafka and Ectd |
| 186 | startKafka() |
| 187 | startEtcd() |
| 188 | |
| 189 | //1. Start the core |
| 190 | startCore() |
| 191 | |
| 192 | // Wait until it's up - TODO: find a better way to check |
| 193 | time.Sleep(10 * time.Second) |
| 194 | |
| 195 | //2. Create a set of devices into the Core |
| 196 | for i := 0; i < 10; i++ { |
| 197 | ctx := context.Background() |
| 198 | device := &voltha.Device{Type: "simulated_olt"} |
| 199 | response, err := stub.CreateDevice(ctx, device) |
Girish Kumar | f56a468 | 2020-03-20 20:07:46 +0000 | [diff] [blame] | 200 | logger.Infow("response", log.Fields{"res": response, "error": err}) |
khenaidoo | 731697e | 2019-01-29 16:03:29 -0500 | [diff] [blame] | 201 | assert.NotNil(t, response) |
| 202 | assert.Nil(t, err) |
| 203 | devices[response.Id] = response |
| 204 | } |
| 205 | |
| 206 | //3. Verify devices have been added correctly |
| 207 | ctx := context.Background() |
| 208 | response, err := stub.ListDeviceIds(ctx, &empty.Empty{}) |
Girish Kumar | f56a468 | 2020-03-20 20:07:46 +0000 | [diff] [blame] | 209 | logger.Infow("response", log.Fields{"res": response, "error": err}) |
khenaidoo | 731697e | 2019-01-29 16:03:29 -0500 | [diff] [blame] | 210 | assert.Nil(t, err) |
| 211 | assert.True(t, hasAllIds(response)) |
| 212 | |
| 213 | //4. Stop the core |
| 214 | stopCore() |
| 215 | |
| 216 | //5. Stop Kafka and Etcd |
| 217 | stopKafka() |
| 218 | stopEtcd() |
| 219 | } |
| 220 | |
| 221 | func TestReconcileDevices(t *testing.T) { |
| 222 | fmt.Println("Testing Reconcile Devices ...") |
| 223 | |
| 224 | //0. Start kafka and Ectd |
| 225 | startKafka() |
| 226 | startEtcd() |
| 227 | |
| 228 | //1. Start the core |
| 229 | startCore() |
| 230 | |
| 231 | // Wait until it's up - TODO: find a better way to check |
| 232 | time.Sleep(10 * time.Second) |
| 233 | |
| 234 | //2. Create a set of devices into the Core |
| 235 | for i := 0; i < 10; i++ { |
| 236 | ctx := context.Background() |
| 237 | device := &voltha.Device{Type: "simulated_olt"} |
| 238 | response, err := stub.CreateDevice(ctx, device) |
Girish Kumar | f56a468 | 2020-03-20 20:07:46 +0000 | [diff] [blame] | 239 | logger.Infow("response", log.Fields{"res": response, "error": err}) |
khenaidoo | 731697e | 2019-01-29 16:03:29 -0500 | [diff] [blame] | 240 | assert.Nil(t, err) |
| 241 | devices[response.Id] = response |
| 242 | } |
| 243 | //3. Verify devices have been added correctly |
| 244 | ctx := context.Background() |
| 245 | response, err := stub.ListDeviceIds(ctx, &empty.Empty{}) |
Girish Kumar | f56a468 | 2020-03-20 20:07:46 +0000 | [diff] [blame] | 246 | logger.Infow("response", log.Fields{"res": response, "error": err}) |
khenaidoo | 731697e | 2019-01-29 16:03:29 -0500 | [diff] [blame] | 247 | assert.Nil(t, err) |
| 248 | assert.True(t, hasAllIds(response)) |
| 249 | |
| 250 | //4. Stop the core and restart it. This will start the core with no data in memory but |
| 251 | // etcd will still have the data. |
| 252 | stopCore() |
| 253 | time.Sleep(5 * time.Second) |
| 254 | startCore() |
| 255 | time.Sleep(10 * time.Second) |
| 256 | |
| 257 | //5. Setup the connection again |
| 258 | stub = setupGrpcConnection() |
| 259 | |
| 260 | //6. Verify there are no devices left |
| 261 | ctx = context.Background() |
| 262 | response, err = stub.ListDeviceIds(ctx, &empty.Empty{}) |
Girish Kumar | f56a468 | 2020-03-20 20:07:46 +0000 | [diff] [blame] | 263 | logger.Infow("response", log.Fields{"res": response, "error": err}) |
khenaidoo | 731697e | 2019-01-29 16:03:29 -0500 | [diff] [blame] | 264 | assert.Nil(t, err) |
| 265 | assert.Equal(t, len(response.Items), 0) |
| 266 | |
| 267 | //7. Invoke reconcile with all stored list |
| 268 | toRestore := &voltha.IDs{Items: make([]*voltha.ID, 0)} |
| 269 | for key, _ := range devices { |
| 270 | toRestore.Items = append(toRestore.Items, &voltha.ID{Id: key}) |
| 271 | } |
| 272 | ctx = context.Background() |
| 273 | _, err = stub.ReconcileDevices(ctx, toRestore) |
| 274 | assert.Nil(t, err) |
| 275 | |
| 276 | //8. Verify all devices have been restored |
| 277 | ctx = context.Background() |
| 278 | response, err = stub.ListDeviceIds(ctx, &empty.Empty{}) |
Girish Kumar | f56a468 | 2020-03-20 20:07:46 +0000 | [diff] [blame] | 279 | logger.Infow("response", log.Fields{"res": response, "error": err}) |
khenaidoo | 731697e | 2019-01-29 16:03:29 -0500 | [diff] [blame] | 280 | assert.Nil(t, err) |
| 281 | assert.True(t, hasAllIds(response)) |
| 282 | |
| 283 | for _, id := range response.Items { |
| 284 | fmt.Println("id", id.Id) |
| 285 | } |
| 286 | |
| 287 | //9. Store the core |
| 288 | stopCore() |
| 289 | |
| 290 | //10. Stop Kafka and Etcd |
| 291 | stopKafka() |
| 292 | stopEtcd() |
| 293 | } |
| 294 | |
| 295 | func TestDeviceManagement(t *testing.T) { |
| 296 | fmt.Println("Testing Device Management ...") |
| 297 | |
| 298 | numberOfOLTDevices := 1 |
| 299 | |
| 300 | //0. Start kafka and Ectd |
| 301 | startKafka() |
| 302 | startEtcd() |
| 303 | |
| 304 | //1. Start the core |
| 305 | startCore() |
| 306 | |
| 307 | //2. Start the simulated adapters |
| 308 | startSimulatedOLTAndONUAdapters() |
| 309 | |
| 310 | // Wait until the core and adapters sync up |
| 311 | time.Sleep(10 * time.Second) |
| 312 | |
| 313 | //3. Create a set of devices into the Core |
| 314 | devices = make(map[string]*voltha.Device) |
| 315 | logicalDevices := make(map[string]*voltha.LogicalDevice) |
| 316 | for i := 0; i < numberOfOLTDevices; i++ { |
| 317 | ctx := context.Background() |
| 318 | randomMacAddress := strings.ToUpper(com.GetRandomMacAddress()) |
Kent Hagerman | 0ab4cb2 | 2019-04-24 13:13:35 -0400 | [diff] [blame] | 319 | device := &voltha.Device{Type: "simulated_olt", MacAddress: randomMacAddress} |
khenaidoo | 731697e | 2019-01-29 16:03:29 -0500 | [diff] [blame] | 320 | response, err := stub.CreateDevice(ctx, device) |
Girish Kumar | f56a468 | 2020-03-20 20:07:46 +0000 | [diff] [blame] | 321 | logger.Infow("response", log.Fields{"res": response, "error": err}) |
khenaidoo | 731697e | 2019-01-29 16:03:29 -0500 | [diff] [blame] | 322 | assert.Nil(t, err) |
| 323 | devices[response.Id] = response |
| 324 | } |
| 325 | |
| 326 | //4. Enable all the devices |
| 327 | for id, _ := range devices { |
| 328 | ctx := context.Background() |
Kent Hagerman | 0ab4cb2 | 2019-04-24 13:13:35 -0400 | [diff] [blame] | 329 | response, err := stub.EnableDevice(ctx, &common.ID{Id: id}) |
Girish Kumar | f56a468 | 2020-03-20 20:07:46 +0000 | [diff] [blame] | 330 | logger.Infow("response", log.Fields{"res": response, "error": err}) |
khenaidoo | 731697e | 2019-01-29 16:03:29 -0500 | [diff] [blame] | 331 | assert.Nil(t, err) |
| 332 | } |
| 333 | |
| 334 | // Wait until all devices have been enabled |
| 335 | if numberOfOLTDevices < 5 { |
| 336 | time.Sleep(3 * time.Second) |
| 337 | } else if numberOfOLTDevices < 20 { |
| 338 | time.Sleep(20 * time.Second) |
| 339 | } else { |
| 340 | time.Sleep(30 * time.Second) |
| 341 | } |
| 342 | //time.Sleep(1 * time.Second * time.Duration(numberOfDevices)) |
| 343 | |
| 344 | //5. Verify that all devices are in enabled state |
| 345 | ctx := context.Background() |
| 346 | response, err := stub.ListDevices(ctx, &empty.Empty{}) |
Girish Kumar | f56a468 | 2020-03-20 20:07:46 +0000 | [diff] [blame] | 347 | logger.Infow("response", log.Fields{"res": response, "error": err}) |
khenaidoo | 731697e | 2019-01-29 16:03:29 -0500 | [diff] [blame] | 348 | assert.Nil(t, err) |
| 349 | assert.Equal(t, len(devices)*2, len(response.Items)) |
Kent Hagerman | 0ab4cb2 | 2019-04-24 13:13:35 -0400 | [diff] [blame] | 350 | for _, d := range response.Items { |
khenaidoo | 731697e | 2019-01-29 16:03:29 -0500 | [diff] [blame] | 351 | devices[d.Id] = d |
| 352 | assert.Equal(t, d.AdminState, voltha.AdminState_ENABLED) |
| 353 | } |
| 354 | |
| 355 | //6. Get the logical devices |
| 356 | ctx = context.Background() |
| 357 | lresponse, lerr := stub.ListLogicalDevices(ctx, &empty.Empty{}) |
Girish Kumar | f56a468 | 2020-03-20 20:07:46 +0000 | [diff] [blame] | 358 | logger.Infow("response", log.Fields{"res": response, "error": lerr}) |
khenaidoo | 731697e | 2019-01-29 16:03:29 -0500 | [diff] [blame] | 359 | assert.Nil(t, lerr) |
| 360 | assert.Equal(t, numberOfOLTDevices, len(lresponse.Items)) |
Kent Hagerman | 0ab4cb2 | 2019-04-24 13:13:35 -0400 | [diff] [blame] | 361 | for _, ld := range lresponse.Items { |
khenaidoo | 731697e | 2019-01-29 16:03:29 -0500 | [diff] [blame] | 362 | logicalDevices[ld.Id] = ld |
| 363 | // Ensure each logical device have two ports |
Kent Hagerman | fa9d6d4 | 2020-05-25 11:49:40 -0400 | [diff] [blame] | 364 | ports, err := stub.ListLogicalDevicePorts(ctx, &voltha.ID{Id: ld.Id}) |
| 365 | assert.Nil(t, err) |
| 366 | assert.Equal(t, 2, len(ports.Items)) |
khenaidoo | 731697e | 2019-01-29 16:03:29 -0500 | [diff] [blame] | 367 | } |
| 368 | |
| 369 | //7. Disable all ONUs & check status & check logical device |
| 370 | for id, d := range devices { |
| 371 | ctx := context.Background() |
| 372 | if d.Type == "simulated_onu" { |
Kent Hagerman | 0ab4cb2 | 2019-04-24 13:13:35 -0400 | [diff] [blame] | 373 | response, err := stub.DisableDevice(ctx, &common.ID{Id: id}) |
Girish Kumar | f56a468 | 2020-03-20 20:07:46 +0000 | [diff] [blame] | 374 | logger.Infow("response", log.Fields{"res": response, "error": err}) |
khenaidoo | 731697e | 2019-01-29 16:03:29 -0500 | [diff] [blame] | 375 | assert.Nil(t, err) |
| 376 | } |
| 377 | } |
| 378 | |
| 379 | // Wait for all the changes to be populated |
| 380 | time.Sleep(3 * time.Second) |
| 381 | |
| 382 | ctx = context.Background() |
| 383 | response, err = stub.ListDevices(ctx, &empty.Empty{}) |
Girish Kumar | f56a468 | 2020-03-20 20:07:46 +0000 | [diff] [blame] | 384 | logger.Infow("response", log.Fields{"res": response, "error": err}) |
khenaidoo | 731697e | 2019-01-29 16:03:29 -0500 | [diff] [blame] | 385 | assert.Nil(t, err) |
| 386 | assert.Equal(t, len(devices), len(response.Items)) |
Kent Hagerman | 0ab4cb2 | 2019-04-24 13:13:35 -0400 | [diff] [blame] | 387 | for _, d := range response.Items { |
khenaidoo | 731697e | 2019-01-29 16:03:29 -0500 | [diff] [blame] | 388 | if d.Type == "simulated_onu" { |
| 389 | assert.Equal(t, d.AdminState, voltha.AdminState_DISABLED) |
| 390 | devices[d.Id] = d |
| 391 | } else { |
| 392 | assert.Equal(t, d.AdminState, voltha.AdminState_ENABLED) |
| 393 | } |
| 394 | } |
| 395 | |
| 396 | ctx = context.Background() |
| 397 | lresponse, lerr = stub.ListLogicalDevices(ctx, &empty.Empty{}) |
Girish Kumar | f56a468 | 2020-03-20 20:07:46 +0000 | [diff] [blame] | 398 | logger.Infow("response", log.Fields{"res": response, "error": lerr}) |
khenaidoo | 731697e | 2019-01-29 16:03:29 -0500 | [diff] [blame] | 399 | assert.Nil(t, lerr) |
| 400 | assert.Equal(t, numberOfOLTDevices, len(lresponse.Items)) |
Kent Hagerman | 0ab4cb2 | 2019-04-24 13:13:35 -0400 | [diff] [blame] | 401 | for _, ld := range lresponse.Items { |
khenaidoo | 731697e | 2019-01-29 16:03:29 -0500 | [diff] [blame] | 402 | logicalDevices[ld.Id] = ld |
| 403 | // Ensure each logical device have one port - only olt port |
Kent Hagerman | fa9d6d4 | 2020-05-25 11:49:40 -0400 | [diff] [blame] | 404 | ports, err := stub.ListLogicalDevicePorts(ctx, &common.ID{Id: ld.Id}) |
| 405 | assert.Nil(t, err) |
| 406 | assert.Equal(t, 1, len(ports.Items)) |
khenaidoo | 731697e | 2019-01-29 16:03:29 -0500 | [diff] [blame] | 407 | } |
| 408 | |
| 409 | //8. Enable all ONUs & check status & check logical device |
| 410 | for id, d := range devices { |
| 411 | ctx := context.Background() |
| 412 | if d.Type == "simulated_onu" { |
Kent Hagerman | 0ab4cb2 | 2019-04-24 13:13:35 -0400 | [diff] [blame] | 413 | response, err := stub.EnableDevice(ctx, &common.ID{Id: id}) |
Girish Kumar | f56a468 | 2020-03-20 20:07:46 +0000 | [diff] [blame] | 414 | logger.Infow("response", log.Fields{"res": response, "error": err}) |
khenaidoo | 731697e | 2019-01-29 16:03:29 -0500 | [diff] [blame] | 415 | assert.Nil(t, err) |
| 416 | } |
| 417 | } |
| 418 | |
| 419 | // Wait for all the changes to be populated |
| 420 | time.Sleep(3 * time.Second) |
| 421 | |
| 422 | ctx = context.Background() |
| 423 | response, err = stub.ListDevices(ctx, &empty.Empty{}) |
Girish Kumar | f56a468 | 2020-03-20 20:07:46 +0000 | [diff] [blame] | 424 | logger.Infow("response", log.Fields{"res": response, "error": err}) |
khenaidoo | 731697e | 2019-01-29 16:03:29 -0500 | [diff] [blame] | 425 | assert.Nil(t, err) |
| 426 | assert.Equal(t, len(devices), len(response.Items)) |
Kent Hagerman | 0ab4cb2 | 2019-04-24 13:13:35 -0400 | [diff] [blame] | 427 | for _, d := range response.Items { |
khenaidoo | 731697e | 2019-01-29 16:03:29 -0500 | [diff] [blame] | 428 | assert.Equal(t, d.AdminState, voltha.AdminState_ENABLED) |
| 429 | devices[d.Id] = d |
| 430 | } |
| 431 | |
| 432 | //ctx = context.Background() |
| 433 | //lresponse, lerr = stub.ListLogicalDevices(ctx, &empty.Empty{}) |
Girish Kumar | f56a468 | 2020-03-20 20:07:46 +0000 | [diff] [blame] | 434 | //logger.Infow("response", log.Fields{"res": response, "error": lerr}) |
khenaidoo | 731697e | 2019-01-29 16:03:29 -0500 | [diff] [blame] | 435 | //assert.Nil(t, lerr) |
| 436 | //assert.Equal(t, numberOfOLTDevices, len(lresponse.Items)) |
| 437 | //for _, ld := range (lresponse.Items) { |
| 438 | // logicalDevices[ld.Id] = ld |
| 439 | // // Ensure each logical device have two ports |
| 440 | // assert.Equal(t, 2, len(ld.Ports)) |
| 441 | //} |
| 442 | |
| 443 | //9. Disable all OLTs & check status & check logical device |
| 444 | |
| 445 | //10. Enable all OLTs & Enable all ONUs & check status & check logical device |
| 446 | |
| 447 | //11. Disable all OLTs & check status & check logical device |
| 448 | |
| 449 | //12. Delete all Devices & check status & check logical device |
| 450 | |
| 451 | ////13. Store simulated adapters |
| 452 | //stopSimulatedOLTAndONUAdapters() |
| 453 | // |
| 454 | ////14. Store the core |
| 455 | //stopCore() |
| 456 | // |
| 457 | ////15. Stop Kafka and Etcd |
| 458 | //stopKafka() |
| 459 | //stopEtcd() |
| 460 | } |
| 461 | |
khenaidoo | 731697e | 2019-01-29 16:03:29 -0500 | [diff] [blame] | 462 | func TestGetDevice(t *testing.T) { |
| 463 | var id common.ID |
| 464 | id.Id = "anyid" |
| 465 | ctx := metadata.NewOutgoingContext(context.Background(), metadata.Pairs(testMode, "true")) |
| 466 | response, err := stub.GetDevice(ctx, &id) |
| 467 | assert.Nil(t, response) |
| 468 | st, _ := status.FromError(err) |
| 469 | assert.Equal(t, id.Id, st.Message()) |
| 470 | assert.Equal(t, codes.NotFound, st.Code()) |
| 471 | } |
| 472 | |
| 473 | func TestUpdateLogLevelError(t *testing.T) { |
| 474 | ctx := metadata.NewOutgoingContext(context.Background(), metadata.Pairs(testMode, "true")) |
| 475 | level := voltha.Logging{PackageName: "github.com/opencord/voltha-go/rw_core/core", Level: common.LogLevel_ERROR} |
| 476 | response, err := stub.UpdateLogLevel(ctx, &level) |
Girish Kumar | f56a468 | 2020-03-20 20:07:46 +0000 | [diff] [blame] | 477 | logger.Infow("response", log.Fields{"res": response, "error": err}) |
khenaidoo | 731697e | 2019-01-29 16:03:29 -0500 | [diff] [blame] | 478 | assert.Equal(t, &empty.Empty{}, response) |
| 479 | assert.Nil(t, err) |
| 480 | } |
| 481 | |
| 482 | func TestGetVoltha(t *testing.T) { |
| 483 | ctx := metadata.NewOutgoingContext(context.Background(), metadata.Pairs(testMode, "true")) |
| 484 | response, err := stub.GetVoltha(ctx, &empty.Empty{}) |
| 485 | assert.Nil(t, response) |
| 486 | st, _ := status.FromError(err) |
| 487 | assert.Equal(t, "UnImplemented", st.Message()) |
| 488 | } |
| 489 | |
| 490 | func TestUpdateLogLevelDebug(t *testing.T) { |
| 491 | ctx := metadata.NewOutgoingContext(context.Background(), metadata.Pairs(testMode, "true")) |
| 492 | level := voltha.Logging{PackageName: "github.com/opencord/voltha-go/rw_core/core", Level: common.LogLevel_DEBUG} |
| 493 | response, err := stub.UpdateLogLevel(ctx, &level) |
Girish Kumar | f56a468 | 2020-03-20 20:07:46 +0000 | [diff] [blame] | 494 | logger.Infow("response", log.Fields{"res": response, "error": err}) |
khenaidoo | 731697e | 2019-01-29 16:03:29 -0500 | [diff] [blame] | 495 | assert.Equal(t, &empty.Empty{}, response) |
| 496 | assert.Nil(t, err) |
| 497 | } |
| 498 | |
| 499 | func TestGetCoreInstance(t *testing.T) { |
| 500 | id := &voltha.ID{Id: "getCoreInstance"} |
| 501 | ctx := metadata.NewOutgoingContext(context.Background(), metadata.Pairs(testMode, "true")) |
| 502 | response, err := stub.GetCoreInstance(ctx, id) |
| 503 | assert.Nil(t, response) |
| 504 | st, _ := status.FromError(err) |
| 505 | assert.Equal(t, "UnImplemented", st.Message()) |
| 506 | } |
| 507 | |
| 508 | func TestGetLogicalDevice(t *testing.T) { |
| 509 | id := &voltha.ID{Id: "getLogicalDevice"} |
| 510 | ctx := metadata.NewOutgoingContext(context.Background(), metadata.Pairs(testMode, "true")) |
| 511 | response, err := stub.GetLogicalDevice(ctx, id) |
| 512 | assert.Nil(t, response) |
| 513 | st, _ := status.FromError(err) |
| 514 | assert.Equal(t, id.Id, st.Message()) |
| 515 | assert.Equal(t, codes.NotFound, st.Code()) |
| 516 | } |
| 517 | |
| 518 | func TestGetLogicalDevicePort(t *testing.T) { |
| 519 | id := &voltha.LogicalPortId{Id: "GetLogicalDevicePort"} |
| 520 | ctx := metadata.NewOutgoingContext(context.Background(), metadata.Pairs(testMode, "true")) |
| 521 | response, err := stub.GetLogicalDevicePort(ctx, id) |
| 522 | assert.Nil(t, response) |
| 523 | st, _ := status.FromError(err) |
| 524 | assert.Equal(t, "UnImplemented", st.Message()) |
| 525 | } |
| 526 | |
| 527 | func TestListLogicalDevicePorts(t *testing.T) { |
| 528 | id := &voltha.ID{Id: "listLogicalDevicePorts"} |
| 529 | ctx := metadata.NewOutgoingContext(context.Background(), metadata.Pairs(testMode, "true")) |
| 530 | response, err := stub.ListLogicalDevicePorts(ctx, id) |
| 531 | assert.Nil(t, response) |
| 532 | st, _ := status.FromError(err) |
| 533 | assert.Equal(t, "UnImplemented", st.Message()) |
| 534 | } |
| 535 | |
| 536 | func TestListLogicalDeviceFlows(t *testing.T) { |
| 537 | id := &voltha.ID{Id: "ListLogicalDeviceFlows"} |
| 538 | ctx := metadata.NewOutgoingContext(context.Background(), metadata.Pairs(testMode, "true")) |
| 539 | response, err := stub.ListLogicalDeviceFlows(ctx, id) |
| 540 | assert.Nil(t, response) |
| 541 | st, _ := status.FromError(err) |
| 542 | assert.Equal(t, "UnImplemented", st.Message()) |
| 543 | } |
| 544 | |
| 545 | func TestListLogicalDeviceFlowGroups(t *testing.T) { |
| 546 | id := &voltha.ID{Id: "ListLogicalDeviceFlowGroups"} |
| 547 | ctx := metadata.NewOutgoingContext(context.Background(), metadata.Pairs(testMode, "true")) |
| 548 | response, err := stub.ListLogicalDeviceFlowGroups(ctx, id) |
| 549 | assert.Nil(t, response) |
| 550 | st, _ := status.FromError(err) |
| 551 | assert.Equal(t, "UnImplemented", st.Message()) |
| 552 | } |
| 553 | |
| 554 | func TestListDevices(t *testing.T) { |
| 555 | ctx := metadata.NewOutgoingContext(context.Background(), metadata.Pairs(testMode, "true")) |
| 556 | response, _ := stub.ListDevices(ctx, &empty.Empty{}) |
| 557 | assert.Equal(t, len(response.Items), 0) |
| 558 | } |
| 559 | |
| 560 | func TestListAdapters(t *testing.T) { |
| 561 | ctx := metadata.NewOutgoingContext(context.Background(), metadata.Pairs(testMode, "true")) |
| 562 | response, err := stub.ListAdapters(ctx, &empty.Empty{}) |
| 563 | assert.Nil(t, response) |
| 564 | st, _ := status.FromError(err) |
| 565 | assert.Equal(t, "UnImplemented", st.Message()) |
| 566 | } |
| 567 | |
| 568 | func TestListLogicalDevices(t *testing.T) { |
| 569 | ctx := metadata.NewOutgoingContext(context.Background(), metadata.Pairs(testMode, "true")) |
| 570 | response, _ := stub.ListLogicalDevices(ctx, &empty.Empty{}) |
| 571 | assert.Equal(t, len(response.Items), 0) |
| 572 | } |
| 573 | |
| 574 | func TestListCoreInstances(t *testing.T) { |
| 575 | ctx := metadata.NewOutgoingContext(context.Background(), metadata.Pairs(testMode, "true")) |
| 576 | response, err := stub.ListCoreInstances(ctx, &empty.Empty{}) |
| 577 | assert.Nil(t, response) |
| 578 | st, _ := status.FromError(err) |
| 579 | assert.Equal(t, "UnImplemented", st.Message()) |
| 580 | } |
| 581 | |
| 582 | func TestCreateDevice(t *testing.T) { |
| 583 | ctx := metadata.NewOutgoingContext(context.Background(), metadata.Pairs(testMode, "true")) |
| 584 | device := &voltha.Device{Id: "newdevice"} |
| 585 | response, err := stub.CreateDevice(ctx, device) |
Girish Kumar | f56a468 | 2020-03-20 20:07:46 +0000 | [diff] [blame] | 586 | logger.Infow("response", log.Fields{"res": response, "error": err}) |
khenaidoo | 731697e | 2019-01-29 16:03:29 -0500 | [diff] [blame] | 587 | assert.Equal(t, &voltha.Device{Id: "newdevice"}, response) |
| 588 | assert.Nil(t, err) |
| 589 | } |
| 590 | |
| 591 | func TestEnableDevice(t *testing.T) { |
| 592 | ctx := metadata.NewOutgoingContext(context.Background(), metadata.Pairs(testMode, "true")) |
| 593 | id := &voltha.ID{Id: "enabledevice"} |
| 594 | response, err := stub.EnableDevice(ctx, id) |
Girish Kumar | f56a468 | 2020-03-20 20:07:46 +0000 | [diff] [blame] | 595 | logger.Infow("response", log.Fields{"res": response, "error": err}) |
khenaidoo | 731697e | 2019-01-29 16:03:29 -0500 | [diff] [blame] | 596 | assert.Equal(t, &empty.Empty{}, response) |
| 597 | assert.Nil(t, err) |
| 598 | } |
| 599 | |
| 600 | func TestDisableDevice(t *testing.T) { |
| 601 | ctx := metadata.NewOutgoingContext(context.Background(), metadata.Pairs(testMode, "true")) |
| 602 | id := &voltha.ID{Id: "DisableDevice"} |
| 603 | response, err := stub.DisableDevice(ctx, id) |
Girish Kumar | f56a468 | 2020-03-20 20:07:46 +0000 | [diff] [blame] | 604 | logger.Infow("response", log.Fields{"res": response, "error": err}) |
khenaidoo | 731697e | 2019-01-29 16:03:29 -0500 | [diff] [blame] | 605 | assert.Equal(t, &empty.Empty{}, response) |
| 606 | assert.Nil(t, err) |
| 607 | } |
| 608 | |
| 609 | func TestRebootDevice(t *testing.T) { |
| 610 | ctx := metadata.NewOutgoingContext(context.Background(), metadata.Pairs(testMode, "true")) |
| 611 | id := &voltha.ID{Id: "RebootDevice"} |
| 612 | response, err := stub.RebootDevice(ctx, id) |
Girish Kumar | f56a468 | 2020-03-20 20:07:46 +0000 | [diff] [blame] | 613 | logger.Infow("response", log.Fields{"res": response, "error": err}) |
khenaidoo | 731697e | 2019-01-29 16:03:29 -0500 | [diff] [blame] | 614 | assert.Equal(t, &empty.Empty{}, response) |
| 615 | assert.Nil(t, err) |
| 616 | } |
| 617 | |
| 618 | func TestDeleteDevice(t *testing.T) { |
| 619 | ctx := metadata.NewOutgoingContext(context.Background(), metadata.Pairs(testMode, "true")) |
| 620 | id := &voltha.ID{Id: "DeleteDevice"} |
| 621 | response, err := stub.DeleteDevice(ctx, id) |
Girish Kumar | f56a468 | 2020-03-20 20:07:46 +0000 | [diff] [blame] | 622 | logger.Infow("response", log.Fields{"res": response, "error": err}) |
khenaidoo | 731697e | 2019-01-29 16:03:29 -0500 | [diff] [blame] | 623 | assert.Equal(t, &empty.Empty{}, response) |
| 624 | assert.Nil(t, err) |
| 625 | } |
| 626 | |
| 627 | func TestEnableLogicalDevicePort(t *testing.T) { |
| 628 | ctx := metadata.NewOutgoingContext(context.Background(), metadata.Pairs(testMode, "true")) |
| 629 | id := &voltha.LogicalPortId{Id: "EnableLogicalDevicePort"} |
| 630 | response, err := stub.EnableLogicalDevicePort(ctx, id) |
| 631 | if e, ok := status.FromError(err); ok { |
Girish Kumar | f56a468 | 2020-03-20 20:07:46 +0000 | [diff] [blame] | 632 | logger.Infow("response", log.Fields{"error": err, "errorcode": e.Code(), "msg": e.Message()}) |
khenaidoo | 731697e | 2019-01-29 16:03:29 -0500 | [diff] [blame] | 633 | } |
Girish Kumar | f56a468 | 2020-03-20 20:07:46 +0000 | [diff] [blame] | 634 | logger.Infow("response", log.Fields{"res": response, "error": err}) |
khenaidoo | 731697e | 2019-01-29 16:03:29 -0500 | [diff] [blame] | 635 | assert.Equal(t, &empty.Empty{}, response) |
| 636 | assert.Nil(t, err) |
| 637 | } |
| 638 | |
| 639 | func TestDisableLogicalDevicePort(t *testing.T) { |
| 640 | ctx := metadata.NewOutgoingContext(context.Background(), metadata.Pairs(testMode, "true")) |
| 641 | id := &voltha.LogicalPortId{Id: "DisableLogicalDevicePort"} |
| 642 | response, err := stub.DisableLogicalDevicePort(ctx, id) |
Girish Kumar | f56a468 | 2020-03-20 20:07:46 +0000 | [diff] [blame] | 643 | logger.Infow("response", log.Fields{"res": response, "error": err}) |
khenaidoo | 731697e | 2019-01-29 16:03:29 -0500 | [diff] [blame] | 644 | assert.Equal(t, &empty.Empty{}, response) |
| 645 | assert.Nil(t, err) |
| 646 | } |
| 647 | |
| 648 | func TestUpdateLogicalDeviceFlowGroupTable(t *testing.T) { |
| 649 | ctx := metadata.NewOutgoingContext(context.Background(), metadata.Pairs(testMode, "true")) |
| 650 | flow := &openflow_13.FlowGroupTableUpdate{Id: "UpdateLogicalDeviceFlowGroupTable"} |
| 651 | response, err := stub.UpdateLogicalDeviceFlowGroupTable(ctx, flow) |
Girish Kumar | f56a468 | 2020-03-20 20:07:46 +0000 | [diff] [blame] | 652 | logger.Infow("response", log.Fields{"res": response, "error": err}) |
khenaidoo | 731697e | 2019-01-29 16:03:29 -0500 | [diff] [blame] | 653 | assert.Equal(t, &empty.Empty{}, response) |
| 654 | assert.Nil(t, err) |
| 655 | } |
| 656 | |
| 657 | func TestGetImageDownloadStatus(t *testing.T) { |
| 658 | ctx := metadata.NewOutgoingContext(context.Background(), metadata.Pairs(testMode, "true")) |
| 659 | img := &voltha.ImageDownload{Id: "GetImageDownloadStatus"} |
| 660 | response, err := stub.GetImageDownloadStatus(ctx, img) |
| 661 | assert.Nil(t, response) |
| 662 | st, _ := status.FromError(err) |
| 663 | assert.Equal(t, "UnImplemented", st.Message()) |
| 664 | } |
| 665 | |
| 666 | // TODO: complete the remaining tests |
| 667 | |
| 668 | func shutdown() { |
| 669 | conn.Close() |
| 670 | } |
| 671 | |
| 672 | func TestMain(m *testing.M) { |
| 673 | setup() |
| 674 | code := m.Run() |
| 675 | shutdown() |
| 676 | os.Exit(code) |
| 677 | } |