@@ -18,9 +18,15 @@ func init() {
1818}
1919
2020func TestBroker_ProvisionServiceInstance (t * testing.T ) {
21+ createInstanceCalled := false
2122 test := map [string ]util.HttpTestCase {
22- "GET::/instances" : util.HttpTestCase {200 , util .Body ("../_fixtures/api_list_instances.json" ), nil },
23- "POST::/instances" : util.HttpTestCase {200 , util .Body ("../_fixtures/api_create_instance.json" ), nil },
23+ "GET::/instances" : util.HttpTestCase {200 , util .Body ("../_fixtures/api_list_instances.json" ), nil },
24+ "POST::/instances" : util.HttpTestCase {200 , util .Body ("../_fixtures/api_create_instance.json" ), func (body string ) {
25+ createInstanceCalled = true
26+ assert .Contains (t , body , `name=47d80867-a199-4b5c-8425-8caec83151a4` )
27+ assert .Contains (t , body , `plan=turtle` )
28+ assert .Contains (t , body , `region=azure-arm%3A%3Awesteurope` )
29+ }},
2430 }
2531 apiServer := util .TestServer ("" , "deadbeef" , test )
2632 defer apiServer .Close ()
@@ -42,6 +48,7 @@ func TestBroker_ProvisionServiceInstance(t *testing.T) {
4248
4349 assert .Equal (t , 201 , rec .Code )
4450 assert .Equal (t , util .Body ("../_fixtures/broker_provision_service_instance.json" ), rec .Body .String ())
51+ assert .Equal (t , true , createInstanceCalled ) // should be called to create a new elephantsql instance
4552}
4653
4754func TestBroker_ProvisionServiceInstance_EmptyBody (t * testing.T ) {
@@ -83,10 +90,15 @@ func TestBroker_ProvisionServiceInstance_UnknownPlan(t *testing.T) {
8390}
8491
8592func TestBroker_ProvisionServiceInstance_Conflicting (t * testing.T ) {
93+ createInstanceCalled := false
8694 test := map [string ]util.HttpTestCase {
87- "/instances" : util.HttpTestCase {200 , util .Body ("../_fixtures/api_list_instances.json" ), nil },
95+ "GET:: /instances" : util.HttpTestCase {200 , util .Body ("../_fixtures/api_list_instances.json" ), nil },
8896 "/instances/4567" : util.HttpTestCase {200 , util .Body ("../_fixtures/api_get_instance.json" ), nil },
97+ "POST::/instances" : util.HttpTestCase {200 , util .Body ("../_fixtures/api_create_instance.json" ), func (body string ) {
98+ createInstanceCalled = true
99+ }},
89100 }
101+
90102 apiServer := util .TestServer ("" , "deadbeef" , test )
91103 defer apiServer .Close ()
92104 r := NewRouter (util .TestConfig (apiServer .URL ))
@@ -108,12 +120,17 @@ func TestBroker_ProvisionServiceInstance_Conflicting(t *testing.T) {
108120 assert .Equal (t , 409 , rec .Code )
109121 assert .Contains (t , rec .Body .String (), `"error": "Conflict"` )
110122 assert .Contains (t , rec .Body .String (), `"description": "The service instance already exists with different attributes"` )
123+ assert .Equal (t , false , createInstanceCalled ) // should not be called
111124}
112125
113126func TestBroker_ProvisionServiceInstance_Existing (t * testing.T ) {
127+ createInstanceCalled := false
114128 test := map [string ]util.HttpTestCase {
115- "/instances" : util.HttpTestCase {200 , util .Body ("../_fixtures/api_list_instances.json" ), nil },
129+ "GET:: /instances" : util.HttpTestCase {200 , util .Body ("../_fixtures/api_list_instances.json" ), nil },
116130 "/instances/4567" : util.HttpTestCase {200 , util .Body ("../_fixtures/api_get_instance.json" ), nil },
131+ "POST::/instances" : util.HttpTestCase {200 , util .Body ("../_fixtures/api_create_instance.json" ), func (body string ) {
132+ createInstanceCalled = true
133+ }},
117134 }
118135 apiServer := util .TestServer ("" , "deadbeef" , test )
119136 defer apiServer .Close ()
@@ -135,6 +152,42 @@ func TestBroker_ProvisionServiceInstance_Existing(t *testing.T) {
135152
136153 assert .Equal (t , 200 , rec .Code )
137154 assert .Equal (t , util .Body ("../_fixtures/broker_provision_service_instance.json" ), rec .Body .String ())
155+ assert .Equal (t , false , createInstanceCalled ) // should not be called
156+ }
157+
158+ func TestBroker_ProvisionServiceInstance_WithRegionParameter (t * testing.T ) {
159+ createInstanceCalled := false
160+ test := map [string ]util.HttpTestCase {
161+ "GET::/instances" : util.HttpTestCase {200 , util .Body ("../_fixtures/api_list_instances.json" ), nil },
162+ "POST::/instances" : util.HttpTestCase {200 , util .Body ("../_fixtures/api_create_instance.json" ), func (body string ) {
163+ createInstanceCalled = true
164+ assert .Contains (t , body , `name=47d80867-a199-4b5c-8425-8caec83151a4` )
165+ assert .Contains (t , body , `plan=cat` )
166+ assert .Contains (t , body , `region=azure%3A%3Aswitzerland-alps` )
167+ }},
168+ }
169+ apiServer := util .TestServer ("" , "deadbeef" , test )
170+ defer apiServer .Close ()
171+ r := NewRouter (util .TestConfig (apiServer .URL ))
172+
173+ provisioning := ServiceInstanceProvisioning {
174+ ServiceID : "8ff5d1c8-c6eb-4f04-928c-6a422e0ea330" ,
175+ PlanID : "c1ee0844-b72e-4310-ad92-646107e04534" ,
176+ }
177+ provisioning .Parameters .Region = "azure::switzerland-alps"
178+ data , _ := json .MarshalIndent (provisioning , "" , " " )
179+
180+ rec := httptest .NewRecorder ()
181+ req , err := http .NewRequest ("PUT" , "/v2/service_instances/47d80867-a199-4b5c-8425-8caec83151a4" , bytes .NewBuffer (data ))
182+ if err != nil {
183+ t .Fatal (err )
184+ }
185+ req .SetBasicAuth ("broker" , "pw" )
186+ r .ServeHTTP (rec , req )
187+
188+ assert .Equal (t , 201 , rec .Code )
189+ assert .Equal (t , util .Body ("../_fixtures/broker_provision_service_instance.json" ), rec .Body .String ())
190+ assert .Equal (t , true , createInstanceCalled ) // should be called to create a new elephantsql instance
138191}
139192
140193func TestBroker_FetchServiceInstance (t * testing.T ) {
0 commit comments