forked from xinyunh0929/golang-samples
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmain_test.go
150 lines (127 loc) · 3.48 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
// 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 (
"sync"
"testing"
"time"
"cloud.google.com/go/iam"
"cloud.google.com/go/pubsub"
"golang.org/x/net/context"
"github.com/GoogleCloudPlatform/golang-samples/internal/testutil"
)
var topic *pubsub.Topic
var subID string
var once sync.Once // guards cleanup related operations in setup.
func setup(t *testing.T) *pubsub.Client {
ctx := context.Background()
tc := testutil.SystemTest(t)
client, err := pubsub.NewClient(ctx, tc.ProjectID)
if err != nil {
t.Fatalf("failed to create client: %v", err)
}
subID = tc.ProjectID + "-test-sub"
topicID := tc.ProjectID + "-test-sub-topic"
// Cleanup resources from the previous failed tests.
once.Do(func() {
// Create a topic.
topic = client.Topic(topicID)
ok, err := topic.Exists(ctx)
if err != nil {
t.Fatalf("failed to check if topic exists: %v", err)
}
if !ok {
if topic, err = client.CreateTopic(ctx, topicID); err != nil {
t.Fatalf("failed to create the topic: %v", err)
}
}
// Delete the sub if already exists.
sub := client.Subscription(subID)
ok, err = sub.Exists(ctx)
if err != nil {
t.Fatalf("failed to check if sub exists: %v", err)
}
if ok {
if err := client.Subscription(subID).Delete(ctx); err != nil {
t.Fatalf("failed to cleanup the topic (%q): %v", subID, err)
}
}
})
return client
}
func TestCreate(t *testing.T) {
c := setup(t)
if err := create(c, subID, topic); err != nil {
t.Fatalf("failed to create a subscription: %v", err)
}
ok, err := c.Subscription(subID).Exists(context.Background())
if err != nil {
t.Fatalf("failed to check if sub exists: %v", err)
}
if !ok {
t.Fatalf("got none; want sub = %q", subID)
}
}
func TestList(t *testing.T) {
c := setup(t)
testutil.Retry(t, 10, time.Second, func(r *testutil.R) {
subs, err := list(c)
if err != nil {
r.Errorf("failed to list subscriptions: %v", err)
return
}
for _, sub := range subs {
if sub.ID() == subID {
return // PASS
}
}
subNames := make([]string, len(subs))
for i, sub := range subs {
subNames[i] = sub.ID()
}
r.Errorf("got %+v; want a list with subscription %q", subNames, subID)
})
}
func TestIAM(t *testing.T) {
c := setup(t)
testutil.Retry(t, 10, time.Second, func(r *testutil.R) {
perms, err := testPermissions(c, subID)
if err != nil {
r.Errorf("testPermissions: %v", err)
}
if len(perms) == 0 {
r.Errorf("want non-zero perms")
}
})
testutil.Retry(t, 10, time.Second, func(r *testutil.R) {
if err := addUsers(c, subID); err != nil {
r.Errorf("addUsers: %v", err)
}
})
testutil.Retry(t, 10, time.Second, func(r *testutil.R) {
policy, err := getPolicy(c, subID)
if err != nil {
r.Errorf("getPolicy: %v", err)
}
if role, member := iam.Editor, "group:cloud-logs@google.com"; !policy.HasRole(member, role) {
r.Errorf("want %q as viewer, policy=%v", member, policy)
}
if role, member := iam.Viewer, iam.AllUsers; !policy.HasRole(member, role) {
r.Errorf("want %q as viewer, policy=%v", member, policy)
}
})
}
func TestDelete(t *testing.T) {
c := setup(t)
if err := delete(c, subID); err != nil {
t.Fatalf("failed to delete subscription (%q): %v", subID, err)
}
ok, err := c.Subscription(subID).Exists(context.Background())
if err != nil {
t.Fatalf("failed to check if sub exists: %v", err)
}
if ok {
t.Fatalf("got sub = %q; want none", subID)
}
}