~bigbes/sr-ht-spec

ref: 0879325b6c7e7d74454ecff0196b8d5ecc12f1ea sr-ht-spec/cmd/specsrht/main_test.go -rw-r--r-- 5.1 KiB
0879325b — bigbes docs: the empty-space-list polarity is retracted, not just documented 27 days ago
                                                                                
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
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
package main

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

	"github.com/vaughan0/go-ini"

	"sourcecraft.dev/bigbes/sr-ht-spec/hooks"
)

// completeConfig is an instance config with every key this daemon needs: the
// two core-go fatals on, and everything service.LoadConfig reads.
func completeConfig() ini.File {
	return ini.File{
		"sr.ht": {
			"network-key": "wF4z9nZ6C0oL0kQyq3M0j0m3iSvWq7iFhk1v9E9WcBM=",
			"owner-name":  "bigbes",
			"owner-email": "bigbes@example.invalid",
		},
		"webhooks": {
			"private-key": "eV5B1o1M4a0dQKr2v4h0hR3vJm0m5g7jL1kQpS4Xk1s=",
		},
		serviceName: {
			"origin":            "https://spec.srht.bigb.es",
			"repos":             "/var/lib/spec",
			"cache":             "/var/cache/spec",
			"connection-string": "postgresql://specsrht@localhost/spec.sr.ht?sslmode=disable",
		},
	}
}

func without(conf ini.File, section, key string) ini.File {
	out := ini.File{}
	for s, kv := range conf {
		copied := ini.Section{}
		for k, v := range kv {
			if s == section && k == key {
				continue
			}
			copied[k] = v
		}
		out[s] = copied
	}
	return out
}

func TestValidateConfigAcceptsACompleteConfig(t *testing.T) {
	cfg, err := validateConfig(completeConfig())
	if err != nil {
		t.Fatalf("validateConfig: %v", err)
	}
	if cfg.Repos != "/var/lib/spec" || cfg.Cache != "/var/cache/spec" {
		t.Errorf("config not carried through: %+v", cfg)
	}
	if cfg.Instance.OwnerName != "bigbes" {
		t.Errorf("owner not carried through: %+v", cfg.Instance)
	}
}

func TestValidateConfigNamesEachMissingKey(t *testing.T) {
	tests := []struct {
		section, key, want string
	}{
		{"sr.ht", "network-key", "[sr.ht] network-key"},
		{"webhooks", "private-key", "[webhooks] private-key"},
		{"sr.ht", "owner-name", "[sr.ht] owner-name"},
		{"sr.ht", "owner-email", "[sr.ht] owner-email"},
		{serviceName, "origin", "[spec.sr.ht] origin"},
		{serviceName, "repos", "[spec.sr.ht] repos"},
		{serviceName, "cache", "[spec.sr.ht] cache"},
		{serviceName, "connection-string", "[spec.sr.ht] connection-string"},
	}
	for _, tt := range tests {
		t.Run(tt.section+"/"+tt.key, func(t *testing.T) {
			_, err := validateConfig(without(completeConfig(), tt.section, tt.key))
			if err == nil {
				t.Fatalf("validateConfig accepted a config with no %s", tt.want)
			}
			if !strings.Contains(err.Error(), tt.want) {
				t.Errorf("the error does not name %s:\n%v", tt.want, err)
			}
		})
	}
}

// TestValidateConfigReportsEveryGapAtOnce is the whole point of doing this at
// startup: an operator fixes the config in one pass instead of discovering
// each gap on a separate restart.
func TestValidateConfigReportsEveryGapAtOnce(t *testing.T) {
	conf := ini.File{}
	_, err := validateConfig(conf)
	if err == nil {
		t.Fatal("validateConfig accepted an empty config")
	}
	for _, want := range []string{
		"[sr.ht] network-key",
		"[webhooks] private-key",
		"[sr.ht] owner-name",
		"[sr.ht] owner-email",
		"[spec.sr.ht] origin",
		"[spec.sr.ht] repos",
		"[spec.sr.ht] cache",
		"[spec.sr.ht] connection-string",
	} {
		if !strings.Contains(err.Error(), want) {
			t.Errorf("the error does not name %s:\n%v", want, err)
		}
	}
}

// TestValidateConfigRejectsAnUnusableValue: a present but wrong key is as
// fatal as a missing one, and service.Config.Validate is what says so.
func TestValidateConfigRejectsAnUnusableValue(t *testing.T) {
	conf := completeConfig()
	conf[serviceName]["repos"] = "var/lib/spec" // relative
	_, err := validateConfig(conf)
	if err == nil {
		t.Fatal("validateConfig accepted a relative repos root")
	}
	if !strings.Contains(err.Error(), "absolute path") {
		t.Errorf("the error does not explain the problem:\n%v", err)
	}
}

// TestBlankIsMissing: an empty value satisfies "the key is present" and
// nothing else.
func TestBlankIsMissing(t *testing.T) {
	conf := completeConfig()
	conf["sr.ht"]["network-key"] = "   "
	_, err := validateConfig(conf)
	if err == nil {
		t.Fatal("validateConfig accepted a blank network-key")
	}
	if !strings.Contains(err.Error(), "[sr.ht] network-key") {
		t.Errorf("the error does not name the blank key:\n%v", err)
	}
}

func TestParseLevel(t *testing.T) {
	tests := map[string]slog.Level{
		"":         slog.LevelInfo,
		"info":     slog.LevelInfo,
		"nonsense": slog.LevelInfo,
		"debug":    slog.LevelDebug,
		" DEBUG ":  slog.LevelDebug,
		"warn":     slog.LevelWarn,
		"warning":  slog.LevelWarn,
		"error":    slog.LevelError,
	}
	for in, want := range tests {
		if got := parseLevel(in); got != want {
			t.Errorf("parseLevel(%q) = %v want %v", in, got, want)
		}
	}
}

// TestHookDispatchIsCheckedBeforeAnythingElse guards the one ordering in main
// that matters: a hook must not read a config file or open Postgres, because
// it runs once per ref of every push and its only job is to reach the daemon.
func TestHookDispatchIsCheckedBeforeAnythingElse(t *testing.T) {
	if _, _, ok := hooks.ModeFromArgs([]string{"/var/lib/spec/~bigbes/rfcs/hooks/update"}); !ok {
		t.Error("main would treat an update hook invocation as a daemon start")
	}
	if _, _, ok := hooks.ModeFromArgs([]string{"/usr/local/bin/specsrht", "-b", "localhost:5091"}); ok {
		t.Error("main would treat a daemon start as a hook invocation")
	}
}