2024-04-07 18:55:50 +00:00
|
|
|
package ddl
|
|
|
|
|
|
|
|
import (
|
|
|
|
"crypto/rand"
|
|
|
|
"math/big"
|
|
|
|
"os"
|
2024-04-12 10:00:55 +00:00
|
|
|
"strconv"
|
2024-04-07 18:55:50 +00:00
|
|
|
"strings"
|
|
|
|
"testing"
|
|
|
|
|
|
|
|
"github.com/davecgh/go-spew/spew"
|
|
|
|
)
|
|
|
|
|
|
|
|
func RequireEnvString(name string, t *testing.T) string {
|
|
|
|
if strVal, isSet := os.LookupEnv(name); isSet {
|
|
|
|
return strVal
|
|
|
|
} else {
|
2024-04-12 10:00:55 +00:00
|
|
|
t.Fatalf("Required environment variable %q not set", name)
|
2024-04-07 18:55:50 +00:00
|
|
|
return ""
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-04-12 10:00:55 +00:00
|
|
|
func RequireEnvInt(name string, t *testing.T) int {
|
|
|
|
if strVal, isSet := os.LookupEnv(name); isSet {
|
|
|
|
if intVal, err := strconv.Atoi(strVal); err != nil {
|
|
|
|
t.Fatalf("Invalid number value given for the environment variable %q: %s", name, strVal)
|
|
|
|
} else if intVal < 1 {
|
|
|
|
t.Fatalf("Environment variable %q has to be greater than 0", name)
|
|
|
|
} else {
|
|
|
|
return intVal
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
t.Fatalf("Required environment variable %q not set", name)
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0
|
|
|
|
}
|
|
|
|
|
2024-04-07 18:55:50 +00:00
|
|
|
func DumpStruct(a ...interface{}) string {
|
|
|
|
dump := spew.Sdump(a...)
|
|
|
|
|
|
|
|
if strings.HasSuffix(dump, "\n") && len(dump) > 2 {
|
|
|
|
dump = dump[0 : len(dump)-len("\n")]
|
|
|
|
}
|
|
|
|
|
|
|
|
return dump
|
|
|
|
}
|
|
|
|
|
|
|
|
// GenerateRandomString returns a securely generated random string.
|
|
|
|
// It will return an error if the system's secure random
|
|
|
|
// number generator fails to function correctly, in which
|
|
|
|
// case the caller should not continue.
|
|
|
|
func GenerateRandomString(n int) (string, error) {
|
|
|
|
const letters = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz"
|
|
|
|
ret := make([]byte, n)
|
|
|
|
for i := 0; i < n; i++ {
|
|
|
|
num, err := rand.Int(rand.Reader, big.NewInt(int64(len(letters))))
|
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
ret[i] = letters[num.Int64()]
|
|
|
|
}
|
|
|
|
|
|
|
|
return string(ret), nil
|
|
|
|
}
|