fix postgres constraints, add postgres testing
This commit fixes the constraint syntax so it is both valid for sqlite and postgres. To validate this, I've added a new postgres testing library and a helper that will spin up local postgres, setup a db and use it in the constraints tests. This should also help testing db stuff in the future. postgres has been added to the nix dev shell and is now required for running the unit tests. Signed-off-by: Kristoffer Dalby <kristoffer@tailscale.com>
This commit is contained in:
parent
7d9b430ec2
commit
f6276ab9d2
7 changed files with 95 additions and 21 deletions
|
@ -1,12 +1,17 @@
|
|||
package db
|
||||
|
||||
import (
|
||||
"context"
|
||||
"log"
|
||||
"net/url"
|
||||
"os"
|
||||
"strconv"
|
||||
"strings"
|
||||
"testing"
|
||||
|
||||
"github.com/juanfont/headscale/hscontrol/types"
|
||||
"gopkg.in/check.v1"
|
||||
"zombiezen.com/go/postgrestest"
|
||||
)
|
||||
|
||||
func Test(t *testing.T) {
|
||||
|
@ -36,13 +41,15 @@ func (s *Suite) ResetDB(c *check.C) {
|
|||
// }
|
||||
|
||||
var err error
|
||||
db, err = newTestDB()
|
||||
db, err = newSQLiteTestDB()
|
||||
if err != nil {
|
||||
c.Fatal(err)
|
||||
}
|
||||
}
|
||||
|
||||
func newTestDB() (*HSDatabase, error) {
|
||||
// TODO(kradalby): make this a t.Helper when we dont depend
|
||||
// on check test framework.
|
||||
func newSQLiteTestDB() (*HSDatabase, error) {
|
||||
var err error
|
||||
tmpDir, err = os.MkdirTemp("", "headscale-db-test-*")
|
||||
if err != nil {
|
||||
|
@ -53,7 +60,7 @@ func newTestDB() (*HSDatabase, error) {
|
|||
|
||||
db, err = NewHeadscaleDatabase(
|
||||
types.DatabaseConfig{
|
||||
Type: "sqlite3",
|
||||
Type: types.DatabaseSqlite,
|
||||
Sqlite: types.SqliteConfig{
|
||||
Path: tmpDir + "/headscale_test.db",
|
||||
},
|
||||
|
@ -67,3 +74,53 @@ func newTestDB() (*HSDatabase, error) {
|
|||
|
||||
return db, nil
|
||||
}
|
||||
|
||||
func newPostgresTestDB(t *testing.T) *HSDatabase {
|
||||
t.Helper()
|
||||
|
||||
var err error
|
||||
tmpDir, err = os.MkdirTemp("", "headscale-db-test-*")
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
log.Printf("database path: %s", tmpDir+"/headscale_test.db")
|
||||
|
||||
ctx := context.Background()
|
||||
srv, err := postgrestest.Start(ctx)
|
||||
if err != nil {
|
||||
t.Fatal(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)
|
||||
|
||||
pass, _ := pu.User.Password()
|
||||
port, _ := strconv.Atoi(pu.Port())
|
||||
|
||||
db, err = NewHeadscaleDatabase(
|
||||
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",
|
||||
},
|
||||
},
|
||||
"",
|
||||
emptyCache(),
|
||||
)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
return db
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue