github.com/mook-as/cf-cli@v7.0.0-beta.28.0.20200120190804-b91c115fae48+incompatible/integration/helpers/fakeservicebroker/fakeservicebroker.go (about) 1 // fakeservicebroker is a test helper for service broker tests. It can push an App that acts as a service broker, and 2 // as an optimisation, it will reuse the same App so that multiple pushes aren't necessary. This saves significant time 3 // when running tests. 4 // 5 // To make use of this optimisation, set 'KEEP_FAKE_SERVICE_BROKERS' to 'true' 6 package fakeservicebroker 7 8 import ( 9 "fmt" 10 "os" 11 "strings" 12 13 "code.cloudfoundry.org/cli/integration/helpers" 14 . "github.com/onsi/gomega" 15 . "github.com/onsi/gomega/gexec" 16 ) 17 18 // Setup should be run in a SynchronisedBeforeSuite() to create the org and space, 19 // otherwise the tests become flaky. 20 func Setup() { 21 helpers.WithRandomHomeDir(func() { 22 helpers.SetAPI() 23 helpers.LoginCF() 24 helpers.CreateOrgAndSpaceUnlessExists(itsOrg, itsSpace) 25 }) 26 } 27 28 // Cleanup should be run in a SynchronizedAfterSuite() to clean up the reusable broker apps 29 func Cleanup() { 30 helpers.WithRandomHomeDir(func() { 31 helpers.SetAPI() 32 helpers.LoginCF() 33 helpers.CreateOrgAndSpaceUnlessExists(itsOrg, itsSpace) 34 helpers.TargetOrgAndSpace(itsOrg, itsSpace) 35 36 broker := New() 37 otherBroker := NewAlternate() 38 39 if os.Getenv("KEEP_FAKE_SERVICE_BROKERS") != "true" { 40 broker.stopReusing() 41 otherBroker.stopReusing() 42 } 43 44 broker.cleanup() 45 otherBroker.cleanup() 46 }) 47 } 48 49 type FakeServiceBroker struct { 50 name string 51 username string 52 password string 53 Services []service 54 domain string 55 behaviors behaviors 56 reusable bool 57 catalogStatus int 58 } 59 60 // New creates a default object that can then be configured 61 func New() *FakeServiceBroker { 62 return defaultConfig() 63 } 64 65 // NewAlternate returns a reusable broker with another name. Can be used in conjunction with New() if you need two brokers in the same test. 66 func NewAlternate() *FakeServiceBroker { 67 f := defaultConfig() 68 f.name = generateReusableBrokerName("other-") 69 return f 70 } 71 72 func (f *FakeServiceBroker) WithCatalogStatus(statusCode int) *FakeServiceBroker { 73 f.catalogStatus = statusCode 74 return f 75 } 76 77 // WithName has the side-effect that the broker is not reusable 78 func (f *FakeServiceBroker) WithName(name string) *FakeServiceBroker { 79 f.name = name 80 f.reusable = false 81 return f 82 } 83 84 // WithAsyncBehaviour makes service instance and binding operations async on the broker side 85 func (f *FakeServiceBroker) WithAsyncBehaviour() *FakeServiceBroker { 86 f.behaviors.Provision["default"] = asyncResponse() 87 f.behaviors.Update["default"] = asyncResponse() 88 f.behaviors.Deprovision["default"] = asyncResponse() 89 f.behaviors.Bind["default"] = asyncResponse().asyncOnly() 90 f.behaviors.Unbind["default"] = asyncResponse().asyncOnly() 91 return f 92 } 93 94 // EnsureAppIsDeployed makes the fake service broker app available and does not run 'cf create-service-broker' 95 func (f *FakeServiceBroker) EnsureAppIsDeployed() *FakeServiceBroker { 96 f.pushAppIfNecessary() 97 f.deregister() 98 f.configure() 99 return f 100 } 101 102 // EnsureBrokerIsAvailable makes the service broker app available and runs 'cf create-service-broker' 103 func (f *FakeServiceBroker) EnsureBrokerIsAvailable() *FakeServiceBroker { 104 f.EnsureAppIsDeployed() 105 f.register() 106 return f 107 } 108 109 // EnsureRegisteredViaV2 uses POST /v2/service_brokers to register the broker. Used to test what happens with old brokers when switching to V7/V3 110 func (f *FakeServiceBroker) EnsureRegisteredViaV2() *FakeServiceBroker { 111 f.EnsureAppIsDeployed() 112 f.registerViaV2() 113 return f 114 } 115 116 // EnableServiceAccess enables access to all service offerings in all orgs for this service broker 117 func (f *FakeServiceBroker) EnableServiceAccess() { 118 session := helpers.CF("enable-service-access", f.ServiceName()) 119 Eventually(session).Should(Exit(0)) 120 } 121 122 // Configure updates the config of a fake service broker app 123 func (f *FakeServiceBroker) Configure() *FakeServiceBroker { 124 f.configure() 125 return f 126 } 127 128 // Update updates the config of a fake service broker app and kicks off catalog synchronization 129 func (f *FakeServiceBroker) Update() *FakeServiceBroker { 130 f.configure() 131 f.update() 132 return f 133 } 134 135 // Destroy always deletes the broker and app, meaning it can't be reused. In general you should not destroy brokers 136 // as it makes tests slower. 137 func (f *FakeServiceBroker) Destroy() { 138 f.deregister() 139 f.deleteApp() 140 } 141 142 func (f *FakeServiceBroker) URL(paths ...string) string { 143 return fmt.Sprintf("http://%s.%s%s", f.name, f.domain, strings.Join(paths, "")) 144 } 145 146 func (f *FakeServiceBroker) ServiceName() string { 147 return f.Services[0].Name 148 } 149 150 func (f *FakeServiceBroker) ServiceDescription() string { 151 return f.Services[0].Description 152 } 153 154 func (f *FakeServiceBroker) ServicePlanDescription() string { 155 return f.Services[0].Plans[0].Description 156 } 157 158 func (f *FakeServiceBroker) ServicePlanName() string { 159 return f.Services[0].Plans[0].Name 160 } 161 162 func (f *FakeServiceBroker) Name() string { 163 return f.name 164 } 165 166 func (f *FakeServiceBroker) Username() string { 167 return f.username 168 } 169 170 func (f *FakeServiceBroker) Password() string { 171 return f.password 172 }