-
Notifications
You must be signed in to change notification settings - Fork 2
/
storage_cleaner.go
94 lines (80 loc) · 2.63 KB
/
storage_cleaner.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
// Copyright 2019 Google LLC
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// https://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package testutil
import (
"context"
"testing"
"time"
"cloud.google.com/go/storage"
"google.golang.org/api/iterator"
"google.golang.org/api/option"
)
// CleanBucket creates a new bucket. If the bucket already exists, it will be
// deleted and recreated.
func CleanBucket(ctx context.Context, t *testing.T, projectID, bucket string) error {
t.Helper()
client, err := storage.NewClient(ctx, option.WithEndpoint("https://storage.mtls.googleapis.com/storage/v1/"))
if err != nil {
t.Fatalf("storage.NewClient: %v", err)
}
// Delete the bucket if it exists.
deleteBucketIfExists(ctx, t, client, bucket)
b := client.Bucket(bucket)
// Now create the bucket.
// Retry because the bucket can take time to fully delete.
Retry(t, 2, 10*time.Second, func(r *R) {
if err := b.Create(ctx, projectID, nil); err != nil {
r.Errorf("Bucket.Create(%q): %v", bucket, err)
}
})
return nil
}
func deleteBucketIfExists(ctx context.Context, t *testing.T, client *storage.Client, bucket string) {
t.Helper()
b := client.Bucket(bucket)
// Check if the bucket does not exist, return nil.
if _, err := b.Attrs(ctx); err != nil {
return
}
// Delete all of the elements in the already existent bucket, including noncurrent objects.
it := b.Objects(ctx, &storage.Query{
// Versions true to output all generations of objects.
Versions: true,
})
for {
attrs, err := it.Next()
if err == iterator.Done {
break
}
if err != nil {
t.Errorf("Bucket.Objects(%q): %v", bucket, err)
}
if attrs.EventBasedHold || attrs.TemporaryHold {
if _, err := b.Object(attrs.Name).Update(ctx, storage.ObjectAttrsToUpdate{
TemporaryHold: false,
EventBasedHold: false,
}); err != nil {
t.Errorf("Bucket(%q).Object(%q).Update: %v", bucket, attrs.Name, err)
}
}
obj := b.Object(attrs.Name).Generation(attrs.Generation)
if err := obj.Delete(ctx); err != nil {
t.Errorf("Bucket(%q).Object(%q).Delete: %v", bucket, attrs.Name, err)
}
}
// Then delete the bucket itself.
if err := b.Delete(ctx); err != nil {
t.Errorf("Bucket.Delete(%q): %v", bucket, err)
}
}