Skip to content

Commit

Permalink
Merge pull request #18118 from agrare/fix_specs_doing_zone_first
Browse files Browse the repository at this point in the history
Fix specs using Zone.first instead of creating a zone
  • Loading branch information
jrafanie committed Oct 22, 2018
2 parents 1544ad2 + 25677ef commit 533b292
Show file tree
Hide file tree
Showing 8 changed files with 102 additions and 113 deletions.
3 changes: 2 additions & 1 deletion app/models/container_deployment.rb
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,8 @@ def add_deployment_provider(options)
:port => options[:provider_port],
:hostname => options[:provider_hostname],
:ipaddress => options[:provider_ipaddress],
:zone => Zone.first)
:zone => Zone.default_zone
)
provider.save!
provider.update_authentication(:bearer => {:auth_key => options[:auth_key], :save => true})
valid_provider = provider.authentication_check.first
Expand Down
2 changes: 1 addition & 1 deletion spec/factories/ext_management_system.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
sequence(:hostname) { |n| "ems-#{seq_padded_for_sorting(n)}" }
sequence(:ipaddress) { |n| ip_from_seq(n) }
guid { SecureRandom.uuid }
zone { Zone.first || FactoryGirl.create(:zone) }
zone { FactoryGirl.create(:zone) }
storage_profiles { [] }

# Traits
Expand Down
2 changes: 1 addition & 1 deletion spec/factories/provider.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
factory :provider do
sequence(:name) { |n| "provider_#{seq_padded_for_sorting(n)}" }
guid { SecureRandom.uuid }
zone { Zone.first || FactoryGirl.create(:zone) }
zone { FactoryGirl.create(:zone) }
end

factory :provider_foreman, :class => "ManageIQ::Providers::Foreman::Provider", :parent => :provider do
Expand Down
6 changes: 4 additions & 2 deletions spec/models/job_proxy_dispatcher_embedded_scan_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -39,8 +39,9 @@ def assert_at_most_x_scan_jobs_per_y_resource(x_scans, y_resource)
end

context "With a zone, server, ems, hosts, vmware vms" do
let(:zone) { FactoryGirl.create(:zone) }
before do
server = EvmSpecHelper.local_miq_server(:is_master => true, :name => "test_server_main_server")
server = EvmSpecHelper.local_miq_server(:is_master => true, :name => "test_server_main_server", :zone => zone)
(NUM_OF_SERVERS - 1).times do |i|
FactoryGirl.create(:miq_server, :zone => server.zone, :name => "test_server_#{i}")
end
Expand All @@ -56,7 +57,8 @@ def assert_at_most_x_scan_jobs_per_y_resource(x_scans, y_resource)
:hosts => NUM_OF_HOSTS,
:storages => NUM_OF_STORAGES,
:vms => NUM_OF_VMS,
:repo_vms => NUM_OF_REPO_VMS
:repo_vms => NUM_OF_REPO_VMS,
:zone => zone,
)
end

Expand Down
183 changes: 85 additions & 98 deletions spec/models/job_proxy_dispatcher_spec.rb
Original file line number Diff line number Diff line change
@@ -1,25 +1,21 @@
describe JobProxyDispatcher do
include Spec::Support::JobProxyDispatcherHelper

DISPATCH_ONLY = false
if DISPATCH_ONLY
NUM_VMS = 200
NUM_REPO_VMS = 200
NUM_HOSTS = 10
NUM_SERVERS = 10
NUM_STORAGES = 30
else
NUM_VMS = 3
NUM_REPO_VMS = 3
NUM_HOSTS = 3
NUM_SERVERS = 3
NUM_STORAGES = 3
NUM_VMS = 3
NUM_REPO_VMS = 3
NUM_HOSTS = 3
NUM_SERVERS = 3
NUM_STORAGES = 3

