diff --git a/app/models/host.rb b/app/models/host.rb index 1988dbc8873..f6e7b1cb08c 100644 --- a/app/models/host.rb +++ b/app/models/host.rb @@ -1346,22 +1346,17 @@ def first_cat_entry(name) Classification.first_cat_entry(name, self) end - # TODO: Rename this to scan_queue and rename scan_from_queue to scan to match - # standard from other places. - def scan(userid = "system", _options = {}) + def scan(userid = "system", options = {}) log_target = "#{self.class.name} name: [#{name}], id: [#{id}]" - - task = MiqTask.create(:name => "SmartState Analysis for '#{name}' ", :userid => userid) - _log.info("Requesting scan of #{log_target}") - begin - MiqEvent.raise_evm_job_event(self, :type => "scan", :prefix => "request") - rescue => err - _log.warn("Error raising request scan event for #{log_target}: #{err.message}") - return - end + check_policy_prevent(:request_host_scan, :scan_queue, userid, options) + end + def scan_queue(userid = 'system', _options = {}) + log_target = "#{self.class.name} name: [#{name}], id: [#{id}]" _log.info("Queuing scan of #{log_target}") + + task = MiqTask.create(:name => "SmartState Analysis for '#{name}' ", :userid => userid) timeout = ::Settings.host_scan.queue_timeout.to_i_with_method cb = {:class_name => task.class.name, :instance_id => task.id, :method_name => :queue_callback_on_exceptions, :args => ['Finished']} MiqQueue.put( diff --git a/spec/models/host_spec.rb b/spec/models/host_spec.rb index 845a2988160..045886fe772 100644 --- a/spec/models/host_spec.rb +++ b/spec/models/host_spec.rb @@ -622,4 +622,34 @@ def assert_remote_credentials_validated expect(Host.non_clustered).to eq([host]) end end + + describe "#scan" do + before do + EvmSpecHelper.create_guid_miq_server_zone + @host = FactoryGirl.create(:host_vmware) + FactoryGirl.create(:miq_event_definition, :name => :request_host_scan) + # admin user is needed to process Events + User.super_admin || FactoryGirl.create(:user_with_group, :userid => "admin") + end + + it "policy passes" do + expect_any_instance_of(ManageIQ::Providers::Vmware::InfraManager::Host).to receive(:scan_queue) + + allow(MiqAeEngine).to receive_messages(:deliver => ['ok', 'sucess', MiqAeEngine::MiqAeWorkspaceRuntime.new]) + @host.scan + status, message, result = MiqQueue.first.deliver + MiqQueue.first.delivered(status, message, result) + end + + it "policy prevented" do + expect_any_instance_of(ManageIQ::Providers::Vmware::InfraManager::Host).to_not receive(:scan_queue) + + event = {:attributes => {"full_data" => {:policy => {:prevented => true}}}} + allow_any_instance_of(MiqAeEngine::MiqAeWorkspaceRuntime).to receive(:get_obj_from_path).with("/").and_return(:event_stream => event) + allow(MiqAeEngine).to receive_messages(:deliver => ['ok', 'sucess', MiqAeEngine::MiqAeWorkspaceRuntime.new]) + @host.scan + status, message, _result = MiqQueue.first.deliver + MiqQueue.first.delivered(status, message, MiqAeEngine::MiqAeWorkspaceRuntime.new) + end + end end