mirror of
https://github.com/juanfont/headscale.git
synced 2026-03-19 16:21:23 +01:00
Buffer the AuthRequest verdict channel to prevent a race where the sender blocks indefinitely if the receiver has already timed out, and increase the auth followup test timeout from 100ms to 5s to prevent spurious failures under load. Skip postgres-backed tests when the postgres server is unavailable instead of calling t.Fatal, which was preventing the rest of the test suite from running. Add TestMain to db, types, and policy/v2 packages to chdir to the source directory before running tests. This ensures relative testdata/ paths resolve correctly when the test binary is executed from an arbitrary working directory (e.g., via "go tool stress").
106 lines
1.9 KiB
Go
106 lines
1.9 KiB
Go
package db
|
|
|
|
import (
|
|
"log"
|
|
"net/url"
|
|
"os"
|
|
"strconv"
|
|
"strings"
|
|
"testing"
|
|
|
|
"github.com/juanfont/headscale/hscontrol/types"
|
|
"github.com/rs/zerolog"
|
|
"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")
|
|
zerolog.SetGlobalLevel(zerolog.Disabled)
|
|
|
|
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
|
|
}
|