kdarapu | f0c0e38 | 2019-09-30 05:26:31 +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 | 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 | 106c61a | 2021-08-11 18:05:46 -0400 | [diff] [blame] | 89 | ad.coreRestarted); err != nil { |
| 90 | t.Errorf("grpc client: expected error:nil, got error: %v", err) |
| 91 | } |
| 92 | // Start the core grpc client |
| 93 | go ad.coreClient.Start(ctx, setAndTestCoreServiceHandler) |
| 94 | defer ad.coreClient.Stop(ctx) |
| 95 | err = ad.registerWithCore(ctx, coreService, 1) |
kdarapu | f0c0e38 | 2019-09-30 05:26:31 +0530 | [diff] [blame] | 96 | if err != nil { |
| 97 | t.Errorf("Expected error:nil, got error: %v", err) |
| 98 | } |
| 99 | } |
kdarapu | f0c0e38 | 2019-09-30 05:26:31 +0530 | [diff] [blame] | 100 | |
| 101 | func Test_startOpenOLT(t *testing.T) { |
| 102 | a, _ := mockserver.StartMockServers(1) |
Matteo Scandolo | dfa7a97 | 2020-11-06 13:03:40 -0800 | [diff] [blame] | 103 | cm := &conf.ConfigManager{} |
Kent Hagerman | e6ff101 | 2020-07-14 15:07:53 -0400 | [diff] [blame] | 104 | _ = a.StartAt(0) |
kdarapu | f0c0e38 | 2019-09-30 05:26:31 +0530 | [diff] [blame] | 105 | defer a.StopAt(0) |
| 106 | |
| 107 | ad := newMockAdapter() |
khenaidoo | 106c61a | 2021-08-11 18:05:46 -0400 | [diff] [blame] | 108 | oolt, err := ad.startOpenOLT(context.TODO(), nil, ad.eventProxy, ad.config, cm) |
kdarapu | f0c0e38 | 2019-09-30 05:26:31 +0530 | [diff] [blame] | 109 | if oolt != nil { |
| 110 | t.Log("Open OLT ", oolt) |
| 111 | } |
| 112 | if err != nil { |
| 113 | t.Errorf("err %v", err) |
| 114 | } |
| 115 | } |
| 116 | |
| 117 | func Test_newKafkaClient(t *testing.T) { |
| 118 | a, _ := mockserver.StartMockServers(1) |
Kent Hagerman | e6ff101 | 2020-07-14 15:07:53 -0400 | [diff] [blame] | 119 | _ = a.StartAt(0) |
kdarapu | f0c0e38 | 2019-09-30 05:26:31 +0530 | [diff] [blame] | 120 | defer a.StopAt(0) |
| 121 | adapter := newMockAdapter() |
| 122 | type args struct { |
| 123 | clientType string |
Neha Sharma | 3f221ae | 2020-04-29 19:02:12 +0000 | [diff] [blame] | 124 | address string |
kdarapu | f0c0e38 | 2019-09-30 05:26:31 +0530 | [diff] [blame] | 125 | } |
| 126 | tests := []struct { |
| 127 | name string |
| 128 | args args |
| 129 | wantErr bool |
| 130 | }{ |
| 131 | // TODO: Add test cases. |
khenaidoo | 106c61a | 2021-08-11 18:05:46 -0400 | [diff] [blame] | 132 | {"newKafkaClient", args{clientType: "sarama", address: adapter.config.KafkaClusterAddress}, false}, |
| 133 | {"newKafkaClient", args{clientType: "sarama", address: adapter.config.KafkaClusterAddress}, false}, |
kdarapu | f0c0e38 | 2019-09-30 05:26:31 +0530 | [diff] [blame] | 134 | } |
| 135 | for _, tt := range tests { |
| 136 | t.Run(tt.name, func(t *testing.T) { |
Neha Sharma | 96b7bf2 | 2020-06-15 10:37:32 +0000 | [diff] [blame] | 137 | _, err := newKafkaClient(context.Background(), tt.args.clientType, tt.args.address) |
kdarapu | f0c0e38 | 2019-09-30 05:26:31 +0530 | [diff] [blame] | 138 | if (err != nil) != tt.wantErr { |
| 139 | t.Errorf("newKafkaClient() error = %v, wantErr %v", err, tt.wantErr) |
| 140 | return |
| 141 | } |
| 142 | |
| 143 | }) |
| 144 | } |
| 145 | } |