Style tweaks and tests added for changing host name

This commit is contained in:
Mitchell Hashimoto 2011-01-28 17:41:48 -08:00
parent 60ef9e482b
commit 8d55bdcae5
6 changed files with 47 additions and 9 deletions

View File

@ -13,6 +13,7 @@
- New `shell` provisioner which simply uploads and executes a script as - New `shell` provisioner which simply uploads and executes a script as
root on the VM. root on the VM.
- Gentoo host only networking no longer fails if alrady setup. [GH-286] - Gentoo host only networking no longer fails if alrady setup. [GH-286]
- Set the host name of your guest OS with `config.vm.host_name` [GH-273]
## 0.7.0 (January 19, 2011) ## 0.7.0 (January 19, 2011)

View File

@ -2,13 +2,13 @@ module Vagrant
class Action class Action
module VM module VM
class HostName class HostName
def initialize(app, env, options=nil) def initialize(app, env)
@app = app @app = app
env.merge!(options || {})
end end
def call(env) def call(env)
@app.call(env) @app.call(env)
host_name = env["config"].vm.host_name host_name = env["config"].vm.host_name
if !host_name.nil? if !host_name.nil?
env.ui.info I18n.t("vagrant.actions.vm.host_name.setting") env.ui.info I18n.t("vagrant.actions.vm.host_name.setting")
@ -18,4 +18,4 @@ module Vagrant
end end
end end
end end
end end

View File

@ -82,7 +82,6 @@ module Vagrant
def change_host_name(name) def change_host_name(name)
raise BaseError, :_key => :unsupported_host_name raise BaseError, :_key => :unsupported_host_name
end end
end end
end end
end end

View File

@ -24,12 +24,12 @@ module Vagrant
def change_host_name(name) def change_host_name(name)
vm.ssh.execute do |ssh| vm.ssh.execute do |ssh|
host_name_already_set = ssh.test?("sudo hostname | grep '#{name}'") if !ssh.test?("sudo hostname | grep '#{name}'")
ssh.exec!("sudo sed -i 's/.*$/#{name}/' /etc/hostname") unless host_name_already_set ssh.exec!("sudo sed -i 's/.*$/#{name}/' /etc/hostname")
ssh.exec!("sudo service hostname start") unless host_name_already_set ssh.exec!("sudo service hostname start")
end
end end
end end
end end
end end
end end

View File

@ -507,7 +507,9 @@ en:
back the SSH commands necessary to set this up. Please report a bug and this back the SSH commands necessary to set this up. Please report a bug and this
will be fixed for your distro. will be fixed for your distro.
unsupported_host_name: |- unsupported_host_name: |-
Setting host name is currently only supported on Debian, Ubuntu and RedHat Setting host name is currently only supported on Debian, Ubuntu and RedHat.
If you'd like your guest OS to be supported, please open a ticket on the
project.
linux: linux:
attempting_halt: "Attempting graceful shutdown of linux..." attempting_halt: "Attempting graceful shutdown of linux..."
mount_fail: "Failed to mount shared folders. `vboxsf` was not available." mount_fail: "Failed to mount shared folders. `vboxsf` was not available."

View File

@ -0,0 +1,36 @@
require "test_helper"
class HostNameVMActionTest < Test::Unit::TestCase
setup do
@klass = Vagrant::Action::VM::HostName
@app, @env = action_env
@instance = @klass.new(@app, @env)
@vm = mock("vm")
@env["vm"] = @vm
@internal_vm = mock("internal")
@vm.stubs(:vm).returns(@internal_vm)
end
should "not run anything if no host name is set" do
@env["config"].vm.host_name = nil
@env["vm"].expects(:system).never
@app.expects(:call).with(@env).once
@instance.call(@env)
end
should "change host name if set" do
@env["config"].vm.host_name = "foo"
system = mock("system")
@vm.stubs(:system).returns(system)
seq = sequence("host_seq")
@app.expects(:call).with(@env).in_sequence(seq)
system.expects(:change_host_name).with(@env["config"].vm.host_name).in_sequence(seq)
@instance.call(@env)
end
end