blob: a70949de80edadf4c8d977dc70bbcf075064ae32 [file] [log] [blame]
kdarapuf0c0e382019-09-30 05:26:31 +05301/*
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 */
16package main
17
18import (
19 "context"
kdarapuf0c0e382019-09-30 05:26:31 +053020 "testing"
21
khenaidoo106c61a2021-08-11 18:05:46 -040022 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 Bakerdbd960e2020-02-28 08:57:51 -080025 "github.com/opencord/voltha-openolt-adapter/internal/pkg/config"
kdarapuf0c0e382019-09-30 05:26:31 +053026 "go.etcd.io/etcd/pkg/mock/mockserver"
27)
28
kdarapuf0c0e382019-09-30 05:26:31 +053029func newMockAdapter() *adapter {
khenaidoo106c61a2021-08-11 18:05:46 -040030 cf := config.NewAdapterFlags()
31 cf.KVStoreType = "etcd"
32 ad := newAdapter(cf)
kdarapuf0c0e382019-09-30 05:26:31 +053033 return ad
34}
35func Test_adapter_setKVClient(t *testing.T) {
36 adapt := newMockAdapter()
37 adapt1 := newMockAdapter()
serkant.uluderya7b8211e2021-02-24 16:39:18 +030038 adapt1.config.KVStoreType = "etcd"
kdarapuf0c0e382019-09-30 05:26:31 +053039 adapt2 := newMockAdapter()
40 adapt2.config.KVStoreType = ""
41 a, _ := mockserver.StartMockServers(1)
Kent Hagermane6ff1012020-07-14 15:07:53 -040042 _ = a.StartAt(0)
kdarapuf0c0e382019-09-30 05:26:31 +053043 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 Sharma96b7bf22020-06-15 10:37:32 +000056 if err := tt.adapter.setKVClient(context.Background()); (err != nil) != tt.wantErr {
kdarapuf0c0e382019-09-30 05:26:31 +053057 t.Errorf("adapter.setKVClient() error = %v, wantErr %v", err, tt.wantErr)
58 }
59 })
60 }
61}
62
63func Test_adapter_KVClient(t *testing.T) {
64 adapt := newMockAdapter()
65 a, _ := mockserver.StartMockServers(1)
Kent Hagermane6ff1012020-07-14 15:07:53 -040066 _ = a.StartAt(0)
kdarapuf0c0e382019-09-30 05:26:31 +053067 defer a.StopAt(0)
68
Neha Sharma96b7bf22020-06-15 10:37:32 +000069 if err := adapt.setKVClient(context.Background()); err != nil {
kdarapuf0c0e382019-09-30 05:26:31 +053070 t.Errorf("adapter.setKVClient() error = %v", err)
71 }
72}
73
74func Test_registerWithCore(t *testing.T) {
75 ad := newMockAdapter()
Rohan Agrawal828bf4e2019-10-22 10:13:19 +000076 ctx := context.TODO()
khenaidoo106c61a2021-08-11 18:05:46 -040077 // 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
khenaidoo27e7ac92021-12-08 14:43:09 -050086 if ad.coreClient, err = vgrpc.NewClient(
87 "olt-endpoint",
88 ms.ApiEndpoint,
khenaidoo106c61a2021-08-11 18:05:46 -040089 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)
kdarapuf0c0e382019-09-30 05:26:31 +053096 if err != nil {
97 t.Errorf("Expected error:nil, got error: %v", err)
98 }
99}
kdarapuf0c0e382019-09-30 05:26:31 +0530100
101func Test_startOpenOLT(t *testing.T) {
102 a, _ := mockserver.StartMockServers(1)
Matteo Scandolodfa7a972020-11-06 13:03:40 -0800103 cm := &conf.ConfigManager{}
Kent Hagermane6ff1012020-07-14 15:07:53 -0400104 _ = a.StartAt(0)
kdarapuf0c0e382019-09-30 05:26:31 +0530105 defer a.StopAt(0)
106
107 ad := newMockAdapter()
khenaidoo106c61a2021-08-11 18:05:46 -0400108 oolt, err := ad.startOpenOLT(context.TODO(), nil, ad.eventProxy, ad.config, cm)
kdarapuf0c0e382019-09-30 05:26:31 +0530109 if oolt != nil {
110 t.Log("Open OLT ", oolt)
111 }
112 if err != nil {
113 t.Errorf("err %v", err)
114 }
115}
116
117func Test_newKafkaClient(t *testing.T) {
118 a, _ := mockserver.StartMockServers(1)
Kent Hagermane6ff1012020-07-14 15:07:53 -0400119 _ = a.StartAt(0)
kdarapuf0c0e382019-09-30 05:26:31 +0530120 defer a.StopAt(0)
121 adapter := newMockAdapter()
122 type args struct {
123 clientType string
Neha Sharma3f221ae2020-04-29 19:02:12 +0000124 address string
kdarapuf0c0e382019-09-30 05:26:31 +0530125 }
126 tests := []struct {
127 name string
128 args args
129 wantErr bool
130 }{
131 // TODO: Add test cases.
khenaidoo106c61a2021-08-11 18:05:46 -0400132 {"newKafkaClient", args{clientType: "sarama", address: adapter.config.KafkaClusterAddress}, false},
133 {"newKafkaClient", args{clientType: "sarama", address: adapter.config.KafkaClusterAddress}, false},
kdarapuf0c0e382019-09-30 05:26:31 +0530134 }
135 for _, tt := range tests {
136 t.Run(tt.name, func(t *testing.T) {
Neha Sharma96b7bf22020-06-15 10:37:32 +0000137 _, err := newKafkaClient(context.Background(), tt.args.clientType, tt.args.address)
kdarapuf0c0e382019-09-30 05:26:31 +0530138 if (err != nil) != tt.wantErr {
139 t.Errorf("newKafkaClient() error = %v, wantErr %v", err, tt.wantErr)
140 return
141 }
142
143 })
144 }
145}