From bc217d5e577457df5ac4ecdfffa17fd0a8c85b18 Mon Sep 17 00:00:00 2001 From: Chris Roberts Date: Thu, 20 Sep 2018 16:46:45 -0700 Subject: [PATCH] Update redhat change host name capability tests for systemd/NetworkManger updates --- .../redhat/cap/change_host_name_test.rb | 101 ++++++++++++------ 1 file changed, 71 insertions(+), 30 deletions(-) diff --git a/test/unit/plugins/guests/redhat/cap/change_host_name_test.rb b/test/unit/plugins/guests/redhat/cap/change_host_name_test.rb index b85802e94..8d0c9ebd4 100644 --- a/test/unit/plugins/guests/redhat/cap/change_host_name_test.rb +++ b/test/unit/plugins/guests/redhat/cap/change_host_name_test.rb @@ -20,50 +20,91 @@ describe "VagrantPlugins::GuestRedHat::Cap::ChangeHostName" do describe ".change_host_name" do let(:cap) { caps.get(:change_host_name) } - let(:name) { "banana-rama.example.com" } + let(:hostname_changed) { true } + let(:systemd) { true } + let(:hostnamectl) { true } + let(:networkd) { true } + let(:network_manager) { false } + + before do + comm.stub_command("hostname -f | grep '^#{name}$'", exit_code: hostname_changed ? 1 : 0) + allow(cap).to receive(:systemd?).and_return(systemd) + allow(cap).to receive(:hostnamectl?).and_return(hostnamectl) + allow(cap).to receive(:systemd_networkd?).and_return(networkd) + allow(cap).to receive(:systemd_controlled?).with(anything, /NetworkManager/).and_return(network_manager) + end it "sets the hostname" do - comm.stub_command("hostname -f | grep '^#{name}$'", exit_code: 1) - cap.change_host_name(machine, name) expect(comm.received_commands[1]).to match(/\/etc\/sysconfig\/network/) expect(comm.received_commands[1]).to match(/\/etc\/sysconfig\/network-scripts\/ifcfg/) - expect(comm.received_commands[1]).to match(/hostnamectl set-hostname --static '#{name}'/) - expect(comm.received_commands[1]).to match(/hostnamectl set-hostname --transient '#{name}'/) - expect(comm.received_commands[1]).to match(/service network restart|systemctl restart NetworkManager.service/) end - it "does not change the hostname if already set" do - comm.stub_command("hostname -f | grep '^#{name}$'", exit_code: 0) - cap.change_host_name(machine, name) - expect(comm.received_commands.size).to eq(1) + context "when hostnamectl is in use" do + let(:hostnamectl) { true } + + it "sets hostname with hostnamectl" do + cap.change_host_name(machine, name) + expect(comm.received_commands[2]).to match(/hostnamectl/) + end + end + + context "when hostnamectl is not in use" do + let(:hostnamectl) { false } + + it "sets hostname with hostname command" do + cap.change_host_name(machine, name) + expect(comm.received_commands[2]).to match(/hostname -F/) + end + end + + context "when host name is already set" do + let(:hostname_changed) { false } + + it "does not change the hostname" do + cap.change_host_name(machine, name) + expect(comm.received_commands.size).to eq(1) + end end context "restarts the network" do - it "uses systemctl and NetworkManager.service" do - comm.stub_command("hostname -f | grep '^#{name}$'", exit_code: 1) - comm.stub_command("test -f /usr/bin/systemctl && systemctl -q is-active NetworkManager.service", exit_code: 0) - cap.change_host_name(machine, name) - expect(comm.received_commands[1]).to match(/systemctl restart NetworkManager.service/) + context "when networkd is in use" do + let(:networkd) { true } + + it "restarts networkd with systemctl" do + cap.change_host_name(machine, name) + expect(comm.received_commands[3]).to match(/systemctl restart systemd-networkd/) + end end - it "uses the service command" do - comm.stub_command("hostname -f | grep '^#{name}$'", exit_code: 1) - comm.stub_command("test -f /usr/bin/systemctl && systemctl -q is-active NetworkManager.service", exit_code: 1) - comm.stub_command("test -f /etc/init.d/network", exit_code: 0) - cap.change_host_name(machine, name) - expect(comm.received_commands[1]).to match(/service network restart/) - end - end + context "when NetworkManager is in use" do + let(:networkd) { false } + let(:network_manager) { true } - context "cannot restart the network" do - it "prints cannot restart message" do - comm.stub_command("hostname -f | grep '^#{name}$'", exit_code: 1) - comm.stub_command("test -f /usr/bin/systemctl && systemctl -q is-active NetworkManager.service", exit_code: 1) - comm.stub_command("test -f /etc/init.d/network", exit_code: 1) - cap.change_host_name(machine, name) - expect(comm.received_commands[1]).to match(/printf "Could not restart the network to set the new hostname!/) + it "restarts NetworkManager with systemctl" do + cap.change_host_name(machine, name) + expect(comm.received_commands[3]).to match(/systemctl restart NetworkManager/) + end + end + + context "when networkd and NetworkManager are not in use" do + let(:networkd) { false } + let(:network_manager) { false } + + it "restarts the network using service" do + cap.change_host_name(machine, name) + expect(comm.received_commands[3]).to match(/service network restart/) + end + end + + context "when systemd is not in use" do + let(:systemd) { false } + + it "restarts the network using service" do + cap.change_host_name(machine, name) + expect(comm.received_commands[3]).to match(/service network restart/) + end end end end