MCPcopy Create free account
hub / github.com/google/go-cloud / TestOpen

Function TestOpen

postgres/postgres_test.go:31–132  ·  view source on GitHub ↗
(t *testing.T)

Source from the content-addressed store, hash-verified

29)
30
31func TestOpen(t *testing.T) {
32 if runtime.GOOS == "windows" {
33 t.Skip("Can't use Unix sockets on Windows")
34 }
35 postgresPath, err := exec.LookPath("postgres")
36 if err != nil {
37 t.Skip("Can't find postgres:", err)
38 }
39 initdbPath, err := exec.LookPath("initdb")
40 if err != nil {
41 t.Skip("Can't find initdb:", err)
42 }
43
44 // Create a temporary database data directory.
45 currUser, err := user.Current()
46 if err != nil {
47 t.Fatal(err)
48 }
49
50 dir := t.TempDir()
51
52 dataDir := filepath.Join(dir, "data")
53 initdbCmd := exec.Command(initdbPath, "-U", currUser.Username, "-D", dataDir)
54 initdbOutput := new(bytes.Buffer)
55 initdbCmd.Stdout = initdbOutput
56 initdbCmd.Stderr = initdbOutput
57 err = initdbCmd.Run()
58 if err != nil {
59 t.Log(initdbOutput)
60 t.Fatal(err)
61 }
62
63 // Configure the database server to listen on a Unix socket located in the temporary directory.
64 socketDir, err := filepath.Abs(filepath.Join(dir, "socket"))
65 if err != nil {
66 t.Fatal(err)
67 }
68 if err := os.Mkdir(socketDir, 0o777); err != nil {
69 t.Fatal(err)
70 }
71 confData := new(bytes.Buffer)
72 fmt.Fprintf(confData, "unix_socket_directories = '%s'\n", socketDir)
73 err = os.WriteFile(filepath.Join(dataDir, "postgresql.conf"), confData.Bytes(), 0o666)
74 if err != nil {
75 t.Fatal(err)
76 }
77
78 // Start the database server (and arrange for it to be stopped at test end).
79 server := exec.Command(postgresPath, "-D", dataDir)
80 serverOutput := new(bytes.Buffer)
81 server.Stdout = serverOutput
82 server.Stderr = serverOutput
83 if err := server.Start(); err != nil {
84 t.Fatal(err)
85 }
86 serverSignaled := false
87 defer func() {
88 if !serverSignaled {

Callers

nothing calls this directly

Calls 8

StartMethod · 0.80
WaitMethod · 0.80
PingMethod · 0.80
LogMethod · 0.65
OpenPostgresURLMethod · 0.65
CloseMethod · 0.65
ErrorMethod · 0.45
EncodeMethod · 0.45

Tested by

no test coverage detected