Skip to content

Commit

Permalink
Style tweaks and tests added for changing host name
Browse files Browse the repository at this point in the history
  • Loading branch information
mitchellh committed Jan 29, 2011
1 parent 60ef9e4 commit 8d55bdc
Show file tree
Hide file tree
Showing 6 changed files with 47 additions and 9 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Expand Up @@ -13,6 +13,7 @@
- New `shell` provisioner which simply uploads and executes a script as
root on the VM.
- 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)

Expand Down
6 changes: 3 additions & 3 deletions lib/vagrant/action/vm/host_name.rb
Expand Up @@ -2,13 +2,13 @@ module Vagrant
class Action
module VM
class HostName
def initialize(app, env, options=nil)
def initialize(app, env)
@app = app
env.merge!(options || {})
end

def call(env)
@app.call(env)

host_name = env["config"].vm.host_name
if !host_name.nil?
env.ui.info I18n.t("vagrant.actions.vm.host_name.setting")
Expand All @@ -18,4 +18,4 @@ def call(env)
end
end
end
end
end
1 change: 0 additions & 1 deletion lib/vagrant/systems/base.rb
Expand Up @@ -82,7 +82,6 @@ def enable_host_only_network(net_options); end
def change_host_name(name)
raise BaseError, :_key => :unsupported_host_name
end

end
end
end
8 changes: 4 additions & 4 deletions lib/vagrant/systems/debian.rb
Expand Up @@ -24,12 +24,12 @@ def enable_host_only_network(net_options)

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

end
end
end
4 changes: 3 additions & 1 deletion templates/locales/en.yml
Expand Up @@ -507,7 +507,9 @@ en:
back the SSH commands necessary to set this up. Please report a bug and this
will be fixed for your distro.
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:
attempting_halt: "Attempting graceful shutdown of linux..."
mount_fail: "Failed to mount shared folders. `vboxsf` was not available."
Expand Down
36 changes: 36 additions & 0 deletions test/vagrant/action/vm/host_name_test.rb
@@ -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

0 comments on commit 8d55bdc

Please sign in to comment.