blob: 55c1b2396a7755e451a524d4fd03952a5b03834f [file] [log] [blame]
kdarapuf0c0e382019-09-30 05:26:31 +05301/*
Abhay Kumara61c5222025-11-10 07:32:50 +00002* Copyright 2018-2025 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"
Abhay Kumara61c5222025-11-10 07:32:50 +000026 "go.etcd.io/etcd/tests/v3/framework/integration"
kdarapuf0c0e382019-09-30 05:26:31 +053027)
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 = ""
Abhay Kumara61c5222025-11-10 07:32:50 +000042
43 integration.BeforeTest(t)
44 cluster := integration.NewCluster(t, &integration.ClusterConfig{Size: 1})
45 defer cluster.Terminate(t)
46
kdarapuf0c0e382019-09-30 05:26:31 +053047 tests := []struct {
48 name string
49 clienttype string
50 adapter *adapter
51 wantErr bool
52 }{
53 {"setKVClient", adapt.config.KVStoreType, adapt, false},
54 {"setKVClient", adapt1.config.KVStoreType, adapt1, false},
55 {"setKVClient", adapt2.config.KVStoreType, adapt2, true},
56 }
57 for _, tt := range tests {
58 t.Run(tt.name, func(t *testing.T) {
Neha Sharma96b7bf22020-06-15 10:37:32 +000059 if err := tt.adapter.setKVClient(context.Background()); (err != nil) != tt.wantErr {
kdarapuf0c0e382019-09-30 05:26:31 +053060 t.Errorf("adapter.setKVClient() error = %v, wantErr %v", err, tt.wantErr)
61 }
62 })
63 }
64}
65
66func Test_adapter_KVClient(t *testing.T) {
67 adapt := newMockAdapter()
Abhay Kumara61c5222025-11-10 07:32:50 +000068
69 integration.BeforeTest(t)
70 cluster := integration.NewCluster(t, &integration.ClusterConfig{Size: 1})
71 defer cluster.Terminate(t)
kdarapuf0c0e382019-09-30 05:26:31 +053072
Neha Sharma96b7bf22020-06-15 10:37:32 +000073 if err := adapt.setKVClient(context.Background()); err != nil {
kdarapuf0c0e382019-09-30 05:26:31 +053074 t.Errorf("adapter.setKVClient() error = %v", err)
75 }
76}
77
78func Test_registerWithCore(t *testing.T) {
79 ad := newMockAdapter()
Rohan Agrawal828bf4e2019-10-22 10:13:19 +000080 ctx := context.TODO()
khenaidoo106c61a2021-08-11 18:05:46 -040081 // Create a and start a mock Core GRPC server
82 ms, err := mgrpc.NewMockGRPCServer(ctx)
83 if err != nil {
84 t.Errorf("grpc server: expected error:nil, got error: %v", err)
85 }
86 ms.AddCoreService(ctx, &vgrpc.MockCoreServiceHandler{})
87 go ms.Start(ctx)
88 defer ms.Stop()
89
khenaidoo27e7ac92021-12-08 14:43:09 -050090 if ad.coreClient, err = vgrpc.NewClient(
91 "olt-endpoint",
92 ms.ApiEndpoint,
khenaidooefff76e2021-12-15 16:51:30 -050093 "core_service.CoreService",
khenaidoo106c61a2021-08-11 18:05:46 -040094 ad.coreRestarted); err != nil {
95 t.Errorf("grpc client: expected error:nil, got error: %v", err)
96 }
97 // Start the core grpc client
khenaidooefff76e2021-12-15 16:51:30 -050098 go ad.coreClient.Start(ctx, getCoreServiceClientHandler)
khenaidoo106c61a2021-08-11 18:05:46 -040099 defer ad.coreClient.Stop(ctx)
100 err = ad.registerWithCore(ctx, coreService, 1)
kdarapuf0c0e382019-09-30 05:26:31 +0530101 if err != nil {
102 t.Errorf("Expected error:nil, got error: %v", err)
103 }
104}
kdarapuf0c0e382019-09-30 05:26:31 +0530105
106func Test_startOpenOLT(t *testing.T) {
Abhay Kumara61c5222025-11-10 07:32:50 +0000107 integration.BeforeTest(t)
108 cluster := integration.NewCluster(t, &integration.ClusterConfig{Size: 1})
109 defer cluster.Terminate(t)
110
Matteo Scandolodfa7a972020-11-06 13:03:40 -0800111 cm := &conf.ConfigManager{}
kdarapuf0c0e382019-09-30 05:26:31 +0530112
113 ad := newMockAdapter()
khenaidoo106c61a2021-08-11 18:05:46 -0400114 oolt, err := ad.startOpenOLT(context.TODO(), nil, ad.eventProxy, ad.config, cm)
kdarapuf0c0e382019-09-30 05:26:31 +0530115 if oolt != nil {
116 t.Log("Open OLT ", oolt)
117 }
118 if err != nil {
119 t.Errorf("err %v", err)
120 }
121}
122
123func Test_newKafkaClient(t *testing.T) {
Abhay Kumara61c5222025-11-10 07:32:50 +0000124 integration.BeforeTest(t)
125 cluster := integration.NewCluster(t, &integration.ClusterConfig{Size: 1})
126 defer cluster.Terminate(t)
127
kdarapuf0c0e382019-09-30 05:26:31 +0530128 adapter := newMockAdapter()
129 type args struct {
130 clientType string
akashreddyk02b2bfe2025-09-05 10:37:40 +0530131 config *config.AdapterFlags
kdarapuf0c0e382019-09-30 05:26:31 +0530132 }
133 tests := []struct {
134 name string
135 args args
136 wantErr bool
137 }{
138 // TODO: Add test cases.
akashreddyk02b2bfe2025-09-05 10:37:40 +0530139 {"newKafkaClient", args{clientType: "sarama", config: adapter.config}, false},
140 {"newKafkaClient", args{clientType: "sarama", config: adapter.config}, false},
kdarapuf0c0e382019-09-30 05:26:31 +0530141 }
142 for _, tt := range tests {
143 t.Run(tt.name, func(t *testing.T) {
akashreddyk02b2bfe2025-09-05 10:37:40 +0530144 _, err := newKafkaClient(context.Background(), tt.args.clientType, tt.args.config)
kdarapuf0c0e382019-09-30 05:26:31 +0530145 if (err != nil) != tt.wantErr {
146 t.Errorf("newKafkaClient() error = %v, wantErr %v", err, tt.wantErr)
147 return
148 }
kdarapuf0c0e382019-09-30 05:26:31 +0530149 })
150 }
151}
Joey Armstrong87b55f72023-06-27 12:12:53 -0400152
153// [EOF]