chore: add extra route config tests
This commit is contained in:
@@ -40,7 +40,6 @@ func loadExtraFiles(routePath string) ([]string, error) {
|
|||||||
})
|
})
|
||||||
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
//log.Fatalf("error walking the path %v: %v", routePath, err)
|
|
||||||
return nil, fmt.Errorf("error loading extra route files: %v", err)
|
return nil, fmt.Errorf("error loading extra route files: %v", err)
|
||||||
}
|
}
|
||||||
return yamlFiles, nil
|
return yamlFiles, nil
|
||||||
|
|||||||
@@ -40,15 +40,15 @@ func (gatewayServer GatewayServer) Initialize() *mux.Router {
|
|||||||
if len(gateway.ExtraRoutes.Directory) != 0 {
|
if len(gateway.ExtraRoutes.Directory) != 0 {
|
||||||
extraRoutes, err := loadExtraRoutes(gateway.ExtraRoutes.Directory)
|
extraRoutes, err := loadExtraRoutes(gateway.ExtraRoutes.Directory)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
logger.Error(err.Error())
|
logger.Error("Error: %v", err.Error())
|
||||||
}
|
}
|
||||||
dynamicRoutes = append(dynamicRoutes, extraRoutes...)
|
dynamicRoutes = append(dynamicRoutes, extraRoutes...)
|
||||||
}
|
}
|
||||||
//find duplicated route name
|
// find duplicated route name
|
||||||
duplicates := findDuplicateRouteNames(dynamicRoutes)
|
duplicates := findDuplicateRouteNames(dynamicRoutes)
|
||||||
if len(duplicates) != 0 {
|
if len(duplicates) != 0 {
|
||||||
for _, duplicate := range duplicates {
|
for _, duplicate := range duplicates {
|
||||||
logger.Error("Duplicate route name found: %s ", duplicate)
|
logger.Error("Duplicated route named was found: %s ", duplicate)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
m := gatewayServer.middlewares
|
m := gatewayServer.middlewares
|
||||||
|
|||||||
178
internal/route_test.go
Normal file
178
internal/route_test.go
Normal file
@@ -0,0 +1,178 @@
|
|||||||
|
/*
|
||||||
|
* Copyright 2024 Jonas Kaninda
|
||||||
|
*
|
||||||
|
* 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 pkg
|
||||||
|
|
||||||
|
import (
|
||||||
|
"fmt"
|
||||||
|
"github.com/jkaninda/goma-gateway/util"
|
||||||
|
"gopkg.in/yaml.v3"
|
||||||
|
"os"
|
||||||
|
)
|
||||||
|
|
||||||
|
// initExtraRoute create extra routes
|
||||||
|
func initExtraRoute(path string) error {
|
||||||
|
|
||||||
|
conf := &ExtraRoute{
|
||||||
|
Routes: []Route{
|
||||||
|
{
|
||||||
|
Name: "Extra1",
|
||||||
|
Path: "/",
|
||||||
|
Methods: []string{"GET"},
|
||||||
|
Destination: "https://extra-example.com",
|
||||||
|
Rewrite: "/",
|
||||||
|
HealthCheck: RouteHealthCheck{
|
||||||
|
Path: "/",
|
||||||
|
Interval: "30s",
|
||||||
|
Timeout: "10s",
|
||||||
|
HealthyStatuses: []int{200, 404},
|
||||||
|
},
|
||||||
|
DisableHostFording: true,
|
||||||
|
Middlewares: []string{"block-access"},
|
||||||
|
},
|
||||||
|
// Duplicate route name
|
||||||
|
{
|
||||||
|
Name: "Load balancer",
|
||||||
|
Path: "/protected",
|
||||||
|
Backends: []string{
|
||||||
|
"https://example.com",
|
||||||
|
"https://example2.com",
|
||||||
|
"https://example3.com",
|
||||||
|
},
|
||||||
|
Rewrite: "/",
|
||||||
|
HealthCheck: RouteHealthCheck{},
|
||||||
|
Cors: Cors{
|
||||||
|
Origins: []string{"http://localhost:3000", "https://dev.example.com"},
|
||||||
|
Headers: map[string]string{
|
||||||
|
"Access-Control-Allow-Headers": "Origin, Authorization",
|
||||||
|
"Access-Control-Allow-Credentials": "true",
|
||||||
|
"Access-Control-Max-Age": "1728000",
|
||||||
|
},
|
||||||
|
},
|
||||||
|
Middlewares: []string{"basic-auth", "block-access"},
|
||||||
|
},
|
||||||
|
},
|
||||||
|
}
|
||||||
|
yamlData, err := yaml.Marshal(&conf)
|
||||||
|
if err != nil {
|
||||||
|
return fmt.Errorf("serializing configuration %v\n", err.Error())
|
||||||
|
}
|
||||||
|
err = os.WriteFile(fmt.Sprintf("%s/extra.yaml", path), yamlData, 0644)
|
||||||
|
if err != nil {
|
||||||
|
return fmt.Errorf("unable to write config file %s\n", err)
|
||||||
|
}
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
// initConfig initializes configs
|
||||||
|
func initConfiguration(configFile string) error {
|
||||||
|
conf := &GatewayConfig{
|
||||||
|
Version: util.ConfigVersion,
|
||||||
|
GatewayConfig: Gateway{
|
||||||
|
WriteTimeout: 15,
|
||||||
|
ReadTimeout: 15,
|
||||||
|
IdleTimeout: 30,
|
||||||
|
AccessLog: "/dev/Stdout",
|
||||||
|
ErrorLog: "/dev/stderr",
|
||||||
|
DisableRouteHealthCheckError: false,
|
||||||
|
DisableDisplayRouteOnStart: false,
|
||||||
|
RateLimit: 0,
|
||||||
|
InterceptErrors: []int{405, 500},
|
||||||
|
ExtraRoutes: ExtraRouteConfig{
|
||||||
|
Directory: extraRoutePath,
|
||||||
|
Watch: false,
|
||||||
|
},
|
||||||
|
Cors: Cors{
|
||||||
|
Origins: []string{"http://localhost:8080", "https://example.com"},
|
||||||
|
Headers: map[string]string{
|
||||||
|
"Access-Control-Allow-Headers": "Origin, Authorization, Accept, Content-Type, Access-Control-Allow-Headers",
|
||||||
|
"Access-Control-Allow-Credentials": "true",
|
||||||
|
"Access-Control-Max-Age": "1728000",
|
||||||
|
},
|
||||||
|
},
|
||||||
|
Routes: []Route{
|
||||||
|
{
|
||||||
|
Name: "Example",
|
||||||
|
Path: "/",
|
||||||
|
Methods: []string{"GET"},
|
||||||
|
Destination: "https://example.com",
|
||||||
|
Rewrite: "/",
|
||||||
|
HealthCheck: RouteHealthCheck{
|
||||||
|
Path: "/",
|
||||||
|
Interval: "30s",
|
||||||
|
Timeout: "10s",
|
||||||
|
HealthyStatuses: []int{200, 404},
|
||||||
|
},
|
||||||
|
DisableHostFording: true,
|
||||||
|
Middlewares: []string{"block-access"},
|
||||||
|
},
|
||||||
|
{
|
||||||
|
Name: "Load balancer",
|
||||||
|
Path: "/protected",
|
||||||
|
Backends: []string{
|
||||||
|
"https://example.com",
|
||||||
|
"https://example2.com",
|
||||||
|
"https://example3.com",
|
||||||
|
},
|
||||||
|
Rewrite: "/",
|
||||||
|
HealthCheck: RouteHealthCheck{},
|
||||||
|
Cors: Cors{
|
||||||
|
Origins: []string{"http://localhost:3000", "https://dev.example.com"},
|
||||||
|
Headers: map[string]string{
|
||||||
|
"Access-Control-Allow-Headers": "Origin, Authorization",
|
||||||
|
"Access-Control-Allow-Credentials": "true",
|
||||||
|
"Access-Control-Max-Age": "1728000",
|
||||||
|
},
|
||||||
|
},
|
||||||
|
Middlewares: []string{"basic-auth", "block-access"},
|
||||||
|
},
|
||||||
|
},
|
||||||
|
},
|
||||||
|
Middlewares: []Middleware{
|
||||||
|
{
|
||||||
|
Name: "basic-auth",
|
||||||
|
Type: BasicAuth,
|
||||||
|
Paths: []string{
|
||||||
|
"/*",
|
||||||
|
},
|
||||||
|
Rule: BasicRuleMiddleware{
|
||||||
|
Username: "admin",
|
||||||
|
Password: "admin",
|
||||||
|
},
|
||||||
|
},
|
||||||
|
{
|
||||||
|
Name: "block-access",
|
||||||
|
Type: AccessMiddleware,
|
||||||
|
Paths: []string{
|
||||||
|
"/swagger-ui/*",
|
||||||
|
"/v2/swagger-ui/*",
|
||||||
|
"/api-docs/*",
|
||||||
|
"/actuator/*",
|
||||||
|
},
|
||||||
|
},
|
||||||
|
},
|
||||||
|
}
|
||||||
|
yamlData, err := yaml.Marshal(&conf)
|
||||||
|
if err != nil {
|
||||||
|
return fmt.Errorf("serializing configuration %v\n", err.Error())
|
||||||
|
}
|
||||||
|
err = os.WriteFile(configFile, yamlData, 0644)
|
||||||
|
if err != nil {
|
||||||
|
return fmt.Errorf("unable to write config file %s\n", err)
|
||||||
|
}
|
||||||
|
return nil
|
||||||
|
}
|
||||||
@@ -10,6 +10,7 @@ import (
|
|||||||
)
|
)
|
||||||
|
|
||||||
const testPath = "./tests"
|
const testPath = "./tests"
|
||||||
|
const extraRoutePath = "./tests/extra"
|
||||||
|
|
||||||
var configFile = filepath.Join(testPath, "goma.yml")
|
var configFile = filepath.Join(testPath, "goma.yml")
|
||||||
|
|
||||||
@@ -18,11 +19,15 @@ func TestInit(t *testing.T) {
|
|||||||
if err != nil {
|
if err != nil {
|
||||||
t.Error(err)
|
t.Error(err)
|
||||||
}
|
}
|
||||||
|
err = os.MkdirAll(extraRoutePath, os.ModePerm)
|
||||||
|
if err != nil {
|
||||||
|
t.Error(err)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestCheckConfig(t *testing.T) {
|
func TestCheckConfig(t *testing.T) {
|
||||||
TestInit(t)
|
TestInit(t)
|
||||||
err := initConfig(configFile)
|
err := initConfiguration(configFile)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
t.Fatal("Error init config:", err)
|
t.Fatal("Error init config:", err)
|
||||||
}
|
}
|
||||||
@@ -35,10 +40,15 @@ func TestCheckConfig(t *testing.T) {
|
|||||||
|
|
||||||
func TestStart(t *testing.T) {
|
func TestStart(t *testing.T) {
|
||||||
TestInit(t)
|
TestInit(t)
|
||||||
err := initConfig(configFile)
|
err := initConfiguration(configFile)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
t.Fatalf("Error initializing config: %s", err.Error())
|
t.Fatalf("Error initializing config: %s", err.Error())
|
||||||
}
|
}
|
||||||
|
|
||||||
|
err = initExtraRoute(extraRoutePath)
|
||||||
|
if err != nil {
|
||||||
|
t.Fatalf("Error creating extra routes file: %s", err.Error())
|
||||||
|
}
|
||||||
ctx := context.Background()
|
ctx := context.Background()
|
||||||
g := GatewayServer{}
|
g := GatewayServer{}
|
||||||
gatewayServer, err := g.Config(configFile, ctx)
|
gatewayServer, err := g.Config(configFile, ctx)
|
||||||
|
|||||||
Reference in New Issue
Block a user