kdarapu | f0c0e38 | 2019-09-30 05:26:31 +0530 | [diff] [blame] | 1 | /* |
Joey Armstrong | 11f5a57 | 2024-01-12 19:11:32 -0500 | [diff] [blame] | 2 | * Copyright 2018-2024 Open Networking Foundation (ONF) and the ONF Contributors |
kdarapu | f0c0e38 | 2019-09-30 05:26:31 +0530 | [diff] [blame] | 3 | |
Joey Armstrong | 87b55f7 | 2023-06-27 12:12:53 -0400 | [diff] [blame] | 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 |
kdarapu | f0c0e38 | 2019-09-30 05:26:31 +0530 | [diff] [blame] | 7 | |
Joey Armstrong | 87b55f7 | 2023-06-27 12:12:53 -0400 | [diff] [blame] | 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
kdarapu | f0c0e38 | 2019-09-30 05:26:31 +0530 | [diff] [blame] | 9 | |
Joey Armstrong | 87b55f7 | 2023-06-27 12:12:53 -0400 | [diff] [blame] | 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. |
kdarapu | f0c0e38 | 2019-09-30 05:26:31 +0530 | [diff] [blame] | 15 | */ |
| 16 | package main |
| 17 | |
| 18 | import ( |
| 19 | "context" |
kdarapu | f0c0e38 | 2019-09-30 05:26:31 +0530 | [diff] [blame] | 20 | "testing" |
| 21 | |
khenaidoo | 106c61a | 2021-08-11 18:05:46 -0400 | [diff] [blame] | 22 | conf "github.com/opencord/voltha-lib-go/v7/pkg/config" |
| 23 | vgrpc "github.com/opencord/voltha-lib-go/v7/pkg/grpc" |
| 24 | mgrpc "github.com/opencord/voltha-lib-go/v7/pkg/mocks/grpc" |
Scott Baker | dbd960e | 2020-02-28 08:57:51 -0800 | [diff] [blame] | 25 | "github.com/opencord/voltha-openolt-adapter/internal/pkg/config" |
kdarapu | f0c0e38 | 2019-09-30 05:26:31 +0530 | [diff] [blame] | 26 | "go.etcd.io/etcd/pkg/mock/mockserver" |
| 27 | ) |
| 28 | |
kdarapu | f0c0e38 | 2019-09-30 05:26:31 +0530 | [diff] [blame] | 29 | func newMockAdapter() *adapter { |
khenaidoo | 106c61a | 2021-08-11 18:05:46 -0400 | [diff] [blame] | 30 | cf := config.NewAdapterFlags() |
| 31 | cf.KVStoreType = "etcd" |
| 32 | ad := newAdapter(cf) |
kdarapu | f0c0e38 | 2019-09-30 05:26:31 +0530 | [diff] [blame] | 33 | return ad |
| 34 | } |
| 35 | func Test_adapter_setKVClient(t *testing.T) { |
| 36 | adapt := newMockAdapter() |
| 37 | adapt1 := newMockAdapter() |
serkant.uluderya | 7b8211e | 2021-02-24 16:39:18 +0300 | [diff] [blame] | 38 | adapt1.config.KVStoreType = "etcd" |
kdarapu | f0c0e38 | 2019-09-30 05:26:31 +0530 | [diff] [blame] | 39 | adapt2 := newMockAdapter() |
| 40 | adapt2.config.KVStoreType = "" |
| 41 | a, _ := mockserver.StartMockServers(1) |
Kent Hagerman | e6ff101 | 2020-07-14 15:07:53 -0400 | [diff] [blame] | 42 | _ = a.StartAt(0) |
kdarapu | f0c0e38 | 2019-09-30 05:26:31 +0530 | [diff] [blame] | 43 | defer a.StopAt(0) |
| 44 | tests := []struct { |
| 45 | name string |
| 46 | clienttype string |
| 47 | adapter *adapter |
| 48 | wantErr bool |
| 49 | }{ |
| 50 | {"setKVClient", adapt.config.KVStoreType, adapt, false}, |
| 51 | {"setKVClient", adapt1.config.KVStoreType, adapt1, false}, |
| 52 | {"setKVClient", adapt2.config.KVStoreType, adapt2, true}, |
| 53 | } |
| 54 | for _, tt := range tests { |
| 55 | t.Run(tt.name, func(t *testing.T) { |
Neha Sharma | 96b7bf2 | 2020-06-15 10:37:32 +0000 | [diff] [blame] | 56 | if err := tt.adapter.setKVClient(context.Background()); (err != nil) != tt.wantErr { |
kdarapu | f0c0e38 | 2019-09-30 05:26:31 +0530 | [diff] [blame] | 57 | t.Errorf("adapter.setKVClient() error = %v, wantErr %v", err, tt.wantErr) |
| 58 | } |
| 59 | }) |
| 60 | } |
| 61 | } |
| 62 | |
| 63 | func Test_adapter_KVClient(t *testing.T) { |
| 64 | adapt := newMockAdapter() |
| 65 | a, _ := mockserver.StartMockServers(1) |
Kent Hagerman | e6ff101 | 2020-07-14 15:07:53 -0400 | [diff] [blame] | 66 | _ = a.StartAt(0) |
kdarapu | f0c0e38 | 2019-09-30 05:26:31 +0530 | [diff] [blame] | 67 | defer a.StopAt(0) |
| 68 | |
Neha Sharma | 96b7bf2 | 2020-06-15 10:37:32 +0000 | [diff] [blame] | 69 | if err := adapt.setKVClient(context.Background()); err != nil { |
kdarapu | f0c0e38 | 2019-09-30 05:26:31 +0530 | [diff] [blame] | 70 | t.Errorf("adapter.setKVClient() error = %v", err) |
| 71 | } |
| 72 | } |
| 73 | |
| 74 | func Test_registerWithCore(t *testing.T) { |
| 75 | ad := newMockAdapter() |
Rohan Agrawal | 828bf4e | 2019-10-22 10:13:19 +0000 | [diff] [blame] | 76 | ctx := context.TODO() |
khenaidoo | 106c61a | 2021-08-11 18:05:46 -0400 | [diff] [blame] | 77 | // Create a and start a mock Core GRPC server |
| 78 | ms, err := mgrpc.NewMockGRPCServer(ctx) |
| 79 | if err != nil { |
| 80 | t.Errorf("grpc server: expected error:nil, got error: %v", err) |
| 81 | } |
| 82 | ms.AddCoreService(ctx, &vgrpc.MockCoreServiceHandler{}) |
| 83 | go ms.Start(ctx) |
| 84 | defer ms.Stop() |
| 85 | |
khenaidoo | 27e7ac9 | 2021-12-08 14:43:09 -0500 | [diff] [blame] | 86 | if ad.coreClient, err = vgrpc.NewClient( |
| 87 | "olt-endpoint", |
| 88 | ms.ApiEndpoint, |
khenaidoo | efff76e | 2021-12-15 16:51:30 -0500 | [diff] [blame] | 89 | "core_service.CoreService", |
khenaidoo | 106c61a | 2021-08-11 18:05:46 -0400 | [diff] [blame] | 90 | ad.coreRestarted); err != nil { |
| 91 | t.Errorf("grpc client: expected error:nil, got error: %v", err) |
| 92 | } |
| 93 | // Start the core grpc client |
khenaidoo | efff76e | 2021-12-15 16:51:30 -0500 | [diff] [blame] | 94 | go ad.coreClient.Start(ctx, getCoreServiceClientHandler) |
khenaidoo | 106c61a | 2021-08-11 18:05:46 -0400 | [diff] [blame] | 95 | defer ad.coreClient.Stop(ctx) |
| 96 | err = ad.registerWithCore(ctx, coreService, 1) |
kdarapu | f0c0e38 | 2019-09-30 05:26:31 +0530 | [diff] [blame] | 97 | if err != nil { |
| 98 | t.Errorf("Expected error:nil, got error: %v", err) |
| 99 | } |
| 100 | } |
kdarapu | f0c0e38 | 2019-09-30 05:26:31 +0530 | [diff] [blame] | 101 | |
| 102 | func Test_startOpenOLT(t *testing.T) { |
| 103 | a, _ := mockserver.StartMockServers(1) |
Matteo Scandolo | dfa7a97 | 2020-11-06 13:03:40 -0800 | [diff] [blame] | 104 | cm := &conf.ConfigManager{} |
Kent Hagerman | e6ff101 | 2020-07-14 15:07:53 -0400 | [diff] [blame] | 105 | _ = a.StartAt(0) |
kdarapu | f0c0e38 | 2019-09-30 05:26:31 +0530 | [diff] [blame] | 106 | defer a.StopAt(0) |
| 107 | |
| 108 | ad := newMockAdapter() |
khenaidoo | 106c61a | 2021-08-11 18:05:46 -0400 | [diff] [blame] | 109 | oolt, err := ad.startOpenOLT(context.TODO(), nil, ad.eventProxy, ad.config, cm) |
kdarapu | f0c0e38 | 2019-09-30 05:26:31 +0530 | [diff] [blame] | 110 | if oolt != nil { |
| 111 | t.Log("Open OLT ", oolt) |
| 112 | } |
| 113 | if err != nil { |
| 114 | t.Errorf("err %v", err) |
| 115 | } |
| 116 | } |
| 117 | |
| 118 | func Test_newKafkaClient(t *testing.T) { |
| 119 | a, _ := mockserver.StartMockServers(1) |
Kent Hagerman | e6ff101 | 2020-07-14 15:07:53 -0400 | [diff] [blame] | 120 | _ = a.StartAt(0) |
kdarapu | f0c0e38 | 2019-09-30 05:26:31 +0530 | [diff] [blame] | 121 | defer a.StopAt(0) |
| 122 | adapter := newMockAdapter() |
| 123 | type args struct { |
| 124 | clientType string |
Neha Sharma | 3f221ae | 2020-04-29 19:02:12 +0000 | [diff] [blame] | 125 | address string |
kdarapu | f0c0e38 | 2019-09-30 05:26:31 +0530 | [diff] [blame] | 126 | } |
| 127 | tests := []struct { |
| 128 | name string |
| 129 | args args |
| 130 | wantErr bool |
| 131 | }{ |
| 132 | // TODO: Add test cases. |
khenaidoo | 106c61a | 2021-08-11 18:05:46 -0400 | [diff] [blame] | 133 | {"newKafkaClient", args{clientType: "sarama", address: adapter.config.KafkaClusterAddress}, false}, |
| 134 | {"newKafkaClient", args{clientType: "sarama", address: adapter.config.KafkaClusterAddress}, false}, |
kdarapu | f0c0e38 | 2019-09-30 05:26:31 +0530 | [diff] [blame] | 135 | } |
| 136 | for _, tt := range tests { |
| 137 | t.Run(tt.name, func(t *testing.T) { |
Neha Sharma | 96b7bf2 | 2020-06-15 10:37:32 +0000 | [diff] [blame] | 138 | _, err := newKafkaClient(context.Background(), tt.args.clientType, tt.args.address) |
kdarapu | f0c0e38 | 2019-09-30 05:26:31 +0530 | [diff] [blame] | 139 | if (err != nil) != tt.wantErr { |
| 140 | t.Errorf("newKafkaClient() error = %v, wantErr %v", err, tt.wantErr) |
| 141 | return |
| 142 | } |
| 143 | |
| 144 | }) |
| 145 | } |
| 146 | } |
Joey Armstrong | 87b55f7 | 2023-06-27 12:12:53 -0400 | [diff] [blame] | 147 | |
| 148 | // [EOF] |