let(:zone) { FactoryGirl.create(:zone) }
let(:dispatcher) do
JobProxyDispatcher.new.tap do |dispatcher|
dispatcher.instance_variable_set(:@zone, zone.name)
end
end

let(:dispatcher) { JobProxyDispatcher.new }

before do
@server = EvmSpecHelper.local_miq_server(:name => "test_server_main_server")
@server = EvmSpecHelper.local_miq_server(:name => "test_server_main_server", :zone => zone)
end

context "With a default zone, server, with hosts with a miq_proxy, vmware vms on storages" do
Expand All @@ -36,94 +32,91 @@
allow_any_instance_of(Host).to receive_messages(:missing_credentials? => false)

@hosts, @proxies, @storages, @vms, @repo_vms, @container_providers = build_entities(
:hosts => NUM_HOSTS, :storages => NUM_STORAGES, :vms => NUM_VMS, :repo_vms => NUM_REPO_VMS
:hosts => NUM_HOSTS, :storages => NUM_STORAGES, :vms => NUM_VMS, :repo_vms => NUM_REPO_VMS, :zone => zone
)
@container_images = @container_providers.collect(&:container_images).flatten
end

describe "#dispatch" do
# Don't run these tests if we only want to run dispatch for load testing
unless DISPATCH_ONLY
it "should have a server in default zone" do
expect(@server.zone).not_to be_nil
expect(@server).not_to be_nil
end
it "should have a server in default zone" do
expect(@server.zone).not_to be_nil
expect(@server).not_to be_nil
end

it "should have #{NUM_HOSTS} hosts" do
expect(NUM_HOSTS).to eq(@hosts.length)
end
it "should have #{NUM_HOSTS} hosts" do
expect(NUM_HOSTS).to eq(@hosts.length)
end

it "should have #{NUM_VMS} vms and #{NUM_REPO_VMS} repo vms" do
expect(NUM_VMS).to eq(@vms.length)
end
it "should have #{NUM_VMS} vms and #{NUM_REPO_VMS} repo vms" do
expect(NUM_VMS).to eq(@vms.length)
end

it "should have #{NUM_REPO_VMS} repo vms" do
expect(NUM_REPO_VMS).to eq(@repo_vms.length)
end
it "should have #{NUM_REPO_VMS} repo vms" do
expect(NUM_REPO_VMS).to eq(@repo_vms.length)
end

context "with a vm without a storage" do
before do
# Test a vm without a storage (ie, removed from VC but retained in the VMDB)
allow(MiqVimBrokerWorker).to receive(:available_in_zone?).and_return(true)
@vm = @vms.first
@vm.storage = nil
@vm.save
@vm.raw_scan
end
context "with a vm without a storage" do
before do
# Test a vm without a storage (ie, removed from VC but retained in the VMDB)
allow(MiqVimBrokerWorker).to receive(:available_in_zone?).and_return(true)
@vm = @vms.first
@vm.storage = nil
@vm.save
@vm.raw_scan
end

it "should expect queue_signal and dispatch without errors" do
expect(dispatcher).to receive(:queue_signal)
expect { dispatcher.dispatch }.not_to raise_error
end
it "should expect queue_signal and dispatch without errors" do
expect(dispatcher).to receive(:queue_signal)
expect { dispatcher.dispatch }.not_to raise_error
end
end

context "with a Microsoft vm without a storage" do
before do
# Test a Microsoft vm without a storage
allow(MiqVimBrokerWorker).to receive(:available_in_zone?).and_return(true)
@vm = @vms.first
@vm.storage = nil
@vm.vendor = "microsoft"
@vm.save
@vm.raw_scan
end
context "with a Microsoft vm without a storage" do
before do
# Test a Microsoft vm without a storage
allow(MiqVimBrokerWorker).to receive(:available_in_zone?).and_return(true)
@vm = @vms.first
@vm.storage = nil
@vm.vendor = "microsoft"
@vm.save
@vm.raw_scan
end

