VOL-1927 Unit test cases for api.go
Change-Id: I4af28d257f50c67f304af91a9a70906db40b23d9
diff --git a/internal/pkg/afrouter/api_test.go b/internal/pkg/afrouter/api_test.go
new file mode 100644
index 0000000..0bcb1d4
--- /dev/null
+++ b/internal/pkg/afrouter/api_test.go
@@ -0,0 +1,193 @@
+/*
+ * Copyright 2018-present Open Networking Foundation
+
+ * Licensed under the Apache License, Version 2.0 (the "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/phayes/freeport"
+ "github.com/stretchr/testify/assert"
+ "testing"
+)
+
+func makeConfig(numBackends int, numConnections int) Configuration {
+
+ var backends []BackendConfig
+ var slbackendClusterConfig []BackendClusterConfig
+ var slRouterConfig []RouterConfig
+
+ 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,
+ }
+
+ slbackendClusterConfig = append(slbackendClusterConfig, backendClusterConfig)
+
+ 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: TEST_PROTOFILE,
+ }
+
+ slRouterConfig = append(slRouterConfig, routerConfig)
+
+ port1, _ := freeport.GetFreePort()
+ apiConfig := ApiConfig{
+ Addr: "127.0.0.1",
+ Port: uint(port1),
+ }
+
+ cfile := "config_file"
+ loglevel := 0
+ glog := false
+
+ conf := Configuration{
+ InstanceID: "1",
+ ConfigFile: &cfile,
+ LogLevel: &loglevel,
+ GrpcLog: &glog,
+ BackendClusters: slbackendClusterConfig,
+ Routers: slRouterConfig,
+ Api: apiConfig,
+ }
+ return conf
+}
+
+func makeProxy(numBackends int, numConnections int) (*ArouterProxy, error) {
+
+ conf := makeConfig(3, 2)
+ arouter, err := NewArouterProxy(&conf)
+ return arouter, err
+}
+
+func TestNewApi(t *testing.T) {
+
+ port, _ := freeport.GetFreePort()
+
+ apiConfig := ApiConfig{
+ Addr: "127.0.0.1",
+ Port: uint(port),
+ }
+ arouter, err := makeProxy(3, 2)
+ assert.NotNil(t, arouter, "Error the proxy confiiguration fails!")
+ assert.Nil(t, err)
+
+ ar, err := newApi(&apiConfig, arouter)
+
+ assert.NotNil(t, ar, "NewApi Fails!")
+ assert.Nil(t, err)
+}
+
+func TestNewApiWrongSocket(t *testing.T) {
+ port, _ := freeport.GetFreePort()
+ apiConfig := ApiConfig{
+ Addr: "256.300.1.83",
+ Port: uint(port),
+ }
+ arouter, err := makeProxy(3, 2)
+ assert.NotNil(t, arouter, "Error the proxy configuration fails!")
+ assert.Nil(t, err)
+ ar, err := newApi(&apiConfig, arouter)
+
+ assert.Nil(t, ar, "Accepted a wrong IP")
+ assert.NotNil(t, err)
+
+ apiConfig = ApiConfig{
+ Addr: "127.0.0.1",
+ Port: 68000,
+ }
+ ar, err = newApi(&apiConfig, arouter)
+ assert.Nil(t, ar, "Accepted a wrong port")
+ assert.NotNil(t, err)
+}
+
+func TestGetBackend(t *testing.T) {
+ port, _ := freeport.GetFreePort()
+ apiConfig := ApiConfig{
+ Addr: "127.0.0.1",
+ Port: uint(port),
+ }
+ arouter, _ := makeProxy(3, 2)
+
+ ar, _ := newApi(&apiConfig, arouter)
+
+ backends := makeBackendClusterConfig(5, 1)
+ cluster, err := newBackendCluster(backends)
+ assert.NotNil(t, cluster)
+ assert.Nil(t, err)
+
+ for backendIndex := 0; backendIndex < 5; backendIndex++ {
+ backend, errb := ar.getBackend(cluster, fmt.Sprintf("ro_vcore%d", backendIndex))
+ assert.NotNil(t, backend)
+ assert.Nil(t, errb)
+ }
+
+ backend, errb := ar.getBackend(cluster, "ro_vcore5")
+ assert.Nil(t, backend)
+ assert.NotNil(t, errb)
+}
+
+func TestGetConnection(t *testing.T) {
+ port, _ := freeport.GetFreePort()
+ apiConfig := ApiConfig{
+ Addr: "127.0.0.1",
+ Port: uint(port),
+ }
+ arouter, _ := makeProxy(3, 2)
+
+ ar, _ := newApi(&apiConfig, arouter)
+
+ backend, err := newBackend(makeBackend(), "Cluster")
+ assert.NotNil(t, backend)
+ assert.Nil(t, err)
+ cc, _ := ar.getConnection(backend, "ro_vcore01")
+ assert.NotNil(t, cc, "Error connection name not found")
+
+ cc2, err2 := ar.getConnection(backend, "wrongConnectionName")
+ assert.Nil(t, cc2)
+ assert.NotNil(t, err2)
+}