-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain_test.go
More file actions
110 lines (96 loc) · 2.89 KB
/
Copy pathmain_test.go
File metadata and controls
110 lines (96 loc) · 2.89 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
package main
import (
"os"
"path/filepath"
"reflect"
"testing"
)
func TestNormalizeSubdomain(t *testing.T) {
t.Parallel()
tests := []struct {
name string
input string
domain string
want string
wantErr bool
}{
{name: "relative", input: "home", domain: "example.com", want: "home"},
{name: "fqdn", input: "Home.Example.com.", domain: "example.com", want: "home"},
{name: "root marker", input: "@", domain: "example.com", want: "@"},
{name: "root fqdn", input: "example.com", domain: "example.com", want: "@"},
{name: "nested", input: "vpn.eu", domain: "example.com", want: "vpn.eu"},
{name: "invalid", input: "bad / name", domain: "example.com", wantErr: true},
}
for _, tt := range tests {
tt := tt
t.Run(tt.name, func(t *testing.T) {
t.Parallel()
got, err := normalizeSubdomain(tt.input, tt.domain)
if tt.wantErr {
if err == nil {
t.Fatalf("expected error, got none")
}
return
}
if err != nil {
t.Fatalf("normalizeSubdomain returned error: %v", err)
}
if got != tt.want {
t.Fatalf("normalizeSubdomain = %q, want %q", got, tt.want)
}
})
}
}
func TestConfigStoreBootstrapAndManualReload(t *testing.T) {
t.Parallel()
path := filepath.Join(t.TempDir(), ".runtime.env")
store := newConfigStore(path, "example.com", managedCfg{
Mode: modeSelected,
Subdomains: []string{"NAS.Example.com", "@", "home", "home"},
})
cfg, err := store.Load()
if err != nil {
t.Fatalf("Load() error = %v", err)
}
if cfg.Mode != modeSelected {
t.Fatalf("Load() mode = %q, want %q", cfg.Mode, modeSelected)
}
if want := []string{"@", "home", "nas"}; !reflect.DeepEqual(cfg.Subdomains, want) {
t.Fatalf("Load() subdomains = %#v, want %#v", cfg.Subdomains, want)
}
manual := "MODE=selected\nSUBDOMAINS=WWW.example.com,@,WWW.example.com\n"
if err := os.WriteFile(path, []byte(manual), 0o600); err != nil {
t.Fatalf("WriteFile() error = %v", err)
}
reloaded, err := store.Load()
if err != nil {
t.Fatalf("Load() after manual edit error = %v", err)
}
if want := []string{"@", "www"}; !reflect.DeepEqual(reloaded.Subdomains, want) {
t.Fatalf("Load() after manual edit subdomains = %#v, want %#v", reloaded.Subdomains, want)
}
}
func TestSelectedModeStaysSelectedWithEmptyList(t *testing.T) {
t.Parallel()
path := filepath.Join(t.TempDir(), ".runtime.env")
store := newConfigStore(path, "example.com", managedCfg{
Mode: modeSelected,
Subdomains: []string{"home"},
})
if _, err := store.Load(); err != nil {
t.Fatalf("Load() error = %v", err)
}
cfg, err := store.Update(func(cfg *managedCfg) error {
removeSubdomain(&cfg.Subdomains, "home")
return nil
})
if err != nil {
t.Fatalf("Update() error = %v", err)
}
if cfg.Mode != modeSelected {
t.Fatalf("Update() mode = %q, want %q", cfg.Mode, modeSelected)
}
if len(cfg.Subdomains) != 0 {
t.Fatalf("Update() subdomains = %#v, want empty list", cfg.Subdomains)
}
}