From a8d39ded979710873c0dd980301d227eac486fbf Mon Sep 17 00:00:00 2001 From: Joe Afflerbach Date: Tue, 5 Jul 2022 12:12:54 +0200 Subject: [PATCH] Start over if `environment` is set via `settings`. --- config/configload/load.go | 11 ++++++ config/settings.go | 2 ++ server/http_integration_test.go | 64 ++++++++++++++++++++++++++++++++- 3 files changed, 76 insertions(+), 1 deletion(-) diff --git a/config/configload/load.go b/config/configload/load.go index 5dd5aadc8..634473856 100644 --- a/config/configload/load.go +++ b/config/configload/load.go @@ -145,6 +145,17 @@ func LoadFiles(filesList []string, env string) (*config.Couper, error) { return nil, err } + if env == "" { + settingsBlock := mergeSettings(parsedBodies) + settings := &config.Settings{} + if diags := gohcl.DecodeBody(settingsBlock.Body, nil, settings); diags.HasErrors() { + return nil, diags + } + if settings.Environment != "" { + return LoadFiles(filesList, settings.Environment) + } + } + defaultsBlock, err := mergeDefaults(parsedBodies) if err != nil { return nil, err diff --git a/config/settings.go b/config/settings.go index 105c81d33..0fedf1713 100644 --- a/config/settings.go +++ b/config/settings.go @@ -53,6 +53,7 @@ const otelCollectorEndpoint = "localhost:4317" // DefaultSettings defines the object. var DefaultSettings = Settings{ DefaultPort: 8080, + Environment: "", HealthPath: "/healthz", LogFormat: "common", LogLevel: "info", @@ -81,6 +82,7 @@ type Settings struct { CAFile string `hcl:"ca_file,optional"` AcceptForwardedURL []string `hcl:"accept_forwarded_url,optional"` DefaultPort int `hcl:"default_port,optional"` + Environment string `hcl:"environment,optional"` HealthPath string `hcl:"health_path,optional"` LogFormat string `hcl:"log_format,optional"` LogLevel string `hcl:"log_level,optional"` diff --git a/server/http_integration_test.go b/server/http_integration_test.go index 5a28bc6c4..18b6589e4 100644 --- a/server/http_integration_test.go +++ b/server/http_integration_test.go @@ -513,7 +513,6 @@ func TestHTTPServer_EnvVars(t *testing.T) { if res.StatusCode != http.StatusUnauthorized { t.Errorf("expected 401, got %d", res.StatusCode) } - } func TestHTTPServer_XFHHeader(t *testing.T) { @@ -5456,3 +5455,66 @@ func TestWildcardURLAttribute(t *testing.T) { }) } } + +func TestEnvironmentSetting(t *testing.T) { + helper := test.New(t) + tests := []struct { + env string + }{ + {""}, + {"foo"}, + {"bar"}, + } + + template := ` + server { + endpoint "/" { + response { + environment "foo" { + headers = { X-Env: "foo" } + } + environment "bar" { + headers = { X-Env: "bar" } + } + } + } + } + settings { + environment = "%s" + } + ` + + file, err := os.CreateTemp("", "tmpfile-") + helper.Must(err) + defer file.Close() + defer os.Remove(file.Name()) + + client := newClient() + for _, tt := range tests { + t.Run(tt.env, func(subT *testing.T) { + config := []byte(fmt.Sprintf(template, tt.env)) + err := os.Truncate(file.Name(), 0) + helper.Must(err) + _, err = file.Seek(0, 0) + helper.Must(err) + _, err = file.Write(config) + helper.Must(err) + + couperConfig, err := configload.LoadFile(file.Name(), "") + helper.Must(err) + + shutdown, _ := newCouperWithConfig(couperConfig, helper) + defer shutdown() + + req, err := http.NewRequest(http.MethodGet, "http://localhost:8080/", nil) + helper.Must(err) + + res, err := client.Do(req) + helper.Must(err) + + if header := res.Header.Get("X-Env"); header != tt.env { + subT.Errorf("Unexpected header:\n\tWant: %q\n\tGot: %q", tt.env, header) + } + }) + } +}