diff --git a/dump_postgres.go b/dump_postgres.go index a1cf431..bb26fd9 100644 --- a/dump_postgres.go +++ b/dump_postgres.go @@ -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) } diff --git a/dump_postgres_test.go b/dump_postgres_test.go new file mode 100644 index 0000000..8e31a7f --- /dev/null +++ b/dump_postgres_test.go @@ -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) + } + }) + } +}