From 44c47db9b2c077a61b657c9266bea7b0a127bee7 Mon Sep 17 00:00:00 2001 From: Fedor Korotkiy Date: Sat, 26 Mar 2022 16:12:29 +0300 Subject: [PATCH] Adapt for ubuntu --- pgfixture/pg.go | 46 +++++++++++++++++++++++++++++++--------------- 1 file changed, 31 insertions(+), 15 deletions(-) diff --git a/pgfixture/pg.go b/pgfixture/pg.go index 7bd5ff1..8b2d3a2 100644 --- a/pgfixture/pg.go +++ b/pgfixture/pg.go @@ -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):