Skip to content

Commit

Permalink
Update version of database used for tests
Browse files Browse the repository at this point in the history
This patch updates the PostgreSQL database used for tests to version 14.

Signed-off-by: Juan Hernandez <[email protected]>
  • Loading branch information
jhernand committed Apr 28, 2022
1 parent 2af77ce commit 0904bdf
Showing 1 changed file with 6 additions and 48 deletions.
54 changes: 6 additions & 48 deletions testing/database.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,11 +20,8 @@ import (
"bytes"
"database/sql"
"fmt"
"io/ioutil"
"net"
"os"
"os/exec"
"path/filepath"
"strings"

"github.com/google/uuid"
Expand All @@ -38,9 +35,6 @@ import (
// DatabaseServer knows how to start a PostgreSQL database server inside a container, and how to
// create databases to be used for tests.
type DatabaseServer struct {
// Temporary directory for database configuration:
tmp string

// Name of the tool used to create containers (podman or docker):
tool string

Expand Down Expand Up @@ -70,9 +64,6 @@ type Database struct {
name string
user string
password string

// List of database handles created, so we don't forget to close them:
handles []*sql.DB
}

// MakeDatabaseServer creates a new database server.
Expand All @@ -89,35 +80,17 @@ func MakeDatabaseServer() *DatabaseServer {
// Generate a random password for the database admnistrator:
password := uuid.NewString()

// Create a temporary directory for the database configuration file. Note that we need to
// explicitly change the permissions of this directory so that everybody can read and
// execute. That is necessary because the database server needs that permission and it runs
// with user 26 which most probably won't match our user or group.
tmp, err := ioutil.TempDir("", "test-*.d")
Expect(err).ToNot(HaveOccurred())
err = os.Chmod(tmp, 0755) // #nosec G302
Expect(err).ToNot(HaveOccurred())

// Create the database configuration file. Like the directory this needs to be readable for
// the user of the database server.
confFile := filepath.Join(tmp, "db.conf")
confText := `
log_destination = 'stderr'
log_statement = 'all'
logging_collector = off
`
err = ioutil.WriteFile(confFile, []byte(confText), 0644)
Expect(err).ToNot(HaveOccurred())

// Start the database server:
runOut := &bytes.Buffer{}
runCmd := exec.Command(
tool, "run",
"--env", "POSTGRESQL_ADMIN_PASSWORD="+password,
"--volume", tmp+":/opt/app-root/src/postgresql-cfg:Z",
"--env", "POSTGRES_PASSWORD="+password,
"--publish", "5432",
"--detach",
"docker.io/centos/postgresql-12-centos8:latest",
"docker.io/postgres:14",
"-c", "log_destination=stderr",
"-c", "log_statement=all",
"-c", "logging_collector=off",
) // #nosec G204
runCmd.Stdout = runOut
runCmd.Stderr = GinkgoWriter
Expand Down Expand Up @@ -153,7 +126,6 @@ func MakeDatabaseServer() *DatabaseServer {

// Create and populate the object:
return &DatabaseServer{
tmp: tmp,
tool: tool,
container: container,
host: host,
Expand Down Expand Up @@ -184,10 +156,6 @@ func (s *DatabaseServer) Close() {
killCmd.Stderr = GinkgoWriter
err = killCmd.Run()
Expect(err).ToNot(HaveOccurred())

// Delete the temporary directories:
err = os.RemoveAll(s.tmp)
Expect(err).ToNot(HaveOccurred())
}

// MakeDatabase creates a new database.
Expand Down Expand Up @@ -230,32 +198,22 @@ func (s *DatabaseServer) MakeDatabase() *Database {

// MakeHandle creates a new database handle for this database.
func (d *Database) MakeHandle() *sql.DB {
// Create the new handle:
url := fmt.Sprintf(
"postgres://%s:%s@%s:%s/%s?sslmode=disable",
d.user, d.password, d.server.host, d.server.port, d.name,
)
handle, err := sql.Open("pgx", url)
Expect(err).ToNot(HaveOccurred())

// Remember to close it:
d.handles = append(d.handles, handle)

return handle
}

// Close deletes the database.
func (d *Database) Close() {
var err error

// Close all the handles:
for _, handle := range d.handles {
_ = handle.Close() // #nosec G104
}

// Drop the database:
_, err = d.server.handle.Exec(fmt.Sprintf(
`drop database if exists %s`,
`drop database if exists %s with (force)`,
d.name,
))
Expect(err).ToNot(HaveOccurred())
Expand Down

0 comments on commit 0904bdf

Please sign in to comment.