blob: 3d35d6e255ac9d9956e90915517e2de487c6f888 [file] [log] [blame]
Scott Baker112b0d42019-08-22 08:32:26 -07001/*
2 * Portions copyright 2019-present Open Networking Foundation
3 * Original copyright 2019-present Ciena Corporation
4 *
5 * Licensed under the Apache License, Version 2.0 (the"github.com/stretchr/testify/assert" "License");
6 * you may not use this file except in compliance with the License.
7 * You may obtain a copy of the License at
8 *
9 * http://www.apache.org/licenses/LICENSE-2.0
10 *
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
16 */
17package afrouter
18
19import (
20 // "github.com/fullstorydev/grpcurl"
21 "github.com/golang/protobuf/proto"
22 // descpb "github.com/golang/protobuf/protoc-gen-go/descriptor"
23 // "github.com/jhump/protoreflect/dynamic"
24 "github.com/opencord/voltha-go/common/log"
25 common_pb "github.com/opencord/voltha-protos/go/common"
26 "github.com/stretchr/testify/assert"
27 //"io/ioutil"
28 "testing"
29)
30
31const (
32 PROTOFILE = "../../vendor/github.com/opencord/voltha-protos/go/voltha.pb"
33)
34
35func init() {
36 log.SetDefaultLogger(log.JSON, log.DebugLevel, nil)
37 log.AddPackage(log.JSON, log.WarnLevel, nil)
38}
39
40func MakeTestConfig() (*ConnectionConfig, *BackendConfig, *BackendClusterConfig, *RouteConfig, *RouterConfig) {
41 connectionConfig := ConnectionConfig{
42 Name: "ro_vcore01",
43 Addr: "foo",
44 Port: "123",
45 }
46
47 backendConfig := BackendConfig{
48 Name: "ro_vcore0",
49 Type: BackendSingleServer,
50 Connections: []ConnectionConfig{connectionConfig},
51 }
52
53 backendClusterConfig := BackendClusterConfig{
54 Name: "ro_vcore",
55 Backends: []BackendConfig{backendConfig},
56 }
57
58 routeConfig := RouteConfig{
59 Name: "logger",
60 Type: RouteTypeSource,
61 RouteField: "component_name",
62 BackendCluster: "ro_vcore",
63 backendCluster: &backendClusterConfig,
64 Methods: []string{"UpdateLogLevel", "GetLogLevel"},
65 }
66
67 routerConfig := RouterConfig{
68 Name: "vcore",
69 ProtoService: "VolthaService",
70 ProtoPackage: "voltha",
71 Routes: []RouteConfig{routeConfig},
72 ProtoFile: PROTOFILE,
73 }
74 return &connectionConfig, &backendConfig, &backendClusterConfig, &routeConfig, &routerConfig
75}
76
77func TestSourceRouterInit(t *testing.T) {
78 _, _, _, routeConfig, routerConfig := MakeTestConfig()
79
80 router, err := newSourceRouter(routerConfig, routeConfig)
81
82 assert.NotEqual(t, router, nil)
83 assert.Equal(t, err, nil)
84
85 assert.Equal(t, router.Service(), "VolthaService")
86 assert.Equal(t, router.Name(), "logger")
87
88 cluster, err := router.BackendCluster("foo", "bar")
89 assert.Equal(t, cluster, clusters["ro_vcore"])
90 assert.Equal(t, err, nil)
91
92 assert.Equal(t, router.FindBackendCluster("ro_vcore"), clusters["ro_vcore"])
93 assert.Equal(t, router.ReplyHandler("foo"), nil)
94}
95
96func TestDecodeProtoField(t *testing.T) {
97 _, _, _, routeConfig, routerConfig := MakeTestConfig()
98
99 router, err := newSourceRouter(routerConfig, routeConfig)
100 assert.Equal(t, err, nil)
101
102 loggingMessage := &common_pb.Logging{Level: 1,
103 PackageName: "default",
104 ComponentName: "ro_vcore0.ro_vcore01"}
105
106 loggingData, err := proto.Marshal(loggingMessage)
107 assert.Equal(t, err, nil)
108
109 s, err := router.(SourceRouter).decodeProtoField(loggingData, 2) // field 2 is package_name
110 assert.Equal(t, s, "default")
111
112 s, err = router.(SourceRouter).decodeProtoField(loggingData, 3) // field 2 is component_name
113 assert.Equal(t, s, "ro_vcore0.ro_vcore01")
114}
115
116func TestRoute(t *testing.T) {
117 _, _, _, routeConfig, routerConfig := MakeTestConfig()
118
119 router, err := newSourceRouter(routerConfig, routeConfig)
120 assert.Equal(t, err, nil)
121
122 loggingMessage := &common_pb.Logging{Level: 1,
123 PackageName: "default",
124 ComponentName: "ro_vcore0.ro_vcore01"}
125
126 loggingData, err := proto.Marshal(loggingMessage)
127 assert.Equal(t, err, nil)
128
129 sel := &requestFrame{payload: loggingData,
130 err: nil,
131 methodInfo: newMethodDetails("/volta.VolthaService/UpdateLogLevel")}
132
133 backend, connection := router.Route(sel)
134
135 assert.Equal(t, sel.err, nil)
136 assert.NotEqual(t, backend, nil)
137 assert.Equal(t, backend.name, "ro_vcore0")
138 assert.NotEqual(t, connection, nil)
139 assert.Equal(t, connection.name, "ro_vcore01")
140}