diff --git a/test/unit/plugins/guests/alt/cap/change_host_name_test.rb b/test/unit/plugins/guests/alt/cap/change_host_name_test.rb index 51eb814b6..4f82fc6e6 100644 --- a/test/unit/plugins/guests/alt/cap/change_host_name_test.rb +++ b/test/unit/plugins/guests/alt/cap/change_host_name_test.rb @@ -20,17 +20,96 @@ describe "VagrantPlugins::GuestALT::Cap::ChangeHostName" do describe ".change_host_name" do let(:cap) { caps.get(:change_host_name) } + let(:name) { 'banana-rama.example.com' } + let(:systemd) { true } + let(:hostnamectl) { true } + let(:networkd) { true } + let(:network_manager) { false } - let(:name) { "banana-rama.example.com" } + before do + 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 + it "sets the hostname if not set" 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(/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/) + comm.stub_command("hostname -f | grep '^#{name}$'", exit_code: 0) + end + + 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 "restarts the network" do + 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 + + context "when NetworkManager is in use" do + let(:networkd) { false } + let(:network_manager) { true } + + 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 } + let(:systemd) { true } + + it "restarts the network using systemctl" do + expect(cap).to receive(:restart_each_interface). + with(machine, anything) + cap.change_host_name(machine, name) + end + + it "restarts networking with networking init script" do + expect(cap).to receive(:restart_each_interface). + with(machine, anything) + cap.change_host_name(machine, name) + end + end + + context "when systemd is not in use" do + let(:systemd) { false } + + it "restarts the network using service" do + expect(cap).to receive(:restart_each_interface). + with(machine, anything) + cap.change_host_name(machine, name) + end + + it "restarts the network using ifdown/ifup" do + expect(cap).to receive(:restart_each_interface). + with(machine, anything) + cap.change_host_name(machine, name) + end + end end it "does not change the hostname if already set" do