From c442b44a72ab16165d2a4e6cf67edc9c2f663c0c Mon Sep 17 00:00:00 2001 From: Tina Fitzgerald Date: Mon, 13 Feb 2017 17:51:42 -0500 Subject: [PATCH] Validate service action. --- .../__methods__/preprocess.rb | 11 +++++- .../__methods__/preprocess_spec.rb | 35 ++++++++++++++++--- 2 files changed, 41 insertions(+), 5 deletions(-) diff --git a/content/automate/ManageIQ/Service/Generic/StateMachines/GenericLifecycle.class/__methods__/preprocess.rb b/content/automate/ManageIQ/Service/Generic/StateMachines/GenericLifecycle.class/__methods__/preprocess.rb index b094ddf25..ddb1ee3d2 100644 --- a/content/automate/ManageIQ/Service/Generic/StateMachines/GenericLifecycle.class/__methods__/preprocess.rb +++ b/content/automate/ManageIQ/Service/Generic/StateMachines/GenericLifecycle.class/__methods__/preprocess.rb @@ -18,7 +18,7 @@ def main dump_root options = {} # user can insert options to override options from dialog - service.preprocess(@handle.root["service_action"], options) + service.preprocess(service_action, options) @handle.root['ae_result'] = 'ok' @handle.log(:info, "Ending preprocess") rescue => err @@ -55,6 +55,15 @@ def service end end end + + def service_action + @handle.root["service_action"].tap do |action| + unless %w(Provision Retirement Reconfigure).include?(action) + @handle.log(:error, "Invalid service action: #{action}") + raise "Invalid service_action" + end + end + end end end end diff --git a/spec/content/automate/ManageIQ/Service/Generic/StateMachines/GenericLifecycle.class/__methods__/preprocess_spec.rb b/spec/content/automate/ManageIQ/Service/Generic/StateMachines/GenericLifecycle.class/__methods__/preprocess_spec.rb index 03d201c24..6a89c10f4 100644 --- a/spec/content/automate/ManageIQ/Service/Generic/StateMachines/GenericLifecycle.class/__methods__/preprocess_spec.rb +++ b/spec/content/automate/ManageIQ/Service/Generic/StateMachines/GenericLifecycle.class/__methods__/preprocess_spec.rb @@ -9,10 +9,9 @@ let(:task) { FactoryGirl.create(:service_template_provision_task, :destination => service_ansible_tower, :miq_request => request) } let(:svc_task) { MiqAeMethodService::MiqAeServiceServiceTemplateProvisionTask.find(task.id) } let(:svc_service) { MiqAeMethodService::MiqAeServiceServiceAnsibleTower.find(service_ansible_tower.id) } - let(:root_object) { Spec::Support::MiqAeMockObject.new('service_template_provision_task' => svc_task) } + let(:root_object) { Spec::Support::MiqAeMockObject.new('service_template_provision_task' => svc_task, 'service_action' => 'Provision') } let(:ae_service) { Spec::Support::MiqAeMockService.new(root_object) } let(:errormsg) { "error" } - let(:status_and_message) { [true, ""] } context "PreProcess" do it "failed scenario" do @@ -25,14 +24,42 @@ expect(ae_service.root['ae_reason']).to eq(errormsg) expect(svc_task.miq_request[:options][:user_message]).to eq(errormsg) end + end - it "successful scenario" do + shared_examples_for "preprocess_error" do + it "preprocess_error" do allow(svc_task).to receive(:destination).and_return(svc_service) allow(svc_service).to receive(:preprocess).and_return(nil) + root_object['service_action'] = service_action described_class.new(ae_service).main - expect(ae_service.root['ae_result']).to eq("ok") + expect(ae_service.root['ae_result']).to eq(ae_result) + expect(ae_service.root['ae_reason']).to eq(errormsg) + expect(svc_task.miq_request[:options][:user_message]).to eq(errormsg) end end + + shared_examples_for "preprocess" do + it "preprocess" do + allow(svc_task).to receive(:destination).and_return(svc_service) + allow(svc_service).to receive(:preprocess).and_return(nil) + + described_class.new(ae_service).main + + expect(ae_service.root['ae_result']).to eq(ae_result) + end + end + + context "successful scenario" do + let(:ae_result) { 'ok' } + it_behaves_like "preprocess" + end + + context "invalid service_action failure" do + let(:service_action) { 'fred' } + let(:ae_result) { 'error' } + let(:errormsg) { 'Invalid service_action' } + it_behaves_like "preprocess_error" + end end