Add tests for building the postgres backup command
This commit is contained in:
parent
1bc3fc85ab
commit
45e7560e0b
|
@ -15,8 +15,7 @@ func NewPostgresDumper(container *types.ContainerJSON) *PostgresDumper {
|
|||
}
|
||||
}
|
||||
|
||||
func (d *PostgresDumper) Dump() error {
|
||||
fmt.Printf("Dumping postgres database from container %s...\n", d.Container.Name)
|
||||
func (d *PostgresDumper) buildConnStr() string {
|
||||
env := parseEnv(d.Container.Config.Env)
|
||||
|
||||
user := "root"
|
||||
|
@ -24,7 +23,7 @@ func (d *PostgresDumper) Dump() error {
|
|||
user = u
|
||||
}
|
||||
|
||||
db := ""
|
||||
db := "postgres"
|
||||
if d, has := env["POSTGRES_DB"]; has {
|
||||
db = d
|
||||
}
|
||||
|
@ -41,9 +40,13 @@ func (d *PostgresDumper) Dump() error {
|
|||
|
||||
host := d.Container.NetworkSettings.DefaultNetworkSettings.IPAddress
|
||||
|
||||
connStr := fmt.Sprintf("postgresql://%s:%s@%s:%s/%s", user, pw, host, port, db)
|
||||
return fmt.Sprintf("postgresql://%s:%s@%s:%s/%s", user, pw, host, port, db)
|
||||
}
|
||||
|
||||
// TODO: Check postgres image version and use the correct pg_dump --> Test!
|
||||
func (d *PostgresDumper) Dump() error {
|
||||
fmt.Printf("Dumping postgres database from container %s...\n", d.Container.Name)
|
||||
|
||||
connStr := d.buildConnStr()
|
||||
|
||||
return runAndSaveCommand(getDumpFilename(d.Container.Name), "pg_dump", "--dbname", connStr)
|
||||
}
|
||||
|
|
|
@ -0,0 +1,117 @@
|
|||
package main
|
||||
|
||||
import (
|
||||
"github.com/docker/docker/api/types"
|
||||
"github.com/docker/docker/api/types/container"
|
||||
"testing"
|
||||
)
|
||||
|
||||
func TestPostgresDumper_buildConnStr(t *testing.T) {
|
||||
nw := &types.NetworkSettings{
|
||||
DefaultNetworkSettings: types.DefaultNetworkSettings{
|
||||
IPAddress: "1.2.3.4",
|
||||
},
|
||||
}
|
||||
tests := []struct {
|
||||
name string
|
||||
container *types.ContainerJSON
|
||||
want string
|
||||
}{
|
||||
{
|
||||
name: "values for everything",
|
||||
container: &types.ContainerJSON{
|
||||
NetworkSettings: nw,
|
||||
Config: &container.Config{
|
||||
Env: []string{
|
||||
"POSTGRES_USER=loremipsum",
|
||||
"POSTGRES_DB=ipsum",
|
||||
"POSTGRES_PASSWORD=notapassword",
|
||||
"POSTGRES_PORT=1234",
|
||||
},
|
||||
},
|
||||
},
|
||||
want: "postgresql://loremipsum:notapassword@1.2.3.4:1234/ipsum",
|
||||
},
|
||||
{
|
||||
name: "no user",
|
||||
container: &types.ContainerJSON{
|
||||
NetworkSettings: nw,
|
||||
Config: &container.Config{
|
||||
Env: []string{
|
||||
"POSTGRES_DB=ipsum",
|
||||
"POSTGRES_PASSWORD=notapassword",
|
||||
"POSTGRES_PORT=1234",
|
||||
},
|
||||
},
|
||||
},
|
||||
want: "postgresql://root:notapassword@1.2.3.4:1234/ipsum",
|
||||
},
|
||||
{
|
||||
name: "no password",
|
||||
container: &types.ContainerJSON{
|
||||
NetworkSettings: nw,
|
||||
Config: &container.Config{
|
||||
Env: []string{
|
||||
"POSTGRES_USER=loremipsum",
|
||||
"POSTGRES_DB=ipsum",
|
||||
"POSTGRES_PORT=1234",
|
||||
},
|
||||
},
|
||||
},
|
||||
want: "postgresql://loremipsum:@1.2.3.4:1234/ipsum",
|
||||
},
|
||||
{
|
||||
name: "no password, but root",
|
||||
container: &types.ContainerJSON{
|
||||
NetworkSettings: nw,
|
||||
Config: &container.Config{
|
||||
Env: []string{
|
||||
"POSTGRES_USER=loremipsum",
|
||||
"POSTGRES_DB=ipsum",
|
||||
"POSTGRES_PORT=1234",
|
||||
"POSTGRES_ROOT_PASSWORD=roooot",
|
||||
},
|
||||
},
|
||||
},
|
||||
want: "postgresql://loremipsum:roooot@1.2.3.4:1234/ipsum",
|
||||
},
|
||||
{
|
||||
name: "no port",
|
||||
container: &types.ContainerJSON{
|
||||
NetworkSettings: nw,
|
||||
Config: &container.Config{
|
||||
Env: []string{
|
||||
"POSTGRES_USER=loremipsum",
|
||||
"POSTGRES_DB=ipsum",
|
||||
"POSTGRES_PASSWORD=notapassword",
|
||||
},
|
||||
},
|
||||
},
|
||||
want: "postgresql://loremipsum:notapassword@1.2.3.4:5432/ipsum",
|
||||
},
|
||||
{
|
||||
name: "no db",
|
||||
container: &types.ContainerJSON{
|
||||
NetworkSettings: nw,
|
||||
Config: &container.Config{
|
||||
Env: []string{
|
||||
"POSTGRES_USER=loremipsum",
|
||||
"POSTGRES_PASSWORD=notapassword",
|
||||
"POSTGRES_PORT=1234",
|
||||
},
|
||||
},
|
||||
},
|
||||
want: "postgresql://loremipsum:notapassword@1.2.3.4:1234/postgres",
|
||||
},
|
||||
}
|
||||
for _, tt := range tests {
|
||||
t.Run(tt.name, func(t *testing.T) {
|
||||
d := &PostgresDumper{
|
||||
Container: tt.container,
|
||||
}
|
||||
if got := d.buildConnStr(); got != tt.want {
|
||||
t.Errorf("buildConnStr() = %v, want %v", got, tt.want)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue