package config import ( "os" "path/filepath" "testing" "time" ) func TestDefaults(t *testing.T) { c, err := Load(filepath.Join(t.TempDir(), "geen.yml")) if err != nil { t.Fatal(err) } if c.ArchiveInterval.Duration != 24*time.Hour || c.RetentionDays != 30 || c.RateLimitKiB != 1024 || c.Listen != "0.0.0.0:8080" { t.Fatalf("%+v", c) } } func TestLoad(t *testing.T) { p := filepath.Join(t.TempDir(), "c.yml") _ = os.WriteFile(p, []byte("archive_interval: 7d\nretention_days: -1\nrate_limit_kib: -1\npublic_url: http://b:1\n"), 0o600) c, err := Load(p) if err != nil { t.Fatal(err) } if c.ArchiveInterval.Duration != 7*24*time.Hour || c.RetentionDays != 0 || c.RateLimitKiB != 0 { t.Fatalf("%+v", c) } _ = os.WriteFile(p, []byte("public_url: ftp://x\n"), 0o600) if _, err := Load(p); err == nil { t.Fatal("ftp geaccepteerd") } _ = os.WriteFile(p, []byte("archive_interval: soms\n"), 0o600) if _, err := Load(p); err == nil { t.Fatal("ongeldige duur geaccepteerd") } if _, err := os.Stat(p); err != nil { t.Fatal(err) } if Example() == "" { t.Fatal("geen voorbeeld") } } func TestFieldsRoundTrip(t *testing.T) { var c Config c.Defaults() for _, f := range Fields { v := c.Get(f.Key) if f.Key != "public_url" && v == "" { t.Errorf("%s: leeg", f.Key) } if err := c.Set(f.Key, v); err != nil && f.Key != "public_url" { t.Errorf("%s=%q: %v", f.Key, v, err) } if got := c.Get(f.Key); got != v { t.Errorf("%s: %q != %q", f.Key, got, v) } } if c.Listen != "0.0.0.0:8080" { t.Fatalf("standaardpoort %s", c.Listen) } } func TestApplyIsAtomic(t *testing.T) { var c Config c.Defaults() if err := c.Apply(map[string]string{"retention_days": "7", "archive_interval": "nee"}); err == nil { t.Fatal("fout verwacht") } if c.RetentionDays != 30 { t.Fatal("half toegepast") } if err := c.Apply(map[string]string{"retention_days": "7", "archive_interval": "7d", "public_url": "http://b:8080/"}); err != nil { t.Fatal(err) } if c.RetentionDays != 7 || c.ArchiveInterval.Duration != 7*24*time.Hour || c.PublicURL != "http://b:8080" { t.Fatalf("%+v", c) } if err := c.Apply(map[string]string{"data_dir": "/tmp"}); err == nil { t.Fatal("data_dir mag niet") } } func TestSettingsFile(t *testing.T) { d := t.TempDir() if err := SaveSettings(d, map[string]string{"retention_days": "3"}); err != nil { t.Fatal(err) } m, err := LoadSettings(d) if err != nil || m["retention_days"] != "3" { t.Fatal(m, err) } }