Scott Baker | e7144bc | 2019-10-01 14:16:47 -0700 | [diff] [blame] | 1 | /* |
| 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 | */ |
| 17 | package afrouter |
| 18 | |
| 19 | import ( |
| 20 | "github.com/golang/protobuf/proto" |
Scott Baker | e7144bc | 2019-10-01 14:16:47 -0700 | [diff] [blame] | 21 | common_pb "github.com/opencord/voltha-protos/go/common" |
| 22 | "github.com/stretchr/testify/assert" |
| 23 | "testing" |
| 24 | ) |
| 25 | |
Scott Baker | e7144bc | 2019-10-01 14:16:47 -0700 | [diff] [blame] | 26 | func 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 Baker | 4989fe9 | 2019-10-09 17:03:06 -0700 | [diff] [blame] | 58 | ProtoFile: TEST_PROTOFILE, |
Scott Baker | e7144bc | 2019-10-01 14:16:47 -0700 | [diff] [blame] | 59 | } |
| 60 | return &routeConfig, &routerConfig |
| 61 | } |
| 62 | |
| 63 | func 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 | |
| 82 | func TestSourceRouterDecodeProtoField(t *testing.T) { |
| 83 | _, routerConfig := MakeSourceRouterTestConfig() |
| 84 | _, err := newRouter(routerConfig) |
| 85 | assert.Nil(t, err) |
| 86 | |
Scott Baker | 8f26db3 | 2019-10-22 15:08:21 -0700 | [diff] [blame] | 87 | // Get the created SourceRouter so we can inspect its state |
Scott Baker | e7144bc | 2019-10-01 14:16:47 -0700 | [diff] [blame] | 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 Baker | 4989fe9 | 2019-10-09 17:03:06 -0700 | [diff] [blame] | 98 | assert.Nil(t, err) |
Scott Baker | e7144bc | 2019-10-01 14:16:47 -0700 | [diff] [blame] | 99 | assert.Equal(t, s, "default") |
| 100 | |
| 101 | s, err = sourceRouter.decodeProtoField(loggingData, 3) // field 2 is component_name |
Scott Baker | 4989fe9 | 2019-10-09 17:03:06 -0700 | [diff] [blame] | 102 | assert.Nil(t, err) |
Scott Baker | e7144bc | 2019-10-01 14:16:47 -0700 | [diff] [blame] | 103 | assert.Equal(t, s, "ro_vcore0.ro_vcore01") |
| 104 | } |
| 105 | |
Scott Baker | 8f26db3 | 2019-10-22 15:08:21 -0700 | [diff] [blame] | 106 | func TestSourceRouterDecodeProtoFieldMissingField(t *testing.T) { |
| 107 | _, routerConfig := MakeSourceRouterTestConfig() |
| 108 | _, err := newRouter(routerConfig) |
| 109 | assert.Nil(t, err) |
| 110 | |
| 111 | // Get the created SourceRouter 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 | _, err = sourceRouter.decodeProtoField(loggingData, 123) // field 123 does not exist |
| 122 | assert.EqualError(t, err, "At end of message, attribute 123 not found") |
| 123 | } |
| 124 | |
Scott Baker | e7144bc | 2019-10-01 14:16:47 -0700 | [diff] [blame] | 125 | func TestSourceRouterRoute(t *testing.T) { |
| 126 | _, routerConfig := MakeSourceRouterTestConfig() |
| 127 | _, err := newRouter(routerConfig) |
| 128 | assert.Nil(t, err) |
| 129 | |
| 130 | // Get the created AffinityRouter so we can inspect its state |
| 131 | sourceRouter := allRouters["vcorelogger"].(SourceRouter) |
| 132 | |
| 133 | loggingMessage := &common_pb.Logging{Level: 1, |
| 134 | PackageName: "default", |
| 135 | ComponentName: "ro_vcore0.ro_vcore01"} |
| 136 | |
| 137 | loggingData, err := proto.Marshal(loggingMessage) |
| 138 | assert.Nil(t, err) |
| 139 | |
| 140 | sel := &requestFrame{payload: loggingData, |
| 141 | err: nil, |
| 142 | methodInfo: newMethodDetails("/voltha.VolthaService/UpdateLogLevel")} |
| 143 | |
| 144 | backend, connection := sourceRouter.Route(sel) |
| 145 | |
| 146 | assert.Nil(t, sel.err) |
| 147 | assert.NotNil(t, backend) |
| 148 | assert.Equal(t, backend.name, "ro_vcore0") |
| 149 | assert.NotNil(t, connection) |
| 150 | assert.Equal(t, connection.name, "ro_vcore01") |
| 151 | } |