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

Add #raw_stdout_via_worker method #16441

Merged
merged 2 commits into from
Nov 16, 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
Original file line number Diff line number Diff line change
Expand Up @@ -7,4 +7,28 @@ def retire_now(requester = nil)
update_attributes(:retirement_requester => requester)
finish_retirement
end

# Intend to be called by UI to display stdout. The stdout is stored in MiqTask#task_results or #message if error
# Since the task_results may contain a large block of data, it is desired to remove the task upon receiving the data
def raw_stdout_via_worker(userid, format = 'txt')
unless MiqRegion.my_region.role_active?("embedded_ansible")
msg = "Cannot get standard output of this playbook because the embedded Ansible role is not enabled"
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

missing i18n? This needs to be at least N_, if you think _( would be wrong.

return MiqTask.create(
:name => 'ansible_stdout',
:userid => userid || 'system',
:state => MiqTask::STATE_FINISHED,
:status => MiqTask::STATUS_ERROR,
:message => msg
).id
end

options = {:userid => userid || 'system', :action => 'ansible_stdout'}
queue_options = {:class_name => self.class,
:method_name => 'raw_stdout',
:instance_id => id,
:args => [format],
:priority => MiqQueue::HIGH_PRIORITY,
:role => 'embedded_ansible'}
MiqTask.generic_action_with_callback(options, queue_options)
end
end
Original file line number Diff line number Diff line change
Expand Up @@ -7,4 +7,57 @@
expect(job).to receive(:finish_retirement).once
job.retire_now
end

describe '#raw_stdout_via_worker' do
before do
EvmSpecHelper.create_guid_miq_server_zone
region = MiqRegion.seed
allow(region).to receive(:role_active?).with("embedded_ansible").and_return(role_enabled)
allow(MiqRegion).to receive(:my_region).and_return(region)
end

context 'when embedded_ansible role is enabled' do
let(:role_enabled) { true }

before do
allow(described_class).to receive(:find).and_return(job)

allow(MiqTask).to receive(:wait_for_taskid) do
request = MiqQueue.find_by(:class_name => described_class.name)
request.update_attributes(:state => MiqQueue::STATE_DEQUEUE)
request.delivered(*request.deliver)
end
end

it 'gets stdout from the job' do
expect(job).to receive(:raw_stdout).and_return('A stdout from the job')
taskid = job.raw_stdout_via_worker('user')
MiqTask.wait_for_taskid(taskid)
expect(MiqTask.find(taskid)).to have_attributes(
:task_results => 'A stdout from the job',
:status => 'Ok'
)
end

it 'returns the error message' do
expect(job).to receive(:raw_stdout).and_throw('Failed to get stdout from the job')
taskid = job.raw_stdout_via_worker('user')
MiqTask.wait_for_taskid(taskid)
expect(MiqTask.find(taskid).message).to include('Failed to get stdout from the job')
expect(MiqTask.find(taskid).status).to eq('Error')
end
end

context 'when embedded_ansible role is disabled' do
let(:role_enabled) { false }

it 'returns an error message' do
taskid = job.raw_stdout_via_worker('user')
expect(MiqTask.find(taskid)).to have_attributes(
:message => 'Cannot get standard output of this playbook because the embedded Ansible role is not enabled',
:status => 'Error'
)
end
end
end
end