vagrant-proxmox 0.0.2

Sign up to get free protection for your applications and to get access to all the features.
Files changed (57) hide show
  1. checksums.yaml +7 -0
  2. data/lib/sanity_checks.rb +11 -0
  3. data/lib/vagrant-proxmox/action/cleanup_after_destroy.rb +20 -0
  4. data/lib/vagrant-proxmox/action/connect_proxmox.rb +33 -0
  5. data/lib/vagrant-proxmox/action/create_vm.rb +63 -0
  6. data/lib/vagrant-proxmox/action/destroy_vm.rb +29 -0
  7. data/lib/vagrant-proxmox/action/get_node_list.rb +24 -0
  8. data/lib/vagrant-proxmox/action/is_created.rb +20 -0
  9. data/lib/vagrant-proxmox/action/is_stopped.rb +20 -0
  10. data/lib/vagrant-proxmox/action/message_already_running.rb +20 -0
  11. data/lib/vagrant-proxmox/action/message_already_stopped.rb +20 -0
  12. data/lib/vagrant-proxmox/action/message_not_created.rb +20 -0
  13. data/lib/vagrant-proxmox/action/proxmox_action.rb +47 -0
  14. data/lib/vagrant-proxmox/action/read_ssh_info.rb +22 -0
  15. data/lib/vagrant-proxmox/action/read_state.rb +37 -0
  16. data/lib/vagrant-proxmox/action/shutdown_vm.rb +31 -0
  17. data/lib/vagrant-proxmox/action/start_vm.rb +40 -0
  18. data/lib/vagrant-proxmox/action/stop_vm.rb +31 -0
  19. data/lib/vagrant-proxmox/action/sync_folders.rb +52 -0
  20. data/lib/vagrant-proxmox/action.rb +159 -0
  21. data/lib/vagrant-proxmox/config.rb +81 -0
  22. data/lib/vagrant-proxmox/errors.rb +39 -0
  23. data/lib/vagrant-proxmox/plugin.rb +75 -0
  24. data/lib/vagrant-proxmox/provider.rb +51 -0
  25. data/lib/vagrant-proxmox/version.rb +7 -0
  26. data/lib/vagrant-proxmox.rb +22 -0
  27. data/locales/en.yml +66 -0
  28. data/spec/actions/cleanup_after_destroy_action_spec.rb +22 -0
  29. data/spec/actions/connect_proxmox_action_spec.rb +49 -0
  30. data/spec/actions/create_vm_action_spec.rb +167 -0
  31. data/spec/actions/destroy_vm_action_spec.rb +37 -0
  32. data/spec/actions/get_node_list_action_spec.rb +25 -0
  33. data/spec/actions/is_created_action_spec.rb +35 -0
  34. data/spec/actions/is_stopped_action_spec.rb +34 -0
  35. data/spec/actions/message_already_running_action_spec.rb +23 -0
  36. data/spec/actions/message_already_stopped_action_spec.rb +23 -0
  37. data/spec/actions/message_not_created_action_spec.rb +23 -0
  38. data/spec/actions/proxmox_action_shared.rb +74 -0
  39. data/spec/actions/read_ssh_info_action_spec.rb +32 -0
  40. data/spec/actions/read_state_action_spec.rb +54 -0
  41. data/spec/actions/shutdown_vm_action_spec.rb +37 -0
  42. data/spec/actions/start_vm_action_spec.rb +47 -0
  43. data/spec/actions/stop_vm_action_spec.rb +37 -0
  44. data/spec/actions/sync_folders_action_spec.rb +51 -0
  45. data/spec/commands/destroy_command_spec.rb +68 -0
  46. data/spec/commands/halt_command_spec.rb +46 -0
  47. data/spec/commands/provision_command_spec.rb +33 -0
  48. data/spec/commands/ssh_command_spec.rb +31 -0
  49. data/spec/commands/ssh_run_command_spec.rb +32 -0
  50. data/spec/commands/status_command_spec.rb +19 -0
  51. data/spec/commands/up_command_spec.rb +55 -0
  52. data/spec/config_spec.rb +71 -0
  53. data/spec/plugin_spec.rb +22 -0
  54. data/spec/provider_spec.rb +25 -0
  55. data/spec/sanity_checks_spec.rb +19 -0
  56. data/spec/spec_helper.rb +128 -0
  57. metadata +242 -0