it "should run dispatch without calling queue_signal" do
expect(dispatcher).not_to receive(:queue_signal)
end
it "should run dispatch without calling queue_signal" do
expect(dispatcher).not_to receive(:queue_signal)
end
end

context "with a Microsoft vm with a Microsoft storage" do
before do
# Test a Microsoft vm without a storage
allow(MiqVimBrokerWorker).to receive(:available_in_zone?).and_return(true)
@vm = @vms.first
@vm.storage.store_type = "CSVFS"
@vm.vendor = "microsoft"
@vm.save
@vm.raw_scan
end
context "with a Microsoft vm with a Microsoft storage" do
before do
# Test a Microsoft vm without a storage
allow(MiqVimBrokerWorker).to receive(:available_in_zone?).and_return(true)
@vm = @vms.first
@vm.storage.store_type = "CSVFS"
@vm.vendor = "microsoft"
@vm.save
@vm.raw_scan
end

it "should run dispatch without calling queue_signal" do
expect(dispatcher).not_to receive(:queue_signal)
end
it "should run dispatch without calling queue_signal" do
expect(dispatcher).not_to receive(:queue_signal)
end
end

context "with a Microsoft vm with an invalid storage" do
before do
# Test a Microsoft vm without a storage
allow(MiqVimBrokerWorker).to receive(:available_in_zone?).and_return(true)
@vm = @vms.first
@vm.storage.store_type = "XFS"
@vm.vendor = "microsoft"
@vm.save
@vm.raw_scan
end
context "with a Microsoft vm with an invalid storage" do
before do
# Test a Microsoft vm without a storage
allow(MiqVimBrokerWorker).to receive(:available_in_zone?).and_return(true)
@vm = @vms.first
@vm.storage.store_type = "XFS"
@vm.vendor = "microsoft"
@vm.save
@vm.raw_scan
end

it "should expect queue_signal and dispatch without errors" do
expect(dispatcher).to receive(:queue_signal)
expect { dispatcher.dispatch }.not_to raise_error
end
it "should expect queue_signal and dispatch without errors" do
expect(dispatcher).to receive(:queue_signal)
expect { dispatcher.dispatch }.not_to raise_error
end
end

Expand All @@ -142,17 +135,15 @@
end

# Don't run these tests if we only want to run dispatch for load testing
unless DISPATCH_ONLY
if @repo_proxy
it "should have repository host set" do
expect(@repo_vms.first.myhost.id).to eq(@repo_proxy.host_id)
end
if @repo_proxy
it "should have repository host set" do
expect(@repo_vms.first.myhost.id).to eq(@repo_proxy.host_id)
end
end

it "should have #{NUM_VMS + NUM_REPO_VMS} jobs" do
total = NUM_VMS + NUM_REPO_VMS
expect(@jobs.length).to eq(total)
end
it "should have #{NUM_VMS + NUM_REPO_VMS} jobs" do
total = NUM_VMS + NUM_REPO_VMS
expect(@jobs.length).to eq(total)
end

it "should run dispatch" do
Expand Down Expand Up @@ -214,7 +205,6 @@
job = @jobs.find { |j| container_image_classes.include?(j.target_class) }
job.update(:dispatch_status => "active")
provider = ExtManagementSystem.find(job.options[:ems_id])
dispatcher.instance_variable_set(:@zone, MiqServer.my_zone) # memoized during pending_jobs call
expect(dispatcher.active_container_scans_by_zone_and_ems).to eq(
job.zone => {provider.id => 1}
)
Expand Down Expand Up @@ -262,14 +252,12 @@
describe "#active_vm_scans_by_zone" do
it "returns active vm scans for this zone" do
job = @vms.first.raw_scan
dispatcher.instance_variable_set(:@zone, MiqServer.my_zone) # memoized during pending_jobs call
job.update(:dispatch_status => "active")
expect(dispatcher.active_vm_scans_by_zone[job.zone]).to eq(1)
end

