This repository has been archived by the owner on Jul 7, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 59
/
example-runner.go
310 lines (249 loc) · 8.16 KB
/
example-runner.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
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
// Copyright 2018 the Service Broker Project Authors.
//
// 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
//
// http://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 client
import (
"encoding/json"
"errors"
"fmt"
"log"
"math/rand"
"time"
"github.com/GoogleCloudPlatform/gcp-service-broker/pkg/broker"
"github.com/pivotal-cf/brokerapi"
)
// RunExamplesForService runs all the exmaples for a given service name against
// the service broker pointed to by client. All examples in the registry get run
// if serviceName is blank.
func RunExamplesForService(registry broker.BrokerRegistry, client *Client, serviceName string) error {
rand.Seed(time.Now().UTC().UnixNano())
services := registry.GetAllServices()
for _, service := range services {
if serviceName != "" && serviceName != service.Name {
continue
}
for _, example := range service.Examples {
err := RunExample(client, example, service)
if err != nil {
return err
}
}
}
return nil
}
// RunExample runs a single example against the given service on the broker
// pointed to by client.
func RunExample(client *Client, example broker.ServiceExample, service *broker.ServiceDefinition) error {
executor, err := newExampleExecutor(client, example, service)
if err != nil {
return err
}
executor.LogTestInfo()
// Cleanup the test if it fails partway through
defer func() {
log.Println("Cleaning up the environment")
executor.Unbind()
executor.Deprovision()
}()
if err := executor.Provision(); err != nil {
return err
}
bindResponse, err := executor.Bind()
if err != nil {
return err
}
if err := executor.Unbind(); err != nil {
return err
}
if err := executor.Deprovision(); err != nil {
return err
}
// Check that the binding response has the same fields as expected
var binding brokerapi.Binding
err = json.Unmarshal(bindResponse, &binding)
if err != nil {
return err
}
credentialsEntry := binding.Credentials.(map[string]interface{})
if err := broker.ValidateVariables(credentialsEntry, service.BindOutputVariables); err != nil {
log.Printf("Error: results don't match JSON Schema: %v", err)
return err
}
return nil
}
func retry(timeout, period time.Duration, function func() (tryAgain bool, err error)) error {
to := time.After(timeout)
tick := time.Tick(period)
if tryAgain, err := function(); !tryAgain {
return err
}
// Keep trying until we're timed out or got a result or got an error
for {
select {
case <-to:
return errors.New("Timeout while waiting for result")
case <-tick:
tryAgain, err := function()
if !tryAgain {
return err
}
}
}
}
func pollUntilFinished(client *Client, instanceId string) error {
return retry(15*time.Minute, 15*time.Second, func() (bool, error) {
log.Println("Polling for async job")
resp := client.LastOperation(instanceId)
if resp.InError() {
return false, resp.Error
}
if resp.StatusCode != 200 {
log.Printf("Bad status code %d, needed 200", resp.StatusCode)
return true, nil
}
var responseBody map[string]string
err := json.Unmarshal(resp.ResponseBody, &responseBody)
if err != nil {
return false, err
}
state := responseBody["state"]
eq := state == string(brokerapi.Succeeded)
log.Printf("Last operation for %q was %q\n", instanceId, state)
return !eq, nil
})
}
func newExampleExecutor(client *Client, example broker.ServiceExample, service *broker.ServiceDefinition) (*exampleExecutor, error) {
provisionParams, err := json.Marshal(example.ProvisionParams)
if err != nil {
return nil, err
}
bindParams, err := json.Marshal(example.BindParams)
if err != nil {
return nil, err
}
catalog, err := service.CatalogEntry()
if err != nil {
return nil, err
}
testid := rand.Uint32()
return &exampleExecutor{
Name: fmt.Sprintf("%s/%s", service.Name, example.Name),
ServiceId: catalog.ID,
PlanId: example.PlanId,
InstanceId: fmt.Sprintf("ex%d", testid),
BindingId: fmt.Sprintf("ex%d", testid),
ProvisionParams: provisionParams,
BindParams: bindParams,
client: client,
}, nil
}
type exampleExecutor struct {
Name string
ServiceId string
PlanId string
InstanceId string
BindingId string
ProvisionParams json.RawMessage
BindParams json.RawMessage
client *Client
}
// Provision attempts to create a service instance from the example.
// Multiple calls to provision will attempt to create a resource with the same
// ServiceId and details.
// If the response is an async result, Provision will attempt to wait until
// the Provision is complete.
func (ee *exampleExecutor) Provision() error {
log.Printf("Provisioning %s\n", ee.Name)
resp := ee.client.Provision(ee.InstanceId, ee.ServiceId, ee.PlanId, ee.ProvisionParams)
log.Println(resp.String())
if resp.InError() {
return resp.Error
}
switch resp.StatusCode {
case 201:
return nil
case 202:
return ee.pollUntilFinished()
default:
return fmt.Errorf("Unexpected response code %d", resp.StatusCode)
}
}
func (ee *exampleExecutor) pollUntilFinished() error {
return pollUntilFinished(ee.client, ee.InstanceId)
}
// Deprovision destroys the instance created by a call to Provision.
func (ee *exampleExecutor) Deprovision() error {
log.Printf("Deprovisioning %s\n", ee.Name)
resp := ee.client.Deprovision(ee.InstanceId, ee.ServiceId, ee.PlanId)
log.Println(resp.String())
if resp.InError() {
return resp.Error
}
switch resp.StatusCode {
case 200:
return nil
case 202:
return ee.pollUntilFinished()
default:
return fmt.Errorf("Unexpected response code %d", resp.StatusCode)
}
}
// Unbind unbinds the exact binding created by a call to Bind.
func (ee *exampleExecutor) Unbind() error {
// XXX(josephlewis42) Due to some unknown reason, binding Postgres and MySQL
// don't wait for all operations to finish before returning even though it
// looks like they do so we can get 500 errors back the first few times we try
// to unbind. Issue #222 was opened to address this. In the meantime this
// is a hack to get around it that will still fail if the 500 errors truly
// occur because of a real, unrecoverable, server error.
return retry(5*time.Minute, 15*time.Second, func() (bool, error) {
log.Printf("Unbinding %s\n", ee.Name)
resp := ee.client.Unbind(ee.InstanceId, ee.BindingId, ee.ServiceId, ee.PlanId)
log.Println(resp.String())
if resp.InError() {
return false, resp.Error
}
if resp.StatusCode == 200 {
return false, nil
}
if resp.StatusCode == 500 {
return true, nil
}
return false, fmt.Errorf("Unexpected response code %d", resp.StatusCode)
})
}
// Bind executes the bind portion of the create, this can only be called
// once successfully as subsequent binds will attempt to create bindings with
// the same ID.
func (ee *exampleExecutor) Bind() (json.RawMessage, error) {
log.Printf("Binding %s\n", ee.Name)
resp := ee.client.Bind(ee.InstanceId, ee.BindingId, ee.ServiceId, ee.PlanId, ee.BindParams)
log.Println(resp.String())
if resp.InError() {
return nil, resp.Error
}
if resp.StatusCode == 201 {
return resp.ResponseBody, nil
}
return nil, fmt.Errorf("Unexpected response code %d", resp.StatusCode)
}
// LogTestInfo writes information about the running example and a manual backout
// strategy if the test dies part of the way through.
func (ee *exampleExecutor) LogTestInfo() {
log.Printf("Running Example: %s\n", ee.Name)
ips := fmt.Sprintf("--instanceid %q --planid %q --serviceid %q", ee.InstanceId, ee.PlanId, ee.ServiceId)
log.Printf("gcp-service-broker client provision %s --params %q\n", ips, ee.ProvisionParams)
log.Printf("gcp-service-broker client bind %s --bindingid %q --params %q\n", ips, ee.BindingId, ee.BindParams)
log.Printf("gcp-service-broker client unbind %s --bindingid %q\n", ips, ee.BindingId)
log.Printf("gcp-service-broker client deprovision %s\n", ips)
}