forked from GoogleCloudPlatform/golang-samples
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main_test.go
80 lines (68 loc) · 1.79 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
// 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 (
"fmt"
"testing"
"time"
"github.com/GoogleCloudPlatform/golang-samples/internal/testutil"
"cloud.google.com/go/storage"
"golang.org/x/net/context"
)
var bucketName = fmt.Sprintf("golang-example-buckets-%d", time.Now().Unix())
func setup(t *testing.T) *storage.Client {
ctx := context.Background()
client, err := storage.NewClient(ctx)
if err != nil {
t.Fatalf("failed to create client: %v", err)
}
return client
}
func TestCreate(t *testing.T) {
tc := testutil.SystemTest(t)
c := setup(t)
if err := create(c, tc.ProjectID, bucketName); err != nil {
t.Fatalf("failed to create bucket (%q): %v", bucketName, err)
}
}
func TestCreateWithAttrs(t *testing.T) {
tc := testutil.SystemTest(t)
c := setup(t)
name := bucketName + "-attrs"
if err := createWithAttrs(c, tc.ProjectID, name); err != nil {
t.Fatalf("failed to create bucket (%q): %v", bucketName, err)
}
if err := delete(c, name); err != nil {
t.Fatalf("failed to delete bucket (%q): %v", bucketName, err)
}
}
func TestList(t *testing.T) {
tc := testutil.SystemTest(t)
c := setup(t)
buckets, err := list(c, 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 TestDelete(t *testing.T) {
testutil.SystemTest(t)
c := setup(t)
if err := delete(c, bucketName); err != nil {
t.Fatalf("failed to delete bucket (%q): %v", bucketName, err)
}
}