VOL-1937 round-robin-router unit tests

Change-Id: I6dd7fd26ff3b498a7efd5ba6996cb90ddabb4be1
diff --git a/afrouter/afrouter/round-robin-router_test.go b/afrouter/afrouter/round-robin-router_test.go
new file mode 100644
index 0000000..4d92e45
--- /dev/null
+++ b/afrouter/afrouter/round-robin-router_test.go
@@ -0,0 +1,289 @@
+/*
+ * Portions copyright 2019-present Open Networking Foundation
+ * Original copyright 2019-present Ciena Corporation
+ *
+ * Licensed under the Apache License, Version 2.0 (the"github.com/stretchr/testify/assert" "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package afrouter
+
+import (
+	"fmt"
+	"github.com/golang/protobuf/proto"
+	"github.com/opencord/voltha-go/common/log"
+	common_pb "github.com/opencord/voltha-protos/go/common"
+	"github.com/stretchr/testify/assert"
+	"google.golang.org/grpc"
+	"testing"
+)
+
+const (
+	ROUND_ROBIN_ROUTER_PROTOFILE = "../../vendor/github.com/opencord/voltha-protos/go/voltha.pb"
+)
+
+func init() {
+	log.SetDefaultLogger(log.JSON, log.DebugLevel, nil)
+	log.AddPackage(log.JSON, log.WarnLevel, nil)
+}
+
+func MakeRoundRobinTestConfig(numBackends int, numConnections int) (*RouteConfig, *RouterConfig) {
+
+	var backends []BackendConfig
+	for backendIndex := 0; backendIndex < numBackends; backendIndex++ {
+		var connections []ConnectionConfig
+		for connectionIndex := 0; connectionIndex < numConnections; connectionIndex++ {
+			connectionConfig := ConnectionConfig{
+				Name: fmt.Sprintf("ro_vcore%d%d", backendIndex, connectionIndex+1),
+				Addr: "foo",
+				Port: "123",
+			}
+			connections = append(connections, connectionConfig)
+		}
+
+		backendConfig := BackendConfig{
+			Name:        fmt.Sprintf("ro_vcore%d", backendIndex),
+			Type:        BackendSingleServer,
+			Connections: connections,
+		}
+
+		backends = append(backends, backendConfig)
+	}
+
+	backendClusterConfig := BackendClusterConfig{
+		Name:     "ro_vcore",
+		Backends: backends,
+	}
+
+	routeConfig := RouteConfig{
+		Name:           "read_only",
+		Type:           RouteTypeRoundRobin,
+		Association:    AssociationRoundRobin,
+		BackendCluster: "ro_vcore",
+		backendCluster: &backendClusterConfig,
+		Methods:        []string{"ListDevicePorts"},
+	}
+
+	routerConfig := RouterConfig{
+		Name:         "vcore",
+		ProtoService: "VolthaService",
+		ProtoPackage: "voltha",
+		Routes:       []RouteConfig{routeConfig},
+		ProtoFile:    ROUND_ROBIN_ROUTER_PROTOFILE,
+	}
+	return &routeConfig, &routerConfig
+}
+
+// Route() requires an open connection, so pretend we have one.
+func PretendRoundRobinOpenConnection(router Router, clusterName string, backendIndex int, connectionName string) {
+	cluster := router.FindBackendCluster(clusterName)
+
+	// Route Method expects an open connection
+	conn := cluster.backends[backendIndex].connections[connectionName]
+	cluster.backends[backendIndex].openConns[conn] = &grpc.ClientConn{}
+}
+
+// Common setup to run before each unit test
+func RoundRobinTestSetup() {
+	// reset globals that need to be clean for each unit test
+
+	clusters = make(map[string]*cluster)
+}
+
+// Test creation of a new RoundRobinRouter, and the Service(), Name(), FindBackendCluster(), and
+// ReplyHandler() methods.
+func TestRoundRobinRouterInit(t *testing.T) {
+	RoundRobinTestSetup()
+
+	routeConfig, routerConfig := MakeRoundRobinTestConfig(1, 1)
+
+	router, err := newRoundRobinRouter(routerConfig, routeConfig)
+
+	assert.NotNil(t, router)
+	assert.Nil(t, err)
+
+	assert.Equal(t, router.Service(), "VolthaService")
+	assert.Equal(t, router.Name(), "read_only")
+
+	cluster, err := router.BackendCluster("foo", "bar")
+	assert.Equal(t, cluster, clusters["ro_vcore"])
+	assert.Nil(t, err)
+
+	assert.Equal(t, router.FindBackendCluster("ro_vcore"), clusters["ro_vcore"])
+	assert.Nil(t, router.ReplyHandler("foo"))
+}
+
+func TestRoundRobinRouterInitNoName(t *testing.T) {
+	RoundRobinTestSetup()
+
+	routeConfig, routerConfig := MakeRoundRobinTestConfig(1, 1)
+	routeConfig.Name = ""
+
+	_, err := newRoundRobinRouter(routerConfig, routeConfig)
+
+	assert.EqualError(t, err, "Failed to create a new router ''")
+}
+
+func TestRoundRobinRouterInitNoProtoPackage(t *testing.T) {
+	RoundRobinTestSetup()
+
+	routeConfig, routerConfig := MakeRoundRobinTestConfig(1, 1)
+	routerConfig.ProtoPackage = ""
+
+	_, err := newRoundRobinRouter(routerConfig, routeConfig)
+
+	assert.EqualError(t, err, "Failed to create a new router 'read_only'")
+}
+
+func TestRoundRobinRouterInitNoProtoService(t *testing.T) {
+	RoundRobinTestSetup()
+
+	routeConfig, routerConfig := MakeRoundRobinTestConfig(1, 1)
+	routerConfig.ProtoService = ""
+
+	_, err := newRoundRobinRouter(routerConfig, routeConfig)
+
+	assert.EqualError(t, err, "Failed to create a new router 'read_only'")
+}
+
+// Tests a cluster with no backends
+func TestRoundRobinRouteZero(t *testing.T) {
+	RoundRobinTestSetup()
+
+	routeConfig, routerConfig := MakeRoundRobinTestConfig(1, 1)
+
+	router, err := newRoundRobinRouter(routerConfig, routeConfig)
+	assert.Nil(t, err)
+
+	idMessage := &common_pb.ID{Id: "1234"}
+
+	idData, err := proto.Marshal(idMessage)
+	assert.Nil(t, err)
+
+	sel := &requestFrame{payload: idData,
+		err:        nil,
+		methodInfo: newMethodDetails("/voltha.VolthaService/ListDevicePorts")}
+
+	backend, connection := router.Route(sel)
+
+	assert.EqualError(t, sel.err, "No backend with open connections found")
+	assert.Nil(t, backend)
+	assert.Nil(t, connection)
+}
+
+// Tests a cluster with only one Backend
+func TestRoundRobinRouteOne(t *testing.T) {
+	RoundRobinTestSetup()
+
+	routeConfig, routerConfig := MakeRoundRobinTestConfig(1, 1)
+
+	router, err := newRoundRobinRouter(routerConfig, routeConfig)
+	assert.Nil(t, err)
+
+	PretendRoundRobinOpenConnection(router, "ro_vcore", 0, "ro_vcore01")
+
+	idMessage := &common_pb.ID{Id: "1234"}
+
+	idData, err := proto.Marshal(idMessage)
+	assert.Nil(t, err)
+
+	sel := &requestFrame{payload: idData,
+		err:        nil,
+		methodInfo: newMethodDetails("/voltha.VolthaService/ListDevicePorts")}
+
+	backend, connection := router.Route(sel)
+
+	assert.Nil(t, sel.err)
+	assert.NotNil(t, backend)
+	assert.Equal(t, "ro_vcore0", backend.name)
+	assert.Nil(t, connection)
+
+	// Since we only have one backend, calling Route a second time should return the same one
+
+	backend, connection = router.Route(sel)
+
+	assert.Nil(t, sel.err)
+	assert.NotNil(t, backend)
+	assert.Equal(t, "ro_vcore0", backend.name)
+	assert.Nil(t, connection)
+}
+
+// Tests a cluster with two Backends
+func TestRoundRobinRouteTwo(t *testing.T) {
+	RoundRobinTestSetup()
+
+	routeConfig, routerConfig := MakeRoundRobinTestConfig(2, 1)
+
+	router, err := newRoundRobinRouter(routerConfig, routeConfig)
+	assert.Nil(t, err)
+
+	PretendRoundRobinOpenConnection(router, "ro_vcore", 0, "ro_vcore01")
+	PretendRoundRobinOpenConnection(router, "ro_vcore", 1, "ro_vcore11")
+
+	idMessage := &common_pb.ID{Id: "1234"}
+
+	idData, err := proto.Marshal(idMessage)
+	assert.Nil(t, err)
+
+	sel := &requestFrame{payload: idData,
+		err:        nil,
+		methodInfo: newMethodDetails("/voltha.VolthaService/ListDevicePorts")}
+
+	backend, connection := router.Route(sel)
+
+	assert.Nil(t, sel.err)
+	assert.NotNil(t, backend)
+	assert.Equal(t, "ro_vcore0", backend.name)
+	assert.Nil(t, connection)
+
+	// Since we have two backends, calling Route a second time should return the second
+
+	backend, connection = router.Route(sel)
+
+	assert.Nil(t, sel.err)
+	assert.NotNil(t, backend)
+	assert.Equal(t, "ro_vcore1", backend.name)
+	assert.Nil(t, connection)
+
+	// Calling Route a third time should return the first again
+
+	backend, connection = router.Route(sel)
+
+	assert.Nil(t, sel.err)
+	assert.NotNil(t, backend)
+	assert.Equal(t, "ro_vcore0", backend.name)
+	assert.Nil(t, connection)
+}
+
+// Tests a cluster with one backend but no open connections
+func TestRoundRobinRouteOneNoOpenConnection(t *testing.T) {
+	RoundRobinTestSetup()
+
+	routeConfig, routerConfig := MakeRoundRobinTestConfig(1, 1)
+
+	router, err := newRoundRobinRouter(routerConfig, routeConfig)
+	assert.Nil(t, err)
+
+	idMessage := &common_pb.ID{Id: "1234"}
+
+	idData, err := proto.Marshal(idMessage)
+	assert.Nil(t, err)
+
+	sel := &requestFrame{payload: idData,
+		err:        nil,
+		methodInfo: newMethodDetails("/voltha.VolthaService/ListDevicePorts")}
+
+	backend, connection := router.Route(sel)
+
+	assert.EqualError(t, sel.err, "No backend with open connections found")
+	assert.Nil(t, backend)
+	assert.Nil(t, connection)
+}
diff --git a/afrouter/afrouter/source-router_test.go b/afrouter/afrouter/source-router_test.go
index 3d35d6e..742c238 100644
--- a/afrouter/afrouter/source-router_test.go
+++ b/afrouter/afrouter/source-router_test.go
@@ -17,19 +17,15 @@
 package afrouter
 
 import (
-	//	"github.com/fullstorydev/grpcurl"
 	"github.com/golang/protobuf/proto"
-	//	descpb "github.com/golang/protobuf/protoc-gen-go/descriptor"
-	//	"github.com/jhump/protoreflect/dynamic"
 	"github.com/opencord/voltha-go/common/log"
 	common_pb "github.com/opencord/voltha-protos/go/common"
 	"github.com/stretchr/testify/assert"
-	//"io/ioutil"
 	"testing"
 )
 
 const (
-	PROTOFILE = "../../vendor/github.com/opencord/voltha-protos/go/voltha.pb"
+	SOURCE_ROUTER_PROTOFILE = "../../vendor/github.com/opencord/voltha-protos/go/voltha.pb"
 )
 
 func init() {
@@ -37,7 +33,7 @@
 	log.AddPackage(log.JSON, log.WarnLevel, nil)
 }
 
-func MakeTestConfig() (*ConnectionConfig, *BackendConfig, *BackendClusterConfig, *RouteConfig, *RouterConfig) {
+func MakeSourceRouterTestConfig() (*ConnectionConfig, *BackendConfig, *BackendClusterConfig, *RouteConfig, *RouterConfig) {
 	connectionConfig := ConnectionConfig{
 		Name: "ro_vcore01",
 		Addr: "foo",
@@ -69,13 +65,13 @@
 		ProtoService: "VolthaService",
 		ProtoPackage: "voltha",
 		Routes:       []RouteConfig{routeConfig},
-		ProtoFile:    PROTOFILE,
+		ProtoFile:    SOURCE_ROUTER_PROTOFILE,
 	}
 	return &connectionConfig, &backendConfig, &backendClusterConfig, &routeConfig, &routerConfig
 }
 
 func TestSourceRouterInit(t *testing.T) {
-	_, _, _, routeConfig, routerConfig := MakeTestConfig()
+	_, _, _, routeConfig, routerConfig := MakeSourceRouterTestConfig()
 
 	router, err := newSourceRouter(routerConfig, routeConfig)
 
@@ -93,8 +89,8 @@
 	assert.Equal(t, router.ReplyHandler("foo"), nil)
 }
 
-func TestDecodeProtoField(t *testing.T) {
-	_, _, _, routeConfig, routerConfig := MakeTestConfig()
+func TestSourceRouterDecodeProtoField(t *testing.T) {
+	_, _, _, routeConfig, routerConfig := MakeSourceRouterTestConfig()
 
 	router, err := newSourceRouter(routerConfig, routeConfig)
 	assert.Equal(t, err, nil)
@@ -113,8 +109,8 @@
 	assert.Equal(t, s, "ro_vcore0.ro_vcore01")
 }
 
-func TestRoute(t *testing.T) {
-	_, _, _, routeConfig, routerConfig := MakeTestConfig()
+func TestSourceRouterRoute(t *testing.T) {
+	_, _, _, routeConfig, routerConfig := MakeSourceRouterTestConfig()
 
 	router, err := newSourceRouter(routerConfig, routeConfig)
 	assert.Equal(t, err, nil)
@@ -128,7 +124,7 @@
 
 	sel := &requestFrame{payload: loggingData,
 		err:        nil,
-		methodInfo: newMethodDetails("/volta.VolthaService/UpdateLogLevel")}
+		methodInfo: newMethodDetails("/voltha.VolthaService/UpdateLogLevel")}
 
 	backend, connection := router.Route(sel)