blob: 3be2a6e2ef604e8621ac68c5add6c8e890b973dc [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"
Scott Bakere7144bc2019-10-01 14:16:47 -070021 common_pb "github.com/opencord/voltha-protos/go/common"
22 "github.com/stretchr/testify/assert"
23 "testing"
24)
25
Scott Bakere7144bc2019-10-01 14:16:47 -070026func MakeSourceRouterTestConfig() (*RouteConfig, *RouterConfig) {
27 connectionConfig := ConnectionConfig{
28 Name: "ro_vcore01",
29 Addr: "foo",
30 Port: "123",
31 }
32
33 backendConfig := BackendConfig{
34 Name: "ro_vcore0",
35 Type: BackendSingleServer,
36 Connections: []ConnectionConfig{connectionConfig},
37 }
38
39 backendClusterConfig := BackendClusterConfig{
40 Name: "ro_vcore",
41 Backends: []BackendConfig{backendConfig},
42 }
43
44 routeConfig := RouteConfig{
45 Name: "logger",
46 Type: RouteTypeSource,
47 RouteField: "component_name",
48 BackendCluster: "ro_vcore",
49 backendCluster: &backendClusterConfig,
50 Methods: []string{"UpdateLogLevel", "GetLogLevel"},
51 }
52
53 routerConfig := RouterConfig{
54 Name: "vcore",
55 ProtoService: "VolthaService",
56 ProtoPackage: "voltha",
57 Routes: []RouteConfig{routeConfig},
Scott Baker4989fe92019-10-09 17:03:06 -070058 ProtoFile: TEST_PROTOFILE,
Scott Bakere7144bc2019-10-01 14:16:47 -070059 }
60 return &routeConfig, &routerConfig
61}
62
63func TestSourceRouterInit(t *testing.T) {
64 routeConfig, routerConfig := MakeSourceRouterTestConfig()
65
66 router, err := newSourceRouter(routerConfig, routeConfig)
67
68 assert.NotNil(t, router)
69 assert.Nil(t, err)
70
71 assert.Equal(t, router.Service(), "VolthaService")
72 assert.Equal(t, router.Name(), "logger")
73
74 cluster, err := router.BackendCluster("foo", "bar")
75 assert.Equal(t, cluster, clusters["ro_vcore"])
76 assert.Nil(t, err)
77
78 assert.Equal(t, router.FindBackendCluster("ro_vcore"), clusters["ro_vcore"])
79 assert.Nil(t, router.ReplyHandler("foo"))
80}
81
82func TestSourceRouterDecodeProtoField(t *testing.T) {
83 _, routerConfig := MakeSourceRouterTestConfig()
84 _, err := newRouter(routerConfig)
85 assert.Nil(t, err)
86
87 // Get the created AffinityRouter so we can inspect its state
88 sourceRouter := allRouters["vcorelogger"].(SourceRouter)
89
90 loggingMessage := &common_pb.Logging{Level: 1,
91 PackageName: "default",
92 ComponentName: "ro_vcore0.ro_vcore01"}
93
94 loggingData, err := proto.Marshal(loggingMessage)
95 assert.Nil(t, err)
96
97 s, err := sourceRouter.decodeProtoField(loggingData, 2) // field 2 is package_name
Scott Baker4989fe92019-10-09 17:03:06 -070098 assert.Nil(t, err)
Scott Bakere7144bc2019-10-01 14:16:47 -070099 assert.Equal(t, s, "default")
100
101 s, err = sourceRouter.decodeProtoField(loggingData, 3) // field 2 is component_name
Scott Baker4989fe92019-10-09 17:03:06 -0700102 assert.Nil(t, err)
Scott Bakere7144bc2019-10-01 14:16:47 -0700103 assert.Equal(t, s, "ro_vcore0.ro_vcore01")
104}
105
106func TestSourceRouterRoute(t *testing.T) {
107 _, routerConfig := MakeSourceRouterTestConfig()
108 _, err := newRouter(routerConfig)
109 assert.Nil(t, err)
110
111 // Get the created AffinityRouter so we can inspect its state
112 sourceRouter := allRouters["vcorelogger"].(SourceRouter)
113
114 loggingMessage := &common_pb.Logging{Level: 1,
115 PackageName: "default",
116 ComponentName: "ro_vcore0.ro_vcore01"}
117
118 loggingData, err := proto.Marshal(loggingMessage)
119 assert.Nil(t, err)
120
121 sel := &requestFrame{payload: loggingData,
122 err: nil,
123 methodInfo: newMethodDetails("/voltha.VolthaService/UpdateLogLevel")}
124
125 backend, connection := sourceRouter.Route(sel)
126
127 assert.Nil(t, sel.err)
128 assert.NotNil(t, backend)
129 assert.Equal(t, backend.name, "ro_vcore0")
130 assert.NotNil(t, connection)
131 assert.Equal(t, connection.name, "ro_vcore01")
132}