/
persi_acceptance_suite_test.go
239 lines (196 loc) · 6.66 KB
/
persi_acceptance_suite_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
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
package persi_acceptance_tests_test
import (
"encoding/json"
"errors"
"io/ioutil"
"math/rand"
"os"
"os/exec"
"path/filepath"
"strconv"
"testing"
"time"
"github.com/cloudfoundry/cf-test-helpers/v2/cf"
"github.com/cloudfoundry/cf-test-helpers/v2/config"
"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"
"github.com/zbiljic/go-filelock"
)
var (
cfConfig = loadConfigAndDefaultValues()
pConfig = getPatsSpecificConfig()
patsSuiteSetup *workflowhelpers.ReproducibleTestSuiteSetup
patsTestSetup *workflowhelpers.ReproducibleTestSuiteSetup
DEFAULT_TIMEOUT = 30 * time.Second
LONG_TIMEOUT = 600 * time.Second
POLL_INTERVAL = 3 * time.Second
brokerName string
)
func TestPersiAcceptance(t *testing.T) {
RegisterFailHandler(Fail)
SetDefaultEventuallyTimeout(5 * time.Second)
serviceBroker := &broker{
Name: pConfig.ServiceName + "-broker",
User: pConfig.BrokerUser,
Password: pConfig.BrokerPassword,
URL: pConfig.BrokerUrl,
}
if serviceBroker.managedByPATs() {
brokerName = serviceBroker.Name
}
componentName := "PATS Suite"
maxParallelSetup := 5
SynchronizedBeforeSuite(func() []byte {
patsSuiteSetup = workflowhelpers.NewTestSuiteSetup(cfConfig)
workflowhelpers.AsUser(patsSuiteSetup.AdminUserContext(), DEFAULT_TIMEOUT, func() {
// make sure we don't have a leftover service broker from another test
serviceBroker.Delete()
})
serviceBroker.Create()
lockFilePath, err := ioutil.TempDir("", "pats-setup-lock")
Expect(err).ToNot(HaveOccurred())
lockFilePath = filepath.Join(lockFilePath, "lock-")
for i := 0; i < maxParallelSetup; i++ {
d1 := []byte("this is a lock file")
ioutil.WriteFile(lockFilePath+strconv.Itoa(i), d1, 0644)
}
return []byte(lockFilePath)
}, func(path []byte) {
src := rand.NewSource(time.Now().UnixNano())
prefix := strconv.Itoa(rand.New(src).Intn(1000))
cfConfig.NamePrefix = prefix
lockFilePath := string(path)
// rate limit spec setup to do no more than maxParallelSetup creates in parallel, so that CF doesn't get upset and time out on UAA calls
fl, err := filelock.New(lockFilePath + strconv.Itoa(GinkgoParallelProcess()%maxParallelSetup))
Expect(err).ToNot(HaveOccurred())
fl.Must().Lock()
defer fl.Must().Unlock()
patsTestSetup = workflowhelpers.NewTestSuiteSetup(cfConfig)
patsTestSetup.Setup()
if val, found := os.LookupEnv("TEST_DOCKER_PORA"); found {
dockerEnabled, err := strconv.ParseBool(val)
Expect(err).NotTo(HaveOccurred())
if dockerEnabled {
workflowhelpers.AsUser(patsTestSetup.AdminUserContext(), DEFAULT_TIMEOUT, func() {
Eventually(cf.Cf("enable-feature-flag", "diego_docker"), DEFAULT_TIMEOUT).Should(Exit(0))
})
}
}
})
SynchronizedAfterSuite(func() {
if patsTestSetup != nil {
patsTestSetup.Teardown()
}
}, func() {
workflowhelpers.AsUser(patsSuiteSetup.AdminUserContext(), DEFAULT_TIMEOUT, func() {
err := serviceBroker.Delete()
if err != nil {
cf.Cf("purge-service-offering", pConfig.ServiceName).Wait(DEFAULT_TIMEOUT)
Fail("pats service broker could not be cleaned up.")
}
})
})
RunSpecs(t, componentName)
}
func defaults(config *config.Config) {
if config.DefaultTimeout > 0 {
DEFAULT_TIMEOUT = time.Duration(config.DefaultTimeout) * time.Second
}
}
type patsConfig struct {
ServiceName string `json:"service_name"`
PlanName string `json:"plan_name"`
BrokerUrl string `json:"broker_url"`
BrokerUser string `json:"broker_user"`
BrokerPassword string `json:"broker_password"`
CreateConfig string `json:"create_config"`
CreateBogusConfig string `json:"create_bogus_config"`
CreateLazyUnmountConfig string `json:"create_lazy_unmount_config"`
LazyUnmountVmInstance string `json:"lazy_unmount_vm_instance"`
LazyUnmountRemoteServerJobName string `json:"lazy_unmount_remote_server_job_name"`
LazyUnmountRemoteServerProcessName string `json:"lazy_unmount_remote_server_process_name"`
BindConfig []string `json:"bind_config"`
BindBogusConfig string `json:"bind_bogus_config"`
BindLazyUnmountConfig string `json:"bind_lazy_unmount_config"`
DisallowedLdapBindConfig string `json:"disallowed_ldap_bind_config"`
DisallowedOverrideBindConfig string `json:"disallowed_override_bind_config"`
}
func getPatsSpecificConfig() patsConfig {
configFile, err := os.Open(config.ConfigPath())
if err != nil {
panic(err)
}
defer configFile.Close()
decoder := json.NewDecoder(configFile)
config := &patsConfig{}
err = decoder.Decode(config)
if err != nil {
panic(err)
}
return *config
}
func loadConfigAndDefaultValues() *config.Config {
config := config.LoadConfig()
defaults(config)
return config
}
type broker struct {
Name string
User string
Password string
URL string
}
func (b *broker) Create() {
if !b.managedByPATs() {
// service broker was created outside of pats, e.g. in the errand
return
}
workflowhelpers.AsUser(patsSuiteSetup.AdminUserContext(), DEFAULT_TIMEOUT, func() {
createServiceBroker := cf.Cf("create-service-broker", brokerName, pConfig.BrokerUser, pConfig.BrokerPassword, pConfig.BrokerUrl).Wait(DEFAULT_TIMEOUT)
Expect(createServiceBroker).To(Exit(0))
Expect(createServiceBroker).To(Say(brokerName))
})
}
func (b *broker) Delete() error {
if !b.managedByPATs() {
// service broker was created outside of pats, e.g. in the errand
return nil
}
if b.Name != "" {
session := cf.Cf("delete-service-broker", "-f", b.Name).Wait(DEFAULT_TIMEOUT)
if session.ExitCode() != 0 {
return errors.New("failed-to-delete-service-broker")
}
return nil
}
if b.URL != "" {
serviceBrokers, err := exec.Command("cf", "curl", "/v2/service_brokers").Output()
Expect(err).NotTo(HaveOccurred())
var serviceBrokerResponse struct {
Resources []struct {
Entity struct {
BrokerUrl string `json:"broker_url"`
Name string
}
}
}
Expect(json.Unmarshal(serviceBrokers, &serviceBrokerResponse)).To(Succeed())
for _, broker := range serviceBrokerResponse.Resources {
if broker.Entity.BrokerUrl == b.URL {
session := cf.Cf("delete-service-broker", "-f", broker.Entity.Name).Wait(DEFAULT_TIMEOUT)
if session.ExitCode() != 0 {
return errors.New("failed-to-delete-service-broker")
}
return nil
}
}
return nil
}
return nil
}
func (b *broker) managedByPATs() bool {
return b.User != "" && b.Password != ""
}