it "returns 0 for active vm scan for other zones" do
job = @vms.first.raw_scan
dispatcher.instance_variable_set(:@zone, MiqServer.my_zone) # memoized during pending_jobs call
job.update(:dispatch_status => "active")
expect(dispatcher.active_vm_scans_by_zone['defult']).to eq(0)
end
Expand Down Expand Up @@ -330,7 +318,6 @@
let(:job) { Job.create_job("VmScan", :name => "Hello, World") }

before do
dispatcher.instance_variable_set(:@zone, @server.my_zone)
dispatcher.instance_variable_set(:@active_container_scans_by_zone_and_ems, @server.my_zone => {ems_id => 0})
end

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,16 @@
include Spec::Support::JobProxyDispatcherHelper

context "with two servers on same zone, vix disk enabled for all, " do
let(:zone) { FactoryGirl.create(:zone) }
before do
@server1 = EvmSpecHelper.local_miq_server
@server2 = FactoryGirl.create(:miq_server, :zone => @server1.zone)
@server1 = EvmSpecHelper.local_miq_server(:zone => zone)
@server2 = FactoryGirl.create(:miq_server, :zone => zone)
allow_any_instance_of(MiqServer).to receive_messages(:is_vix_disk? => true)
end

context "with hosts with a miq_proxy, vmware vms on storages" do
before do
@hosts, @proxies, @storages, @vms = build_entities
@hosts, @proxies, @storages, @vms = build_entities(:zone => zone)
@vm = @vms.first
end

Expand Down
6 changes: 2 additions & 4 deletions spec/models/metric/statistic_spec.rb
Original file line number Diff line number Diff line change
@@ -1,13 +1,11 @@
describe Metric::Statistic do
context ".calculate_stat_columns" do
let(:ems_openshift) do
FactoryGirl.create(:ems_openshift, :hostname => 't', :port => 8443, :name => 't',
:zone => Zone.first)
FactoryGirl.create(:ems_openshift, :hostname => 't', :port => 8443, :name => 't')
end

let(:project) do
FactoryGirl.create(:container_project,
:name => "project")
FactoryGirl.create(:container_project, :name => "project")
end

hour = Time.parse(Metric::Helper.nearest_hourly_timestamp(Time.now)).utc
Expand Down
6 changes: 3 additions & 3 deletions spec/support/job_proxy_dispatcher_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ module Spec
module Support
module JobProxyDispatcherHelper
def build_entities(options = {})
options = {:hosts => 2, :storages => 2, :vms => 3, :repo_vms => 3, :container_providers => [1, 2]}.merge(options)
options = {:hosts => 2, :storages => 2, :vms => 3, :repo_vms => 3, :container_providers => [1, 2], :zone => FactoryGirl.create(:zone)}.merge(options)

proxies = []
storages = []
Expand All @@ -11,7 +11,7 @@ def build_entities(options = {})
storages << storage
end

ems = FactoryGirl.create(:ems_vmware, :name => "ems1")
ems = FactoryGirl.create(:ems_vmware, :name => "ems1", :zone => options[:zone])
hosts = []
options[:hosts].times do |i|
host = FactoryGirl.create(:host, :name => "test_host_#{i}", :hostname => "test_host_#{i}")
Expand Down Expand Up @@ -48,7 +48,7 @@ def build_entities(options = {})

container_providers = []
options[:container_providers].each_with_index do |images_count, i|
ems_openshift = FactoryGirl.create(:ems_openshift, :name => "test_container_provider_#{i}")
ems_openshift = FactoryGirl.create(:ems_openshift, :name => "test_container_provider_#{i}", :zone => options[:zone])
container_providers << ems_openshift
container_image_classes = ContainerImage.descendants.append(ContainerImage)
images_count.times do |idx|
Expand Down

0 comments on commit 533b292

Please sign in to comment.