Skip to content

Commit

Permalink
Merge pull request #15228 from lfu/vm_scan_with_automate_1454936
Browse files Browse the repository at this point in the history
Queue the VM scan command after vm_scan_start event is handled by automate.
(cherry picked from commit 7a7350e)

https://bugzilla.redhat.com/show_bug.cgi?id=1460339
  • Loading branch information
gmcculloug authored and simaishi committed Jun 9, 2017
1 parent f0c6c2d commit 0ec4e76
Show file tree
Hide file tree
Showing 2 changed files with 124 additions and 17 deletions.
34 changes: 18 additions & 16 deletions app/models/vm_scan.rb
Original file line number Diff line number Diff line change
Expand Up @@ -43,29 +43,23 @@ def call_check_policy
begin
vm = VmOrTemplate.find(target_id)

cb = {
:class_name => self.class.to_s,
:instance_id => id,
:method_name => :check_policy_complete,
:server_guid => MiqServer.my_guid
q_options = {
:miq_callback => {
:class_name => self.class.to_s,
:instance_id => id,
:method_name => :check_policy_complete,
:args => [MiqServer.my_zone] # Store the zone where the scan job was initiated.
}
}
inputs = {:vm => vm, :host => vm.host}
if !MiqEvent.raise_evm_job_event(vm, {:type => "scan", :suffix => "start"}, inputs, :miq_callback => cb)
msg = "Aborted policy resolution - scan event was not raised to automate."
_log.error(msg)
signal(:abort, msg, "error")
end
MiqEvent.raise_evm_job_event(vm, {:type => "scan", :suffix => "start"}, inputs, q_options)
rescue => err
_log.log_backtrace(err)
signal(:abort, err.message, "error")
rescue Timeout::Error
msg = "Request to check policy timed out"
_log.error(msg)
signal(:abort, msg, "error")
end
end

def check_policy_complete(status, message, result)
def check_policy_complete(from_zone, status, message, result)
unless status == 'ok'
_log.error("Status = #{status}, message = #{message}")
signal(:abort, message, "error")
Expand All @@ -82,7 +76,15 @@ def check_policy_complete(status, message, result)
options[:scan_profiles] = scan_profiles unless scan_profiles.blank?
end
end
signal(:start_snapshot)

MiqQueue.put(
:class_name => self.class.to_s,
:instance_id => id,
:method_name => "signal",
:args => [:start_snapshot],
:zone => from_zone,
:role => "smartstate"
)
end

def call_snapshot_create
Expand Down
107 changes: 106 additions & 1 deletion spec/models/vm_scan_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -127,7 +127,7 @@

it "should call callback when message is delivered" do
allow_any_instance_of(VmScan).to receive_messages(:signal => true)
expect_any_instance_of(VmScan).to receive(:check_policy_complete)
expect_any_instance_of(VmScan).to receive(:check_policy_complete).with(@server.my_zone, "ok", any_args)
q = MiqQueue.where(:class_name => "MiqAeEngine", :method_name => "deliver").first
q.delivered(*q.deliver)
end
Expand Down Expand Up @@ -255,4 +255,109 @@
end
end
end

# test cases for BZ #1454936
context "A VM Scan job in multiple zones" do
before do
# local zone
@server1 = EvmSpecHelper.local_miq_server(:capabilities => {:vixDisk => true})
@user = FactoryGirl.create(:user_with_group, :userid => "tester")
@ems = FactoryGirl.create(:ems_vmware_with_authentication, :name => "Test EMS", :zone => @server1.zone,
:tenant => FactoryGirl.create(:tenant))
@storage = FactoryGirl.create(:storage, :name => "test_storage", :store_type => "VMFS")
@host = FactoryGirl.create(:host, :name => "test_host", :hostname => "test_host",
:state => 'on', :ext_management_system => @ems)
@vm = FactoryGirl.create(:vm_vmware, :name => "test_vm", :location => "abc/abc.vmx",
:raw_power_state => 'poweredOn',
:host => @host,
:ext_management_system => @ems,
:miq_group => @user.current_group,
:evm_owner => @user,
:storage => @storage)

# remote zone
@server2 = EvmSpecHelper.remote_miq_server(:capabilities => {:vixDisk => true})
@user2 = FactoryGirl.create(:user_with_group, :userid => "tester2")
@storage2 = FactoryGirl.create(:storage, :name => "test_storage2", :store_type => "VMFS")
@host2 = FactoryGirl.create(:host, :name => "test_host2", :hostname => "test_host2",
:state => 'on', :ext_management_system => @ems)
@vm2 = FactoryGirl.create(:vm_vmware, :name => "test_vm2", :location => "abc2/abc2.vmx",
:raw_power_state => 'poweredOn',
:host => @host2,
:ext_management_system => @ems,
:miq_group => @user2.current_group,
:evm_owner => @user2,
:storage => @storage2)

allow(MiqEventDefinition).to receive_messages(:find_by => true)
allow(@server1).to receive(:has_active_role?).with('automate').and_return(true) # set automate role in local zone
end

describe "#check_policy_complete" do
context "in local zone" do
before do
@vm.scan
job_item = MiqQueue.find_by(:class_name => "MiqAeEngine", :method_name => "deliver")
job_item.delivered(*job_item.deliver)

@job = Job.first
end

it "signals :abort if passed status is not 'ok' to local zone" do
message = "Hello, World!"
expect(@job).to receive(:signal).with(:abort, message, "error")
@job.check_policy_complete(@server1.my_zone, 'some status', message, nil)
end

it "does not send signal :abort if passed status is 'ok' " do
expect(@job).not_to receive(:signal).with(:abort, nil, "error")
@job.check_policy_complete(@server1.my_zone, 'ok', nil, nil)
end

it "sends signal :start_snapshot if status is 'ok' to local zone" do
expect(MiqQueue).to receive(:put).with(
:class_name => @job.class.to_s,
:instance_id => @job.id,
:method_name => "signal",
:args => [:start_snapshot],
:zone => @server1.my_zone,
:role => "smartstate"
)
@job.check_policy_complete(@server1.my_zone, 'ok', nil, nil)
end
end

context "in remote zone" do
before do
@vm2.scan
job_item = MiqQueue.find_by(:class_name => "MiqAeEngine", :method_name => "deliver")
job_item.delivered(*job_item.deliver)

@job = Job.first
end
it "signals :abort if status is not 'ok' to remote zone" do
message = "Hello, World!"
expect(@job).to receive(:signal).with(:abort, message, "error")
@job.check_policy_complete(@server2.my_zone, 'some status', message, nil)
end

it "does not send signal :abort if passed status is 'ok' " do
expect(@job).not_to receive(:signal).with(:abort, nil, "error")
@job.check_policy_complete(@server2.my_zone, 'ok', nil, nil)
end

it "signals :start_snapshot if status is 'ok' to remote zone" do
expect(MiqQueue).to receive(:put).with(
:class_name => @job.class.to_s,
:instance_id => @job.id,
:method_name => "signal",
:args => [:start_snapshot],
:zone => @server2.my_zone,
:role => "smartstate"
)
@job.check_policy_complete(@server2.my_zone, 'ok', nil, nil)
end
end
end
end
end

0 comments on commit 0ec4e76

Please sign in to comment.