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

Fix the dynamic service task naming generation for subclasses #18511

Merged
merged 1 commit into from
Mar 4, 2019
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
12 changes: 9 additions & 3 deletions app/models/service_retire_task.rb
Original file line number Diff line number Diff line change
Expand Up @@ -51,18 +51,24 @@ def create_retire_subtasks(parent_service, parent_task)
end

def create_task(svc_rsc, parent_service, nh, parent_task)
resource_type = svc_rsc.resource.type.presence || "Service"
(resource_type.demodulize + "RetireTask").constantize.new(nh).tap do |task|
task_type = retire_task_type(svc_rsc.resource.class)
task_type.new(nh).tap do |task|
task.options.merge!(
:src_ids => [svc_rsc.resource.id],
:service_resource_id => svc_rsc.id,
:parent_service_id => parent_service.id,
:parent_task_id => parent_task.id,
)
task.request_type = resource_type.demodulize.underscore.downcase + "_retire"
task.request_type = task_type.name.underscore[0..-6]
task.source = svc_rsc.resource
parent_task.miq_request_tasks << task
task.save!
end
end

private

def retire_task_type(resource_type)
(resource_type.base_class.name + "RetireTask").safe_constantize || (resource_type.name.demodulize + "RetireTask").safe_constantize
end
end
56 changes: 40 additions & 16 deletions spec/models/service_retire_task_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@

expect(service_retire_task.description).to eq("Service Retire for: #{service.name} - ")
expect(VmRetireTask.count).to eq(0)
expect(ServiceRetireTask.count).to eq(1)
end
end

Expand All @@ -47,26 +48,49 @@
miq_request.approve(approver, reason)
end

context "resource lacks type" do
it "creates service retire subtask" do
resource = FactoryBot.create(:service_resource, :resource_type => nil, :service_id => service.id, :resource_id => vm.id)
service.service_resources << resource
service_retire_task.after_request_task_create
it "creates service retire subtask" do
service.add_resource!(FactoryBot.create(:service_orchestration))
service_retire_task.after_request_task_create

expect(service_retire_task.description).to eq("Service Retire for: #{service.name} - ")
expect(ServiceRetireTask.count).to eq(1)
end
expect(service_retire_task.description).to eq("Service Retire for: #{service.name} - ")
expect(ServiceRetireTask.count).to eq(2)
Copy link
Member

Choose a reason for hiding this comment

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

Why 2?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Because the top level service has one too.

Copy link
Member

Choose a reason for hiding this comment

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

Oh, so one for the Service and one for the ServiceOrchestration?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Yup!

end

context "resource has type" do
it "creates vm retire subtask" do
resource = FactoryBot.create(:service_resource, :resource_type => "VmOrTemplate", :service_id => service.id, :resource_id => vm.id)
service.service_resources << resource
service_retire_task.after_request_task_create
it "creates service retire subtask" do
service.add_resource!(FactoryBot.create(:service))
service_retire_task.after_request_task_create

expect(service_retire_task.description).to eq("Service Retire for: #{service.name} - ")
expect(VmRetireTask.count).to eq(1)
end
expect(service_retire_task.description).to eq("Service Retire for: #{service.name} - ")
expect(ServiceRetireTask.count).to eq(2)
end

it "creates stack retire subtask" do
service.add_resource!(FactoryBot.create(:orchestration_stack))
service_retire_task.after_request_task_create

expect(service_retire_task.description).to eq("Service Retire for: #{service.name} - ")
expect(OrchestrationStackRetireTask.count).to eq(1)
expect(ServiceRetireTask.count).to eq(1)
end

it "doesn't create subtask for miq_provision_request_template" do
admin = FactoryBot.create(:user_admin)
vm_template = FactoryBot.create(:vm_openstack, :ext_management_system => FactoryBot.create(:ext_management_system))
service.add_resource!(FactoryBot.create(:miq_provision_request_template, :requester => admin, :src_vm_id => vm_template.id))
service_retire_task.after_request_task_create

expect(service_retire_task.description).to eq("Service Retire for: #{service.name} - ")
expect(MiqRetireTask.count).to eq(1)
expect(ServiceRetireTask.count).to eq(1)
end

it "creates vm retire subtask" do
service.add_resource!(FactoryBot.create(:vm_openstack))
service_retire_task.after_request_task_create

expect(service_retire_task.description).to eq("Service Retire for: #{service.name} - ")
expect(VmRetireTask.count).to eq(1)
expect(ServiceRetireTask.count).to eq(1)
end
end

Expand Down