blob: 3f7e52e8f1e7e4d4a826871a937d05c1546d6627 [file] [log] [blame]
kdarapuf0c0e382019-09-30 05:26:31 +05301/*
Joey Armstrong11f5a572024-01-12 19:11:32 -05002* Copyright 2018-2024 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}
Akash Kankanala041a2122024-10-16 15:49:22 +053035
kdarapuf0c0e382019-09-30 05:26:31 +053036func Test_adapter_setKVClient(t *testing.T) {
37 adapt := newMockAdapter()
38 adapt1 := newMockAdapter()
serkant.uluderya7b8211e2021-02-24 16:39:18 +030039 adapt1.config.KVStoreType = "etcd"
kdarapuf0c0e382019-09-30 05:26:31 +053040 adapt2 := newMockAdapter()
41 adapt2.config.KVStoreType = ""
42 a, _ := mockserver.StartMockServers(1)
Kent Hagermane6ff1012020-07-14 15:07:53 -040043 _ = a.StartAt(0)
kdarapuf0c0e382019-09-30 05:26:31 +053044 defer a.StopAt(0)
45 tests := []struct {
46 name string
47 clienttype string
48 adapter *adapter
49 wantErr bool
50 }{
51 {"setKVClient", adapt.config.KVStoreType, adapt, false},
52 {"setKVClient", adapt1.config.KVStoreType, adapt1, false},
53 {"setKVClient", adapt2.config.KVStoreType, adapt2, true},
54 }
55 for _, tt := range tests {
56 t.Run(tt.name, func(t *testing.T) {
Neha Sharma96b7bf22020-06-15 10:37:32 +000057 if err := tt.adapter.setKVClient(context.Background()); (err != nil) != tt.wantErr {
kdarapuf0c0e382019-09-30 05:26:31 +053058 t.Errorf("adapter.setKVClient() error = %v, wantErr %v", err, tt.wantErr)
59 }
60 })
61 }
62}
63
64func Test_adapter_KVClient(t *testing.T) {
65 adapt := newMockAdapter()
66 a, _ := mockserver.StartMockServers(1)
Kent Hagermane6ff1012020-07-14 15:07:53 -040067 _ = a.StartAt(0)
kdarapuf0c0e382019-09-30 05:26:31 +053068 defer a.StopAt(0)
69
Neha Sharma96b7bf22020-06-15 10:37:32 +000070 if err := adapt.setKVClient(context.Background()); err != nil {
kdarapuf0c0e382019-09-30 05:26:31 +053071 t.Errorf("adapter.setKVClient() error = %v", err)
72 }
73}
74
75func Test_registerWithCore(t *testing.T) {
76 ad := newMockAdapter()
Rohan Agrawal828bf4e2019-10-22 10:13:19 +000077 ctx := context.TODO()
khenaidoo106c61a2021-08-11 18:05:46 -040078 // Create a and start a mock Core GRPC server
79 ms, err := mgrpc.NewMockGRPCServer(ctx)
80 if err != nil {
81 t.Errorf("grpc server: expected error:nil, got error: %v", err)
82 }
83 ms.AddCoreService(ctx, &vgrpc.MockCoreServiceHandler{})
84 go ms.Start(ctx)
85 defer ms.Stop()
86
khenaidoo27e7ac92021-12-08 14:43:09 -050087 if ad.coreClient, err = vgrpc.NewClient(
88 "olt-endpoint",
89 ms.ApiEndpoint,
khenaidooefff76e2021-12-15 16:51:30 -050090 "core_service.CoreService",
khenaidoo106c61a2021-08-11 18:05:46 -040091 ad.coreRestarted); err != nil {
92 t.Errorf("grpc client: expected error:nil, got error: %v", err)
93 }
94 // Start the core grpc client
khenaidooefff76e2021-12-15 16:51:30 -050095 go ad.coreClient.Start(ctx, getCoreServiceClientHandler)
khenaidoo106c61a2021-08-11 18:05:46 -040096 defer ad.coreClient.Stop(ctx)
97 err = ad.registerWithCore(ctx, coreService, 1)
kdarapuf0c0e382019-09-30 05:26:31 +053098 if err != nil {
99 t.Errorf("Expected error:nil, got error: %v", err)
100 }
101}
kdarapuf0c0e382019-09-30 05:26:31 +0530102
103func Test_startOpenOLT(t *testing.T) {
104 a, _ := mockserver.StartMockServers(1)
Matteo Scandolodfa7a972020-11-06 13:03:40 -0800105 cm := &conf.ConfigManager{}
Kent Hagermane6ff1012020-07-14 15:07:53 -0400106 _ = a.StartAt(0)
kdarapuf0c0e382019-09-30 05:26:31 +0530107 defer a.StopAt(0)
108
109 ad := newMockAdapter()
khenaidoo106c61a2021-08-11 18:05:46 -0400110 oolt, err := ad.startOpenOLT(context.TODO(), nil, ad.eventProxy, ad.config, cm)
kdarapuf0c0e382019-09-30 05:26:31 +0530111 if oolt != nil {
112 t.Log("Open OLT ", oolt)
113 }
114 if err != nil {
115 t.Errorf("err %v", err)
116 }
117}
118
119func Test_newKafkaClient(t *testing.T) {
120 a, _ := mockserver.StartMockServers(1)
Kent Hagermane6ff1012020-07-14 15:07:53 -0400121 _ = a.StartAt(0)
kdarapuf0c0e382019-09-30 05:26:31 +0530122 defer a.StopAt(0)
123 adapter := newMockAdapter()
124 type args struct {
125 clientType string
Neha Sharma3f221ae2020-04-29 19:02:12 +0000126 address string
kdarapuf0c0e382019-09-30 05:26:31 +0530127 }
128 tests := []struct {
129 name string
130 args args
131 wantErr bool
132 }{
133 // TODO: Add test cases.
khenaidoo106c61a2021-08-11 18:05:46 -0400134 {"newKafkaClient", args{clientType: "sarama", address: adapter.config.KafkaClusterAddress}, false},
135 {"newKafkaClient", args{clientType: "sarama", address: adapter.config.KafkaClusterAddress}, false},
kdarapuf0c0e382019-09-30 05:26:31 +0530136 }
137 for _, tt := range tests {
138 t.Run(tt.name, func(t *testing.T) {
Neha Sharma96b7bf22020-06-15 10:37:32 +0000139 _, err := newKafkaClient(context.Background(), tt.args.clientType, tt.args.address)
kdarapuf0c0e382019-09-30 05:26:31 +0530140 if (err != nil) != tt.wantErr {
141 t.Errorf("newKafkaClient() error = %v, wantErr %v", err, tt.wantErr)
142 return
143 }
kdarapuf0c0e382019-09-30 05:26:31 +0530144 })
145 }
146}
Joey Armstrong87b55f72023-06-27 12:12:53 -0400147
148// [EOF]