From 8f8f9d4d6cd50bbfed251cd44844b32259115bf6 Mon Sep 17 00:00:00 2001 From: Jonas Kaninda Date: Mon, 18 Nov 2024 10:03:58 +0100 Subject: [PATCH] chore: add extra route config tests --- internal/extra_config.go | 1 - internal/route.go | 6 +- internal/route_test.go | 178 +++++++++++++++++++++++++++++++++++++++ internal/server_test.go | 14 ++- 4 files changed, 193 insertions(+), 6 deletions(-) create mode 100644 internal/route_test.go diff --git a/internal/extra_config.go b/internal/extra_config.go index 60d72f0..4781e58 100644 --- a/internal/extra_config.go +++ b/internal/extra_config.go @@ -40,7 +40,6 @@ func loadExtraFiles(routePath string) ([]string, error) { }) 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 yamlFiles, nil diff --git a/internal/route.go b/internal/route.go index 7dc7aeb..50a6d4b 100644 --- a/internal/route.go +++ b/internal/route.go @@ -40,15 +40,15 @@ func (gatewayServer GatewayServer) Initialize() *mux.Router { if len(gateway.ExtraRoutes.Directory) != 0 { extraRoutes, err := loadExtraRoutes(gateway.ExtraRoutes.Directory) if err != nil { - logger.Error(err.Error()) + logger.Error("Error: %v", err.Error()) } dynamicRoutes = append(dynamicRoutes, extraRoutes...) } - //find duplicated route name + // find duplicated route name duplicates := findDuplicateRouteNames(dynamicRoutes) if len(duplicates) != 0 { for _, duplicate := range duplicates { - logger.Error("Duplicate route name found: %s ", duplicate) + logger.Error("Duplicated route named was found: %s ", duplicate) } } m := gatewayServer.middlewares diff --git a/internal/route_test.go b/internal/route_test.go new file mode 100644 index 0000000..3917a01 --- /dev/null +++ b/internal/route_test.go @@ -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 +} diff --git a/internal/server_test.go b/internal/server_test.go index 23d752f..4fc0ce6 100644 --- a/internal/server_test.go +++ b/internal/server_test.go @@ -10,6 +10,7 @@ import ( ) const testPath = "./tests" +const extraRoutePath = "./tests/extra" var configFile = filepath.Join(testPath, "goma.yml") @@ -18,11 +19,15 @@ func TestInit(t *testing.T) { if err != nil { t.Error(err) } + err = os.MkdirAll(extraRoutePath, os.ModePerm) + if err != nil { + t.Error(err) + } } func TestCheckConfig(t *testing.T) { TestInit(t) - err := initConfig(configFile) + err := initConfiguration(configFile) if err != nil { t.Fatal("Error init config:", err) } @@ -35,10 +40,15 @@ func TestCheckConfig(t *testing.T) { func TestStart(t *testing.T) { TestInit(t) - err := initConfig(configFile) + err := initConfiguration(configFile) if err != nil { 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() g := GatewayServer{} gatewayServer, err := g.Config(configFile, ctx)