Skip to content

Commit

Permalink
Output constant must match the constantized class name
Browse files Browse the repository at this point in the history
Constantize can demodulize to an incorrect class, so we should skip any class
names that don't constantize to the expected class.

In this example, if you do the following in rails console, you'll get the
following:

Autoload the base manager eventcatcher:

ManageIQ::Providers::BaseManager::EventCatcher

Try to constantize a non existing event catcher in a valid provider namespace:
"ManageIQ::Providers::Foreman::ProvisioningManager::EventCatcher".constantize
   =>  ManageIQ::Providers::BaseManager::EventCatcher

Seen in the logs for https://bugzilla.redhat.com/show_bug.cgi?id=1759711
  • Loading branch information
jrafanie committed Oct 15, 2019
1 parent c0664f7 commit 2cc0efc
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 0 deletions.
2 changes: 2 additions & 0 deletions app/models/miq_server/worker_management/monitor.rb
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,8 @@ def sync_workers
self.class.monitor_class_names.each do |class_name|
begin
c = class_name.constantize
raise NameError.new("Constant problem: expected: #{class_name}, constantized: #{c.name}") unless c.name == class_name

c.ensure_systemd_files if c.systemd_worker?
result[c.name] = c.sync_workers
result[c.name][:adds].each { |pid| worker_add(pid) unless pid.nil? }
Expand Down
15 changes: 15 additions & 0 deletions spec/models/miq_server/worker_management/monitor_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,21 @@

context "#sync_workers" do
let(:server) { EvmSpecHelper.local_miq_server }

it "ensures only expected worker classes are constantized" do
# Autoload abstract base class for the event catcher
ManageIQ::Providers::BaseManager::EventCatcher

# We'll try to constantize a non-existing EventCatcher class in an existing namespace,
# which incorrectly resolves to the base manager event catcher.
allow(MiqServer).to receive(:monitor_class_names).and_return(%w(ManageIQ::Providers::Foreman::ProvisioningManager::EventCatcher MiqGenericWorker))

expect(ManageIQ::Providers::BaseManager::EventCatcher).to receive(:sync_workers).never
expect(MiqGenericWorker).to receive(:sync_workers).and_return(:adds => [111])
expect(server.sync_workers).to eq("MiqGenericWorker"=>{:adds=>[111]})
end


it "rescues exceptions and moves on" do
allow(MiqServer).to receive(:monitor_class_names).and_return(%w(MiqGenericWorker MiqPriorityWorker))
allow(MiqGenericWorker).to receive(:sync_workers).and_raise
Expand Down

0 comments on commit 2cc0efc

Please sign in to comment.