blob: b911dcdeced36900cc76d2dfe3e56f3e80193628 [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 Baker056aa952019-09-23 17:25:04 -070036func MakeSourceRouterTestConfig() (*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 }
Scott Baker056aa952019-09-23 17:25:04 -070070 return &routeConfig, &routerConfig
Scott Baker112b0d42019-08-22 08:32:26 -070071}
72
73func TestSourceRouterInit(t *testing.T) {
Scott Baker056aa952019-09-23 17:25:04 -070074 routeConfig, routerConfig := MakeSourceRouterTestConfig()
Scott Baker112b0d42019-08-22 08:32:26 -070075
76 router, err := newSourceRouter(routerConfig, routeConfig)
77
Scott Baker056aa952019-09-23 17:25:04 -070078 assert.NotNil(t, router)
79 assert.Nil(t, err)
Scott Baker112b0d42019-08-22 08:32:26 -070080
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"])
Scott Baker056aa952019-09-23 17:25:04 -070086 assert.Nil(t, err)
Scott Baker112b0d42019-08-22 08:32:26 -070087
88 assert.Equal(t, router.FindBackendCluster("ro_vcore"), clusters["ro_vcore"])
Scott Baker056aa952019-09-23 17:25:04 -070089 assert.Nil(t, router.ReplyHandler("foo"))
Scott Baker112b0d42019-08-22 08:32:26 -070090}
91
Scott Baker2f9f77c2019-09-18 15:26:26 -070092func TestSourceRouterDecodeProtoField(t *testing.T) {
Scott Baker056aa952019-09-23 17:25:04 -070093 _, routerConfig := MakeSourceRouterTestConfig()
94 _, err := newRouter(routerConfig)
95 assert.Nil(t, err)
Scott Baker112b0d42019-08-22 08:32:26 -070096
Scott Baker056aa952019-09-23 17:25:04 -070097 // Get the created AffinityRouter so we can inspect its state
98 sourceRouter := allRouters["vcorelogger"].(SourceRouter)
Scott Baker112b0d42019-08-22 08:32:26 -070099
100 loggingMessage := &common_pb.Logging{Level: 1,
101 PackageName: "default",
102 ComponentName: "ro_vcore0.ro_vcore01"}
103
104 loggingData, err := proto.Marshal(loggingMessage)
Scott Baker056aa952019-09-23 17:25:04 -0700105 assert.Nil(t, err)
Scott Baker112b0d42019-08-22 08:32:26 -0700106
Scott Baker056aa952019-09-23 17:25:04 -0700107 s, err := sourceRouter.decodeProtoField(loggingData, 2) // field 2 is package_name
Scott Baker112b0d42019-08-22 08:32:26 -0700108 assert.Equal(t, s, "default")
109
Scott Baker056aa952019-09-23 17:25:04 -0700110 s, err = sourceRouter.decodeProtoField(loggingData, 3) // field 2 is component_name
Scott Baker112b0d42019-08-22 08:32:26 -0700111 assert.Equal(t, s, "ro_vcore0.ro_vcore01")
112}
113
Scott Baker2f9f77c2019-09-18 15:26:26 -0700114func TestSourceRouterRoute(t *testing.T) {
Scott Baker056aa952019-09-23 17:25:04 -0700115 _, routerConfig := MakeSourceRouterTestConfig()
116 _, err := newRouter(routerConfig)
117 assert.Nil(t, err)
Scott Baker112b0d42019-08-22 08:32:26 -0700118
Scott Baker056aa952019-09-23 17:25:04 -0700119 // Get the created AffinityRouter so we can inspect its state
120 sourceRouter := allRouters["vcorelogger"].(SourceRouter)
Scott Baker112b0d42019-08-22 08:32:26 -0700121
122 loggingMessage := &common_pb.Logging{Level: 1,
123 PackageName: "default",
124 ComponentName: "ro_vcore0.ro_vcore01"}
125
126 loggingData, err := proto.Marshal(loggingMessage)
Scott Baker056aa952019-09-23 17:25:04 -0700127 assert.Nil(t, err)
Scott Baker112b0d42019-08-22 08:32:26 -0700128
129 sel := &requestFrame{payload: loggingData,
130 err: nil,
Scott Baker2f9f77c2019-09-18 15:26:26 -0700131 methodInfo: newMethodDetails("/voltha.VolthaService/UpdateLogLevel")}
Scott Baker112b0d42019-08-22 08:32:26 -0700132
Scott Baker056aa952019-09-23 17:25:04 -0700133 backend, connection := sourceRouter.Route(sel)
Scott Baker112b0d42019-08-22 08:32:26 -0700134
Scott Baker056aa952019-09-23 17:25:04 -0700135 assert.Nil(t, sel.err)
136 assert.NotNil(t, backend)
Scott Baker112b0d42019-08-22 08:32:26 -0700137 assert.Equal(t, backend.name, "ro_vcore0")
Scott Baker056aa952019-09-23 17:25:04 -0700138 assert.NotNil(t, connection)
Scott Baker112b0d42019-08-22 08:32:26 -0700139 assert.Equal(t, connection.name, "ro_vcore01")
140}