vagrant-hostmaster 0.8.0 → 0.8.1

Sign up to get free protection for your applications and to get access to all the features.
data/.gitignore CHANGED
@@ -1,5 +1,8 @@
1
+ .DS_Store
1
2
  *.gem
2
3
  *.rbc
4
+ *.swo
5
+ *.swp
3
6
  .bundle
4
7
  .config
5
8
  .yardoc
data/README.md CHANGED
@@ -4,17 +4,9 @@
4
4
 
5
5
  ## Installation
6
6
 
7
- Add this line to your application's Gemfile:
7
+ Install into vagrant's isolated RubyGems instance using:
8
8
 
9
- gem 'vagrant-hostmaster'
10
-
11
- And then execute:
12
-
13
- $ bundle
14
-
15
- Or install it yourself as:
16
-
17
- $ gem install vagrant-hostmaster
9
+ $ vagrant gem install vagrant-hostmaster
18
10
 
19
11
  ## Usage
20
12
 
data/Rakefile CHANGED
@@ -1,2 +1,11 @@
1
1
  #!/usr/bin/env rake
2
2
  require "bundler/gem_tasks"
3
+ require 'rake/testtask'
4
+
5
+ Rake::TestTask.new do |t|
6
+ t.libs << 'test'
7
+ t.pattern = 'test/**/*_test.rb'
8
+ end
9
+
10
+ desc "Run tests"
11
+ task :default => :test
@@ -1,4 +1,4 @@
1
- require 'vagrant/hostmaster/version'
1
+ require 'vagrant'
2
2
  require 'vagrant/hostmaster/command/base'
3
3
  require 'vagrant/hostmaster/command/root'
4
4
  require 'vagrant/hostmaster/config'
@@ -8,7 +8,6 @@ require 'vagrant/hostmaster/middleware/update'
8
8
 
9
9
  Vagrant.commands.register(:hosts) { Vagrant::Hostmaster::Command::Root }
10
10
  Vagrant.config_keys.register(:hosts) { Vagrant::Hostmaster::Config }
11
- Vagrant.commands.register(:hosts) { Vagrant::Hostmaster::Command::Root }
12
11
 
13
12
  Vagrant.actions[:destroy].insert_after(Vagrant::Action::VM::ProvisionerCleanup, Vagrant::Hostmaster::Middleware::Remove)
14
13
 
@@ -14,8 +14,10 @@ module Vagrant
14
14
  return if !argv
15
15
 
16
16
  with_target_vms(argv) do |vm|
17
- if vm.created?
17
+ if vm.state == :running
18
18
  Hostmaster::VM.new(vm).send sub_command.to_sym
19
+ elsif vm.created?
20
+ vm.ui.info I18n.t("vagrant.commands.common.vm_not_running")
19
21
  else
20
22
  vm.ui.info I18n.t("vagrant.commands.common.vm_not_created")
21
23
  end
@@ -7,7 +7,7 @@ module Vagrant
7
7
  end
8
8
 
9
9
  def call(env)
10
- remove env[:vm]
10
+ remove env[:vm] if env["vm"].created?
11
11
  @app.call(env)
12
12
  end
13
13
 
@@ -8,7 +8,7 @@ module Vagrant
8
8
 
9
9
  def call(env)
10
10
  @app.call(env)
11
- update env[:vm]
11
+ update env[:vm] if env["vm"].state == :running
12
12
  end
13
13
 
14
14
  protected
@@ -1,5 +1,5 @@
1
1
  module Vagrant
2
2
  module Hostmaster
3
- VERSION = "0.8.0"
3
+ VERSION = "0.8.1"
4
4
  end
5
5
  end
@@ -7,6 +7,12 @@ module Vagrant
7
7
 
8
8
  def_delegators :@vm, :channel, :config, :env, :name, :uuid
9
9
 
10
+ class << self
11
+ def hosts_path
12
+ Util::Platform.windows? ? "#{ENV['SYSTEMROOT']}/system32/drivers/etc/hosts" : "/etc/hosts"
13
+ end
14
+ end
15
+
10
16
  def initialize(vm)
11
17
  @vm = vm
12
18
  end
@@ -17,26 +23,29 @@ module Vagrant
17
23
  sudo add_command
18
24
  end
19
25
 
20
- with_other_vms { |vm| channel.sudo vm.add_command(uuid) } if process_guests?(options)
26
+ with_other_vms { |vm| channel.sudo vm.add_command(:uuid => uuid, :hosts_path => hosts_path) } if process_guests?(options)
27
+ end
28
+
29
+ def hosts_path
30
+ # TODO: if windows guests are supported, this will need to be smarter
31
+ "/etc/hosts"
21
32
  end
22
33
 
23
34
  def list(options = {})
24
35
  if process_local?(options)
25
36
  output = `#{list_command}`.chomp
26
- env.ui.info("Local host entry for #{name}...\n#{output}\n\n", :prefix => false) unless output.empty?
37
+ env.ui.info("[local] #{output}\n\n", :prefix => false) unless output.empty?
27
38
  end
28
39
 
