From 14c23e0983d5abe535ca21692f2de1874912bdea Mon Sep 17 00:00:00 2001 From: Brandon Dunne Date: Wed, 19 Jul 2017 10:28:04 -0400 Subject: [PATCH 1/2] Add VcrHelper.with_cassette_library_dir ensure that tests wrapped in this block use the cassettes in the given path --- spec/support/vcr_helper.rb | 15 +++++++++++++++ 1 file changed, 15 insertions(+) create mode 100644 spec/support/vcr_helper.rb diff --git a/spec/support/vcr_helper.rb b/spec/support/vcr_helper.rb new file mode 100644 index 00000000..8f986a0c --- /dev/null +++ b/spec/support/vcr_helper.rb @@ -0,0 +1,15 @@ +module Spec + module Support + module VcrHelper + def self.with_cassette_library_dir(new_path) + old_path = VCR.configuration.cassette_library_dir + VCR.configure { |c| c.cassette_library_dir = new_path } + + yield + + ensure + VCR.configure { |c| c.cassette_library_dir = old_path } + end + end + end +end From 0d73ee8bc29fb11eaf3ac26c6988328e64e79455 Mon Sep 17 00:00:00 2001 From: Brandon Dunne Date: Wed, 19 Jul 2017 10:29:17 -0400 Subject: [PATCH 2/2] Ensure that shared_examples use the VCR cassettes from this library --- .../event_catcher/stream.rb | 22 ++++---- .../refresh_configuartion_script_source.rb | 50 ++++++++++--------- .../refresh_configuartion_script_source_v2.rb | 50 ++++++++++--------- .../automation_manager/refresher.rb | 28 ++++++----- .../automation_manager/refresher_v2.rb | 28 ++++++----- 5 files changed, 94 insertions(+), 84 deletions(-) diff --git a/spec/support/ansible_shared/automation_manager/event_catcher/stream.rb b/spec/support/ansible_shared/automation_manager/event_catcher/stream.rb index 3269a644..47ad01b4 100644 --- a/spec/support/ansible_shared/automation_manager/event_catcher/stream.rb +++ b/spec/support/ansible_shared/automation_manager/event_catcher/stream.rb @@ -17,17 +17,19 @@ context "#poll" do it "yields valid events" do - VCR.use_cassette(cassette_file) do - last_activity = subject.send(:last_activity) - # do something on tower that creates an activity in activity_stream - provider.connect.api.credentials.create!(:name => 'test_stream', :user => 1) - polled_event = nil - subject.poll do |event| - expect(event['id']).to eq(last_activity.id + 1) - subject.stop - polled_event = event + Spec::Support::VcrHelper.with_cassette_library_dir(ManageIQ::Providers::AnsibleTower::Engine.root.join("spec/vcr_cassettes")) do + VCR.use_cassette(cassette_file) do + last_activity = subject.send(:last_activity) + # do something on tower that creates an activity in activity_stream + provider.connect.api.credentials.create!(:name => 'test_stream', :user => 1) + polled_event = nil + subject.poll do |event| + expect(event['id']).to eq(last_activity.id + 1) + subject.stop + polled_event = event + end + expect(subject.send(:last_activity).id).to eq(polled_event['id']) end - expect(subject.send(:last_activity).id).to eq(polled_event['id']) end end end diff --git a/spec/support/ansible_shared/automation_manager/refresh_configuartion_script_source.rb b/spec/support/ansible_shared/automation_manager/refresh_configuartion_script_source.rb index 9516dd2b..a870c4bc 100644 --- a/spec/support/ansible_shared/automation_manager/refresh_configuartion_script_source.rb +++ b/spec/support/ansible_shared/automation_manager/refresh_configuartion_script_source.rb @@ -33,36 +33,38 @@ # this is to check if a project will be updated on tower last_project_update = Time.zone.parse("2017-04-10 20:50:11.429285000 +0000") - 1.minute - 2.times do - VCR.use_cassette(cassette_path) do - EmsRefresh.refresh([[configuration_script_source.class.to_s, configuration_script_source.id]]) + Spec::Support::VcrHelper.with_cassette_library_dir(ManageIQ::Providers::AnsibleTower::Engine.root.join("spec/vcr_cassettes")) do + 2.times do + VCR.use_cassette(cassette_path) do + EmsRefresh.refresh([[configuration_script_source.class.to_s, configuration_script_source.id]]) - expect(automation_manager.reload.last_refresh_error).to be_nil - expect(automation_manager.configuration_script_sources.count).to eq(2) + expect(automation_manager.reload.last_refresh_error).to be_nil + expect(automation_manager.configuration_script_sources.count).to eq(2) - configuration_script_source.reload - configuration_script_source_other.reload + configuration_script_source.reload + configuration_script_source_other.reload - last_updated = Time.zone.parse(configuration_script_source.provider_object.last_updated) - expect(last_updated).to be >= last_project_update - last_project_update = last_updated + last_updated = Time.zone.parse(configuration_script_source.provider_object.last_updated) + expect(last_updated).to be >= last_project_update + last_project_update = last_updated - expect(configuration_script_source.name).to eq("targeted_refresh") - expect(ConfigurationScriptPayload.count).to eq(81) - expect(ConfigurationScriptPayload.where(:name => '2b_rm')).to be_empty - expect(configuration_script_source.configuration_script_payloads.count).to eq(81) - expect( - configuration_script_source.configuration_script_payloads.where( - :name => "test/utils/docker/httptester/httptester.yml" - ).count - ).to eq(1) - expect(configuration_script_source.authentication.name).to eq('db-github') - expect(credential.reload).to eq(credential) + expect(configuration_script_source.name).to eq("targeted_refresh") + expect(ConfigurationScriptPayload.count).to eq(81) + expect(ConfigurationScriptPayload.where(:name => '2b_rm')).to be_empty + expect(configuration_script_source.configuration_script_payloads.count).to eq(81) + expect( + configuration_script_source.configuration_script_payloads.where( + :name => "test/utils/docker/httptester/httptester.yml" + ).count + ).to eq(1) + expect(configuration_script_source.authentication.name).to eq('db-github') + expect(credential.reload).to eq(credential) - expect(configuration_script_source_other.name).to eq("Dont touch this") + expect(configuration_script_source_other.name).to eq("Dont touch this") + end + # check if a playbook will be added back in on the second run + configuration_script_source.configuration_script_payloads.where(:name => "test/utils/docker/httptester/httptester.yml").destroy_all end - # check if a playbook will be added back in on the second run - configuration_script_source.configuration_script_payloads.where(:name => "test/utils/docker/httptester/httptester.yml").destroy_all end end end diff --git a/spec/support/ansible_shared/automation_manager/refresh_configuartion_script_source_v2.rb b/spec/support/ansible_shared/automation_manager/refresh_configuartion_script_source_v2.rb index de3f18ba..cbd35c58 100644 --- a/spec/support/ansible_shared/automation_manager/refresh_configuartion_script_source_v2.rb +++ b/spec/support/ansible_shared/automation_manager/refresh_configuartion_script_source_v2.rb @@ -33,36 +33,38 @@ # this is to check if a project will be updated on tower last_project_update = Time.zone.parse("2017-04-26T07:57:08.810Z") - 1.minute - 2.times do - VCR.use_cassette(cassette_path) do - EmsRefresh.refresh([[configuration_script_source.class.to_s, configuration_script_source.id]]) + Spec::Support::VcrHelper.with_cassette_library_dir(ManageIQ::Providers::AnsibleTower::Engine.root.join("spec/vcr_cassettes")) do + 2.times do + VCR.use_cassette(cassette_path) do + EmsRefresh.refresh([[configuration_script_source.class.to_s, configuration_script_source.id]]) - expect(automation_manager.reload.last_refresh_error).to be_nil - expect(automation_manager.configuration_script_sources.count).to eq(2) + expect(automation_manager.reload.last_refresh_error).to be_nil + expect(automation_manager.configuration_script_sources.count).to eq(2) - configuration_script_source.reload - configuration_script_source_other.reload + configuration_script_source.reload + configuration_script_source_other.reload - last_updated = Time.zone.parse(configuration_script_source.provider_object.last_updated) - expect(last_updated).to be >= last_project_update - last_project_update = last_updated + last_updated = Time.zone.parse(configuration_script_source.provider_object.last_updated) + expect(last_updated).to be >= last_project_update + last_project_update = last_updated - expect(configuration_script_source.name).to eq("targeted_refresh") - expect(ConfigurationScriptPayload.count).to eq(60) - expect(ConfigurationScriptPayload.where(:name => '2b_rm')).to be_empty - expect(configuration_script_source.configuration_script_payloads.count).to eq(60) - expect( - configuration_script_source.configuration_script_payloads.where( - :name => "jboss-standalone/demo-aws-launch.yml" - ).count - ).to eq(1) - expect(configuration_script_source.authentication.name).to eq('db-github') - expect(credential.reload).to eq(credential) + expect(configuration_script_source.name).to eq("targeted_refresh") + expect(ConfigurationScriptPayload.count).to eq(60) + expect(ConfigurationScriptPayload.where(:name => '2b_rm')).to be_empty + expect(configuration_script_source.configuration_script_payloads.count).to eq(60) + expect( + configuration_script_source.configuration_script_payloads.where( + :name => "jboss-standalone/demo-aws-launch.yml" + ).count + ).to eq(1) + expect(configuration_script_source.authentication.name).to eq('db-github') + expect(credential.reload).to eq(credential) - expect(configuration_script_source_other.name).to eq("Dont touch this") + expect(configuration_script_source_other.name).to eq("Dont touch this") + end + # check if a playbook will be added back in on the second run + configuration_script_source.configuration_script_payloads.where(:name => "jboss-standalone/demo-aws-launch.yml").destroy_all end - # check if a playbook will be added back in on the second run - configuration_script_source.configuration_script_payloads.where(:name => "jboss-standalone/demo-aws-launch.yml").destroy_all end end end diff --git a/spec/support/ansible_shared/automation_manager/refresher.rb b/spec/support/ansible_shared/automation_manager/refresher.rb index 6609d60e..ff096acc 100644 --- a/spec/support/ansible_shared/automation_manager/refresher.rb +++ b/spec/support/ansible_shared/automation_manager/refresher.rb @@ -80,20 +80,22 @@ it "will perform a full refresh" do expected_counterpart_vm - 2.times do - # to re-record cassettes see comment at the beginning of this file - VCR.use_cassette(cassette_path) do - EmsRefresh.refresh(automation_manager) - expect(automation_manager.reload.last_refresh_error).to be_nil + Spec::Support::VcrHelper.with_cassette_library_dir(ManageIQ::Providers::AnsibleTower::Engine.root.join("spec/vcr_cassettes")) do + 2.times do + # to re-record cassettes see comment at the beginning of this file + VCR.use_cassette(cassette_path) do + EmsRefresh.refresh(automation_manager) + expect(automation_manager.reload.last_refresh_error).to be_nil + end + assert_counts + assert_configured_system + assert_configuration_script_with_nil_survey_spec + assert_configuration_script_with_survey_spec + assert_inventory_root_group + assert_configuration_script_sources + assert_playbooks + assert_credentials end - assert_counts - assert_configured_system - assert_configuration_script_with_nil_survey_spec - assert_configuration_script_with_survey_spec - assert_inventory_root_group - assert_configuration_script_sources - assert_playbooks - assert_credentials end end diff --git a/spec/support/ansible_shared/automation_manager/refresher_v2.rb b/spec/support/ansible_shared/automation_manager/refresher_v2.rb index a9b68cb8..019a3248 100644 --- a/spec/support/ansible_shared/automation_manager/refresher_v2.rb +++ b/spec/support/ansible_shared/automation_manager/refresher_v2.rb @@ -27,20 +27,22 @@ it "will perform a full refresh" do expected_counterpart_vm - 2.times do - VCR.use_cassette("#{cassette_path}_v2") do - EmsRefresh.refresh(automation_manager) - expect(automation_manager.reload.last_refresh_error).to be_nil + Spec::Support::VcrHelper.with_cassette_library_dir(ManageIQ::Providers::AnsibleTower::Engine.root.join("spec/vcr_cassettes")) do + 2.times do + VCR.use_cassette("#{cassette_path}_v2") do + EmsRefresh.refresh(automation_manager) + expect(automation_manager.reload.last_refresh_error).to be_nil + end + + assert_counts + assert_configured_system + assert_configuration_script_with_nil_survey_spec + assert_configuration_script_with_survey_spec + assert_inventory_root_group + assert_configuration_script_sources + assert_playbooks + assert_credentials end - - assert_counts - assert_configured_system - assert_configuration_script_with_nil_survey_spec - assert_configuration_script_with_survey_spec - assert_inventory_root_group - assert_configuration_script_sources - assert_playbooks - assert_credentials end end