mirror of
https://github.com/juanfont/headscale.git
synced 2026-09-15 13:02:02 +09:00
c0fd9b60ed
The previous commit centralizes test log silencing in hscontrol/types/testlog.go, so the per-test SetGlobalLevel calls scattered across the batcher and db tests are now redundant. Most of them were also buggy: they restored to DebugLevel instead of capturing and restoring the prior level, polluting global state for subsequent tests in the same binary. Remove all such pairs from batcher_bench_test.go, batcher_concurrency_test.go, batcher_scale_bench_test.go, and suite_test.go, along with the now-unused zerolog imports. The one correct save/restore pattern in batcher_test.go is left untouched: it captures originalLevel via zerolog.GlobalLevel() and works transparently with the new ErrorLevel default.
104 lines
1.8 KiB
Go
104 lines
1.8 KiB
Go
package db
|
|
|
|
import (
|
|
"log"
|
|
"net/url"
|
|
"os"
|
|
"strconv"
|
|
"strings"
|
|
"testing"
|
|
|
|
"github.com/juanfont/headscale/hscontrol/types"
|
|
"zombiezen.com/go/postgrestest"
|
|
)
|
|
|
|
func newSQLiteTestDB() (*HSDatabase, error) {
|
|
tmpDir, err := os.MkdirTemp("", "headscale-db-test-*")
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
log.Printf("database path: %s", tmpDir+"/headscale_test.db")
|
|
|
|
db, err := NewHeadscaleDatabase(
|
|
&types.Config{
|
|
Database: types.DatabaseConfig{
|
|
Type: types.DatabaseSqlite,
|
|
Sqlite: types.SqliteConfig{
|
|
Path: tmpDir + "/headscale_test.db",
|
|
},
|
|
},
|
|
Policy: types.PolicyConfig{
|
|
Mode: types.PolicyModeDB,
|
|
},
|
|
},
|
|
emptyCache(),
|
|
)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
return db, nil
|
|
}
|
|
|
|
func newPostgresTestDB(t *testing.T) *HSDatabase {
|
|
t.Helper()
|
|
|
|
return newHeadscaleDBFromPostgresURL(t, newPostgresDBForTest(t))
|
|
}
|
|
|
|
func newPostgresDBForTest(t *testing.T) *url.URL {
|
|
t.Helper()
|
|
|
|
ctx := t.Context()
|
|
|
|
srv, err := postgrestest.Start(ctx)
|
|
if err != nil {
|
|
t.Skipf("start postgres: %s", err)
|
|
}
|
|
|
|
t.Cleanup(srv.Cleanup)
|
|
|
|
u, err := srv.CreateDatabase(ctx)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
|
|
t.Logf("created local postgres: %s", u)
|
|
pu, _ := url.Parse(u)
|
|
|
|
return pu
|
|
}
|
|
|
|
func newHeadscaleDBFromPostgresURL(t *testing.T, pu *url.URL) *HSDatabase {
|
|
t.Helper()
|
|
|
|
pass, _ := pu.User.Password()
|
|
port, _ := strconv.Atoi(pu.Port())
|
|
|
|
db, err := NewHeadscaleDatabase(
|
|
&types.Config{
|
|
Database: types.DatabaseConfig{
|
|
Type: types.DatabasePostgres,
|
|
Postgres: types.PostgresConfig{
|
|
Host: pu.Hostname(),
|
|
User: pu.User.Username(),
|
|
Name: strings.TrimLeft(pu.Path, "/"),
|
|
Pass: pass,
|
|
Port: port,
|
|
Ssl: "disable",
|
|
},
|
|
},
|
|
Policy: types.PolicyConfig{
|
|
Mode: types.PolicyModeDB,
|
|
},
|
|
},
|
|
emptyCache(),
|
|
)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
|
|
return db
|
|
}
|