-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathmain_test.go
153 lines (125 loc) · 4.89 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
package main_test
import (
"fmt"
"io"
"net/http"
"strconv"
"time"
"github.com/google/uuid"
"github.com/cloudfoundry/cf-test-helpers/v2/cf"
"github.com/cloudfoundry/cf-test-helpers/v2/workflowhelpers"
. "github.com/onsi/ginkgo/v2"
. "github.com/onsi/gomega"
. "github.com/onsi/gomega/gbytes"
. "github.com/onsi/gomega/gexec"
)
const printErrorsOn = true
const printErrorsOff = false
const instanceNameBase = "pats-volume-instance"
const appNameBase = "pats-pora"
func get(uri string, printErrors bool) (body string, status int, err error) {
req, err := http.NewRequest("GET", uri, nil)
if err != nil {
return "", status, err
}
response, err := (&http.Client{}).Do(req)
if err != nil {
return "", status, err
}
bodyBytes, err := io.ReadAll(response.Body)
defer response.Body.Close()
if printErrors && response.StatusCode >= http.StatusInternalServerError {
fmt.Printf("Request: [[%v]]\nResponse: [[%v]] [[%s]]\n", req, response, string(bodyBytes))
}
return string(bodyBytes[:]), response.StatusCode, err
}
func eventuallyExpect(endpoint string, expectedSubstring string) string {
EventuallyWithOffset(1, func() int {
_, status, err := get(endpoint, printErrorsOn)
ExpectWithOffset(1, err).NotTo(HaveOccurred())
return status
}, 5*time.Second, 1*time.Second).Should(Equal(http.StatusOK))
var body string
EventuallyWithOffset(1, func() string {
var err error
body, _, err = get(endpoint, printErrorsOn)
ExpectWithOffset(1, err).NotTo(HaveOccurred())
return body
}, 5*time.Second, 1*time.Second).Should(ContainSubstring(expectedSubstring))
return body
}
func enableServiceAccess(serviceName, org string) {
workflowhelpers.AsUser(cfTestSuiteSetup.AdminUserContext(), DEFAULT_TIMEOUT, func() {
publishService := cf.Cf("enable-service-access", serviceName, "-o", org, "-b", pConfig.BrokerName).Wait(DEFAULT_TIMEOUT)
ExpectWithOffset(1, publishService).To(Exit(0))
})
}
func pushPoraNoStart(a string, dockerApp bool) {
workflowhelpers.AsUser(cfTestSuiteSetup.RegularUserContext(), DEFAULT_TIMEOUT, func() {
if dockerApp {
EventuallyWithOffset(1, cf.Cf("push", a, "--docker-image", "cloudfoundry/pora", "--no-start", "--no-route"), DEFAULT_TIMEOUT).Should(Exit(0))
} else {
EventuallyWithOffset(1, cf.Cf("push", a, "-p", appPath, "-f", appPath+"/manifest.yml", "--no-start", "--no-route"), DEFAULT_TIMEOUT).Should(Exit(0))
}
})
workflowhelpers.AsUser(cfTestSuiteSetup.RegularUserContext(), DEFAULT_TIMEOUT, func() {
marketplaceItems := cf.Cf("apps").Wait(DEFAULT_TIMEOUT)
ExpectWithOffset(1, marketplaceItems).To(Exit(0))
ExpectWithOffset(1, marketplaceItems).To(Say(a))
})
}
func createService(s, c string) {
workflowhelpers.AsUser(cfTestSuiteSetup.RegularUserContext(), DEFAULT_TIMEOUT, func() {
EventuallyWithOffset(1, func() *Session {
createService := cf.Cf("create-service", pConfig.ServiceName, pConfig.PlanName, s, "-c", c, "-b", pConfig.BrokerName).Wait(DEFAULT_TIMEOUT)
ExpectWithOffset(1, createService).To(Exit(0))
serviceDetails := cf.Cf("service", s).Wait(DEFAULT_TIMEOUT)
ExpectWithOffset(1, serviceDetails).To(Exit(0))
return serviceDetails
}, LONG_TIMEOUT, POLL_INTERVAL).Should(Say("create succeeded"))
})
}
func bindAppToService(a, s, c string) {
workflowhelpers.AsUser(cfTestSuiteSetup.RegularUserContext(), DEFAULT_TIMEOUT, func() {
bindResponse := cf.Cf("bind-service", a, s, "-c", c).Wait(DEFAULT_TIMEOUT)
ExpectWithOffset(1, bindResponse).To(Exit(0))
services := cf.Cf("services").Wait(DEFAULT_TIMEOUT)
ExpectWithOffset(1, services).To(Exit(0))
ExpectWithOffset(1, services).To(Say(s + "[^\\n]+" + pConfig.ServiceName + "[^\\n]+" + a))
})
}
func mapAppRoute(a string) {
domain := pConfig.AppsDomain
if pConfig.IncludeIsolationSegment {
domain = pConfig.IsolationSegmentDomain
}
workflowhelpers.AsUser(cfTestSuiteSetup.RegularUserContext(), DEFAULT_TIMEOUT, func() {
mapRouteResponse := cf.Cf("map-route", a, domain, "--hostname", a).Wait(LONG_TIMEOUT)
ExpectWithOffset(1, mapRouteResponse).To(Exit(0))
})
}
func startApp(a string) {
mapAppRoute(a)
workflowhelpers.AsUser(cfTestSuiteSetup.RegularUserContext(), DEFAULT_TIMEOUT, func() {
bindResponse := cf.Cf("start", a).Wait(LONG_TIMEOUT)
ExpectWithOffset(1, bindResponse).To(Exit(0))
})
}
func stopApp(a string) {
workflowhelpers.AsUser(cfTestSuiteSetup.RegularUserContext(), DEFAULT_TIMEOUT, func() {
bindResponse := cf.Cf("stop", a).Wait(LONG_TIMEOUT)
ExpectWithOffset(1, bindResponse).To(Exit(0))
})
}
func generateTestNames() (instanceName, appName, appURL string) {
parallelNode := strconv.Itoa(GinkgoParallelProcess())
uuid := uuid.NewString()
instanceName = uuid + "-" + instanceNameBase + parallelNode
appName = uuid + "-" + appNameBase + parallelNode
appHost := appName + "." + pConfig.AppsDomain
if pConfig.IncludeIsolationSegment {
appHost = appName + "." + pConfig.IsolationSegmentDomain
}
appURL = "http://" + appHost
return instanceName, appName, appURL
}