/
apps.go
221 lines (190 loc) · 6.26 KB
/
apps.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
// Copyright (C) 2016-Present Pivotal Software, Inc. All rights reserved.
// This program and the accompanying materials are made available under the terms of the 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 cf_helpers
import (
"bytes"
"encoding/json"
"fmt"
"io"
"io/ioutil"
"net/http"
"os"
"path/filepath"
"strings"
"time"
"github.com/craigfurman/herottp"
"github.com/onsi/ginkgo/v2"
. "github.com/onsi/gomega"
"github.com/onsi/gomega/gexec"
"github.com/pivotal-cf/on-demand-service-broker/system_tests/test_helpers/service_helpers"
)
type binding struct {
Credentials interface{} `json:"credentials"`
Name string `json:"name"`
}
var certIgnoringHTTPClient = herottp.New(herottp.Config{
DisableTLSCertificateVerification: true,
Timeout: 120 * time.Second,
MaxRetries: 5,
})
func findURL(cliOutput string) string {
for _, line := range strings.Split(cliOutput, "\n") {
if strings.HasPrefix(line, "urls:") {
return strings.Fields(line)[1]
}
if strings.HasPrefix(line, "routes:") {
return strings.Fields(line)[1]
}
}
return ""
}
func GetAppPath(serviceType service_helpers.ServiceType) string {
switch serviceType {
case service_helpers.Redis:
return os.Getenv("REDIS_EXAMPLE_APP_PATH")
case service_helpers.Kafka:
return os.Getenv("KAFKA_EXAMPLE_APP_PATH")
}
return ""
}
func PushAndBindApp(appName, serviceName, testAppPath string) string {
Expect(Cf("push", "-p", testAppPath, "-f", filepath.Join(testAppPath, "manifest.yml"), "--no-start", appName)).To(gexec.Exit(0))
Expect(Cf("bind-service", appName, serviceName)).To(gexec.Exit(0))
// The first time apps start, it is very slow as the buildpack downloads runtimes and caches them
Expect(CfWithTimeout(LongCfTimeout, "start", appName)).To(gexec.Exit(0))
appDetails := Cf("app", appName)
Expect(appDetails).To(gexec.Exit(0))
appDetailsOutput := string(appDetails.Buffer().Contents())
testAppURL := findURL(appDetailsOutput)
Expect(testAppURL).NotTo(BeEmpty())
return testAppURL
}
func UnbindAndDeleteApp(appName, serviceName string) {
Expect(Cf("unbind-service", appName, serviceName)).To(gexec.Exit(0))
DeleteApp(appName)
}
func DeleteApp(appName string) {
Expect(Cf("delete", appName, "-f")).To(gexec.Exit(0))
}
func ExerciseApp(serviceType service_helpers.ServiceType, appURL string) {
switch serviceType {
case service_helpers.Redis:
exerciseRedis(appURL)
return
case service_helpers.Kafka:
exerciseKafka(appURL)
return
}
ginkgo.Fail(fmt.Sprintf("wrong service type: %d", serviceType))
}
func exerciseRedis(appURL string) {
PutToTestApp(appURL, "foo", "bar")
Expect(GetFromTestApp(appURL, "foo")).To(Equal("bar"))
}
func exerciseKafka(appURL string) {
queue := "a-test-queue"
PushToTestAppQueue(appURL, queue, "foo")
PushToTestAppQueue(appURL, queue, "bar")
Expect(PopFromTestAppQueue(appURL, queue)).To(Equal("foo"))
Expect(PopFromTestAppQueue(appURL, queue)).To(Equal("bar"))
}
func PutToTestApp(testAppURL, key, value string) {
putReq, err := http.NewRequest(
"PUT",
fmt.Sprintf("https://%s/%s", testAppURL, key),
strings.NewReader(fmt.Sprintf("data=%s", value)),
)
Expect(err).ToNot(HaveOccurred())
putReq.Header.Add("Content-Type", "application/x-www-form-urlencoded")
makeAndCheckHttpRequest(putReq)
}
func GetFromTestApp(testAppURL, key string) string {
getReq, err := http.NewRequest(
"GET",
fmt.Sprintf("https://%s/%s", testAppURL, key),
nil,
)
Expect(err).ToNot(HaveOccurred())
return makeAndCheckHttpRequest(getReq)
}
func PushToTestAppQueue(testAppURL, queueName, message string) {
postReq, err := http.NewRequest(
"POST",
fmt.Sprintf("https://%s/queues/%s", testAppURL, queueName),
strings.NewReader(message),
)
Expect(err).ToNot(HaveOccurred())
makeAndCheckHttpRequest(postReq)
}
func PopFromTestAppQueue(testAppURL, queueName string) string {
getReq, err := http.NewRequest(
"GET",
fmt.Sprintf("https://%s/queues/%s", testAppURL, queueName),
nil,
)
Expect(err).ToNot(HaveOccurred())
return makeAndCheckHttpRequest(getReq)
}
func appEnv(appName string) (io.Reader, error) {
session := Cf(
"curl",
fmt.Sprintf("/v2/apps/%s/env", appGUID(appName)),
)
Expect(session).To(gexec.Exit(0))
return bytes.NewReader(session.Out.Contents()), nil
}
func appBinding(appName, serviceName string) (*binding, error) {
env, err := appEnv(appName)
if err != nil {
return nil, err
}
var envBindings struct {
Env struct {
Services map[string][]binding `json:"VCAP_SERVICES"`
} `json:"system_env_json"`
}
if err = json.NewDecoder(env).Decode(&envBindings); err != nil {
return nil, err
}
if bindings, found := envBindings.Env.Services[serviceName]; found {
return &bindings[0], nil
}
return nil, fmt.Errorf("app not bound to service %q", serviceName)
}
func AppBindingCreds(appName, serviceName string) (interface{}, error) {
b, err := appBinding(appName, serviceName)
if err != nil {
return "", err
}
return b.Credentials, nil
}
func guid(name, typ string) string {
session := Cf(typ, name, "--guid")
Expect(session).To(gexec.Exit(0))
return strings.Trim(string(session.Out.Contents()), " \n")
}
func appGUID(appName string) string {
return guid(appName, "app")
}
func ServiceInstanceGUID(serviceName string) string {
return guid(serviceName, "service")
}
func makeAndCheckHttpRequest(req *http.Request) string {
resp, err := certIgnoringHTTPClient.Do(req)
Expect(err).ToNot(HaveOccurred())
defer resp.Body.Close()
bodyContent, err := ioutil.ReadAll(resp.Body)
Expect(err).ToNot(HaveOccurred())
ginkgo.GinkgoWriter.Write([]byte(fmt.Sprintf(
"response from %s %s: %d\n----------------------------------------\n%s\n----------------------------------------\n",
req.Method,
req.URL.String(),
resp.StatusCode,
bodyContent,
)))
Expect(resp.StatusCode).To(BeNumerically("<", 300))
return string(bodyContent)
}