| 1 |
// Copyright 2019 The Go Cloud Development Kit Authors |
| 2 |
// |
| 3 |
// Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 |
// you may not use this file except in compliance with the License. |
| 5 |
// You may obtain a copy of the License at |
| 6 |
// |
| 7 |
// https://www.apache.org/licenses/LICENSE-2.0 |
| 8 |
// |
| 9 |
// Unless required by applicable law or agreed to in writing, software |
| 10 |
// distributed under the License is distributed on an "AS IS" BASIS, |
| 11 |
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 |
// See the License for the specific language governing permissions and |
| 13 |
// limitations under the License. |
| 14 |
|
| 15 |
package postgres |
| 16 |
|
| 17 |
import ( |
| 18 |
"bytes" |
| 19 |
"context" |
| 20 |
"fmt" |
| 21 |
"net/url" |
| 22 |
"os" |
| 23 |
"os/exec" |
| 24 |
"os/user" |
| 25 |
"path/filepath" |
| 26 |
"runtime" |
| 27 |
"testing" |
| 28 |
"time" |
| 29 |
) |
| 30 |
|
| 31 |
func 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 { |
| 89 |
if err := server.Process.Kill(); err != nil { |
| 90 |
t.Error("Stopping server:", err) |
| 91 |
} |
| 92 |
} |
| 93 |
// Wait for server to exit, but ignore the expected failure error code. |
| 94 |
server.Wait() |
| 95 |
if t.Failed() { |
| 96 |
t.Log(serverOutput) |
| 97 |
} |
| 98 |
}() |
| 99 |
|
| 100 |
// Now the actual test: can we connect to the database via URL opener? |
| 101 |
ctx := context.Background() |
| 102 |
dbURL := &url.URL{ |
| 103 |
Scheme: "blablabla", // Intentionally not "postgres" to ensure any scheme works. |
| 104 |
User: url.User(currUser.Username), |
| 105 |
Path: "/postgres", |
| 106 |
// Use the query parameter to avoid https://github.com/lib/pq/issues/796 |
| 107 |
RawQuery: url.Values{"host": {socketDir}}.Encode(), |
| 108 |
} |
| 109 |
t.Logf("PostgreSQL URL: %s", dbURL) |
| 110 |
db, err := new(URLOpener).OpenPostgresURL(ctx, dbURL) |
| 111 |
if err != nil { |
| 112 |
t.Fatal(err) |
| 113 |
} |
| 114 |
// Developing a realistic query would be hard, so instead we trust that the |
| 115 |
// PostgreSQL library reports healthy correctly. Since there's no way to |
| 116 |
// synchronize the server start and the ping, we might have to ping a few |
| 117 |
// times before it is healthy. (The overall test runner timeout will interrupt |
| 118 |
// if this takes too long.) |
| 119 |
for { |
| 120 |
err := db.Ping() |
| 121 |
if err == nil { |
| 122 |
break |
| 123 |
} |
| 124 |
t.Log("Ping:", err) |
| 125 |
time.Sleep(100 * time.Millisecond) |
| 126 |
} |
| 127 |
if err := db.Close(); err != nil { |
| 128 |
t.Error("Close:", err) |
| 129 |
} |
| 130 |
server.Process.Signal(os.Interrupt) |
| 131 |
serverSignaled = true |
| 132 |
} |