pleschev-vagrant-hostmaster 0.8.1

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1 @@
1
+ require 'vagrant/hostmaster'
@@ -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,78 @@
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('C:\Windows')
35
+ Vagrant::Hostmaster::VM.stubs(:expand_path).with('system32/drivers/etc/hosts', 'C:\Windows').returns('C:/Windows/system32/drivers/etc/hosts')
36
+ assert_equal 'C:/Windows/system32/drivers/etc/hosts', Vagrant::Hostmaster::VM.hosts_path
37
+ end
38
+
39
+ def test_vm_hosts_path
40
+ assert_equal '/etc/hosts', @vms.first.hosts_path
41
+ end
42
+
43
+ def test_vm_hosts_path_on_windows
44
+ Util::Platform.stubs(:windows?).returns(true)
45
+ assert_equal '/etc/hosts', @vms.first.hosts_path
46
+ end
47
+
48
+ def test_add_local_hosts_entry
49
+ Vagrant::Hostmaster::VM.stubs(:hosts_path).returns @hosts_file.path
50
+ @vms.each { |vm| vm.add }
51
+ assert_local_host_entries_for @vms, @hosts_file
52
+ end
53
+
54
+ def test_list_local_hosts_entry
55
+ Vagrant::Hostmaster::VM.stubs(:hosts_path).returns @hosts_file.path
56
+ write_local_host_entries @hosts_file
57
+ hostmaster_boxes.each do |name,box|
58
+ output = Vagrant::Hostmaster::UI::Capture.capture { box[:vm].list }
59
+ assert_match /^\[local\]\s+#{box[:address]}\s+#{box[:host_name]}\s*# VAGRANT: #{box[:uuid]} \(#{name}\)$/, output
60
+ end
61
+ end
62
+
63
+ def test_remove_local_hosts_entry
64
+ Vagrant::Hostmaster::VM.stubs(:hosts_path).returns @hosts_file.path
65
+ write_local_host_entries @hosts_file
66
+ @vms.each { |vm| vm.remove }
67
+ assert_no_local_host_entries_for @vms, @hosts_file
68
+ end
69
+
70
+ def test_update
71
+ Vagrant::Hostmaster::VM.stubs(:hosts_path).returns @hosts_file.path
72
+ write_local_host_entries @hosts_file, :default => {:address => '10.10.10.10', :host_name => 'www.hostmaster.dev'}
73
+ @vms.each { |vm| vm.update }
74
+ assert_local_host_entries_for @vms, @hosts_file
75
+ end
76
+ end
77
+ end
78
+ end