Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Start over if environment is set via settings. #534

Merged
merged 2 commits into from
Jul 27, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 11 additions & 0 deletions config/configload/load.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 2 additions & 0 deletions config/settings.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@ const otelCollectorEndpoint = "localhost:4317"
// DefaultSettings defines the <DefaultSettings> object.
var DefaultSettings = Settings{
DefaultPort: 8080,
Environment: "",
HealthPath: "/healthz",
LogFormat: "common",
LogLevel: "info",
Expand Down Expand Up @@ -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"`
Expand Down
64 changes: 63 additions & 1 deletion server/http_integration_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down Expand Up @@ -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)
}
})
}
}