~bigbes/sr-ht-dolt

ref: 8ba716c343fd6c99a38fe208cb07268c08ac2f18 sr-ht-dolt/cmd/doltsrht/main_test.go -rw-r--r-- 4.6 KiB
8ba716c3 — Eugene Blikh instconf: take the origin and required-key helpers from ecore 9 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
package main

import (
	"strings"
	"testing"

	"github.com/stretchr/testify/assert"
	"github.com/stretchr/testify/require"
	"github.com/vaughan0/go-ini"

	"sourcecraft.dev/bigbes/sr-ht-ecore/instconf"
)

// loadConf builds an ini.File directly from a literal, bypassing
// config.LoadConfig so the tests need no config.ini on disk and no
// internal-ipnet parsing.
func loadConf(t *testing.T, body string) ini.File {
	t.Helper()
	conf, err := ini.Load(strings.NewReader(body))
	if err != nil {
		t.Fatalf("ini.Load: %v", err)
	}
	return conf
}

func TestResolveSettingsDefaults(t *testing.T) {
	conf := loadConf(t, `[dolt.sr.ht]
origin=https://dolt.example.org
connection-string=postgres://u@localhost/d?sslmode=disable
`)

	got, err := resolveSettings(conf)
	if err != nil {
		t.Fatalf("resolveSettings: %v", err)
	}

	if got.connString != "postgres://u@localhost/d?sslmode=disable" {
		t.Errorf("connString = %q", got.connString)
	}
	if got.httpHost != "dolt.example.org" {
		t.Errorf("httpHost = %q, want dolt.example.org", got.httpHost)
	}
	if got.reposRoot != defaultReposRoot {
		t.Errorf("reposRoot = %q, want %q", got.reposRoot, defaultReposRoot)
	}
	if got.staticDir != defaultStaticDir {
		t.Errorf("staticDir = %q, want %q", got.staticDir, defaultStaticDir)
	}
	if got.remotesapiAddr != defaultRemotesapiAddr {
		t.Errorf("remotesapiAddr = %q, want %q", got.remotesapiAddr, defaultRemotesapiAddr)
	}
	if got.credsapiAddr != defaultCredsapiAddr {
		t.Errorf("credsapiAddr = %q, want %q", got.credsapiAddr, defaultCredsapiAddr)
	}
}

func TestResolveSettingsOverrides(t *testing.T) {
	conf := loadConf(t, `[dolt.sr.ht]
origin=https://dolt.example.org:8443
connection-string=postgres://u@localhost/d
repos=/srv/dolt
static-dir=/usr/share/sourcehut/dolt.sr.ht/static
remotesapi-listen=0.0.0.0:6306
credsapi-listen=0.0.0.0:6308
`)

	got, err := resolveSettings(conf)
	if err != nil {
		t.Fatalf("resolveSettings: %v", err)
	}

	if got.reposRoot != "/srv/dolt" {
		t.Errorf("reposRoot = %q", got.reposRoot)
	}
	if got.staticDir != "/usr/share/sourcehut/dolt.sr.ht/static" {
		t.Errorf("staticDir = %q", got.staticDir)
	}
	if got.remotesapiAddr != "0.0.0.0:6306" {
		t.Errorf("remotesapiAddr = %q", got.remotesapiAddr)
	}
	if got.credsapiAddr != "0.0.0.0:6308" {
		t.Errorf("credsapiAddr = %q", got.credsapiAddr)
	}
	// The port is part of the sealed-URL authority and is preserved.
	if got.httpHost != "dolt.example.org:8443" {
		t.Errorf("httpHost = %q, want dolt.example.org:8443", got.httpHost)
	}
}

func TestResolveSettingsMissingConnString(t *testing.T) {
	conf := loadConf(t, `[dolt.sr.ht]
origin=https://dolt.example.org
`)

	_, err := resolveSettings(conf)
	require.Error(t, err)
	assert.ErrorIs(t, err, instconf.ErrIncompleteConfig)
	assert.Contains(t, err.Error(), "connection-string")
}

func TestResolveSettingsMissingOrigin(t *testing.T) {
	conf := loadConf(t, `[dolt.sr.ht]
connection-string=postgres://u@localhost/d
`)

	_, err := resolveSettings(conf)
	require.Error(t, err)
	assert.ErrorIs(t, err, instconf.ErrIncompleteConfig)
	assert.Contains(t, err.Error(), "origin")
}

// TestResolveSettingsReportsEveryMissingKey is the reason the two checks became
// one Require: an operator with an empty section gets both keys off one boot
// rather than one key per restart.
func TestResolveSettingsReportsEveryMissingKey(t *testing.T) {
	_, err := resolveSettings(loadConf(t, "[dolt.sr.ht]\n"))
	require.Error(t, err)
	assert.Contains(t, err.Error(), "connection-string")
	assert.Contains(t, err.Error(), "origin")
}

// TestResolveSettingsHostlessOrigin covers the gap Require cannot: a present,
// non-blank origin that names no host. "dolt.example.org" without a scheme is a
// URL path, and a service whose sealed chunk URLs and JWT audience are built
// from an empty authority must refuse to start rather than guess a host.
func TestResolveSettingsHostlessOrigin(t *testing.T) {
	conf := loadConf(t, `[dolt.sr.ht]
connection-string=postgres://u@localhost/d
origin=dolt.example.org
`)

	_, err := resolveSettings(conf)
	require.Error(t, err)
	assert.Contains(t, err.Error(), "names no host")
}

// TestResolveSettingsTrimsTheOrigin: an origin written with a trailing slash is
// the same origin. It used to reach url.Parse untouched, which happened to
// answer the same host; the authority now comes off the canonical form, so the
// two spellings cannot diverge for any other consumer either.
func TestResolveSettingsTrimsTheOrigin(t *testing.T) {
	conf := loadConf(t, `[dolt.sr.ht]
connection-string=postgres://u@localhost/d
origin=https://dolt.example.org:8443/
`)

	got, err := resolveSettings(conf)
	require.NoError(t, err)
	assert.Equal(t, "dolt.example.org:8443", got.httpHost)
}