Adapt for ubuntu

This commit is contained in:
Fedor Korotkiy 2022-03-26 16:12:29 +03:00
parent e410636456
commit 44c47db9b2

View file

@ -2,12 +2,38 @@ package pgfixture
import (
"fmt"
"io/ioutil"
"os"
"os/exec"
"path/filepath"
"testing"
"time"
)
func lookPath(t *testing.T, name string) string {
t.Helper()
path, err := exec.LookPath(name)
if err == nil {
return path
}
const ubuntuPostgres = "/usr/lib/postgresql"
if dirs, err := ioutil.ReadDir(ubuntuPostgres); err == nil {
for _, d := range dirs {
path := filepath.Join(ubuntuPostgres, d.Name(), "bin", name)
if _, err := os.Stat(path); err == nil {
return path
}
}
}
t.Fatalf("%s binary not found; is postgres installed?", name)
return ""
}
func Start(t *testing.T) string {
pgconn, ok := os.LookupEnv("PGCONN")
if ok {
@ -15,31 +41,21 @@ func Start(t *testing.T) string {
return pgconn
}
_, err := exec.LookPath("initdb")
if err != nil {
t.Fatalf("initdb binary not found; is postgres installed?")
}
_, err = exec.LookPath("postgres")
if err != nil {
t.Fatalf("postgres binary not found; is postgres installed?")
}
pgdata := t.TempDir()
cmd := exec.Command("initdb", "-N", "-D", pgdata)
cmd := exec.Command(lookPath(t, "initdb"), "-N", "-D", pgdata)
cmd.Stderr = os.Stderr
cmd.Stdout = os.Stdout
if err = cmd.Run(); err != nil {
if err := cmd.Run(); err != nil {
t.Fatalf("initdb failed: %v", err)
}
pgrun := t.TempDir()
cmd = exec.Command("postgres", "-D", pgdata, "-k", pgrun, "-F")
cmd = exec.Command(lookPath(t, "postgres"), "-D", pgdata, "-k", pgrun, "-F")
cmd.Stderr = os.Stderr
cmd.Stdout = os.Stdout
if err = cmd.Start(); err != nil {
if err := cmd.Start(); err != nil {
t.Fatalf("postgres failed: %v", err)
}
@ -49,7 +65,7 @@ func Start(t *testing.T) string {
}()
select {
case <-finished:
case err := <-finished:
t.Fatalf("postgres server terminated: %v", err)
case <-time.After(time.Second / 2):