blob: b33916cb89d5e9737c5d35e644e5964ff315d065 [file] [log] [blame]
Scott Bakere7144bc2019-10-01 14:16:47 -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/golang/protobuf/proto"
21 "github.com/opencord/voltha-go/common/log"
22 common_pb "github.com/opencord/voltha-protos/go/common"
23 "github.com/stretchr/testify/assert"
24 "testing"
25)
26
27const (
Scott Bakerb3a288a2019-10-02 14:57:29 -070028 SOURCE_ROUTER_PROTOFILE = "../../../vendor/github.com/opencord/voltha-protos/voltha.pb"
Scott Bakere7144bc2019-10-01 14:16:47 -070029)
30
31func init() {
Hardik Windlass9f949c92019-10-10 06:39:24 +000032 log.SetDefaultLogger(log.JSON, log.DebugLevel, log.Fields{"instanceId": 1})
Scott Bakere7144bc2019-10-01 14:16:47 -070033 log.AddPackage(log.JSON, log.WarnLevel, nil)
34}
35
36func MakeSourceRouterTestConfig() (*RouteConfig, *RouterConfig) {
37 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},
68 ProtoFile: SOURCE_ROUTER_PROTOFILE,
69 }
70 return &routeConfig, &routerConfig
71}
72
73func TestSourceRouterInit(t *testing.T) {
74 routeConfig, routerConfig := MakeSourceRouterTestConfig()
75
76 router, err := newSourceRouter(routerConfig, routeConfig)
77
78 assert.NotNil(t, router)
79 assert.Nil(t, err)
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.Nil(t, err)
87
88 assert.Equal(t, router.FindBackendCluster("ro_vcore"), clusters["ro_vcore"])
89 assert.Nil(t, router.ReplyHandler("foo"))
90}
91
92func TestSourceRouterDecodeProtoField(t *testing.T) {
93 _, routerConfig := MakeSourceRouterTestConfig()
94 _, err := newRouter(routerConfig)
95 assert.Nil(t, err)
96
97 // Get the created AffinityRouter so we can inspect its state
98 sourceRouter := allRouters["vcorelogger"].(SourceRouter)
99
100 loggingMessage := &common_pb.Logging{Level: 1,
101 PackageName: "default",
102 ComponentName: "ro_vcore0.ro_vcore01"}
103
104 loggingData, err := proto.Marshal(loggingMessage)
105 assert.Nil(t, err)
106
107 s, err := sourceRouter.decodeProtoField(loggingData, 2) // field 2 is package_name
108 assert.Equal(t, s, "default")
109
110 s, err = sourceRouter.decodeProtoField(loggingData, 3) // field 2 is component_name
111 assert.Equal(t, s, "ro_vcore0.ro_vcore01")
112}
113
114func TestSourceRouterRoute(t *testing.T) {
115 _, routerConfig := MakeSourceRouterTestConfig()
116 _, err := newRouter(routerConfig)
117 assert.Nil(t, err)
118
119 // Get the created AffinityRouter so we can inspect its state
120 sourceRouter := allRouters["vcorelogger"].(SourceRouter)
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.Nil(t, err)
128
129 sel := &requestFrame{payload: loggingData,
130 err: nil,
131 methodInfo: newMethodDetails("/voltha.VolthaService/UpdateLogLevel")}
132
133 backend, connection := sourceRouter.Route(sel)
134
135 assert.Nil(t, sel.err)
136 assert.NotNil(t, backend)
137 assert.Equal(t, backend.name, "ro_vcore0")
138 assert.NotNil(t, connection)
139 assert.Equal(t, connection.name, "ro_vcore01")
140}