From 303322f72c97f102e7ff106b83fbdf096c534679 Mon Sep 17 00:00:00 2001 From: d-m-u Date: Wed, 27 Feb 2019 09:10:24 -0500 Subject: [PATCH 1/2] Add custom_button and custom_button_set copy to service_template copy --- app/models/service_template/copy.rb | 20 ++++++++++++++++---- spec/models/service_template_spec.rb | 24 ++++++++++++++++++++++++ 2 files changed, 40 insertions(+), 4 deletions(-) diff --git a/app/models/service_template/copy.rb b/app/models/service_template/copy.rb index ff90bbe2e22..97d6c33c260 100644 --- a/app/models/service_template/copy.rb +++ b/app/models/service_template/copy.rb @@ -6,12 +6,24 @@ def template_copy(new_name = "Copy of " + name + Time.zone.now.to_s) ActiveRecord::Base.transaction do dup.tap do |template| template.update_attributes(:name => new_name, :display => false) - service_resources.each do |sr| - resource = sr.resource.respond_to?(:service_template_resource_copy) ? sr.resource.service_template_resource_copy : sr.resource - template.add_resource(resource, sr) - end + service_resources.each { |sr| resource_copy(sr, template) } + custom_buttons.each { |cb| custom_button_copy(cb, template) } + custom_button_sets.each { |cbs| custom_button_set_copy(cbs, template) } end.save! end end end + + def resource_copy(sr, template) + resource = sr.resource.respond_to?(:service_template_resource_copy) ? sr.resource.service_template_resource_copy : sr.resource + template.add_resource(resource, sr) + end + + def custom_button_copy(custom_button, template) + custom_button.copy(:applies_to => template) + end + + def custom_button_set_copy(cbs, template) + cbs.deep_copy(:owner => template) + end end diff --git a/spec/models/service_template_spec.rb b/spec/models/service_template_spec.rb index 1224c0aedaa..f5f4cdc3f3e 100644 --- a/spec/models/service_template_spec.rb +++ b/spec/models/service_template_spec.rb @@ -271,6 +271,8 @@ context "#template_copy" do let(:service_template_ansible_tower) { FactoryBot.create(:service_template_ansible_tower, :name => "thing") } let(:service_template_orchestration) { FactoryBot.create(:service_template_orchestration, :name => "thing2") } + let(:custom_button) { FactoryBot.create(:custom_button, :applies_to_class => "Service") } + let(:custom_button_set) { FactoryBot.create(:custom_button_set, :owner => @st1) } before do @st1 = FactoryBot.create(:service_template) end @@ -285,6 +287,28 @@ expect(ServiceTemplate.find_by(:name => "drew").guid).not_to eq(@st1.guid) end + it "with custom button" do + custom_button + expect(@st1.custom_buttons.count).to eq(1) + @st1.template_copy("drew") + expect(ServiceTemplate.count).to eq(2) + expect(ServiceTemplate.find_by(:name => "drew")).not_to be(nil) + expect(ServiceTemplate.find_by(:name => "drew").display).to be(false) + expect(ServiceTemplate.find_by(:name => "drew").guid).not_to eq(@st1.guid) + expect(ServiceTemplate.find_by(:name => "drew").custom_buttons.count).to eq(2) + end + + it "with custom button set" do + custom_button_set.add_member(custom_button) + expect(@st1.custom_button_sets.count).to eq(1) + @st1.template_copy("drew") + expect(ServiceTemplate.count).to eq(2) + expect(ServiceTemplate.find_by(:name => "drew")).not_to be(nil) + expect(ServiceTemplate.find_by(:name => "drew").display).to be(false) + expect(ServiceTemplate.find_by(:name => "drew").guid).not_to eq(@st1.guid) + expect(ServiceTemplate.find_by(:name => "drew").custom_button_sets.count).to eq(1) + end + it "with non-copyable resource (configuration script base)" do @st1.add_resource(FactoryBot.create(:configuration_script_base)) expect(ServiceTemplate.count).to eq(1) From 8f0d26ad746af7266438120efaa6c95393e62421 Mon Sep 17 00:00:00 2001 From: d-m-u Date: Wed, 27 Feb 2019 09:54:04 -0500 Subject: [PATCH 2/2] Fix the naming and remove superfluous tests --- app/models/service_template/copy.rb | 16 ++-- spec/models/service_template_spec.rb | 134 ++++++++++++++------------- 2 files changed, 79 insertions(+), 71 deletions(-) diff --git a/app/models/service_template/copy.rb b/app/models/service_template/copy.rb index 97d6c33c260..798eb3032b7 100644 --- a/app/models/service_template/copy.rb +++ b/app/models/service_template/copy.rb @@ -6,24 +6,24 @@ def template_copy(new_name = "Copy of " + name + Time.zone.now.to_s) ActiveRecord::Base.transaction do dup.tap do |template| template.update_attributes(:name => new_name, :display => false) - service_resources.each { |sr| resource_copy(sr, template) } - custom_buttons.each { |cb| custom_button_copy(cb, template) } - custom_button_sets.each { |cbs| custom_button_set_copy(cbs, template) } + service_resources.each { |service_resource| resource_copy(service_resource, template) } + custom_buttons.each { |custom_button| custom_button_copy(custom_button, template) } + custom_button_sets.each { |custom_button_set| custom_button_set_copy(custom_button_set, template) } end.save! end end end - def resource_copy(sr, template) - resource = sr.resource.respond_to?(:service_template_resource_copy) ? sr.resource.service_template_resource_copy : sr.resource - template.add_resource(resource, sr) + def resource_copy(service_resource, template) + resource = service_resource.resource.respond_to?(:service_template_resource_copy) ? service_resource.resource.service_template_resource_copy : service_resource.resource + template.add_resource(resource, service_resource) end def custom_button_copy(custom_button, template) custom_button.copy(:applies_to => template) end - def custom_button_set_copy(cbs, template) - cbs.deep_copy(:owner => template) + def custom_button_set_copy(custom_button_set, template) + custom_button_set.deep_copy(:owner => template) end end diff --git a/spec/models/service_template_spec.rb b/spec/models/service_template_spec.rb index f5f4cdc3f3e..600a896e682 100644 --- a/spec/models/service_template_spec.rb +++ b/spec/models/service_template_spec.rb @@ -269,8 +269,8 @@ end context "#template_copy" do - let(:service_template_ansible_tower) { FactoryBot.create(:service_template_ansible_tower, :name => "thing") } - let(:service_template_orchestration) { FactoryBot.create(:service_template_orchestration, :name => "thing2") } + let(:service_template_ansible_tower) { FactoryBot.create(:service_template_ansible_tower, :name => "new_template") } + let(:service_template_orchestration) { FactoryBot.create(:service_template_orchestration, :name => "new_template2") } let(:custom_button) { FactoryBot.create(:custom_button, :applies_to_class => "Service") } let(:custom_button_set) { FactoryBot.create(:custom_button_set, :owner => @st1) } before do @@ -280,71 +280,71 @@ context "with given name" do it "without resource " do expect(ServiceTemplate.count).to eq(1) - @st1.template_copy("drew") + @st1.template_copy("new_template") expect(ServiceTemplate.count).to eq(2) - expect(ServiceTemplate.find_by(:name => "drew")).not_to be(nil) - expect(ServiceTemplate.find_by(:name => "drew").display).to be(false) - expect(ServiceTemplate.find_by(:name => "drew").guid).not_to eq(@st1.guid) + new_service_template = ServiceTemplate.find_by(:name => "new_template") + expect(new_service_template.display).to be(false) + expect(new_service_template.guid).not_to eq(@st1.guid) end it "with custom button" do custom_button expect(@st1.custom_buttons.count).to eq(1) - @st1.template_copy("drew") + @st1.template_copy("new_template") expect(ServiceTemplate.count).to eq(2) - expect(ServiceTemplate.find_by(:name => "drew")).not_to be(nil) - expect(ServiceTemplate.find_by(:name => "drew").display).to be(false) - expect(ServiceTemplate.find_by(:name => "drew").guid).not_to eq(@st1.guid) - expect(ServiceTemplate.find_by(:name => "drew").custom_buttons.count).to eq(2) + new_service_template = ServiceTemplate.find_by(:name => "new_template") + expect(new_service_template.display).to be(false) + expect(new_service_template.guid).not_to eq(@st1.guid) + expect(new_service_template.custom_buttons.count).to eq(2) end it "with custom button set" do custom_button_set.add_member(custom_button) expect(@st1.custom_button_sets.count).to eq(1) - @st1.template_copy("drew") + @st1.template_copy("new_template") + new_service_template = ServiceTemplate.find_by(:name => "new_template") expect(ServiceTemplate.count).to eq(2) - expect(ServiceTemplate.find_by(:name => "drew")).not_to be(nil) - expect(ServiceTemplate.find_by(:name => "drew").display).to be(false) - expect(ServiceTemplate.find_by(:name => "drew").guid).not_to eq(@st1.guid) - expect(ServiceTemplate.find_by(:name => "drew").custom_button_sets.count).to eq(1) + expect(new_service_template.display).to be(false) + expect(new_service_template.guid).not_to eq(@st1.guid) + expect(new_service_template.custom_button_sets.count).to eq(1) end it "with non-copyable resource (configuration script base)" do @st1.add_resource(FactoryBot.create(:configuration_script_base)) expect(ServiceTemplate.count).to eq(1) - @st1.template_copy("thing") + @st1.template_copy("new_template") + new_service_template = ServiceTemplate.find_by(:name => "new_template") expect(ServiceTemplate.count).to eq(2) - expect(ServiceTemplate.find_by(:name => "thing").service_resources).not_to be(nil) expect(@st1.service_resources.first.resource).not_to be(nil) - expect(ServiceTemplate.find_by(:name => "thing").service_resources.first.resource).to eq(@st1.service_resources.first.resource) + expect(new_service_template.service_resources.first.resource).to eq(@st1.service_resources.first.resource) expect(ConfigurationScriptBase.count).to eq(1) - expect(ServiceTemplate.find_by(:name => "thing").display).to be(false) - expect(ServiceTemplate.find_by(:name => "thing").guid).not_to eq(@st1.guid) + expect(new_service_template.display).to be(false) + expect(new_service_template.guid).not_to eq(@st1.guid) end it "with non-copyable resource (ext management system)" do @st1.add_resource(FactoryBot.create(:ext_management_system)) expect(ServiceTemplate.count).to eq(1) - @st1.template_copy("thing") + @st1.template_copy("new_template") + new_service_template = ServiceTemplate.find_by(:name => "new_template") expect(ServiceTemplate.count).to eq(2) - expect(ServiceTemplate.find_by(:name => "thing").service_resources.first.resource_id).to eq(@st1.service_resources.first.resource_id) + expect(new_service_template.service_resources.first.resource_id).to eq(@st1.service_resources.first.resource_id) expect(ExtManagementSystem.count).to eq(1) - expect(ServiceTemplate.find_by(:name => "thing").guid).not_to eq(@st1.guid) - expect(ServiceTemplate.find_by(:name => "thing").service_resources).not_to be(nil) - expect(ServiceTemplate.find_by(:name => "thing").display).to be(false) + expect(new_service_template.guid).not_to eq(@st1.guid) + expect(new_service_template.display).to be(false) expect(@st1.service_resources.first.resource).not_to be(nil) end it "with non-copyable resource (orchestration template)" do @st1.add_resource(FactoryBot.create(:orchestration_template)) expect(ServiceTemplate.count).to eq(1) - @st1.template_copy("thing") + @st1.template_copy("new_template") + new_service_template = ServiceTemplate.find_by(:name => "new_template") expect(ServiceTemplate.count).to eq(2) - expect(ServiceTemplate.find_by(:name => "thing").service_resources.first.resource_id).to eq(@st1.service_resources.first.resource_id) + expect(new_service_template.service_resources.first.resource_id).to eq(@st1.service_resources.first.resource_id) expect(OrchestrationTemplate.count).to eq(1) - expect(ServiceTemplate.find_by(:name => "thing").guid).not_to eq(@st1.guid) - expect(ServiceTemplate.find_by(:name => "thing").service_resources).not_to be(nil) - expect(ServiceTemplate.find_by(:name => "thing").display).to be(false) + expect(new_service_template.guid).not_to eq(@st1.guid) + expect(new_service_template.display).to be(false) expect(@st1.service_resources.first.resource).not_to be(nil) end @@ -354,12 +354,13 @@ ptr = FactoryBot.create(:miq_provision_request_template, :requester => admin, :src_vm_id => vm_template.id) @st1.add_resource(ptr) expect(ServiceTemplate.count).to eq(1) - @st1.template_copy("thing1") + @st1.template_copy("new_template") + new_service_template = ServiceTemplate.find_by(:name => "new_template") expect(ServiceTemplate.count).to eq(2) expect(MiqProvisionRequestTemplate.count).to eq(2) - expect(ServiceTemplate.find_by(:name => "thing1").guid).not_to eq(@st1.guid) - expect(ServiceTemplate.find_by(:name => "thing1").display).to be(false) - expect(ServiceTemplate.find_by(:name => "thing1").service_resources).not_to be(nil) + expect(new_service_template.guid).not_to eq(@st1.guid) + expect(new_service_template.display).to be(false) + expect(new_service_template.service_resources).not_to be(nil) expect(@st1.service_resources.first.resource).not_to be(nil) end @@ -371,13 +372,13 @@ @st1.service_resources.first.update_attributes(:scaling_min => 4) expect(ServiceTemplate.count).to eq(1) expect(@st1.service_resources.first.scaling_min).to eq(4) - @st1.template_copy("thing1") + @st1.template_copy("new_template") + new_service_template = ServiceTemplate.find_by(:name => "new_template") expect(ServiceTemplate.count).to eq(2) expect(MiqProvisionRequestTemplate.count).to eq(2) - expect(ServiceTemplate.find_by(:name => "thing1").guid).not_to eq(@st1.guid) - expect(ServiceTemplate.find_by(:name => "thing1").display).to be(false) - expect(ServiceTemplate.find_by(:name => "thing1").service_resources.first.scaling_min).to eq(4) - expect(ServiceTemplate.find_by(:name => "thing1").service_resources).not_to be(nil) + expect(new_service_template.guid).not_to eq(@st1.guid) + expect(new_service_template.display).to be(false) + expect(new_service_template.service_resources.first.scaling_min).to eq(4) expect(@st1.service_resources.first.resource).not_to be(nil) end @@ -387,12 +388,13 @@ ptr = FactoryBot.create(:miq_provision_request_template, :requester => admin, :src_vm_id => vm_template.id) service_template_ansible_tower.add_resource(ptr) expect(ServiceTemplate.count).to eq(2) - service_template_ansible_tower.template_copy("thing1") + service_template_ansible_tower.template_copy("new_template_copy") + new_service_template = ServiceTemplate.find_by(:name => "new_template_copy") expect(ServiceTemplate.count).to eq(3) expect(MiqProvisionRequestTemplate.count).to eq(2) - expect(ServiceTemplate.find_by(:name => "thing1").guid).not_to eq(service_template_ansible_tower.guid) - expect(ServiceTemplate.find_by(:name => "thing1").display).to be(false) - expect(ServiceTemplate.find_by(:name => "thing1").service_resources).not_to be(nil) + expect(new_service_template.guid).not_to eq(service_template_ansible_tower.guid) + expect(new_service_template.display).to be(false) + expect(new_service_template.service_resources).not_to be(nil) expect(service_template_ansible_tower.service_resources.first.resource).not_to be(nil) end @@ -402,12 +404,13 @@ ptr = FactoryBot.create(:miq_provision_request_template, :requester => admin, :src_vm_id => vm_template.id) service_template_orchestration.add_resource(ptr) expect(ServiceTemplate.count).to eq(2) - service_template_orchestration.template_copy("thing1") + service_template_orchestration.template_copy("new_template") + new_service_template = ServiceTemplate.find_by(:name => "new_template") expect(ServiceTemplate.count).to eq(3) expect(MiqProvisionRequestTemplate.count).to eq(2) - expect(ServiceTemplate.find_by(:name => "thing1").guid).not_to eq(service_template_orchestration.guid) - expect(ServiceTemplate.find_by(:name => "thing1").display).to be(false) - expect(ServiceTemplate.find_by(:name => "thing1").service_resources).not_to be(nil) + expect(new_service_template.guid).not_to eq(service_template_orchestration.guid) + expect(new_service_template.display).to be(false) + expect(new_service_template.service_resources).not_to be(nil) expect(service_template_orchestration.service_resources.first.resource).not_to be(nil) end end @@ -416,10 +419,11 @@ it "without resource" do expect(ServiceTemplate.count).to eq(1) @st1.template_copy + new_service_template = ServiceTemplate.find_by("name ILIKE ?", "Copy of service%") expect(ServiceTemplate.count).to eq(2) - expect(ServiceTemplate.find_by("name ILIKE ?", "Copy of service%").guid).not_to eq(@st1.guid) - expect(ServiceTemplate.find_by("name ILIKE ?", "Copy of service%").display).to be(false) - expect(ServiceTemplate.find_by("name ILIKE ?", "Copy of service%").service_resources.count).to eq(0) + expect(new_service_template.guid).not_to eq(@st1.guid) + expect(new_service_template.display).to be(false) + expect(new_service_template.service_resources.count).to eq(0) expect(@st1.service_resources.count).to eq(0) end @@ -427,23 +431,25 @@ @st1.add_resource(FactoryBot.create(:configuration_script_base)) expect(ServiceTemplate.count).to eq(1) @st1.template_copy + new_service_template = ServiceTemplate.find_by("name ILIKE ?", "Copy of service%") expect(ServiceTemplate.count).to eq(2) - expect(ServiceTemplate.where("name ILIKE ?", "Copy of service%").first.service_resources.first.resource_id).to eq(@st1.service_resources.first.resource_id) + expect(new_service_template.service_resources.first.resource_id).to eq(@st1.service_resources.first.resource_id) expect(ConfigurationScriptBase.count).to eq(1) - expect(ServiceTemplate.find_by("name ILIKE ?", "Copy of service%").display).to be(false) - expect(ServiceTemplate.find_by("name ILIKE ?", "Copy of service%").guid).not_to eq(@st1.guid) + expect(new_service_template.display).to be(false) + expect(new_service_template.guid).not_to eq(@st1.guid) end it "with non-copyable resource (ext management system)" do @st1.add_resource(FactoryBot.create(:ext_management_system)) expect(ServiceTemplate.count).to eq(1) @st1.template_copy + new_service_template = ServiceTemplate.find_by("name ILIKE ?", "Copy of service%") expect(ServiceTemplate.count).to eq(2) expect(ServiceTemplate.where("name ILIKE ?", "Copy of service%").first.service_resources.first.resource_id).to eq(@st1.service_resources.first.resource_id) expect(ExtManagementSystem.count).to eq(1) - expect(ServiceTemplate.find_by("name ILIKE ?", "Copy of service%").guid).not_to eq(@st1.guid) - expect(ServiceTemplate.find_by("name ILIKE ?", "Copy of service%").display).to be(false) - expect(ServiceTemplate.find_by("name ILIKE ?", "Copy of service%").service_resources).not_to be(nil) + expect(new_service_template.guid).not_to eq(@st1.guid) + expect(new_service_template.display).to be(false) + expect(new_service_template.service_resources).not_to be(nil) expect(@st1.service_resources.first.resource).not_to be(nil) end @@ -451,12 +457,13 @@ @st1.add_resource(FactoryBot.create(:orchestration_template)) expect(ServiceTemplate.count).to eq(1) @st1.template_copy + new_service_template = ServiceTemplate.find_by("name ILIKE ?", "Copy of service%") expect(ServiceTemplate.count).to eq(2) expect(ServiceTemplate.where("name ILIKE ?", "Copy of service%").first.service_resources.first.resource_id).to eq(@st1.service_resources.first.resource_id) expect(OrchestrationTemplate.count).to eq(1) - expect(ServiceTemplate.find_by("name ILIKE ?", "Copy of service%").guid).not_to eq(@st1.guid) - expect(ServiceTemplate.find_by("name ILIKE ?", "Copy of service%").display).to be(false) - expect(ServiceTemplate.find_by("name ILIKE ?", "Copy of service%").service_resources).not_to be(nil) + expect(new_service_template.guid).not_to eq(@st1.guid) + expect(new_service_template.display).to be(false) + expect(new_service_template.service_resources).not_to be(nil) expect(@st1.service_resources.first.resource).not_to be(nil) end @@ -467,11 +474,12 @@ @st1.add_resource(ptr) expect(ServiceTemplate.count).to eq(1) @st1.template_copy + new_service_template = ServiceTemplate.find_by("name ILIKE ?", "Copy of service%") expect(ServiceTemplate.count).to eq(2) expect(MiqProvisionRequestTemplate.count).to eq(2) - expect(ServiceTemplate.find_by("name ILIKE ?", "Copy of service%").guid).not_to eq(@st1.guid) - expect(ServiceTemplate.find_by("name ILIKE ?", "Copy of service%").display).to be(false) - expect(ServiceTemplate.find_by("name ILIKE ?", "Copy of service%").service_resources).not_to be(nil) + expect(new_service_template.guid).not_to eq(@st1.guid) + expect(new_service_template.display).to be(false) + expect(new_service_template.service_resources).not_to be(nil) expect(@st1.service_resources.first.resource).not_to be(nil) end end