forked from GoogleCloudPlatform/golang-samples
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main_test.go
218 lines (191 loc) · 6.2 KB
/
main_test.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
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
// Copyright 2016 Google Inc. All rights reserved.
// Use of this source code is governed by the Apache 2.0
// license that can be found in the LICENSE file.
package main
import (
"context"
"fmt"
"io/ioutil"
"log"
"os"
"testing"
"time"
"github.com/GoogleCloudPlatform/golang-samples/internal/testutil"
"cloud.google.com/go/storage"
)
var (
storageClient *storage.Client
bucketName string
)
func TestMain(m *testing.M) {
// These functions are noisy.
log.SetOutput(ioutil.Discard)
s := m.Run()
log.SetOutput(os.Stderr)
os.Exit(s)
}
func setup(t *testing.T) {
tc := testutil.SystemTest(t)
ctx := context.Background()
var err error
storageClient, err = storage.NewClient(ctx)
if err != nil {
t.Fatalf("failed to create client: %v", err)
}
bucketName = tc.ProjectID + "-storage-buckets-tests"
}
func TestCreate(t *testing.T) {
tc := testutil.SystemTest(t)
setup(t)
// Clean up bucket before running tests.
deleteBucket(storageClient, bucketName)
if err := create(storageClient, tc.ProjectID, bucketName); err != nil {
t.Fatalf("failed to create bucket (%q): %v", bucketName, err)
}
}
func TestCreateWithAttrs(t *testing.T) {
tc := testutil.SystemTest(t)
name := bucketName + "-attrs"
// Clean up bucket before running test.
deleteBucket(storageClient, name)
if err := createWithAttrs(storageClient, tc.ProjectID, name); err != nil {
t.Fatalf("failed to create bucket (%q): %v", name, err)
}
if err := deleteBucket(storageClient, name); err != nil {
t.Fatalf("failed to delete bucket (%q): %v", name, err)
}
}
func TestList(t *testing.T) {
tc := testutil.SystemTest(t)
setup(t)
buckets, err := list(storageClient, tc.ProjectID)
if err != nil {
t.Fatal(err)
}
var ok bool
outer:
for attempt := 0; attempt < 5; attempt++ { // for eventual consistency
for _, b := range buckets {
if b == bucketName {
ok = true
break outer
}
}
time.Sleep(2 * time.Second)
}
if !ok {
t.Errorf("got bucket list: %v; want %q in the list", buckets, bucketName)
}
}
func TestIAM(t *testing.T) {
testutil.SystemTest(t)
setup(t)
if _, err := getPolicy(storageClient, bucketName); err != nil {
t.Errorf("getPolicy: %#v", err)
}
if err := addUser(storageClient, bucketName); err != nil {
t.Errorf("addUser: %v", err)
}
if err := removeUser(storageClient, bucketName); err != nil {
t.Errorf("removeUser: %v", err)
}
}
func TestRequesterPays(t *testing.T) {
testutil.SystemTest(t)
setup(t)
if err := enableRequesterPays(storageClient, bucketName); err != nil {
t.Errorf("enableRequesterPays: %#v", err)
}
if err := disableRequesterPays(storageClient, bucketName); err != nil {
t.Errorf("disableRequesterPays: %#v", err)
}
if err := checkRequesterPays(storageClient, bucketName); err != nil {
t.Errorf("checkRequesterPays: %#v", err)
}
}
func TestKMS(t *testing.T) {
tc := testutil.SystemTest(t)
setup(t)
keyRingID := os.Getenv("GOLANG_SAMPLES_KMS_KEYRING")
cryptoKeyID := os.Getenv("GOLANG_SAMPLES_KMS_CRYPTOKEY")
if keyRingID == "" || cryptoKeyID == "" {
t.Skip("GOLANG_SAMPLES_KMS_KEYRING and GOLANG_SAMPLES_KMS_CRYPTOKEY must be set")
}
kmsKeyName := fmt.Sprintf("projects/%s/locations/%s/keyRings/%s/cryptoKeys/%s", tc.ProjectID, "global", keyRingID, cryptoKeyID)
if err := setDefaultKMSkey(storageClient, bucketName, kmsKeyName); err != nil {
t.Fatalf("failed to enable default kms key (%q): %v", bucketName, err)
}
}
func TestBucketLock(t *testing.T) {
tc := testutil.SystemTest(t)
setup(t)
retentionPeriod := 5 * time.Second
if err := setRetentionPolicy(storageClient, bucketName, retentionPeriod); err != nil {
t.Fatalf("failed to set retention policy (%q): %v", bucketName, err)
}
attrs, err := getRetentionPolicy(storageClient, bucketName)
if err != nil {
t.Fatalf("failed to get retention policy (%q): %v", bucketName, err)
}
if attrs.RetentionPolicy.RetentionPeriod != retentionPeriod {
t.Fatalf("retention period is not the expected value (%q): %v", retentionPeriod, attrs.RetentionPolicy.RetentionPeriod)
}
if err := enableDefaultEventBasedHold(storageClient, bucketName); err != nil {
t.Fatalf("failed to enable default event-based hold (%q): %v", bucketName, err)
}
attrs, err = getDefaultEventBasedHold(storageClient, bucketName)
if err != nil {
t.Fatalf("failed to get default event-based hold (%q): %v", bucketName, err)
}
if !attrs.DefaultEventBasedHold {
t.Fatalf("default event-based hold was not enabled")
}
if err := disableDefaultEventBasedHold(storageClient, bucketName); err != nil {
t.Fatalf("failed to disable event-based hold (%q): %v", bucketName, err)
}
attrs, err = getDefaultEventBasedHold(storageClient, bucketName)
if err != nil {
t.Fatalf("failed to get default event-based hold (%q): %v", bucketName, err)
}
if attrs.DefaultEventBasedHold {
t.Fatalf("default event-based hold was not disabled")
}
if err := removeRetentionPolicy(storageClient, bucketName); err != nil {
t.Fatalf("failed to remove retention policy (%q): %v", bucketName, err)
}
attrs, err = getRetentionPolicy(storageClient, bucketName)
if err != nil {
t.Fatalf("failed to get retention policy (%q): %v", bucketName, err)
}
if attrs.RetentionPolicy != nil {
t.Fatalf("retention period to not be set")
}
if err := setRetentionPolicy(storageClient, bucketName, retentionPeriod); err != nil {
t.Fatalf("failed to set retention policy (%q): %v", bucketName, err)
}
testutil.Retry(t, 10, time.Second, func(r *testutil.R) {
if err := lockRetentionPolicy(storageClient, bucketName); err != nil {
r.Errorf("failed to lock retention policy (%q): %v", bucketName, err)
}
attrs, err := getRetentionPolicy(storageClient, bucketName)
if err != nil {
r.Errorf("failed to check if retention policy is locked (%q): %v", bucketName, err)
}
if !attrs.RetentionPolicy.IsLocked {
r.Errorf("retention policy is not locked")
}
})
time.Sleep(5 * time.Second)
deleteBucket(storageClient, bucketName)
time.Sleep(5 * time.Second)
if err := create(storageClient, tc.ProjectID, bucketName); err != nil {
t.Fatalf("failed to create bucket (%q): %v", bucketName, err)
}
}
func TestDelete(t *testing.T) {
testutil.SystemTest(t)
setup(t)
if err := deleteBucket(storageClient, bucketName); err != nil {
t.Fatalf("failed to delete bucket (%q): %v", bucketName, err)
}
}