Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Ensure job is refreshed in the condition of state machine exits on error #14684

Merged
merged 2 commits into from
Apr 7, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 5 additions & 5 deletions app/models/service_ansible_playbook.rb
Original file line number Diff line number Diff line change
Expand Up @@ -42,13 +42,14 @@ def job(action)
end

def postprocess(action)
hosts = options.fetch_path(job_option_key(action), :inventory)
delete_inventory(action) unless use_default_inventory?(hosts)
inventory_raw_id = options.fetch_path(job_option_key(action), :inventory)
delete_inventory(action, inventory_raw_id) if inventory_raw_id
end

def on_error(action)
_log.info("on_error called for service action: #{action}")
update_attributes(:retirement_state => 'error') if action == "Retirement"
job(action).try(:refresh_ems)
postprocess(action)
end

Expand Down Expand Up @@ -122,10 +123,9 @@ def create_inventory_with_hosts(action, hosts)
end
end

def delete_inventory(action)
def delete_inventory(action, inventory_raw_id)
manager(action).with_provider_connection do |connection|
inventory_id = options.fetch_path(job_option_key(action), :inventory)
connection.api.inventories.find(inventory_id).destroy!
connection.api.inventories.find(inventory_raw_id).destroy!
end
end

Expand Down
29 changes: 25 additions & 4 deletions spec/models/service_ansible_playbook_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,8 @@

let(:executed_service) do
FactoryGirl.create(:service_ansible_playbook, :options => provision_options).tap do |service|
allow(service).to receive(:job).with(action).and_return(tower_job)
regex = /(#{ResourceAction::PROVISION})|(#{ResourceAction::RETIREMENT})/
allow(service).to receive(:job).with(regex).and_return(tower_job)
end
end

Expand Down Expand Up @@ -201,21 +202,41 @@
end

describe '#postprocess' do
it 'deletes inventory' do
expect(executed_service).to receive(:delete_inventory)
executed_service.postprocess(action)
context 'with user selected hosts' do
it 'deletes temporary inventory' do
expect(executed_service).to receive(:delete_inventory)
executed_service.postprocess(action)
end
end

context 'with default localhost' do
let(:provision_options) do
{
:provision_job_options => {
:credential => 1,
:extra_vars => {'var1' => 'value1', 'var2' => 'value2', 'pswd' => encrypted_val}
}
}
end

it 'needs not to delete the inventory' do
expect(executed_service).not_to receive(:delete_inventory)
executed_service.postprocess(action)
end
end
end

describe '#on_error' do
it 'handles retirement error' do
executed_service.update_attributes(:retirement_state => 'Retiring')
expect(tower_job).to receive(:refresh_ems)
expect(executed_service).to receive(:postprocess)
executed_service.on_error(ResourceAction::RETIREMENT)
expect(executed_service.retirement_state).to eq('error')
end

it 'handles provisioning error' do
expect(tower_job).to receive(:refresh_ems)
expect(executed_service).to receive(:postprocess)
executed_service.on_error(action)
expect(executed_service.retirement_state).to be_nil
Expand Down