@@ -2528,17 +2528,6 @@ def check_filtered_instances(*instances)
25282528 to_return ( status : broker_status_code , body : broker_response . to_json , headers : { } )
25292529 }
25302530
2531- it 'sets the service instance last operation to delete in progress' do
2532- api_call . call ( admin_headers )
2533- expect ( last_response ) . to have_status_code ( HTTP ::Status ::ACCEPTED )
2534-
2535- instance . reload
2536-
2537- expect ( instance . last_operation ) . to_not be_nil
2538- expect ( instance . last_operation . type ) . to eq ( 'delete' )
2539- expect ( instance . last_operation . state ) . to eq ( 'in progress' )
2540- end
2541-
25422531 it 'responds with job resource' do
25432532 api_call . call ( admin_headers )
25442533 expect ( last_response ) . to have_status_code ( 202 )
@@ -2655,6 +2644,17 @@ def check_filtered_instances(*instances)
26552644 expect ( Delayed ::Job . count ) . to eq ( 1 )
26562645 end
26572646
2647+ it 'sets the service instance last operation to delete in progress' do
2648+ api_call . call ( admin_headers )
2649+ execute_all_jobs ( expected_successes : 1 , expected_failures : 0 )
2650+
2651+ instance . reload
2652+
2653+ expect ( instance . last_operation ) . to_not be_nil
2654+ expect ( instance . last_operation . type ) . to eq ( 'delete' )
2655+ expect ( instance . last_operation . state ) . to eq ( 'in progress' )
2656+ end
2657+
26582658 context 'when last operation eventually returns `delete succeeded`' do
26592659 before do
26602660 stub_request ( :get , "#{ instance . service_broker . broker_url } /v2/service_instances/#{ instance . guid } /last_operation" ) .
@@ -2983,6 +2983,86 @@ def check_filtered_instances(*instances)
29832983 expect ( response ) . to include ( 'detail' => include ( 'Service instances must be unshared before they can be deleted.' ) )
29842984 end
29852985 end
2986+
2987+ context 'when the creation is still in progress' do
2988+ before do
2989+ instance . save_with_new_operation ( { } , {
2990+ type : 'create' ,
2991+ state : 'in progress' ,
2992+ broker_provided_operation : 'some create operation'
2993+ } )
2994+ end
2995+
2996+ context 'and the broker confirms the deletion' do
2997+ it 'deletes the service instance' do
2998+ api_call . call ( admin_headers )
2999+ execute_all_jobs ( expected_successes : 1 , expected_failures : 0 )
3000+
3001+ expect ( VCAP ::CloudController ::ServiceInstance . first ( guid : instance . guid ) ) . to be_nil
3002+ end
3003+ end
3004+
3005+ context 'and the broker accepts the delete' do
3006+ let ( :broker_status_code ) { 202 }
3007+ let ( :broker_response ) { { operation : 'some delete operation' } }
3008+ let ( :last_operation_response ) { { state : 'in progress' , description : 'deleting si' } }
3009+ let ( :last_operation_status_code ) { 200 }
3010+
3011+ before do
3012+ stub_request ( :get , "#{ instance . service_broker . broker_url } /v2/service_instances/#{ instance . guid } /last_operation" ) .
3013+ with (
3014+ query : {
3015+ operation : 'some delete operation' ,
3016+ service_id : instance . service . broker_provided_id ,
3017+ plan_id : instance . service_plan . broker_provided_id
3018+ } ) .
3019+ to_return ( status : last_operation_status_code , body : last_operation_response . to_json , headers : { } )
3020+ end
3021+
3022+ it 'triggers the delete process' do
3023+ api_call . call ( admin_headers )
3024+ expect ( last_response ) . to have_status_code ( HTTP ::Status ::ACCEPTED )
3025+ execute_all_jobs ( expected_successes : 1 , expected_failures : 0 )
3026+
3027+ instance . reload
3028+
3029+ expect ( instance . last_operation ) . to_not be_nil
3030+ expect ( instance . last_operation . type ) . to eq ( 'delete' )
3031+ expect ( instance . last_operation . state ) . to eq ( 'in progress' )
3032+ expect ( instance . last_operation . broker_provided_operation ) . to eq ( 'some delete operation' )
3033+ end
3034+ end
3035+
3036+ context 'but the broker rejects the delete' do
3037+ let ( :broker_status_code ) { 422 }
3038+ let ( :broker_response ) { { error : 'ConcurrencyError' , description : 'Cannot delete right now' } }
3039+
3040+ it 'responds with an error' do
3041+ api_call . call ( admin_headers )
3042+ expect ( last_response ) . to have_status_code ( HTTP ::Status ::ACCEPTED )
3043+ execute_all_jobs ( expected_successes : 0 , expected_failures : 1 )
3044+
3045+ job = VCAP ::CloudController ::PollableJobModel . last
3046+ expect ( job . state ) . to eq ( VCAP ::CloudController ::PollableJobModel ::FAILED_STATE )
3047+
3048+ expect ( job . cf_api_error ) . to_not be_nil
3049+ api_error = YAML . safe_load ( job . cf_api_error ) [ 'errors' ] . first
3050+ expect ( api_error [ 'title' ] ) . to eql ( 'CF-UnableToPerform' )
3051+ expect ( api_error [ 'detail' ] ) . to eql ( 'delete could not be completed: create in progress' )
3052+ end
3053+
3054+ it 'does not change the operation in progress' do
3055+ api_call . call ( admin_headers )
3056+ expect ( last_response ) . to have_status_code ( HTTP ::Status ::ACCEPTED )
3057+ execute_all_jobs ( expected_successes : 0 , expected_failures : 1 )
3058+
3059+ instance . reload
3060+
3061+ expect ( "#{ instance . last_operation . type } #{ instance . last_operation . state } " ) . to eq ( 'create in progress' )
3062+ expect ( instance . last_operation . broker_provided_operation ) . to eq ( 'some create operation' )
3063+ end
3064+ end
3065+ end
29863066 end
29873067
29883068 context 'when associations are not empty' do
0 commit comments