From 5e87435938cba17eaaf1d3798cab2c73823a5597 Mon Sep 17 00:00:00 2001 From: Nick Carboni Date: Fri, 24 Jan 2020 16:09:40 -0500 Subject: [PATCH] Auto-create and auto-delete servers in zones when podified This is required to manage servers when running in containers. Without this change a user would be stuck with a single server and would not be able to make use of the features that zones provide. --- app/models/zone.rb | 15 ++++++++++++ spec/models/zone_spec.rb | 53 ++++++++++++++++++++++++++++++++++++++-- 2 files changed, 66 insertions(+), 2 deletions(-) diff --git a/app/models/zone.rb b/app/models/zone.rb index 9dc9c52a7ee..749b9a828a2 100644 --- a/app/models/zone.rb +++ b/app/models/zone.rb @@ -29,7 +29,9 @@ class Zone < ApplicationRecord has_many :containers, :through => :container_managers virtual_has_many :active_miq_servers, :class_name => "MiqServer" + before_destroy :remove_servers_if_podified before_destroy :check_zone_in_use_on_destroy + after_create :create_server_if_podified include AuthenticationMixin @@ -241,6 +243,19 @@ def ntp_reload_queue protected + def remove_servers_if_podified + return unless MiqEnvironment::Command.is_podified? + + miq_servers.destroy_all + end + + def create_server_if_podified + return unless MiqEnvironment::Command.is_podified? + return if name == "default" || !visible + + miq_servers.create!(:name => name) + end + def check_zone_in_use_on_destroy raise _("cannot delete default zone") if name == "default" raise _("cannot delete maintenance zone") if self == miq_region.maintenance_zone diff --git a/spec/models/zone_spec.rb b/spec/models/zone_spec.rb index 7d9ec7af3bb..0a7bb9086d0 100644 --- a/spec/models/zone_spec.rb +++ b/spec/models/zone_spec.rb @@ -167,8 +167,8 @@ context "#ntp_reload_queue" do it "queues a ntp reload for all active servers in the zone" do - expect(MiqEnvironment::Command).to receive(:is_appliance?).and_return(true) - expect(MiqEnvironment::Command).to receive(:is_container?).and_return(false) + allow(MiqEnvironment::Command).to receive(:is_appliance?).and_return(true) + allow(MiqEnvironment::Command).to receive(:is_container?).and_return(false) zone = FactoryBot.create(:zone) server_1 = FactoryBot.create(:miq_server, :zone => zone) FactoryBot.create(:miq_server, :zone => zone, :status => "stopped") @@ -224,4 +224,53 @@ zone.destroy! expect(MiqQueue.where(:zone => zone.name).count).to eq(0) end + + it "doesn't create a server for the zone when not podified" do + zone = Zone.create!(:name => "my_zone", :description => "some zone") + expect(zone.miq_servers.count).to eq(0) + end + + it "fails to destroy a zone with servers when not podified" do + MiqRegion.seed + zone = Zone.create!(:name => "my_zone", :description => "some zone") + zone.miq_servers.create!(:name => "my_server") + expect { zone.destroy! }.to raise_error(RuntimeError) + end + + context "when podified" do + before do + allow(MiqEnvironment::Command).to receive(:is_podified?).and_return(true) + end + + describe ".create" do + it "automatically creates a server" do + zone = Zone.create!(:name => "my_zone", :description => "some zone") + expect(zone.miq_servers.count).to eq(1) + + server = zone.miq_servers.first + expect(server.name).to eq("my_zone") + end + + it "doesn't create a server for non-visible zones" do + zone = Zone.create!(:name => "my_zone", :description => "some zone", :visible => false) + expect(zone.miq_servers.count).to eq(0) + end + + it "doesn't create a server for the default zone" do + zone = Zone.create!(:name => "default", :description => "Default Zone") + expect(zone.miq_servers.count).to eq(0) + end + end + + + + it ".destroy deletes the server in the zone" do + MiqRegion.seed + zone = Zone.create!(:name => "my_zone", :description => "some zone") + server = zone.miq_servers.first + zone.destroy! + + expect(MiqServer.find_by(:id => server.id)).to be_nil + end + end end