29
40
  if process_guests?(options)
30
- entries = []
41
+ entries = ""
31
42
  with_other_vms do |vm|
32
- entry = ""
33
- channel.execute(vm.list_command(uuid), :error_check => false) do |type, data|
34
- entry << data if type == :stdout
43
+ channel.execute(vm.list_command(:uuid => uuid, :hosts_path => hosts_path), :error_check => false) do |type, data|
44
+ entries << data if type == :stdout
35
45
  end
36
- entry.chomp!
37
- entries << entry unless entry.empty?
38
46
  end
39
- env.ui.info("Guest host #{entries.size > 1 ? 'entries' : 'entry'} on #{name}...\n#{entries.join("\n")}\n\n", :prefix => false) unless entries.empty?
47
+ entries = entries.split($/).collect { |entry| "[#{name}] #{entry}" }.join("\n")
48
+ env.ui.info("#{entries}\n\n", :prefix => false) unless entries.empty?
40
49
  end
41
50
  end
42
51
 
@@ -45,7 +54,7 @@ module Vagrant
45
54
  env.ui.info("Removing host entry for #{name} VM. Administrator privileges will be required...") unless options[:quiet]
46
55
  sudo remove_command
47
56
  end
48
- with_other_vms { |vm| channel.sudo vm.remove_command(uuid) } if process_guests?(options)
57
+ with_other_vms { |vm| channel.sudo vm.remove_command(:uuid => uuid, :hosts_path => hosts_path) } if process_guests?(options)
49
58
  end
50
59
 
51
60
  def update(options = {})
@@ -53,12 +62,14 @@ module Vagrant
53
62
  env.ui.info("Updating host entry for #{name} VM. Administrator privileges will be required...") unless options[:quiet]
54
63
  sudo(remove_command) && sudo(add_command)
55
64
  end
56
- with_other_vms { |vm| channel.sudo(vm.remove_command(uuid)) && channel.sudo(vm.add_command(uuid)) } if process_guests?(options)
65
+ with_other_vms { |vm| channel.sudo(vm.remove_command(:uuid => uuid, :hosts_path => hosts_path)) && channel.sudo(vm.add_command(:uuid => uuid, :hosts_path => hosts_path)) } if process_guests?(options)
57
66
  end
58
67
 
59
68
  protected
60
- def add_command(uuid = self.uuid)
61
- %Q(sh -c 'echo "#{host_entry(uuid)}" >>/etc/hosts')
69
+ def add_command(options = {})
70
+ uuid = options[:uuid] || self.uuid
71
+ hosts_path = options[:hosts_path] || self.class.hosts_path
72
+ %Q(sh -c 'echo "#{host_entry(uuid)}" >>#{hosts_path}')
62
73
  end
63
74
 
64
75
  def address
@@ -90,8 +101,10 @@ module Vagrant
90
101
  {:local => true}.merge(options)[:local]
91
102
  end
92
103
 
