Change crypto/rand to use math/rand in pgutil (#1589)

* Added retry logic and tests for handling if crypto/rand fails.

* Added retry logic and tests for handling if crypto/rand fails.

* Fixing linting error.

* Removing use of `crypto/rand` for use of `math/rand`.

* Changing code comment about why we ignore this error.
This commit is contained in:
Simon Guindon 2019-04-02 12:52:25 -04:00 committed by GitHub
parent 59b3eb190e
commit d3885b7b78
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
7 changed files with 11 additions and 9 deletions

View File

@ -20,7 +20,7 @@ type DB struct {
// Open opens a postgres database with a schema
func Open(connstr string, schemaPrefix string) (*DB, error) {
schemaName := schemaPrefix + "-" + RandomString(8)
schemaName := schemaPrefix + "-" + CreateRandomTestingSchemaName(8)
db, err := sql.Open("postgres", ConnstrWithSchema(connstr, schemaName))
if err != nil {

View File

@ -5,17 +5,19 @@
package pgutil
import (
"crypto/rand"
"database/sql"
"encoding/hex"
"math/rand"
"net/url"
"strconv"
"strings"
)
// RandomString creates a random safe string
func RandomString(n int) string {
// CreateRandomTestingSchemaName creates a random schema name string.
func CreateRandomTestingSchemaName(n int) string {
data := make([]byte, n)
// math/rand.Read() always returns a nil error so there's no need to handle the error.
_, _ = rand.Read(data)
return hex.EncodeToString(data)
}

View File

@ -54,7 +54,7 @@ func TestCreate_Postgres(t *testing.T) {
t.Skipf("postgres flag missing, example:\n-postgres-test-db=%s", defaultPostgresConn)
}
schema := "create-" + pgutil.RandomString(8)
schema := "create-" + pgutil.CreateRandomTestingSchemaName(8)
db, err := sql.Open("postgres", pgutil.ConnstrWithSchema(*testPostgres, schema))
if err != nil {

View File

@ -34,7 +34,7 @@ func TestBasicMigrationPostgres(t *testing.T) {
t.Skipf("postgres flag missing, example:\n-postgres-test-db=%s", defaultPostgresConn)
}
schema := "create-" + pgutil.RandomString(8)
schema := "create-" + pgutil.CreateRandomTestingSchemaName(8)
db, err := sql.Open("postgres", pgutil.ConnstrWithSchema(*testPostgres, schema))
if err != nil {

View File

@ -21,7 +21,7 @@ import (
// Run runs testplanet in multiple configurations.
func Run(t *testing.T, config Config, test func(t *testing.T, ctx *testcontext.Context, planet *Planet)) {
schemaSuffix := pgutil.RandomString(8)
schemaSuffix := pgutil.CreateRandomTestingSchemaName(8)
t.Log("schema-suffix ", schemaSuffix)
for _, satelliteDB := range satellitedbtest.Databases() {

View File

@ -105,7 +105,7 @@ func TestMigratePostgres(t *testing.T) {
t.Run(strconv.Itoa(base.Version), func(t *testing.T) {
log := zaptest.NewLogger(t)
schemaName := "migrate/satellite/" + strconv.Itoa(base.Version) + pgutil.RandomString(8)
schemaName := "migrate/satellite/" + strconv.Itoa(base.Version) + pgutil.CreateRandomTestingSchemaName(8)
connstr := pgutil.ConnstrWithSchema(*satellitedbtest.TestPostgres, schemaName)
// create a new satellitedb connection

View File

@ -49,7 +49,7 @@ func Databases() []Database {
// Run method will iterate over all supported databases. Will establish
// connection and will create tables for each DB.
func Run(t *testing.T, test func(t *testing.T, db satellite.DB)) {
schemaSuffix := pgutil.RandomString(8)
schemaSuffix := pgutil.CreateRandomTestingSchemaName(8)
t.Log("schema-suffix ", schemaSuffix)
for _, dbInfo := range Databases() {