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

WIP: read config from environment variables #1608

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
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
10 changes: 9 additions & 1 deletion internal/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import (
"fmt"
"log/slog"
"math"
"os"
"strings"

"github.com/fsouza/fake-gcs-server/fakestorage"
Expand Down Expand Up @@ -60,6 +61,13 @@ type EventConfig struct {
list []string
}

func envVarOrDefault(key string, defaultValue string) string {
if val, ok := os.LookupEnv(key); ok {
return val
}
return defaultValue
}

// Load parses the given arguments list and return a config object (and/or an
// error in case of failures).
func Load(args []string) (Config, error) {
Expand All @@ -69,7 +77,7 @@ func Load(args []string) (Config, error) {
var logLevel string

fs := flag.NewFlagSet("fake-gcs-server", flag.ContinueOnError)
fs.StringVar(&cfg.backend, "backend", filesystemBackend, "storage backend (memory or filesystem)")
fs.StringVar(&cfg.backend, "backend", envVarOrDefault("FAKE_GCS_BACKEND", filesystemBackend), "storage backend (memory or filesystem)")
fs.StringVar(&cfg.fsRoot, "filesystem-root", "/storage", "filesystem root (required for the filesystem backend). folder will be created if it doesn't exist")
fs.StringVar(&cfg.publicHost, "public-host", "storage.googleapis.com", "Optional URL for public host")
fs.StringVar(&cfg.externalURL, "external-url", "", "optional external URL, returned in the Location header for uploads. Defaults to the address where the server is running")
Expand Down
76 changes: 71 additions & 5 deletions internal/config/config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ package config

import (
"log/slog"
"os"
"strings"
"testing"

"github.com/fsouza/fake-gcs-server/fakestorage"
Expand All @@ -18,10 +20,11 @@ import (
func TestLoadConfig(t *testing.T) {
t.Parallel()
tests := []struct {
name string
args []string
expectedConfig Config
expectErr bool
name string
args []string
environmentVariables map[string]string
expectedConfig Config
expectErr bool
}{
{
name: "all parameters",
Expand Down Expand Up @@ -274,6 +277,56 @@ func TestLoadConfig(t *testing.T) {
LogLevel: slog.LevelInfo,
},
},
{
name: "using environment variables",
args: []string{},
environmentVariables: map[string]string{
"FAKE_GCS_BACKEND": "memory",
},
expectedConfig: Config{
Seed: "",
backend: "memory",
fsRoot: "/storage",
publicHost: "storage.googleapis.com",
externalURL: "https://0.0.0.0:4443",
allowedCORSHeaders: nil,
Host: "0.0.0.0",
Port: 4443,
PortHTTP: 0,
Scheme: "https",
event: EventConfig{
list: []string{"finalize"},
},
bucketLocation: "US-CENTRAL1",
LogLevel: slog.LevelInfo,
},
},
{
name: "args have precedence over environment variables",
args: []string{
"-backend", "filesystem",
},
environmentVariables: map[string]string{
"FAKE_GCS_BACKEND": "memory",
},
expectedConfig: Config{
Seed: "",
backend: "filesystem",
fsRoot: "/storage",
publicHost: "storage.googleapis.com",
externalURL: "https://0.0.0.0:4443",
allowedCORSHeaders: nil,
Host: "0.0.0.0",
Port: 4443,
PortHTTP: 0,
Scheme: "https",
event: EventConfig{
list: []string{"finalize"},
},
bucketLocation: "US-CENTRAL1",
LogLevel: slog.LevelInfo,
},
},
{
name: "invalid port value type",
args: []string{"-port", "not-a-number"},
Expand Down Expand Up @@ -333,7 +386,20 @@ func TestLoadConfig(t *testing.T) {
for _, test := range tests {
test := test
t.Run(test.name, func(t *testing.T) {
t.Parallel()
// Set up environment
beforeEnv := os.Environ()
os.Clearenv()
for k, v := range test.environmentVariables {
os.Setenv(k, v)
}
t.Cleanup(func() {
os.Clearenv()
for _, envVar := range beforeEnv {
parts := strings.SplitN(envVar, "=", 2)
os.Setenv(parts[0], parts[1])
}
})

cfg, err := Load(test.args)
if err != nil && !test.expectErr {
t.Fatalf("unexpected non-nil error: %v", err)
Expand Down