93
- def list_command(uuid = self.uuid)
94
- %Q(grep '#{signature(uuid)}$' /etc/hosts)
104
+ def list_command(options = {})
105
+ uuid = options[:uuid] || self.uuid
106
+ hosts_path = options[:hosts_path] || self.class.hosts_path
107
+ %Q(grep '#{signature(uuid)}$' #{hosts_path})
95
108
  end
96
109
 
97
110
  def network
@@ -108,8 +121,10 @@ module Vagrant
108
121
  @networks ||= config.vm.networks
109
122
  end
110
123
 
111
- def remove_command(uuid = self.uuid)
112
- %Q(sed -e '/#{signature(uuid)}$/ d' -ibak /etc/hosts)
124
+ def remove_command(options = {})
125
+ uuid = options[:uuid] || self.uuid
126
+ hosts_path = options[:hosts_path] || self.class.hosts_path
127
+ %Q(sed -e '/#{signature(uuid)}$/ d' -ibak #{hosts_path})
113
128
  end
114
129
 
115
130
  def signature(uuid = self.uuid)
@@ -117,7 +132,11 @@ module Vagrant
117
132
  end
118
133
 
119
134
  def sudo(command)
120
- `sudo #{command}`
135
+ if Util::Platform.windows?
136
+ `#{command}`
137
+ else
138
+ `sudo #{command}`
139
+ end
121
140
  end
122
141
 
123
142
  def with_other_vms
@@ -0,0 +1,43 @@
1
+ require 'test/unit'
2
+ require 'mocha'
3
+ require 'vagrant/hostmaster'
4
+ require 'vagrant/hostmaster/command/list'
5
+
6
+ module Vagrant
7
+ module Hostmaster
8
+ module CommandTests
9
+ class ListTest < Test::Unit::TestCase
10
+ include Vagrant::TestHelpers
11
+
12
+ def setup
13
+ @env = vagrant_env
14
+ @vm = @env.vms.values.first
15
+ @vm.stubs(:state).returns(:running)
16
+ @command = Vagrant::Hostmaster::Command::List.new([], @env)
17
+ end
18
+
19
+ def test_not_created
20
+ @vm.stubs(:state).returns(:not_created)
21
+ Vagrant::Hostmaster::Command::List.expects(:new).with([], @env).returns(@command)
22
+ Vagrant::Hostmaster::VM.expects(:new).never
23
+ @env.cli('hosts', 'list')
24
+ end
25
+
26
+ def test_not_running
27
+ @vm.stubs(:state).returns(:poweroff)
28
+ Vagrant::Hostmaster::Command::List.expects(:new).with([], @env).returns(@command)
29
+ Vagrant::Hostmaster::VM.expects(:new).never
30
+ @env.cli('hosts', 'list')
31
+ end
32
+
33
+ def test_list
34
+ Vagrant::Hostmaster::Command::List.stubs(:new).with([], @env).returns(@command)
35
+ vm = Vagrant::Hostmaster::VM.new(@vm)
36
+ Vagrant::Hostmaster::VM.expects(:new).with(@vm).returns(vm)
37
+ vm.expects(:list).with()
38
+ @env.cli('hosts', 'list')
39
+ end
40
+ end
41
+ end
42
+ end
43
+ end
@@ -0,0 +1,43 @@
1
+ require 'test/unit'
2
+ require 'mocha'
3
+ require 'vagrant/hostmaster'
4
+ require 'vagrant/hostmaster/command/remove'
5
+
6
+ module Vagrant
7
+ module Hostmaster
8
+ module CommandTests
9
+ class RemoveTest < Test::Unit::TestCase
10
+ include Vagrant::TestHelpers
11
+
12
+ def setup
13
+ @env = vagrant_env
14
+ @vm = @env.vms.values.first
15
+ @vm.stubs(:state).returns(:running)
16
+ @command = Vagrant::Hostmaster::Command::Remove.new([], @env)
17
+ end
18
+
19
+ def test_not_created
20
+ @vm.stubs(:state).returns(:not_created)
21
+ Vagrant::Hostmaster::Command::Remove.expects(:new).with([], @env).returns(@command)
22
+ Vagrant::Hostmaster::VM.expects(:new).never
23
+ @env.cli('hosts', 'remove')
24
+ end
25
+
26
+ def test_not_running
27
+ @vm.stubs(:state).returns(:poweroff)
28
+ Vagrant::Hostmaster::Command::Remove.expects(:new).with([], @env).returns(@command)
29
+ Vagrant::Hostmaster::VM.expects(:new).never
30
+ @env.cli('hosts', 'remove')
31
+ end
32
+
33
+ def test_remove
34
+ Vagrant::Hostmaster::Command::Remove.stubs(:new).with([], @env).returns(@command)
35
+ vm = Vagrant::Hostmaster::VM.new(@vm)
36
+ Vagrant::Hostmaster::VM.expects(:new).with(@vm).returns(vm)
37
+ vm.expects(:remove).with()
38
+ @env.cli('hosts', 'remove')
39
+ end
40
+ end
41
+ end
42
+ end
43
+ end
@@ -0,0 +1,49 @@
1
+ require 'test/unit'
2
+ require 'mocha'
3
+ require 'vagrant/hostmaster'
4
+ require 'vagrant/hostmaster/command/list'
5
+ require 'vagrant/hostmaster/command/remove'
6
+ require 'vagrant/hostmaster/command/update'
7
+
8
+ module Vagrant
9
+ module Hostmaster
10
+ module CommandTests
11
+ class RootTest < Test::Unit::TestCase
12
+ include Vagrant::TestHelpers
13
+
14
+ def setup
15
+ @env = vagrant_env
16
+ @vm = @env.vms.values.first
17
+ end
18
+
19
+ def test_no_command
20
+ command = Vagrant::Hostmaster::Command::Root.new([], @env)
21
+ Vagrant::Hostmaster::Command::Root.expects(:new).with([], @env).returns(command)
22
+ command.expects(:help)
23
+ @env.cli('hosts')
24
+ end
25
+
26
+ def test_list
27
+ command = Vagrant::Hostmaster::Command::List.new([], @env)
28
+ Vagrant::Hostmaster::Command::List.expects(:new).with([], @env).returns(command)
29
+ command.expects(:execute).with()
30
+ @env.cli('hosts', 'list')
31
+ end
32
+
33
+ def test_remove
34
+ command = Vagrant::Hostmaster::Command::Remove.new([], @env)
35
+ Vagrant::Hostmaster::Command::Remove.expects(:new).with([], @env).returns(command)
36
+ command.expects(:execute).with()
37
+ @env.cli('hosts', 'remove')
38
+ end
39
+
40
+ def test_update
41
+ command = Vagrant::Hostmaster::Command::Update.new([], @env)
42
+ Vagrant::Hostmaster::Command::Update.expects(:new).with([], @env).returns(command)
43
+ command.expects(:execute).with()
44
+ @env.cli('hosts', 'update')
45
+ end
46
+ end
47
+ end
48
+ end
49
+ end
@@ -0,0 +1,43 @@
1
+ require 'test/unit'
2
+ require 'mocha'
3
+ require 'vagrant/hostmaster'
4
+ require 'vagrant/hostmaster/command/update'
5
+
6
+ module Vagrant
7
+ module Hostmaster
8
+ module CommandTests
9
+ class UpdateTest < Test::Unit::TestCase
10
+ include Vagrant::TestHelpers
11
+
12
+ def setup
13
+ @env = vagrant_env
14
+ @vm = @env.vms.values.first
15
+ @vm.stubs(:state).returns(:running)
16
+ @command = Vagrant::Hostmaster::Command::Update.new([], @env)
17
+ end
18
+
19
+ def test_not_created
20
+ @vm.stubs(:state).returns(:not_created)
21
+ Vagrant::Hostmaster::Command::Update.expects(:new).with([], @env).returns(@command)
22
+ Vagrant::Hostmaster::VM.expects(:new).never
23
+ @env.cli('hosts', 'update')
24
+ end
25
+
26
+ def test_not_running
27
+ @vm.stubs(:state).returns(:poweroff)
28
+ Vagrant::Hostmaster::Command::Update.expects(:new).with([], @env).returns(@command)
29
+ Vagrant::Hostmaster::VM.expects(:new).never
30
+ @env.cli('hosts', 'update')
31
+ end
32
+
33
+ def test_update
34
+ Vagrant::Hostmaster::Command::Update.stubs(:new).with([], @env).returns(@command)
35
+ vm = Vagrant::Hostmaster::VM.new(@vm)
36
+ Vagrant::Hostmaster::VM.expects(:new).with(@vm).returns(vm)
37
+ vm.expects(:update).with()
38
+ @env.cli('hosts', 'update')
39
+ end
40
+ end
41
+ end
42
+ end
43
+ end
@@ -0,0 +1,29 @@
1
+ require 'test/unit'
2
+ require "mocha"
3
+ require 'vagrant/hostmaster'
4
+
5
+ module Vagrant
6
+ module Hostmaster
7
+ class ConfigTest < Test::Unit::TestCase
8
+ include Vagrant::TestHelpers
9
+
10
+ def setup
11
+ @config = Vagrant::Hostmaster::Config.new
12
+ @errors = Vagrant::Config::ErrorRecorder.new
13
+ @env = vagrant_env
14
+ end
15
+
16
+ def test_default_valid
17
+ @config.validate(@env, @errors)
18
+ assert @errors.errors.empty?, "Default configuration should not have any errors."
19
+ end
20
+
21
+ def test_valid
22
+ @config.name = 'hostmaster.dev'
23
+ @config.aliases = %w(www.hostmaster.dev)
24
+ @config.validate(@env, @errors)
25
+ assert @errors.errors.empty?, "Configuration should not have any errors."
26
+ end
27
+ end
28
+ end
29
+ end
@@ -0,0 +1,40 @@
1
+ require 'test/unit'
2
+ require "mocha"
3
+ require 'vagrant/hostmaster'
4
+
5
+ module Vagrant
6
+ module Hostmaster
7
+ module MiddlewareTests
8
+ class RemoveTest < Test::Unit::TestCase
9
+ include Vagrant::TestHelpers
10
+
11
+ def setup
12
+ @app, @env = action_env(vagrant_env.vms.values.first.env)
13
+ @env['vm'].stubs(:state).returns(:running)
14
+ @middleware = Vagrant::Hostmaster::Middleware::Remove.new(@app, @env)
15
+ end
16
+
17
+ def test_not_created
18
+ @env['vm'].stubs(:state).returns(:not_created)
19
+ Vagrant::Hostmaster::VM.expects(:new).never
20
+ @middleware.call(@env)
21
+ end
22
+
23
+ def test_not_running
24
+ @env['vm'].stubs(:state).returns(:poweroff)
25
+ vm = Vagrant::Hostmaster::VM.new(@env['vm'])
26
+ Vagrant::Hostmaster::VM.expects(:new).with(@env['vm']).returns(vm)
27
+ vm.expects(:remove).with(:guests => false)
28
+ @middleware.call(@env)
29
+ end
30
+
31
+ def test_remove
32
+ vm = Vagrant::Hostmaster::VM.new(@env['vm'])
33
+ Vagrant::Hostmaster::VM.expects(:new).with(@env['vm']).returns(vm)
34
+ vm.expects(:remove).with(:guests => false)
35
+ @middleware.call(@env)
36
+ end
37
+ end
38
+ end
39
+ end
40
+ end
@@ -0,0 +1,38 @@
1
+ require 'test/unit'
2
+ require "mocha"
3
+ require 'vagrant/hostmaster'
4
+
5
+ module Vagrant
6
+ module Hostmaster
7
+ module MiddlewareTests
8
+ class UpdateTest < Test::Unit::TestCase
9
+ include Vagrant::TestHelpers
10
+
11
+ def setup
12
+ @app, @env = action_env(vagrant_env.vms.values.first.env)
13
+ @env['vm'].stubs(:state).returns(:running)
14
+ @middleware = Vagrant::Hostmaster::Middleware::Update.new(@app, @env)
15
+ end
16
+
17
+ def test_not_created
18
+ @env['vm'].stubs(:state).returns(:not_created)
19
+ Vagrant::Hostmaster::VM.expects(:new).never
20
+ @middleware.call(@env)
21
+ end
22
+
23
+ def test_not_running
24
+ @env['vm'].stubs(:state).returns(:poweroff)
25
+ Vagrant::Hostmaster::VM.expects(:new).never
26
+ @middleware.call(@env)
27
+ end
28
+
29
+ def test_update
30
+ vm = Vagrant::Hostmaster::VM.new(@env['vm'])
31
+ Vagrant::Hostmaster::VM.expects(:new).with(@env['vm']).returns(vm)
32
+ vm.expects(:update).with()
33
+ @middleware.call(@env)
34
+ end
35
+ end
36
+ end
37
+ end
38
+ end
@@ -0,0 +1,88 @@
1
+ require 'test/unit'
2
+ require "mocha"
3
+ require 'vagrant/hostmaster'
4
+ require 'vagrant/hostmaster/test_helpers'
5
+
6
+ module Vagrant
7
+ module Hostmaster
8
+ class MultipleVMTest < Test::Unit::TestCase
9
+ include Vagrant::TestHelpers
10
+ include Vagrant::Hostmaster::TestHelpers
11
+
12
+ def setup
13
+ super
14
+
15
+ # TODO: test both windows and non-windows
16
+ Util::Platform.stubs(:windows?).returns(false)
17
+ @hosts_file = Tempfile.new('hosts')
18
+ hostmaster_box :one, 'one.hostmaster.dev', '10.0.0.100', '11111111-1111-1111-1111-111111111111'
19
+ hostmaster_box :two, 'two.hostmaster.dev', '10.0.0.200', '22222222-2222-2222-2222-222222222222'
20
+ @vms = hostmaster_vms(hostmaster_env)
21
+ end
22
+
23
+ def teardown
24
+ @hosts_file.unlink
25
+ clean_paths
26
+ super
27
+ end
28
+
29
+ def test_add
30
+ Vagrant::Hostmaster::VM.stubs(:hosts_path).returns @hosts_file.path
31
+ @vms.each do |vm|
32
+ hostmaster_boxes.each do |name,box|
33
+ next if name == vm.name
34
+ param = %Q(sh -c 'echo \"#{box[:address]} #{box[:host_name]} # VAGRANT: #{vm.uuid} \(#{name}\)\" >>#{vm.hosts_path}')
35
+ vm.channel.expects(:sudo).with(param)
36
+ end
37
+ vm.add
38
+ end
39
+ assert_local_host_entries_for @vms, @hosts_file
40
+ end
41
+
42
+ def test_list
43
+ Vagrant::Hostmaster::VM.stubs(:hosts_path).returns @hosts_file.path
44
+ write_local_host_entries @hosts_file
45
+ hostmaster_boxes.each do |target_name,target_box|
46
+ address, host_name, uuid, vm = target_box.values_at(:address, :host_name, :uuid, :vm)
47
+ hostmaster_boxes.each do |name,box|
48
+ next if name == target_name
49
+ param = "grep '# VAGRANT: #{uuid} (#{name})$' #{vm.hosts_path}"
50
+ vm.channel.expects(:execute).with(param, :error_check => false)
51
+ end
52
+ output = Vagrant::Hostmaster::UI::Capture.capture { vm.list }
53
+ assert_match /^\[local\] #{address} #{host_name} # VAGRANT: #{uuid} \(#{target_name}\)$/, output
54
+ end
55
+ end
56
+
57
+ def test_remove_local_hosts_entry
58
+ Vagrant::Hostmaster::VM.stubs(:hosts_path).returns @hosts_file.path
59
+ write_local_host_entries @hosts_file
60
+ @vms.each do |vm|
61
+ hostmaster_boxes.each do |name,box|
62
+ next if name == vm.name
63
+ param = %Q(sed -e '/# VAGRANT: #{vm.uuid} (#{name})$/ d' -ibak #{vm.hosts_path})
64
+ vm.channel.expects(:sudo).with(param)
65
+ end
66
+ vm.remove
67
+ end
68
+ assert_no_local_host_entries_for @vms, @hosts_file
69
+ end
70
+
71
+ def test_update_local_hosts_entry
72
+ Vagrant::Hostmaster::VM.stubs(:hosts_path).returns @hosts_file.path
73
+ write_local_host_entries @hosts_file, :one => {:address => '10.10.10.11', :host_name => 'a.hostmaster.dev'}, :two => {:address => '10.10.10.22', :host_name => 'b.hostmaster.dev'}
74
+ @vms.each do |vm|
75
+ hostmaster_boxes.each do |name,box|
76
+ next if name == vm.name
77
+ param = %Q(sed -e '/# VAGRANT: #{vm.uuid} (#{name})$/ d' -ibak #{vm.hosts_path})
78
+ vm.channel.expects(:sudo).with(param).returns(0)
79
+ param = %Q(sh -c 'echo \"#{box[:address]} #{box[:host_name]} # VAGRANT: #{vm.uuid} \(#{name}\)\" >>#{vm.hosts_path}')
80
+ vm.channel.expects(:sudo).with(param).returns(0)
81
+ end
82
+ vm.update
83
+ end
84
+ assert_local_host_entries_for @vms, @hosts_file
85
+ end
86
+ end
87
+ end
88
+ end
@@ -0,0 +1,77 @@
1
+ require 'test/unit'
2
+ require "mocha"
3
+ require 'vagrant/hostmaster'
4
+ require 'vagrant/hostmaster/test_helpers'
5
+
6
+ module Vagrant
7
+ module Hostmaster
8
+ class SimpleVMTest < Test::Unit::TestCase
9
+ include Vagrant::TestHelpers
10
+ include Vagrant::Hostmaster::TestHelpers
11
+
12
+ def setup
13
+ super
14
+
15
+ # TODO: test both windows and non-windows
16
+ Util::Platform.stubs(:windows?).returns(false)
17
+ @hosts_file = Tempfile.new('hosts')
18
+ hostmaster_box :default, 'hostmaster.dev', '123.45.67.89', '01234567-89ab-cdef-fedc-ba9876543210'
19
+ @vms = hostmaster_vms(hostmaster_env)
20
+ end
21
+
22
+ def teardown
23
+ @hosts_file.unlink
24
+ clean_paths
25
+ super
26
+ end
27
+
28
+ def test_hosts_path
29
+ assert_equal '/etc/hosts', Vagrant::Hostmaster::VM.hosts_path
30
+ end
31
+
32
+ def test_hosts_path_on_windows
33
+ Util::Platform.stubs(:windows?).returns(true)
34
+ ENV.stubs(:[]).with('SYSTEMROOT').returns('/windows')
35
+ assert_equal '/windows/system32/drivers/etc/hosts', Vagrant::Hostmaster::VM.hosts_path
36
+ end
37
+
38
+ def test_vm_hosts_path
39
+ assert_equal '/etc/hosts', @vms.first.hosts_path
40
+ end
41
+
42
+ def test_hosts_path_on_windows
43
+ Util::Platform.stubs(:windows?).returns(true)
44
+ assert_equal '/etc/hosts', @vms.first.hosts_path
45
+ end
46
+
47
+ def test_add_local_hosts_entry
48
+ Vagrant::Hostmaster::VM.stubs(:hosts_path).returns @hosts_file.path
49
+ @vms.each { |vm| vm.add }
50
+ assert_local_host_entries_for @vms, @hosts_file
51
+ end
52
+
53
+ def test_list_local_hosts_entry
54
+ Vagrant::Hostmaster::VM.stubs(:hosts_path).returns @hosts_file.path
55
+ write_local_host_entries @hosts_file
56
+ hostmaster_boxes.each do |name,box|
57
+ output = Vagrant::Hostmaster::UI::Capture.capture { box[:vm].list }
58
+ assert_match /^\[local\]\s+#{box[:address]}\s+#{box[:host_name]}\s*# VAGRANT: #{box[:uuid]} \(#{name}\)$/, output
59
+ end
60
+ end
61
+
62
+ def test_remove_local_hosts_entry
63
+ Vagrant::Hostmaster::VM.stubs(:hosts_path).returns @hosts_file.path
64
+ write_local_host_entries @hosts_file
65
+ @vms.each { |vm| vm.remove }
66
+ assert_no_local_host_entries_for @vms, @hosts_file
67
+ end
68
+
69
+ def test_update
70
+ Vagrant::Hostmaster::VM.stubs(:hosts_path).returns @hosts_file.path
71
+ write_local_host_entries @hosts_file, :default => {:address => '10.10.10.10', :host_name => 'www.hostmaster.dev'}
72
+ @vms.each { |vm| vm.update }
73
+ assert_local_host_entries_for @vms, @hosts_file
74
+ end
75
+ end
76
+ end
77
+ end
@@ -0,0 +1,137 @@
1
+ require 'generator'
2
+ require 'vagrant/hostmaster/ui/capture'
3
+
4
+ module Vagrant
5
+ module Hostmaster
6
+ module TestHelpers
7
+ protected
8
+ def assert_local_host_address_of(address, vm, hosts_file, message=nil)
9
+ _wrap_assertion do
10
+ hosts_file.open
11
+ full_message = build_message(message,
12
+ "Expected host address of ? for ?.\n?",
13
+ address,
14
+ vm.uuid,
15
+ hosts_file.read
16
+ )
17
+ hosts_file.rewind
18
+ assert_block(full_message) do
19
+ hosts_file.grep(/# VAGRANT: #{vm.uuid} \(#{vm.name}\)$/).any? do |entry|
20
+ scanner = StringScanner.new(entry.chomp)
21
+ scanner.skip(/\s+/)
22
+ scanner.scan(/[0-9.]+/) == address
23
+ end
24
+ end
25
+ end
26
+ end
27
+
28
+ def assert_local_host_entries_for(vms, hosts_file)
29
+ hostmaster_boxes.each do |name,box|
30
+ assert_local_host_entry_for box[:vm], hosts_file
31
+ assert_local_host_address_of box[:address], box[:vm], hosts_file
32
+ assert_local_host_name_of box[:host_name], box[:vm], hosts_file
33
+ end
34
+ end
35
+
36
+ def assert_local_host_entry_for(vm, hosts_file, message=nil)
37
+ _wrap_assertion do
38
+ hosts_file.open
39
+ full_message = build_message(message,
40
+ "Expected host entry for ? to exist.\n?",
41
+ vm.uuid,
42
+ hosts_file.read
43
+ )
44
+ hosts_file.rewind
45
+ assert_block(full_message) do
46
+ hosts_file.grep(/# VAGRANT: #{vm.uuid} \(#{vm.name}\)$/).any?
47
+ end
48
+ end
49
+ end
50
+
51
+ def assert_local_host_name_of(name, vm, hosts_file, message=nil)
52
+ _wrap_assertion do
53
+ hosts_file.open
54
+ full_message = build_message(message,
55
+ "Expected host name of ? for ?.\n?",
56
+ name,
57
+ vm.uuid,
58
+ hosts_file.read
59
+ )
60
+ hosts_file.rewind
61
+ assert_block(full_message) do
62
+ hosts_file.grep(/# VAGRANT: #{vm.uuid} \(#{vm.name}\)$/).any? do |entry|
63
+ scanner = StringScanner.new(entry.chomp)
64
+ scanner.skip(/\s+/)
65
+ scanner.skip(/[0-9.]+/)
66
+ scanner.skip(/\s+/)
67
+ scanner.scan(/[^\s#]+/) == name
68
+ end
69
+ end
70
+ end
71
+ end
72
+
73
+ def assert_no_local_host_entries_for(vms, hosts_file)
74
+ vms.each do |vm|
75
+ assert_no_local_host_entry_for(vm, hosts_file)
76
+ end
77
+ end
78
+
79
+ def assert_no_local_host_entry_for(vm, hosts_file, message=nil)
80
+ _wrap_assertion do
81
+ hosts_file.open
82
+ full_message = build_message(message,
83
+ "Expected host entry for ? to not exist.\n?",
84
+ vm.uuid,
85
+ hosts_file.read
86
+ )
87
+ hosts_file.rewind
88
+ assert_block(full_message) do
89
+ hosts_file.grep(/# VAGRANT: #{vm.uuid} \(#{vm.name}\)$/).empty?
90
+ end
91
+ end
92
+ end
93
+
94
+ def hostmaster_box(name, host_name, address, uuid)
95
+ hostmaster_boxes[name] = {:name => name, :host_name => host_name, :address => address, :uuid => uuid}
96
+ hostmaster_boxes
97
+ end
98
+
99
+ def hostmaster_boxes
100
+ @boxes ||= {}
101
+ end
102
+
103
+ def hostmaster_config
104
+ hostmaster_boxes.inject("") do |config,(name,box)|
105
+ config << <<-EOF
106
+ config.vm.define :#{name} do |box|
107
+ box.vm.host_name = "#{box[:host_name]}"
108
+ box.vm.network :hostonly, "#{box[:address]}"
109
+ end
110
+ EOF
111
+ end
112
+ end
113
+
114
+ def hostmaster_env
115
+ Vagrant::Environment.new(:cwd => vagrantfile(hostmaster_config), :ui_class => Vagrant::Hostmaster::UI::Capture).load!
116
+ end
117
+
118
+ def hostmaster_vms(env)
119
+ env.vms.values.collect do |vm|
120
+ box = hostmaster_boxes[vm.name]
121
+ vm.stubs(:state).returns(:running)
122
+ vm.stubs(:uuid).returns(box[:uuid])
123
+ box[:vm] = Vagrant::Hostmaster::VM.new(vm)
124
+ end
125
+ end
126
+
127
+ def write_local_host_entries(hosts_file, entries={})
128
+ hostmaster_boxes.each do |name,box|
129
+ entry = box.merge(entries[name] || {})
130
+ hosts_file.puts "#{entry[:address]} #{entry[:host_name]} # VAGRANT: #{entry[:uuid]} (#{entry[:name]})"
131
+ end
132
+ hosts_file.fsync
133
+ hosts_file
134
+ end
135
+ end
136
+ end
137
+ end
@@ -0,0 +1,35 @@
1
+ module Vagrant
2
+ module Hostmaster
3
+ module UI
4
+ class Capture < Vagrant::UI::Silent
5
+ class << self
6
+ def capture
7
+ return @capture unless block_given?
8
+ begin
9
+ current, @capture = @capture, ""
10
+ yield
11
+ result = @capture
12
+ ensure
13
+ @capture = current
14
+ end
15
+ end
16
+
17
+ def capture?
18
+ @capture
19
+ end
20
+ end
21
+ [:info, :warn, :error, :success].each do |method|
22
+ class_eval <<-CODE
23
+ def #{method}(message, *args)
24
+ super(message)
25
+ if self.class.capture?
26
+ self.class.capture << message
27
+ self.class.capture << "\n" unless message[-1,1] == "\n"
28
+ end
29
+ end
30
+ CODE
31
+ end
32
+ end
33
+ end
34
+ end
35
+ end
@@ -15,6 +15,8 @@ Gem::Specification.new do |gem|
15
15
  gem.require_paths = ["lib"]
16
16
  gem.version = Vagrant::Hostmaster::VERSION
17
17
 
18
- gem.add_dependency('vagrant', '~>1.0.3')
19
- gem.add_development_dependency('rake')
18
+ gem.add_dependency 'vagrant', '~>1.0.3'
19
+ gem.add_development_dependency 'bundler'
20
+ gem.add_development_dependency 'mocha', '~>0.12.3'
21
+ gem.add_development_dependency 'rake'
20
22
  end
metadata CHANGED
@@ -5,8 +5,8 @@ version: !ruby/object:Gem::Version
5
5
  segments:
6
6
  - 0
7
7
  - 8
8
- - 0
9
- version: 0.8.0
8
+ - 1
9
+ version: 0.8.1
10
10
  platform: ruby
11
11
  authors:
12
12
  - S. Brent Faulkner
@@ -14,7 +14,7 @@ autorequire:
14
14
  bindir: bin
15
15
  cert_chain: []
16
16
 
17
- date: 2012-08-19 00:00:00 -04:00
17
+ date: 2012-08-29 00:00:00 -04:00
18
18
  default_executable:
19
19
  dependencies:
20
20
  - !ruby/object:Gem::Dependency
@@ -40,9 +40,35 @@ dependencies:
40
40
  segments:
41
41
  - 0
42
42
  version: "0"
43
- name: rake
43
+ name: bundler
44
44
  requirement: *id002
45
45
  prerelease: false
46
+ - !ruby/object:Gem::Dependency
47
+ type: :development
48
+ version_requirements: &id003 !ruby/object:Gem::Requirement
49
+ requirements:
50
+ - - ~>
51
+ - !ruby/object:Gem::Version
52
+ segments:
53
+ - 0
54
+ - 12
55
+ - 3
56
+ version: 0.12.3
57
+ name: mocha
58
+ requirement: *id003
59
+ prerelease: false
60
+ - !ruby/object:Gem::Dependency
61
+ type: :development
62
+ version_requirements: &id004 !ruby/object:Gem::Requirement
63
+ requirements:
64
+ - - ">="
65
+ - !ruby/object:Gem::Version
66
+ segments:
67
+ - 0
68
+ version: "0"
69
+ name: rake
70
+ requirement: *id004
71
+ prerelease: false
46
72
  description: vagrant-hostmaster is a Vagrant plugin to manage /etc/hosts entries on both the host OS and guest VMs.
47
73
  email:
48
74
  - brent.faulkner@mosaic.com
@@ -70,6 +96,17 @@ files:
70
96
  - lib/vagrant/hostmaster/version.rb
71
97
  - lib/vagrant/hostmaster/vm.rb
72
98
  - lib/vagrant_init.rb
99
+ - test/vagrant/hostmaster/command_tests/list_test.rb
100
+ - test/vagrant/hostmaster/command_tests/remove_test.rb
101
+ - test/vagrant/hostmaster/command_tests/root_test.rb
102
+ - test/vagrant/hostmaster/command_tests/update_test.rb
103
+ - test/vagrant/hostmaster/config_test.rb
104
+ - test/vagrant/hostmaster/middleware_tests/remove_test.rb
105
+ - test/vagrant/hostmaster/middleware_tests/update_test.rb
106
+ - test/vagrant/hostmaster/multiple_vm_test.rb
107
+ - test/vagrant/hostmaster/simple_vm_test.rb
108
+ - test/vagrant/hostmaster/test_helpers.rb
109
+ - test/vagrant/hostmaster/ui/capture.rb
73
110
  - vagrant-hostmaster.gemspec
74
111
  has_rdoc: true
75
112
  homepage: https://github.com/mosaicxm/vagrant-hostmaster
@@ -101,5 +138,15 @@ rubygems_version: 1.3.6
101
138
  signing_key:
102
139
  specification_version: 3
103
140
  summary: Vagrant plugin to manage /etc/hosts entries.
104
- test_files: []
105
-
141
+ test_files:
142
+ - test/vagrant/hostmaster/command_tests/list_test.rb
143
+ - test/vagrant/hostmaster/command_tests/remove_test.rb
144
+ - test/vagrant/hostmaster/command_tests/root_test.rb
145
+ - test/vagrant/hostmaster/command_tests/update_test.rb
146
+ - test/vagrant/hostmaster/config_test.rb
147
+ - test/vagrant/hostmaster/middleware_tests/remove_test.rb
148
+ - test/vagrant/hostmaster/middleware_tests/update_test.rb
149
+ - test/vagrant/hostmaster/multiple_vm_test.rb
150
+ - test/vagrant/hostmaster/simple_vm_test.rb
151
+ - test/vagrant/hostmaster/test_helpers.rb
152
+ - test/vagrant/hostmaster/ui/capture.rb