Skip to content

Commit

Permalink
remove db password file once db has been initialized (#20)
Browse files Browse the repository at this point in the history
* remove db password file once db has been initialized

* test: add test to check if password file really got removed
  • Loading branch information
bearsh committed Mar 2, 2021
1 parent b86afce commit b9e72af
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 0 deletions.
5 changes: 5 additions & 0 deletions prepare_database.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
"fmt"
"io"
"io/ioutil"
"os"
"os/exec"
"path/filepath"

Expand Down Expand Up @@ -42,6 +43,10 @@ func defaultInitDatabase(binaryExtractLocation, username, password, locale strin
return fmt.Errorf("unable to init database using: %s", postgresInitDbProcess.String())
}

if err = os.Remove(passwordFile); err != nil {
return fmt.Errorf("unable to remove password file '%v': %v", passwordFile, err)
}

return nil
}

Expand Down
29 changes: 29 additions & 0 deletions prepare_database_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,35 @@ func Test_defaultInitDatabase_ErrorInvalidLocaleSetting(t *testing.T) {
tempDir))
}

func Test_defaultInitDatabase_PwFileRemoved(t *testing.T) {
tempDir, err := ioutil.TempDir("", "prepare_database_test")
if err != nil {
panic(err)
}

defer func() {
if err := os.RemoveAll(tempDir); err != nil {
panic(err)
}
}()

database := NewDatabase(DefaultConfig().RuntimePath(tempDir))
if err := database.Start(); err != nil {
t.Fatal(err)
}

defer func() {
if err := database.Stop(); err != nil {
t.Fatal(err)
}
}()

pwFile := filepath.Join(tempDir, "pwfile")
_, err = os.Stat(pwFile)

assert.True(t, os.IsNotExist(err), "pwfile (%v) still exists after starting the db", pwFile)
}

func Test_defaultCreateDatabase_ErrorWhenSQLOpenError(t *testing.T) {
err := defaultCreateDatabase(1234, "user client_encoding=lol", "password", "database")

Expand Down

0 comments on commit b9e72af

Please sign in to comment.