BlueGit

timfa bluegit

Files

bluegit / internal / backupserver / config / config_test.go

config_test.go

config_test.go 2.4 KiB Download Raw
1package config
2
3import (
4 "os"
5 "path/filepath"
6 "testing"
7 "time"
8)
9
10func TestDefaults(t *testing.T) {
11 c, err := Load(filepath.Join(t.TempDir(), "geen.yml"))
12 if err != nil {
13 t.Fatal(err)
14 }
15 if c.ArchiveInterval.Duration != 24*time.Hour || c.RetentionDays != 30 || c.RateLimitKiB != 1024 || c.Listen != "0.0.0.0:8080" {
16 t.Fatalf("%+v", c)
17 }
18}
19
20func TestLoad(t *testing.T) {
21 p := filepath.Join(t.TempDir(), "c.yml")
22 _ = os.WriteFile(p, []byte("archive_interval: 7d\nretention_days: -1\nrate_limit_kib: -1\npublic_url: http://b:1\n"), 0o600)
23 c, err := Load(p)
24 if err != nil {
25 t.Fatal(err)
26 }
27 if c.ArchiveInterval.Duration != 7*24*time.Hour || c.RetentionDays != 0 || c.RateLimitKiB != 0 {
28 t.Fatalf("%+v", c)
29 }
30 _ = os.WriteFile(p, []byte("public_url: ftp://x\n"), 0o600)
31 if _, err := Load(p); err == nil {
32 t.Fatal("ftp geaccepteerd")
33 }
34 _ = os.WriteFile(p, []byte("archive_interval: soms\n"), 0o600)
35 if _, err := Load(p); err == nil {
36 t.Fatal("ongeldige duur geaccepteerd")
37 }
38 if _, err := os.Stat(p); err != nil {
39 t.Fatal(err)
40 }
41 if Example() == "" {
42 t.Fatal("geen voorbeeld")
43 }
44}
45
46func TestFieldsRoundTrip(t *testing.T) {
47 var c Config
48 c.Defaults()
49 for _, f := range Fields {
50 v := c.Get(f.Key)
51 if f.Key != "public_url" && v == "" {
52 t.Errorf("%s: leeg", f.Key)
53 }
54 if err := c.Set(f.Key, v); err != nil && f.Key != "public_url" {
55 t.Errorf("%s=%q: %v", f.Key, v, err)
56 }
57 if got := c.Get(f.Key); got != v {
58 t.Errorf("%s: %q != %q", f.Key, got, v)
59 }
60 }
61 if c.Listen != "0.0.0.0:8080" {
62 t.Fatalf("standaardpoort %s", c.Listen)
63 }
64}
65
66func TestApplyIsAtomic(t *testing.T) {
67 var c Config
68 c.Defaults()
69 if err := c.Apply(map[string]string{"retention_days": "7", "archive_interval": "nee"}); err == nil {
70 t.Fatal("fout verwacht")
71 }
72 if c.RetentionDays != 30 {
73 t.Fatal("half toegepast")
74 }
75 if err := c.Apply(map[string]string{"retention_days": "7", "archive_interval": "7d", "public_url": "http://b:8080/"}); err != nil {
76 t.Fatal(err)
77 }
78 if c.RetentionDays != 7 || c.ArchiveInterval.Duration != 7*24*time.Hour || c.PublicURL != "http://b:8080" {
79 t.Fatalf("%+v", c)
80 }
81 if err := c.Apply(map[string]string{"data_dir": "/tmp"}); err == nil {
82 t.Fatal("data_dir mag niet")
83 }
84}
85
86func TestSettingsFile(t *testing.T) {
87 d := t.TempDir()
88 if err := SaveSettings(d, map[string]string{"retention_days": "3"}); err != nil {
89 t.Fatal(err)
90 }
91 m, err := LoadSettings(d)
92 if err != nil || m["retention_days"] != "3" {
93 t.Fatal(m, err)
94 }
95}