all repos

mugit @ 744deadf94fe1f0d42872dd25dd3c52576e7325b

🐮 git server that your cow will love

mugit/internal/config/validate_test.go (view raw)

Oleksandr Smirnov Oleksandr Smirnov
olexsmir@gmail.com
config: parse interval directly as time.Duration, 3 months ago
1
package config
2
3
import (
4
	"testing"
5
6
	"olexsmir.xyz/x/is"
7
)
8
9
func TestCheckPort(t *testing.T) {
10
	is.Err(t, checkPort(1), nil)
11
	is.Err(t, checkPort(80), nil)
12
	is.Err(t, checkPort(65535), nil)
13
14
	is.Err(t, checkPort(0), "must be between")
15
	is.Err(t, checkPort(-1), "must be between")
16
	is.Err(t, checkPort(65536), "must be between")
17
}
18
19
func TestConfig_Validate(t *testing.T) {
20
	hostKey := "testdata/hostkey"
21
	tests := []struct {
22
		name     string
23
		expected any
24
		c        Config
25
	}{
26
		{
27
			name: "minimal",
28
			c: Config{
29
				Meta: MetaConfig{Host: "example.com"},
30
				Repo: RepoConfig{Dir: t.TempDir()},
31
			},
32
		},
33
		{
34
			name: "minimal with ssh",
35
			c: Config{
36
				Meta: MetaConfig{Host: "example.com"},
37
				Repo: RepoConfig{Dir: t.TempDir()},
38
				SSH: SSHConfig{
39
					Enable:  true,
40
					HostKey: hostKey,
41
				},
42
			},
43
		},
44
		{
45
			name:     "not set meta.host",
46
			expected: "meta.host is required",
47
			c: Config{
48
				Repo: RepoConfig{Dir: t.TempDir()},
49
			},
50
		},
51
		{
52
			name:     "invalid meta.host",
53
			expected: "meta.host shouldn't include protocol",
54
			c: Config{
55
				Meta: MetaConfig{Host: "https://example.com"},
56
				Repo: RepoConfig{Dir: t.TempDir()},
57
			},
58
		},
59
		{
60
			name:     "invalid repo.dir",
61
			expected: "repo.dir",
62
			c: Config{
63
				Meta: MetaConfig{Host: "example.com"},
64
				Repo: RepoConfig{Dir: "nonexistent"},
65
			},
66
		},
67
		{
68
			name:     "invalid server port",
69
			expected: "server.port",
70
			c: Config{
71
				Meta:   MetaConfig{Host: "example.com"},
72
				Repo:   RepoConfig{Dir: t.TempDir()},
73
				Server: ServerConfig{Port: -1},
74
			},
75
		},
76
		{
77
			name:     "invalid ssh port",
78
			expected: "ssh.port",
79
			c: Config{
80
				Meta: MetaConfig{Host: "example.com"},
81
				Repo: RepoConfig{Dir: t.TempDir()},
82
				SSH: SSHConfig{
83
					Enable:  true,
84
					HostKey: hostKey,
85
					Port:    100000,
86
				},
87
			},
88
		},
89
		{
90
			name:     "same ssh and http ports",
91
			expected: "ssh.port must differ",
92
			c: Config{
93
				Meta:   MetaConfig{Host: "example.com"},
94
				Repo:   RepoConfig{Dir: t.TempDir()},
95
				SSH:    SSHConfig{Enable: true, Port: 228},
96
				Server: ServerConfig{Port: 228},
97
			},
98
		},
99
		{
100
			name:     "invalid ssh.host_key path",
101
			expected: "ssh.host_key",
102
			c: Config{
103
				Meta: MetaConfig{Host: "example.com"},
104
				Repo: RepoConfig{Dir: t.TempDir()},
105
				SSH: SSHConfig{
106
					Enable:  true,
107
					HostKey: "/somewhere",
108
				},
109
			},
110
		},
111
	}
112
113
	for _, tt := range tests {
114
		t.Run(tt.name, func(t *testing.T) {
115
			tt.c.ensureDefaults()
116
			err := tt.c.validate()
117
			is.Err(t, err, tt.expected)
118
		})
119
	}
120
}