Skip to content

Commit

Permalink
Extract variable Tower data from tests
Browse files Browse the repository at this point in the history
Some properties of the records expected in a Tower instance are not
entered directly by the user: ids, timestamps, external data… These
are subject to change with every population and cassette re-recording.

In such case manual update of expectations in specs was
required. Collection of this data to a single resource file allows to
change only this single file instead of hunting the right magic
literals in the specs examples.
  • Loading branch information
Glutexo committed May 29, 2018
1 parent 9f93dc5 commit a18a58b
Show file tree
Hide file tree
Showing 8 changed files with 451 additions and 63 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,9 @@
:verify_ssl => false,).tap { |provider| provider.authentications << auth }
end

let(:tower_data) { Spec::Support::TowerDataHelper.tower_data }
let(:user_id) { tower_data['user']['id'] }

subject do
described_class.new(automation_manager)
end
Expand All @@ -21,7 +24,7 @@
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)
provider.connect.api.credentials.create!(:name => 'test_stream', :user => user_id)
polled_event = nil
subject.poll do |event|
expect(event['id']).to eq(last_activity.id + 1)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,27 +14,38 @@
end
let(:manager_class) { manager_class }

let(:tower_data) { Spec::Support::TowerDataHelper.tower_data }

let(:targeted_refresh_id) { tower_data['items']['targeted_refresh']['id'] }
let(:targeted_refresh_last_updated) { tower_data['items']['targeted_refresh']['last_updated'].utc }
let(:targeted_refresh_playbooks) { tower_data['items']['targeted_refresh']['playbooks'] }
let(:nonexistent_repo_id) { tower_data['items']['nonexistent_repo']['id'] }

let(:targeted_refresh_playbook_count) { tower_data['counts']['playbooks']['targeted_refresh'] }

it "will perform a targeted refresh" do
credential = FactoryGirl.create(:"#{ems_type}_scm_credential", :name => '2keep')
automation_manager.credentials << credential
configuration_script_source = FactoryGirl.create(:"#{ems_type}_configuration_script_source",
:authentication => credential,
:manager => automation_manager,
:manager_ref => 472)
:manager_ref => targeted_refresh_id)
configuration_script_source.configuration_script_payloads.create!(:manager_ref => '2b_rm', :name => '2b_rm')
configuration_script_source_other = FactoryGirl.create(:"#{ems_type}_configuration_script_source",
:manager_ref => 5,
:manager_ref => nonexistent_repo_id,
:manager => automation_manager,
:name => 'Dont touch this')

# When re-recording the cassetes, comment this to default to normal poll sleep time
stub_const("ManageIQ::Providers::AnsibleTower::Shared::AutomationManager::ConfigurationScriptSource::REFRESH_ON_TOWER_SLEEP", 0.seconds)

# 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
last_project_update = targeted_refresh_last_updated

Spec::Support::VcrHelper.with_cassette_library_dir(ManageIQ::Providers::AnsibleTower::Engine.root.join("spec/vcr_cassettes")) do
2.times do
configuration_script_payloads = configuration_script_source.configuration_script_payloads

VCR.use_cassette(cassette_path) do
EmsRefresh.refresh([[configuration_script_source.class.to_s, configuration_script_source.id]])

Expand All @@ -44,27 +55,27 @@
configuration_script_source.reload
configuration_script_source_other.reload

last_updated = Time.zone.parse(configuration_script_source.provider_object.last_updated)
last_updated = Time.parse(configuration_script_source.provider_object.last_updated).utc
expect(last_updated).to be >= last_project_update
last_project_update = last_updated

expect(configuration_script_source.name).to eq("targeted_refresh")
expect(configuration_script_source.last_updated_on).to eq(last_updated)
expect(ConfigurationScriptPayload.count).to eq(81)
expect(ConfigurationScriptPayload.count).to eq(targeted_refresh_playbook_count)
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_payloads.count).to eq(targeted_refresh_playbook_count)
targeted_refresh_playbooks.each do |playbook|
expect(configuration_script_payloads.where(:name => playbook).count).to eq(1)
end

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")
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
# check if playbooks will be added back in on the second run
configuration_script_payloads.destroy_all
end
end
end
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,27 +14,38 @@
end
let(:manager_class) { manager_class }

let(:tower_data) { Spec::Support::TowerDataHelper.tower_data_v2 }

