blob: 378f885fb3247877fe5ffd57bf3a5ec267655e04 [file] [log] [blame]
kdarapuf0c0e382019-09-30 05:26:31 +05301/*
Joey Armstrong87b55f72023-06-27 12:12:53 -04002* Copyright 2018-2023 Open Networking Foundation (ONF) and the ONF Contributors
kdarapuf0c0e382019-09-30 05:26:31 +05303
Joey Armstrong87b55f72023-06-27 12:12:53 -04004* 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
kdarapuf0c0e382019-09-30 05:26:31 +05307
Joey Armstrong87b55f72023-06-27 12:12:53 -04008* http://www.apache.org/licenses/LICENSE-2.0
kdarapuf0c0e382019-09-30 05:26:31 +05309
Joey Armstrong87b55f72023-06-27 12:12:53 -040010* 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.
kdarapuf0c0e382019-09-30 05:26:31 +053015 */
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,
khenaidooefff76e2021-12-15 16:51:30 -050089 "core_service.CoreService",
khenaidoo106c61a2021-08-11 18:05:46 -040090 ad.coreRestarted); err != nil {
91 t.Errorf("grpc client: expected error:nil, got error: %v", err)
92 }
93 // Start the core grpc client
khenaidooefff76e2021-12-15 16:51:30 -050094 go ad.coreClient.Start(ctx, getCoreServiceClientHandler)
khenaidoo106c61a2021-08-11 18:05:46 -040095 defer ad.coreClient.Stop(ctx)
96 err = ad.registerWithCore(ctx, coreService, 1)
kdarapuf0c0e382019-09-30 05:26:31 +053097 if err != nil {
98 t.Errorf("Expected error:nil, got error: %v", err)
99 }
100}
kdarapuf0c0e382019-09-30 05:26:31 +0530101
102func Test_startOpenOLT(t *testing.T) {
103 a, _ := mockserver.StartMockServers(1)
Matteo Scandolodfa7a972020-11-06 13:03:40 -0800104 cm := &conf.ConfigManager{}
Kent Hagermane6ff1012020-07-14 15:07:53 -0400105 _ = a.StartAt(0)
kdarapuf0c0e382019-09-30 05:26:31 +0530106 defer a.StopAt(0)
107
108 ad := newMockAdapter()
khenaidoo106c61a2021-08-11 18:05:46 -0400109 oolt, err := ad.startOpenOLT(context.TODO(), nil, ad.eventProxy, ad.config, cm)
kdarapuf0c0e382019-09-30 05:26:31 +0530110 if oolt != nil {
111 t.Log("Open OLT ", oolt)
112 }
113 if err != nil {
114 t.Errorf("err %v", err)
115 }
116}
117
118func Test_newKafkaClient(t *testing.T) {
119 a, _ := mockserver.StartMockServers(1)
Kent Hagermane6ff1012020-07-14 15:07:53 -0400120 _ = a.StartAt(0)
kdarapuf0c0e382019-09-30 05:26:31 +0530121 defer a.StopAt(0)
122 adapter := newMockAdapter()
123 type args struct {
124 clientType string
Neha Sharma3f221ae2020-04-29 19:02:12 +0000125 address string
kdarapuf0c0e382019-09-30 05:26:31 +0530126 }
127 tests := []struct {
128 name string
129 args args
130 wantErr bool
131 }{
132 // TODO: Add test cases.
khenaidoo106c61a2021-08-11 18:05:46 -0400133 {"newKafkaClient", args{clientType: "sarama", address: adapter.config.KafkaClusterAddress}, false},
134 {"newKafkaClient", args{clientType: "sarama", address: adapter.config.KafkaClusterAddress}, false},
kdarapuf0c0e382019-09-30 05:26:31 +0530135 }
136 for _, tt := range tests {
137 t.Run(tt.name, func(t *testing.T) {
Neha Sharma96b7bf22020-06-15 10:37:32 +0000138 _, err := newKafkaClient(context.Background(), tt.args.clientType, tt.args.address)
kdarapuf0c0e382019-09-30 05:26:31 +0530139 if (err != nil) != tt.wantErr {
140 t.Errorf("newKafkaClient() error = %v, wantErr %v", err, tt.wantErr)
141 return
142 }
143
144 })
145 }
146}
Joey Armstrong87b55f72023-06-27 12:12:53 -0400147
148// [EOF]