Skip to content

Commit

Permalink
fixed filter_for_schedule to return Hash and added rspec for it as we…
Browse files Browse the repository at this point in the history
…ll as fixing different typos
  • Loading branch information
yrudman committed Aug 7, 2019
1 parent 962897c commit 5384b1c
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 15 deletions.
18 changes: 7 additions & 11 deletions app/models/miq_widget.rb
Original file line number Diff line number Diff line change
Expand Up @@ -493,7 +493,7 @@ def self.sync_from_hash(attrs)
end

def filter_for_schedule
%["=" => {"field" => "MiqWidget-id", "value" => #{id}}]
{"=" => {"field" => "MiqWidget-id", "value" => id}}
end

def sync_schedule(schedule_info)
Expand All @@ -515,7 +515,7 @@ def sync_schedule(schedule_info)
raise _("Unsupported interval '%{interval}'") % {:interval => interval}
end

sched = existing_scheduler
sched = existing_schedule
sched ||= MiqSchedule.create!(
:name => description,
:description => description,
Expand All @@ -537,20 +537,16 @@ def sync_schedule(schedule_info)
sched
end

def existing_scheduler
def existing_schedule
return nil if (sched = MiqSchedule.find_by(:name => description)).nil?

# return existing sheduler if filter referr to the same widget
return sched if sched.filter == filter_for_schedule
return sched if sched.filter.exp == filter_for_schedule

# change name of existed schedule in case it is in use

suff = Time.new.utc.to_s
_log.warn("Schedule #{sched.name} already exists, renaming it to `#{sched.name} #{suff}`")
sched.name = "#{sched.name} #{suff}"
sched.description = "#{sched.description} #{suff}"
sched.save

suffix = Time.new.utc.to_s
_log.warn("Schedule #{sched.name} already exists, renaming it to `#{sched.name} #{suffix}`")
sched.update(:name => "#{sched.name} #{suffix}", :description => "#{sched.description} #{suffix}")
nil
end

Expand Down
15 changes: 11 additions & 4 deletions spec/models/miq_widget_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -56,11 +56,18 @@
'))
end

describe "#filter_for_schedule" do
it "returns Hash object representing valid MiqExpression" do
exp = MiqExpression.new(@widget_chart_vendor_and_guest_os.filter_for_schedule)
expect(exp.valid?).to be_truthy
end
end

describe "#sync_schedule" do
let(:schedule) do
filter = @widget_chart_vendor_and_guest_os.filter_for_schedule
FactoryBot.create(:miq_schedule, :filter => filter,
:resource_type => "MiqWidget", :name => @widget_chart_vendor_and_guest_os.name)
FactoryBot.create(:miq_schedule, :filter => MiqExpression.new(filter), :resource_type => "MiqWidget",
:name => @widget_chart_vendor_and_guest_os.name)
end

it "uses existing schedule if link between widget and schedule broken" do
Expand All @@ -71,8 +78,8 @@
expect(@widget_chart_vendor_and_guest_os.miq_schedule.id).to eq(schedule.id)
end

it "rename existing scheduler by adding timestamp go name if existing scheduler use different filter" do
schedule.update(:filter => "\"=\" => {\"field\" => \"MiqWidget-id\", \"value\" => 9999}")
it "rename existing scheduler by adding timestamp to name if existing scheduler use different filter" do
schedule.update(:filter => MiqExpression.new("=" => {"field" => "MiqWidget-id", "value" => 9999}))

time_now = Time.now.utc
Timecop.freeze(time_now) { @widget_chart_vendor_and_guest_os.sync_schedule(:run_at => schedule.run_at) }
Expand Down

0 comments on commit 5384b1c

Please sign in to comment.