github.com/hashicorp/vault/sdk@v0.13.0/database/dbplugin/v5/testing/test_helpers.go (about) 1 // Copyright (c) HashiCorp, Inc. 2 // SPDX-License-Identifier: MPL-2.0 3 4 package dbtesting 5 6 import ( 7 "context" 8 "io/ioutil" 9 "os" 10 "testing" 11 "time" 12 13 "github.com/hashicorp/go-secure-stdlib/parseutil" 14 "github.com/hashicorp/vault/sdk/database/dbplugin/v5" 15 ) 16 17 func getRequestTimeout(t *testing.T) time.Duration { 18 rawDur := os.Getenv("VAULT_TEST_DATABASE_REQUEST_TIMEOUT") 19 if rawDur == "" { 20 // Note: we incremented the default timeout from 5 to 10 seconds in a bid 21 // to fix sporadic failures of mssql_test.go tests TestInitialize() and 22 // TestUpdateUser_password(). 23 24 return 10 * time.Second 25 } 26 27 dur, err := parseutil.ParseDurationSecond(rawDur) 28 if err != nil { 29 t.Fatalf("Failed to parse custom request timeout %q: %s", rawDur, err) 30 } 31 return dur 32 } 33 34 // AssertInitializeCircleCiTest help to diagnose CircleCI failures within AssertInitialize for mssql tests failing 35 // with "Failed to initialize: error verifying connection ...". This will now mark a test as failed instead of being fatal 36 func AssertInitializeCircleCiTest(t *testing.T, db dbplugin.Database, req dbplugin.InitializeRequest) dbplugin.InitializeResponse { 37 t.Helper() 38 maxAttempts := 5 39 var resp dbplugin.InitializeResponse 40 var err error 41 42 for i := 1; i <= maxAttempts; i++ { 43 resp, err = VerifyInitialize(t, db, req) 44 if err != nil { 45 t.Errorf("Failed AssertInitialize attempt: %d with error:\n%+v\n", i, err) 46 time.Sleep(1 * time.Second) 47 continue 48 } 49 50 if i > 1 { 51 t.Logf("AssertInitialize worked the %d time around with a 1 second sleep", i) 52 } 53 break 54 } 55 56 return resp 57 } 58 59 func AssertInitialize(t *testing.T, db dbplugin.Database, req dbplugin.InitializeRequest) dbplugin.InitializeResponse { 60 t.Helper() 61 resp, err := VerifyInitialize(t, db, req) 62 if err != nil { 63 t.Fatalf("Failed to initialize: %s", err) 64 } 65 return resp 66 } 67 68 func VerifyInitialize(t *testing.T, db dbplugin.Database, req dbplugin.InitializeRequest) (dbplugin.InitializeResponse, error) { 69 ctx, cancel := context.WithTimeout(context.Background(), getRequestTimeout(t)) 70 defer cancel() 71 72 return db.Initialize(ctx, req) 73 } 74 75 func AssertNewUser(t *testing.T, db dbplugin.Database, req dbplugin.NewUserRequest) dbplugin.NewUserResponse { 76 t.Helper() 77 78 ctx, cancel := context.WithTimeout(context.Background(), getRequestTimeout(t)) 79 defer cancel() 80 81 resp, err := db.NewUser(ctx, req) 82 if err != nil { 83 t.Fatalf("Failed to create new user: %s", err) 84 } 85 86 if resp.Username == "" { 87 t.Fatalf("Missing username from NewUser response") 88 } 89 return resp 90 } 91 92 func AssertUpdateUser(t *testing.T, db dbplugin.Database, req dbplugin.UpdateUserRequest) { 93 t.Helper() 94 95 ctx, cancel := context.WithTimeout(context.Background(), getRequestTimeout(t)) 96 defer cancel() 97 98 _, err := db.UpdateUser(ctx, req) 99 if err != nil { 100 t.Fatalf("Failed to update user: %s", err) 101 } 102 } 103 104 func AssertDeleteUser(t *testing.T, db dbplugin.Database, req dbplugin.DeleteUserRequest) { 105 t.Helper() 106 107 ctx, cancel := context.WithTimeout(context.Background(), getRequestTimeout(t)) 108 defer cancel() 109 110 _, err := db.DeleteUser(ctx, req) 111 if err != nil { 112 t.Fatalf("Failed to delete user %q: %s", req.Username, err) 113 } 114 } 115 116 func AssertClose(t *testing.T, db dbplugin.Database) { 117 t.Helper() 118 err := db.Close() 119 if err != nil { 120 t.Fatalf("Failed to close database: %s", err) 121 } 122 } 123 124 // GetGCPTestCredentials reads the credentials from the 125 // GOOGLE_APPLICATIONS_CREDENTIALS environment variable 126 // The credentials are read from a file if a file exists 127 // otherwise they are returned as JSON 128 func GetGCPTestCredentials(t *testing.T) string { 129 t.Helper() 130 envCredentials := "GOOGLE_APPLICATIONS_CREDENTIALS" 131 132 var credsStr string 133 credsEnv := os.Getenv(envCredentials) 134 if credsEnv == "" { 135 t.Skipf("env var %s not set, skipping test", envCredentials) 136 } 137 138 // Attempt to read as file path; if invalid, assume given JSON value directly 139 if _, err := os.Stat(credsEnv); err == nil { 140 credsBytes, err := ioutil.ReadFile(credsEnv) 141 if err != nil { 142 t.Fatalf("unable to read credentials file %s: %v", credsStr, err) 143 } 144 credsStr = string(credsBytes) 145 } else { 146 credsStr = credsEnv 147 } 148 149 return credsStr 150 }