let(:targeted_refresh_id) { tower_data['items']['targeted_refresh']['id'] }
let(:targeted_refresh_last_updated) { tower_data['items']['targeted_refresh']['last_updated'].utc }
let(:targeted_refresh_playbooks) { tower_data['items']['targeted_refresh']['playbooks'] }
let(:nonexistent_repo_id) { tower_data['items']['nonexistent_repo']['id'] }

let(:targeted_refresh_playbook_count) { tower_data['counts']['playbooks']['targeted_refresh'] }

it "will perform a targeted refresh" do
credential = FactoryGirl.create(:"#{ems_type}_scm_credential", :name => '2keep')
automation_manager.credentials << credential
configuration_script_source = FactoryGirl.create(:"#{ems_type}_configuration_script_source",
:authentication => credential,
:manager => automation_manager,
:manager_ref => 437)
:manager_ref => targeted_refresh_id)
configuration_script_source.configuration_script_payloads.create!(:manager_ref => '2b_rm', :name => '2b_rm')
configuration_script_source_other = FactoryGirl.create(:"#{ems_type}_configuration_script_source",
:manager_ref => 5,
:manager_ref => nonexistent_repo_id,
:manager => automation_manager,
:name => 'Dont touch this')

# When re-recording the cassetes, comment this to default to normal poll sleep time
stub_const("ManageIQ::Providers::AnsibleTower::Shared::AutomationManager::ConfigurationScriptSource::REFRESH_ON_TOWER_SLEEP", 0.seconds)

# 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
last_project_update = targeted_refresh_last_updated

Spec::Support::VcrHelper.with_cassette_library_dir(ManageIQ::Providers::AnsibleTower::Engine.root.join("spec/vcr_cassettes")) do
2.times do
configuration_script_payloads = configuration_script_source.configuration_script_payloads

VCR.use_cassette(cassette_path) do
EmsRefresh.refresh([[configuration_script_source.class.to_s, configuration_script_source.id]])

Expand All @@ -44,26 +55,27 @@
configuration_script_source.reload
configuration_script_source_other.reload

last_updated = Time.zone.parse(configuration_script_source.provider_object.last_updated)
last_updated = Time.parse(configuration_script_source.provider_object.last_updated).utc
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.count).to eq(targeted_refresh_playbook_count)
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_payloads.count).to eq(targeted_refresh_playbook_count)

targeted_refresh_playbooks.each do |playbook|
expect(configuration_script_payloads.where(:name => playbook).count).to eq(1)
end

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")
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
# check if playbooks will be added back in on the second run
configuration_script_payloads.destroy_all
end
end
end
Expand Down
59 changes: 41 additions & 18 deletions spec/support/ansible_shared/automation_manager/refresher.rb
Original file line number Diff line number Diff line change
Expand Up @@ -55,14 +55,34 @@
end
let(:manager_class) { manager_class }

let(:tower_data) { Spec::Support::TowerDataHelper.tower_data }

let(:api_version) { tower_data['config']['version'] }

let(:host_count) { tower_data['counts']['hosts']['total'] }
let(:job_template_count) { tower_data['counts']['job_templates']['total'] }
let(:inventory_count) { tower_data['counts']['inventories']['total'] }
let(:project_count) { tower_data['counts']['projects']['total'] }
let(:playbook_count) { tower_data['counts']['playbooks']['total'] }
let(:credential_count) { tower_data['counts']['credentials']['total'] }

let(:hello_inventory_id) { tower_data['items']['hello_inventory']['id'] }
# let(:hello_repo_id) { tower_data['items']['hello_repo']['id'] }
let(:hello_repo_playbooks) { tower_data['items']['hello_repo']['playbooks'] }
let(:hello_repo_playbook_count) { tower_data['counts']['playbooks']['hello_repo'] }
let(:hello_repo_status) { tower_data['items']['hello_repo']['status'] }
let(:hello_template_id) { tower_data['items']['hello_template']['id'] }
let(:hello_template_with_survey_id) { tower_data['items']['hello_template_with_survey']['id'] }
let(:hello_vm_id) { tower_data['items']['hello_vm']['id'] }

it ".ems_type" do
expect(described_class.ems_type).to eq(ems_type)
end

