blob: 742c2380d9d98e9237d793c9670f33da5e815ae8 [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 (
Scott Baker112b0d42019-08-22 08:32:26 -070020 "github.com/golang/protobuf/proto"
Scott Baker112b0d42019-08-22 08:32:26 -070021 "github.com/opencord/voltha-go/common/log"
22 common_pb "github.com/opencord/voltha-protos/go/common"
23 "github.com/stretchr/testify/assert"
Scott Baker112b0d42019-08-22 08:32:26 -070024 "testing"
25)
26
27const (
Scott Baker2f9f77c2019-09-18 15:26:26 -070028 SOURCE_ROUTER_PROTOFILE = "../../vendor/github.com/opencord/voltha-protos/go/voltha.pb"
Scott Baker112b0d42019-08-22 08:32:26 -070029)
30
31func init() {
32 log.SetDefaultLogger(log.JSON, log.DebugLevel, nil)
33 log.AddPackage(log.JSON, log.WarnLevel, nil)
34}
35
Scott Baker2f9f77c2019-09-18 15:26:26 -070036func MakeSourceRouterTestConfig() (*ConnectionConfig, *BackendConfig, *BackendClusterConfig, *RouteConfig, *RouterConfig) {
Scott Baker112b0d42019-08-22 08:32:26 -070037 connectionConfig := ConnectionConfig{
38 Name: "ro_vcore01",
39 Addr: "foo",
40 Port: "123",
41 }
42
43 backendConfig := BackendConfig{
44 Name: "ro_vcore0",
45 Type: BackendSingleServer,
46 Connections: []ConnectionConfig{connectionConfig},
47 }
48
49 backendClusterConfig := BackendClusterConfig{
50 Name: "ro_vcore",
51 Backends: []BackendConfig{backendConfig},
52 }
53
54 routeConfig := RouteConfig{
55 Name: "logger",
56 Type: RouteTypeSource,
57 RouteField: "component_name",
58 BackendCluster: "ro_vcore",
59 backendCluster: &backendClusterConfig,
60 Methods: []string{"UpdateLogLevel", "GetLogLevel"},
61 }
62
63 routerConfig := RouterConfig{
64 Name: "vcore",
65 ProtoService: "VolthaService",
66 ProtoPackage: "voltha",
67 Routes: []RouteConfig{routeConfig},
Scott Baker2f9f77c2019-09-18 15:26:26 -070068 ProtoFile: SOURCE_ROUTER_PROTOFILE,
Scott Baker112b0d42019-08-22 08:32:26 -070069 }
70 return &connectionConfig, &backendConfig, &backendClusterConfig, &routeConfig, &routerConfig
71}
72
73func TestSourceRouterInit(t *testing.T) {
Scott Baker2f9f77c2019-09-18 15:26:26 -070074 _, _, _, routeConfig, routerConfig := MakeSourceRouterTestConfig()
Scott Baker112b0d42019-08-22 08:32:26 -070075
76 router, err := newSourceRouter(routerConfig, routeConfig)
77
78 assert.NotEqual(t, router, nil)
79 assert.Equal(t, err, nil)
80
81 assert.Equal(t, router.Service(), "VolthaService")
82 assert.Equal(t, router.Name(), "logger")
83
84 cluster, err := router.BackendCluster("foo", "bar")
85 assert.Equal(t, cluster, clusters["ro_vcore"])
86 assert.Equal(t, err, nil)
87
88 assert.Equal(t, router.FindBackendCluster("ro_vcore"), clusters["ro_vcore"])
89 assert.Equal(t, router.ReplyHandler("foo"), nil)
90}
91
Scott Baker2f9f77c2019-09-18 15:26:26 -070092func TestSourceRouterDecodeProtoField(t *testing.T) {
93 _, _, _, routeConfig, routerConfig := MakeSourceRouterTestConfig()
Scott Baker112b0d42019-08-22 08:32:26 -070094
95 router, err := newSourceRouter(routerConfig, routeConfig)
96 assert.Equal(t, err, nil)
97
98 loggingMessage := &common_pb.Logging{Level: 1,
99 PackageName: "default",
100 ComponentName: "ro_vcore0.ro_vcore01"}
101
102 loggingData, err := proto.Marshal(loggingMessage)
103 assert.Equal(t, err, nil)
104
105 s, err := router.(SourceRouter).decodeProtoField(loggingData, 2) // field 2 is package_name
106 assert.Equal(t, s, "default")
107
108 s, err = router.(SourceRouter).decodeProtoField(loggingData, 3) // field 2 is component_name
109 assert.Equal(t, s, "ro_vcore0.ro_vcore01")
110}
111
Scott Baker2f9f77c2019-09-18 15:26:26 -0700112func TestSourceRouterRoute(t *testing.T) {
113 _, _, _, routeConfig, routerConfig := MakeSourceRouterTestConfig()
Scott Baker112b0d42019-08-22 08:32:26 -0700114
115 router, err := newSourceRouter(routerConfig, routeConfig)
116 assert.Equal(t, err, nil)
117
118 loggingMessage := &common_pb.Logging{Level: 1,
119 PackageName: "default",
120 ComponentName: "ro_vcore0.ro_vcore01"}
121
122 loggingData, err := proto.Marshal(loggingMessage)
123 assert.Equal(t, err, nil)
124
125 sel := &requestFrame{payload: loggingData,
126 err: nil,
Scott Baker2f9f77c2019-09-18 15:26:26 -0700127 methodInfo: newMethodDetails("/voltha.VolthaService/UpdateLogLevel")}
Scott Baker112b0d42019-08-22 08:32:26 -0700128
129 backend, connection := router.Route(sel)
130
131 assert.Equal(t, sel.err, nil)
132 assert.NotEqual(t, backend, nil)
133 assert.Equal(t, backend.name, "ro_vcore0")
134 assert.NotEqual(t, connection, nil)
135 assert.Equal(t, connection.name, "ro_vcore01")
136}