test(storage): unflake TestGetKey
@@ -44,7 +44,7 @@ func TestMain(m *testing.M) {
func TestListKeys(t *testing.T) {
tc := testutil.SystemTest(t)
- key, err := createTestKey(tc.ProjectID)
+ key, err := createTestKey(tc.ProjectID, t)
defer deleteTestKey(key)
if err != nil {
t.Fatalf("Error in key creation: %s", err)
@@ -79,7 +79,7 @@ func TestCreateKey(t *testing.T) {
func TestActivateKey(t *testing.T) {
tc := testutil.SystemTest(t)
- key, err := createTestKey(tc.ProjectID)
+ key, err := createTestKey(tc.ProjectID, t)
defer deleteTestKey(key)
if err != nil {
t.Errorf("Error in key creation: %s", err)
@@ -102,7 +102,7 @@ func TestActivateKey(t *testing.T) {
func TestDeactivateKey(t *testing.T) {
tc := testutil.SystemTest(t)
- key, err := createTestKey(tc.ProjectID)
+ key, err := createTestKey(tc.ProjectID, t)
defer deleteTestKey(key)
if err != nil {
t.Errorf("Error in key creation: %s", err)
@@ -119,7 +119,7 @@ func TestDeactivateKey(t *testing.T) {
func TestGetKey(t *testing.T) {
tc := testutil.SystemTest(t)
- key, err := createTestKey(tc.ProjectID)
+ key, err := createTestKey(tc.ProjectID, t)
defer deleteTestKey(key)
if err != nil {
t.Errorf("Error in key creation: %s", err)
@@ -139,7 +139,7 @@ func TestGetKey(t *testing.T) {
func TestDeleteKey(t *testing.T) {
tc := testutil.SystemTest(t)
- key, err := createTestKey(tc.ProjectID)
+ key, err := createTestKey(tc.ProjectID, t)
defer deleteTestKey(key)
if err != nil {
t.Errorf("Error in key creation: %s", err)
@@ -162,13 +162,27 @@ func TestDeleteKey(t *testing.T) {
}
// Create a key for testing purposes.
-func createTestKey(projectID string) (*storage.HMACKey, error) {
+func createTestKey(projectID string, t *testing.T) (*storage.HMACKey, error) {
ctx := context.Background()
- key, err := storageClient.CreateHMACKey(ctx, projectID, serviceAccountEmail)
+ var key *storage.HMACKey
+ var err error
+
+ testutil.Retry(t, 10, 10*time.Second, func(r *testutil.R) {
+ key, err = storageClient.CreateHMACKey(ctx, projectID, serviceAccountEmail)
if err != nil {
- return nil, fmt.Errorf("storage.NewClient: %v", err)
+ r.Errorf("Error in CreateHMACKey: %v", err)
+ return
+ }
+
+ // Nil key check should not happen but is added to handle flaky
+ // "nil pointer dereference" error.
+ if key == nil {
+ r.Errorf("Returned nil key.")
+ return
}
- return key, nil
+ })
+
+ return key, err
}
// Deactivate and delete the given key. Should operate as a teardown method.