it "will remove all objects if an empty collection is returned by tower" do
mock_api = double
mock_collection = double(:all => [])
allow(mock_api).to receive(:version).and_return('3.0')
allow(mock_api).to receive(:version).and_return(api_version)
allow(mock_api).to receive_messages(
:inventories => mock_collection,
:hosts => mock_collection,
Expand Down Expand Up @@ -101,13 +121,13 @@

def assert_counts
expect(Provider.count).to eq(1)
expect(automation_manager).to have_attributes(:api_version => "3.0.1")
expect(automation_manager.configured_systems.count).to eq(131)
expect(automation_manager.configuration_scripts.count).to eq(120)
expect(automation_manager.inventory_groups.count).to eq(30)
expect(automation_manager.configuration_script_sources.count).to eq(34)
expect(automation_manager.configuration_script_payloads.count).to eq(2721)
expect(automation_manager.credentials.count).to eq(62)
expect(automation_manager).to have_attributes(:api_version => api_version)
expect(automation_manager.configured_systems.count).to eq(host_count)
expect(automation_manager.configuration_scripts.count).to eq(job_template_count)
expect(automation_manager.inventory_groups.count).to eq(inventory_count)
expect(automation_manager.configuration_script_sources.count).to eq(project_count)
expect(automation_manager.configuration_script_payloads.count).to eq(playbook_count)
expect(automation_manager.credentials.count).to eq(credential_count)
end

def assert_credentials
Expand Down Expand Up @@ -174,9 +194,12 @@ def assert_credentials
end

def assert_playbooks
expect(expected_configuration_script_source.configuration_script_payloads.first).to be_an_instance_of(manager_class::Playbook)
expect(expected_configuration_script_source.configuration_script_payloads.count).to eq(61)
expect(expected_configuration_script_source.configuration_script_payloads.map(&:name)).to include('jboss-standalone/site.yml')
configuration_script_payloads = expected_configuration_script_source.configuration_script_payloads
expect(configuration_script_payloads.count).to eq(hello_repo_playbook_count)
configuration_script_payloads.each do |payload|
expect(payload).to be_an_instance_of(manager_class::Playbook)
end
expect(configuration_script_payloads.map(&:name).sort).to eq(hello_repo_playbooks.sort)
end

def assert_configuration_script_sources
Expand All @@ -190,7 +213,7 @@ def assert_configuration_script_sources
:scm_clean => false,
:scm_delete_on_update => false,
:scm_update_on_launch => false,
:status => 'successful'
:status => hello_repo_status
)
expect(expected_configuration_script_source.authentication.name).to eq('hello_scm_cred')
end
Expand All @@ -199,7 +222,7 @@ def assert_configured_system
expect(expected_configured_system).to have_attributes(
:type => manager_class::ConfiguredSystem.name,
:hostname => "hello_vm",
:manager_ref => "252",
:manager_ref => hello_vm_id.to_s,
:virtual_instance_ref => "4233080d-7467-de61-76c9-c8307b6e4830",
)
expect(expected_configured_system.counterpart).to eq(expected_counterpart_vm)
Expand All @@ -210,21 +233,21 @@ def assert_configuration_script_with_nil_survey_spec
expect(expected_configuration_script).to have_attributes(
:name => "hello_template",
:description => "test job",
:manager_ref => "604",
:manager_ref => hello_template_id.to_s,
:survey_spec => {},
:variables => {},
)
# expect(expected_configuration_script.inventory_root_group).to have_attributes(:ems_ref => "1")
# expect(expected_configuration_script.inventory_root_group).to have_attributes(:ems_ref => hello_inventory_id.to_s)
expect(expected_configuration_script.parent.name).to eq('hello_world.yml')
# expect(expected_configuration_script.parent.configuration_script_source.manager_ref).to eq('37')
# expect(expected_configuration_script.parent.configuration_script_source.manager_ref).to eq(hello_repo_id.to_s)
end

def assert_configuration_script_with_survey_spec
system = automation_manager.configuration_scripts.where(:name => "hello_template_with_survey").first
expect(system).to have_attributes(
:name => "hello_template_with_survey",
:description => "test job with survey spec",
:manager_ref => "605",
:manager_ref => hello_template_with_survey_id.to_s,
:variables => {}
)
survey = system.survey_spec
Expand All @@ -235,7 +258,7 @@ def assert_configuration_script_with_survey_spec
def assert_inventory_root_group
expect(expected_inventory_root_group).to have_attributes(
:name => "hello_inventory",
:ems_ref => "115",
:ems_ref => hello_inventory_id.to_s,
:type => "ManageIQ::Providers::AutomationManager::InventoryRootGroup",
)
end
Expand Down
Loading

0 comments on commit a18a58b

Please sign in to comment.