Add optional password-ability to tests

This commit is contained in:
Aaron L 2016-07-31 21:24:33 -07:00
parent fd7cb01b89
commit 735012c4e9
2 changed files with 14 additions and 8 deletions

View file

@ -214,11 +214,13 @@ var defaultTestMainImports = map[string]imports{
`"io/ioutil"`, `"io/ioutil"`,
`"bytes"`, `"bytes"`,
`"database/sql"`, `"database/sql"`,
`"path/filepath"`,
`"time"`, `"time"`,
`"math/rand"`, `"math/rand"`,
}, },
thirdParty: importList{ thirdParty: importList{
`"github.com/nullbio/sqlboiler/boil"`, `"github.com/nullbio/sqlboiler/boil"`,
`"github.com/nullbio/sqlboiler/bdb/drivers"`,
`_ "github.com/lib/pq"`, `_ "github.com/lib/pq"`,
`"github.com/spf13/viper"`, `"github.com/spf13/viper"`,
`"github.com/kat-co/vala"`, `"github.com/kat-co/vala"`,

View file

@ -97,8 +97,7 @@ func dropTestDB() error {
// DBConnect connects to a database and returns the handle. // DBConnect connects to a database and returns the handle.
func DBConnect(user, pass, dbname, host string, port int, sslmode string) (*sql.DB, error) { func DBConnect(user, pass, dbname, host string, port int, sslmode string) (*sql.DB, error) {
connStr := fmt.Sprintf("user=%s password=%s dbname=%s host=%s port=%d sslmode=%s", connStr := drivers.BuildQueryString(user, pass, dbname, host, port, sslmode)
user, pass, dbname, host, port, sslmode)
return sql.Open("postgres", connStr) return sql.Open("postgres", connStr)
} }
@ -133,7 +132,6 @@ func setup() error {
err = vala.BeginValidation().Validate( err = vala.BeginValidation().Validate(
vala.StringNotEmpty(testCfg.Postgres.User, "postgres.user"), vala.StringNotEmpty(testCfg.Postgres.User, "postgres.user"),
vala.StringNotEmpty(testCfg.Postgres.Pass, "postgres.pass"),
vala.StringNotEmpty(testCfg.Postgres.Host, "postgres.host"), vala.StringNotEmpty(testCfg.Postgres.Host, "postgres.host"),
vala.Not(vala.Equals(testCfg.Postgres.Port, 0, "postgres.port")), vala.Not(vala.Equals(testCfg.Postgres.Port, 0, "postgres.port")),
vala.StringNotEmpty(testCfg.Postgres.DBName, "postgres.dbname"), vala.StringNotEmpty(testCfg.Postgres.DBName, "postgres.dbname"),
@ -163,15 +161,18 @@ func setup() error {
defer os.RemoveAll(passDir) defer os.RemoveAll(passDir)
// Write the postgres user password to a tmp file for pg_dump // Write the postgres user password to a tmp file for pg_dump
pwBytes := []byte(fmt.Sprintf("%s:%d:%s:%s:%s", pwBytes := []byte(fmt.Sprintf("%s:%d:%s:%s",
viper.GetString("postgres.host"), viper.GetString("postgres.host"),
viper.GetInt("postgres.port"), viper.GetInt("postgres.port"),
viper.GetString("postgres.dbname"), viper.GetString("postgres.dbname"),
viper.GetString("postgres.user"), viper.GetString("postgres.user"),
viper.GetString("postgres.pass"),
)) ))
passFilePath := passDir + "/pwfile" if pw := viper.GetString("postgres.pass"); len(pw) > 0 {
pwBytes = []byte(fmt.Sprintf("%s:%s", pwBytes, pw))
}
passFilePath := filepath.Join(passDir, "pwfile")
err = ioutil.WriteFile(passFilePath, pwBytes, 0600) err = ioutil.WriteFile(passFilePath, pwBytes, 0600)
if err != nil { if err != nil {
@ -229,14 +230,17 @@ func setup() error {
} }
// Write the test config credentials to a tmp file for pg_dump // Write the test config credentials to a tmp file for pg_dump
testPwBytes := []byte(fmt.Sprintf("%s:%d:%s:%s:%s", testPwBytes := []byte(fmt.Sprintf("%s:%d:%s:%s",
testCfg.Postgres.Host, testCfg.Postgres.Host,
testCfg.Postgres.Port, testCfg.Postgres.Port,
testCfg.Postgres.DBName, testCfg.Postgres.DBName,
testCfg.Postgres.User, testCfg.Postgres.User,
testCfg.Postgres.Pass,
)) ))
if len(testCfg.Postgres.Pass) > 0 {
testPwBytes = []byte(fmt.Sprintf("%s:%s", testPwBytes, testCfg.Postgres.Pass))
}
testPassFilePath := passDir + "/testpwfile" testPassFilePath := passDir + "/testpwfile"
err = ioutil.WriteFile(testPassFilePath, testPwBytes, 0600) err = ioutil.WriteFile(testPassFilePath, testPwBytes, 0600)