@@ -0,0 +1,51 @@
1
+ require 'spec_helper'
2
+ require 'actions/proxmox_action_shared'
3
+
4
+ module VagrantPlugins::Proxmox
5
+
6
+ describe Action::SyncFolders do
7
+
8
+ let(:environment) { Vagrant::Environment.new vagrantfile_name: 'dummy_box/Vagrantfile' }
9
+ let(:env) { {machine: environment.machine(environment.primary_machine_name, :proxmox), ui: double('ui').as_null_object} }
10
+
11
+ subject { described_class.new(-> (_) {}, environment) }
12
+
13
+ describe '#call' do
14
+
15
+ before do
16
+ env[:machine].stub(:ssh_info) { {host: '127.0.0.1', port: 22, username: 'vagrant', private_key_path: 'key'} }
17
+ env[:machine].communicate.stub :sudo
18
+ Vagrant::Util::Subprocess.stub(:execute).and_return Vagrant::Util::Subprocess::Result.new 0, nil, nil
19
+ end
20
+
21
+ it_behaves_like 'a proxmox action call'
22
+
23
+ it 'should print a message to the user interface' do
24
+ expect(env[:ui]).to receive(:info).with("Rsyncing folder: #{Dir.pwd}/ => /vagrant")
25
+ subject.call env
26
+ end
27
+
28
+ it 'should create a directory on the vm with the predefined ownership' do
29
+ expect(env[:machine].communicate).to receive(:sudo).with("mkdir -p '/vagrant'")
30
+ expect(env[:machine].communicate).to receive(:sudo).with("chown vagrant '/vagrant'")
31
+ subject.call env
32
+ end
33
+
34
+ it 'should rsync the directory with the vm' do
35
+ expect(Vagrant::Util::Subprocess).to receive(:execute).with(
36
+ 'rsync', '--verbose', '--archive', '--compress', '--delete',
37
+ '-e', "ssh -p 22 -i 'key' -o StrictHostKeyChecking=no -o UserKnownHostsFile=/dev/null",
38
+ "#{Dir.pwd}/", 'vagrant@127.0.0.1:/vagrant')
39
+ subject.call env
40
+ end
41
+
42
+ it 'should raise an error if the rsync fails' do
43
+ allow(Vagrant::Util::Subprocess).to receive(:execute).and_return(Vagrant::Util::Subprocess::Result.new 1, nil, nil)
44
+ expect { subject.call(env) }.to raise_error Errors::RsyncError
45
+ end
46
+
47
+ end
48
+
49
+ end
50
+
51
+ end
@@ -0,0 +1,68 @@
1
+ require 'spec_helper'
2
+
3
+ module VagrantPlugins::Proxmox
4
+
5
+ describe 'Vagrant Proxmox provider destroy command', :need_box do
6
+
7
+ let(:environment) { Vagrant::Environment.new vagrantfile_name: 'dummy_box/Vagrantfile' }
8
+ let(:env) { {machine: environment.machine(environment.primary_machine_name, :proxmox)} }
9
+ let(:ui) { double('ui').as_null_object }
10
+
11
+ context 'the vm is not created on the proxmox server' do
12
+ it 'should call the appropriate actions to print a ui message that the vm is not created' do
13
+ Action::ConnectProxmox.should be_called { |env| env[:proxmox_ticket] = 'ticket' }
14
+ Action::IsCreated.should be_called { |env| env[:result] = false }
15
+ Action::MessageNotCreated.should be_called
16
+ Action::DestroyVm.should be_ommited
17
+ execute_vagrant_command environment, :destroy
18
+ end
19
+ end
20
+
21
+ context 'the vm exists on the proxmox server' do
22
+
23
+ context 'the destroy command is not confirmed' do
24
+ it 'should call the appropriate actions to print a ui message that the vm will not be destroyed' do
25
+ Action::ConnectProxmox.should be_called { |env| env[:proxmox_ticket] = 'ticket' }
26
+ Action::IsCreated.should be_called { |env| env[:result] = true }
27
+ ::Vagrant::Action::Builtin::DestroyConfirm.should be_called { |env| env[:result] = false }
28
+ ::VagrantPlugins::ProviderVirtualBox::Action::MessageWillNotDestroy.should be_called
29
+ Action::DestroyVm.should be_ommited
30
+ execute_vagrant_command environment, :destroy
31
+ end
32
+ end
33
+
34
+ context 'the destroy command is confirmed' do
35
+ context 'the vm is running' do
36
+ it 'should call the appropriate actions to destroy the vm' do
37
+ Action::ConnectProxmox.should be_called { |env| env[:proxmox_ticket] = 'ticket' }
38
+ Action::IsCreated.should be_called { |env| env[:result] = true }
39
+ ::Vagrant::Action::Builtin::DestroyConfirm.should be_called { |env| env[:result] = true }
40
+ Action::IsStopped.should be_called { |env| env[:result] = false }
41
+ Action::ShutdownVm.should be_called
42
+ Action::DestroyVm.should be_called
43
+ ::Vagrant::Action::Builtin::ProvisionerCleanup.should be_called
44
+ Action::CleanupAfterDestroy.should be_called
45
+ execute_vagrant_command environment, :destroy
46
+ end
47
+ end
48
+
49
+ context 'the vm is stopped' do
50
+ it 'should call the appropriate actions to destroy the vm' do
51
+ Action::ConnectProxmox.should be_called { |env| env[:proxmox_ticket] = 'ticket' }
52
+ Action::IsCreated.should be_called { |env| env[:result] = true }
53
+ Action::DestroyConfirm.should be_called { |env| env[:result] = true }
54
+ Action::IsStopped.should be_called { |env| env[:result] = true }
55
+ Action::DestroyVm.should be_called
56
+ ::Vagrant::Action::Builtin::ProvisionerCleanup.should be_called
57
+ Action::CleanupAfterDestroy.should be_called
58
+ Action::ShutdownVm.should be_ommited
59
+ execute_vagrant_command environment, :destroy
60
+ end
61
+ end
62
+ end
63
+
64
+ end
65
+
66
+ end
67
+
68
+ end
@@ -0,0 +1,46 @@
1
+ require 'spec_helper'
2
+
3
+ module VagrantPlugins::Proxmox
4
+
5
+ describe 'Vagrant Proxmox provider halt command', :need_box do
6
+
7
+ let(:environment) { Vagrant::Environment.new vagrantfile_name: 'dummy_box/Vagrantfile' }
8
+ let(:ui) { double('ui').as_null_object }
9
+
10
+ context 'the vm is not created on the proxmox server' do
11
+ it 'should call the appropriate actions and print a ui message that the vm is not created' do
12
+ Action::ConfigValidate.should be_called
13
+ Action::IsCreated.should be_called { |env| env[:result] = false }
14
+ Action::MessageNotCreated.should be_called
15
+ Action::ConnectProxmox.should be_ommited
16
+ Action::ShutdownVm.should be_ommited
17
+ execute_vagrant_command environment, :halt
18
+ end
19
+ end
20
+
21
+ context 'the vm is stopped' do
22
+ it 'should call the appropriate actions to shut down the vm' do
23
+ Action::ConfigValidate.should be_called
24
+ Action::IsCreated.should be_called { |env| env[:result] = true }
25
+ Action::IsStopped.should be_called { |env| env[:result] = true }
26
+ Action::MessageAlreadyStopped.should be_called
27
+ Action::ConnectProxmox.should be_ommited
28
+ Action::ShutdownVm.should be_ommited
29
+ execute_vagrant_command environment, :halt
30
+ end
31
+ end
32
+
33
+ context 'the vm is running' do
34
+ it 'should call the appropriate actions to shut down the vm' do
35
+ Action::ConfigValidate.should be_called
36
+ Action::IsCreated.should be_called { |env| env[:result] = true }
37
+ Action::IsStopped.should be_called { |env| env[:result] = false }
38
+ Action::ConnectProxmox.should be_called { |env| env[:proxmox_ticket] = 'ticket' }
39
+ Action::ShutdownVm.should be_called
40
+ execute_vagrant_command environment, :halt
41
+ end
42
+ end
43
+
44
+ end
45
+
46
+ end
@@ -0,0 +1,33 @@
1
+ require 'spec_helper'
2
+
3
+ module VagrantPlugins::Proxmox
4
+
5
+ describe 'Vagrant Proxmox provider provision command', :need_box do
6
+
7
+ let(:environment) { Vagrant::Environment.new vagrantfile_name: 'dummy_box/Vagrantfile' }
8
+ let(:ui) { double('ui').as_null_object }
9
+
10
+ context 'the vm is not created on the proxmox server' do
11
+ it 'should call the appropriate actions and print a ui message that the vm is not created' do
12
+ Action::ConfigValidate.should be_called
13
+ Action::IsCreated.should be_called { |env| env[:result] = false }
14
+ Action::MessageNotCreated.should be_called
15
+ Action::Provision.should be_ommited
16
+ Action::SyncFolders.should be_ommited
17
+ execute_vagrant_command environment, :provision
18
+ end
19
+ end
20
+
21
+ context 'the vm exists on the proxmox server' do
22
+ it 'should call the appropriate actions and provision the vm' do
23
+ Action::ConfigValidate.should be_called
24
+ Action::IsCreated.should be_called { |env| env[:result] = true }
25
+ Action::Provision.should be_called
26
+ Action::SyncFolders.should be_called
27
+ execute_vagrant_command environment, :provision
28
+ end
29
+ end
30
+
31
+ end
32
+
33
+ end
@@ -0,0 +1,31 @@
1
+ require 'spec_helper'
2
+
3
+ module VagrantPlugins::Proxmox
4
+
5
+ describe 'Vagrant Proxmox provider ssh command', :need_box do
6
+
7
+ let(:environment) { Vagrant::Environment.new vagrantfile_name: 'dummy_box/Vagrantfile' }
8
+ let(:ui) { double('ui').as_null_object }
9
+
10
+ context 'the vm is not created on the proxmox server' do
11
+ it 'should call the appropriate actions and print a ui message that the vm is not created' do
12
+ Action::IsCreated.should be_called { |env| env[:result] = false }
13
+ Action::MessageNotCreated.should be_called
14
+ Action::SSHExec.should be_ommited
15
+ execute_vagrant_command environment, :ssh
16
+ end
17
+ end
18
+
19
+ context 'the vm exists on the proxmox server' do
20
+
21
+ it 'should call the appropriate actions to open a ssh connection' do
22
+ Action::IsCreated.should be_called { |env| env[:result] = true }
23
+ Action::SSHExec.should be_called
24
+ execute_vagrant_command environment, :ssh
25
+ end
26
+
27
+ end
28
+
29
+ end
30
+
31
+ end
@@ -0,0 +1,32 @@
1
+ require 'spec_helper'
2
+
3
+ module VagrantPlugins::Proxmox
4
+
5
+ describe 'Vagrant Proxmox provider ssh exec command', :need_box do
6
+
7
+ let(:environment) { Vagrant::Environment.new vagrantfile_name: 'dummy_box/Vagrantfile' }
8
+ let(:env) { {machine: environment.machine(environment.primary_machine_name, :proxmox), ui: double('ui').as_null_object} }
9
+ let(:ui) { double('ui').as_null_object }
10
+
11
+ context 'the vm is not created on the proxmox server' do
12
+ it 'should call the appropriate actions and print a ui message that the vm is not created' do
13
+ Action::IsCreated.should be_called { |env| env[:result] = false }
14
+ Action::MessageNotCreated.should be_called
15
+ Action::SSHRun.should be_ommited
16
+ execute_vagrant_command environment, :ssh, '--command', 'foo'
17
+ end
18
+ end
19
+
20
+ context 'the vm exists on the proxmox server' do
21
+ before { env[:machine].stub(:ssh_info) { {host: '127.0.0.1', port: 22, username: 'vagrant', private_key_path: 'key'} } }
22
+
23
+ it 'should call the appropriate actions to execute a command via ssh on the vm' do
24
+ Action::IsCreated.should be_called { |env| env[:result] = true }
25
+ Action::SSHRun.should be_called
26
+ execute_vagrant_command environment, :ssh, '--command', 'foo'
27
+ end
28
+ end
29
+
30
+ end
31
+
32
+ end
@@ -0,0 +1,19 @@
1
+ require 'spec_helper'
2
+
3
+ module VagrantPlugins::Proxmox
4
+
5
+ describe 'Vagrant Proxmox provider status command', :need_box do
6
+
7
+ let(:environment) { Vagrant::Environment.new vagrantfile_name: 'dummy_box/Vagrantfile' }
8
+ let(:ui) { double('ui').as_null_object }
9
+
10
+ specify do
11
+ Action::ConnectProxmox.should be_called { |env| env[:proxmox_ticket] = 'ticket' }
12
+ Action::ReadState.should be_called { |env| env[:machine_state_id] = :stopped }
13
+ expect(ui).to receive(:info).with /stopped \(proxmox\)/, anything
14
+ execute_vagrant_command environment, :status
15
+ end
16
+
17
+ end
18
+
19
+ end
@@ -0,0 +1,55 @@
1
+ require 'spec_helper'
2
+
3
+ module VagrantPlugins::Proxmox
4
+
5
+ describe 'Vagrant Proxmox provider up command' do
6
+
7
+ let(:environment) { Vagrant::Environment.new vagrantfile_name: 'dummy_box/Vagrantfile' }
8
+ let(:ui) { double('ui').as_null_object }
9
+
10
+ before do
11
+ Vagrant::UI::Interface.stub(:new).and_return ui
12
+ end
13
+
14
+ context 'the vm is not yet created' do
15
+ it 'should call the appropriate actions and print a ui message that the vm will be created' do
16
+ Action::ConnectProxmox.should be_called { |env| env[:proxmox_ticket] = 'ticket' }
17
+ Action::GetNodeList.should be_called { |env| env[:proxmox_nodes] = [{node: 'localhost'}] }
18
+ Action::IsCreated.should be_called { |env| env[:result] = false }
19
+ Action::CreateVm.should be_called { |env| env[:machine].id = 'localhost/100' }
20
+ Action::Provision.should be_called
21
+ Action::StartVm.should be_called
22
+ Action::SyncFolders.should be_called
23
+ execute_vagrant_command environment, :up, '--provider=proxmox'
24
+ end
25
+ end
26
+
27
+ context 'the vm is stopped' do
28
+ it 'should call the appropriate actions and print a ui message that the vm will be started' do
29
+ Action::ConnectProxmox.should be_called { |env| env[:proxmox_ticket] = 'ticket' }
30
+ Action::IsCreated.should be_called { |env| env[:result] = true }
31
+ Action::IsStopped.should be_called { |env| env[:result] = true }
32
+ Action::Provision.should be_called
33
+ Action::StartVm.should be_called
34
+ Action::SyncFolders.should be_called
35
+ Action::CreateVm.should be_ommited
36
+ execute_vagrant_command environment, :up, '--provider=proxmox'
37
+ end
38
+ end
39
+
40
+ context 'the vm is already running' do
41
+ it 'should call the appropriate actions and print a ui message that the vm is already running' do
42
+ Action::ConnectProxmox.should be_called { |env| env[:proxmox_ticket] = 'ticket' }
43
+ Action::IsCreated.should be_called { |env| env[:result] = true }
44
+ Action::IsStopped.should be_called { |env| env[:result] = false }
45
+ Action::MessageAlreadyRunning.should be_called
46
+ Action::Provision.should be_ommited
47
+ Action::CreateVm.should be_ommited
48
+ Action::StartVm.should be_ommited
49
+ execute_vagrant_command environment, :up, '--provider=proxmox'
50
+ end
51
+ end
52
+
53
+ end
54
+
55
+ end
@@ -0,0 +1,71 @@
1
+ require 'spec_helper'
2
+ require 'vagrant-proxmox/config'
3
+
4
+ describe VagrantPlugins::Proxmox::Config do
5
+
6
+ describe 'defaults' do
7
+ subject { super().tap { |o| o.finalize! } }
8
+
9
+ its(:endpoint) { should be_nil }
10
+ its(:user_name) { should be_nil }
11
+ its(:password) { should be_nil }
12
+ its(:os_template) { should be_nil }
13
+ its(:vm_id_range) { should == (900..999) }
14
+ its(:vm_name_prefix) { should == 'vagrant_' }
15
+ its(:vm_memory) { should == 512 }
16
+ its(:task_timeout) { should == 60 }
17
+ its(:task_status_check_interval) { should == 2 }
18
+ end
19
+
20
+ describe 'overwriting defaults using a Vagrantfile' do
21
+ let(:environment) { Vagrant::Environment.new vagrantfile_name: 'dummy_box/Vagrantfile' }
22
+ let(:config) { environment.machine(environment.primary_machine_name, :proxmox).config.vm.get_provider_config :proxmox }
23
+
24
+ subject { config }
25
+
26
+ its(:endpoint) { should == 'https://your.proxmox.server/api2/json' }
27
+ its(:user_name) { should == 'vagrant' }
28
+ its(:password) { should == 'password' }
29
+ its(:os_template) { should == 'local:vztmpl/template.tgz' }
30
+ its(:vm_id_range) { should == (900..910) }
31
+ its(:vm_name_prefix) { should == 'vagrant_test_' }
32
+ its(:vm_memory) { should == 256 }
33
+ its(:task_timeout) { should == 30 }
34
+ its(:task_status_check_interval) { should == 1 }
35
+ end
36
+
37
+ describe 'Configuration validation' do
38
+ let(:machine) { double 'machine' }
39
+ let(:environment) { Vagrant::Environment.new vagrantfile_name: 'dummy_box/Vagrantfile' }
40
+ let(:config) { environment.machine(environment.primary_machine_name, :proxmox).config.vm.get_provider_config :proxmox }
41
+
42
+ subject { config }
43
+
44
+ describe 'with a valid configuration' do
45
+ it 'should validate without any error' do
46
+ subject.validate(machine).should == {'Proxmox Provider' => []}
47
+ end
48
+ end
49
+
50
+ describe 'with a missing endpoint' do
51
+ before { subject.endpoint = nil }
52
+ specify { subject.validate(machine).should == {'Proxmox Provider' => ['No endpoint specified.']} }
53
+ end
54
+
55
+ describe 'with a missing user_name' do
56
+ before { subject.user_name = nil }
57
+ specify { subject.validate(machine).should == {'Proxmox Provider' => ['No user_name specified.']} }
58
+ end
59
+
60
+ describe 'with a missing password' do
61
+ before { subject.password = nil }
62
+ specify { subject.validate(machine).should == {'Proxmox Provider' => ['No password specified.']} }
63
+ end
64
+
65
+ describe 'with a missing os_template' do
66
+ before { subject.os_template = nil }
67
+ specify { subject.validate(machine).should == {'Proxmox Provider' => ['No os_template specified.']} }
68
+ end
69
+ end
70
+
71
+ end
@@ -0,0 +1,22 @@
1
+ require 'spec_helper'
2
+
3
+ describe 'Vagrant Proxmox plugin' do
4
+
5
+ specify { Vagrant.should have_plugin 'Proxmox' }
6
+
7
+ describe 'contains a fix for RestClient header unescaping bug' do
8
+ let(:request) { RestClient::Request.new method: :get, url: 'url', cookies: { key: '+%20%21' }}
9
+ it 'should not unescape the cookies' do
10
+ request.make_headers({})['Cookie'].should == 'key=+%20%21'
11
+ end
12
+ end
13
+
14
+ describe 'when vagrant log level is set in ENV[VAGRANT_LOG]' do
15
+ before { ENV['VAGRANT_LOG'] = 'DEBUG' }
16
+ it 'should create a new vagrant proxmox logger ' do
17
+ expect(Log4r::Logger).to receive(:new).with('vagrant_proxmox').and_call_original
18
+ VagrantPlugins::Proxmox::Plugin.setup_logging
19
+ end
20
+ end
21
+
22
+ end
@@ -0,0 +1,25 @@
1
+ require 'spec_helper'
2
+ require 'vagrant-proxmox/provider'
3
+
4
+ module VagrantPlugins::Proxmox
5
+
6
+ describe Provider do
7
+
8
+ let(:environment) { Vagrant::Environment.new vagrantfile_name: 'dummy_box/Vagrantfile' }
9
+ let(:machine) { environment.machine(environment.primary_machine_name, :proxmox) }
10
+ let(:ui) { double('ui').as_null_object }
11
+
12
+ subject { described_class.new(machine) }
13
+
14
+ describe '#ssh_info', :need_box do
15
+ it 'should call the appropriate actions and return the ssh info' do
16
+ Action::ConfigValidate.should be_called
17
+ Action::ConnectProxmox.should be_called { |env| env[:proxmox_ticket] = 'ticket' }
18
+ Action::ReadSSHInfo.should be_called { |env| env[:machine_ssh_info] = 'ssh_info'}
19
+ subject.ssh_info.should == 'ssh_info'
20
+ end
21
+ end
22
+
23
+ end
24
+
25
+ end
@@ -0,0 +1,19 @@
1
+ require 'spec_helper'
2
+
3
+ describe 'Vagrant Proxmox sanity checks' do
4
+
5
+ describe 'when loaded without vagrant installed' do
6
+ before { Object.any_instance.stub(:require) { raise LoadError } }
7
+ it 'should raise an error' do
8
+ expect { load 'sanity_checks.rb' }.to raise_error
9
+ end
10
+ end
11
+
12
+ describe 'when used with an outdated vagrant version' do
13
+ before { stub_const 'Vagrant::VERSION', '1.0.7' }
14
+ it 'should raise an error' do
15
+ expect { load 'sanity_checks.rb' }.to raise_error
16
+ end
17
+ end
18
+
19
+ end
@@ -0,0 +1,128 @@
1
+ if ENV['RUN_WITH_COVERAGE']
2
+ require 'simplecov'
3
+ require 'simplecov-rcov'
4
+ SimpleCov.formatter = SimpleCov::Formatter::MultiFormatter[
5
+ SimpleCov::Formatter::HTMLFormatter,
6
+ SimpleCov::Formatter::RcovFormatter,
7
+ ]
8
+ SimpleCov.start do
9
+ add_filter '/dummy_box/Vagrantfile'
10
+ add_filter '/spec/'
11
+ add_filter '/lib/sanity_checks.rb'
12
+ end
13
+ end
14
+
15
+ require 'vagrant-proxmox'
16
+ require 'active_support/core_ext/string'
17
+
18
+ RSpec.configure do |config|
19
+ config.treat_symbols_as_metadata_keys_with_true_values = true
20
+
21
+ config.before(:suite) do
22
+ begin
23
+ Vagrant::Environment.new.boxes.add 'dummy_box/dummy.box', 'dummy', :proxmox
24
+ rescue Vagrant::Errors::BoxAlreadyExists
25
+
26
+ end
27
+ FileUtils.rm_r '.vagrant', force: true
28
+ end
29
+ config.after(:suite) do
30
+ execute_vagrant_command Vagrant::Environment.new, :box, 'remove', 'dummy'
31
+ end
32
+ config.after(:each) do
33
+ FileUtils.rm_r '.vagrant', force: true
34
+ end
35
+
36
+ config.before(:each, :need_box) do
37
+ up_local_box
38
+ end
39
+ end
40
+
41
+
42
+ def execute_vagrant_command environment, command, *params
43
+ Vagrant.plugin('2').manager.commands[command].new(params, environment).execute
44
+ end
45
+
46
+ # If you want expectations on the action_class use something like:
47
+ #
48
+ # let(:environment) { Vagrant::Environment.new vagrantfile_name: 'dummy_box/Vagrantfile' }
49
+ #
50
+ # mock_action(VagrantPlugins::Proxmox::Action::ConnectProxmox).tap do |action|
51
+ # action.should receive(:perform) {|env| env[:proxmox_ticket] = 'ticket' }
52
+ # end
53
+ #
54
+ def mock_action action_class, env = RSpec::Mocks::Mock.new('env').as_null_object
55
+ action_class.new(nil, env).tap do |action_instance|
56
+ action_instance_name = "@#{action_class.name.demodulize}Action".underscore
57
+ self.instance_variable_set action_instance_name, action_instance
58
+
59
+ action_class.stub(:new) do |app, _|
60
+ action_instance.instance_variable_set '@app', app
61
+ action_instance
62
+ end
63
+ end
64
+ end
65
+
66
+ # If you want to stub your actions (stub their call methods), use something like this:
67
+ #
68
+ # let(:environment) { Vagrant::Environment.new vagrantfile_name: 'dummy_box/Vagrantfile' }
69
+ #
70
+ # stub_action(Action::ConnectProxmox) { |env| env[:proxmox_ticket] = 'ticket' }
71
+ #
72
+ def stub_action action_class
73
+ mock_action(action_class).tap do |action|
74
+ action.stub(:call) do |env|
75
+ yield env if block_given?
76
+ action.instance_variable_get(:@app).call env
77
+ end
78
+ end
79
+ end
80
+
81
+ # If you want to expect your actions to be called (and optionally stub their call methods),
82
+ # use something like this:
83
+ #
84
+ # let(:environment) { Vagrant::Environment.new vagrantfile_name: 'dummy_box/Vagrantfile' }
85
+ #
86
+ # Action::ConnectProxmox.should be_called { |env| env[:proxmox_ticket] = 'ticket' }
87
+ #
88
+ class ActionCallMatcher < RSpec::Matchers::BuiltIn::BaseMatcher
89
+ def initialize action_stub, environment, example_group, kind=:called
90
+ super action_stub
91
+ @environment = environment
92
+ @example_group = example_group
93
+ @kind = kind
94
+ end
95
+
96
+ def match(action_stub, action_class)
97
+ mock_action(action_class).tap do |action|
98
+ case @kind
99
+ when :called
100
+ @example_group.expect(action).to @example_group.receive(:call).at_least(:once) do |env|
101
+ action_stub.call(env) if action_stub
102
+ action.instance_variable_get(:@app).call env
103
+ end
104
+ when :ommited
105
+ action.should_not_receive :call
106
+ end
107
+ end
108
+ end
109
+ end
110
+
111
+ def be_called &action_stub
112
+ ActionCallMatcher.new action_stub, environment, self, :called
113
+ end
114
+
115
+ def be_ommited &action_stub
116
+ ActionCallMatcher.new action_stub, environment, self, :ommited
117
+ end
118
+
119
+ def up_local_box
120
+ Vagrant::UI::Interface.stub(:new).and_return ui
121
+ stub_action(VagrantPlugins::Proxmox::Action::ConnectProxmox)
122
+ stub_action(VagrantPlugins::Proxmox::Action::GetNodeList) { |env| env[:proxmox_nodes] = [{node: 'localhost'}] }
123
+ stub_action(VagrantPlugins::Proxmox::Action::IsCreated) { |env| env[:result] = false }
124
+ stub_action(VagrantPlugins::Proxmox::Action::CreateVm) { |env| env[:machine].id = 'localhost/100' }
125
+ stub_action(VagrantPlugins::Proxmox::Action::StartVm)
126
+ stub_action(VagrantPlugins::Proxmox::Action::SyncFolders)
127
+ execute_vagrant_command environment, :up, '--provider=proxmox'
128
+ end