vagrant-libvirt 0.5.0 → 0.6.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (97) hide show
  1. checksums.yaml +4 -4
  2. data/README.md +134 -23
  3. data/lib/vagrant-libvirt/action/clean_machine_folder.rb +4 -0
  4. data/lib/vagrant-libvirt/action/create_domain.rb +11 -3
  5. data/lib/vagrant-libvirt/action/create_domain_volume.rb +7 -2
  6. data/lib/vagrant-libvirt/action/create_network_interfaces.rb +8 -2
  7. data/lib/vagrant-libvirt/action/create_networks.rb +12 -8
  8. data/lib/vagrant-libvirt/action/destroy_domain.rb +2 -0
  9. data/lib/vagrant-libvirt/action/destroy_networks.rb +2 -0
  10. data/lib/vagrant-libvirt/action/forward_ports.rb +7 -5
  11. data/lib/vagrant-libvirt/action/halt_domain.rb +4 -34
  12. data/lib/vagrant-libvirt/action/handle_box_image.rb +18 -13
  13. data/lib/vagrant-libvirt/action/handle_storage_pool.rb +7 -1
  14. data/lib/vagrant-libvirt/action/is_created.rb +2 -0
  15. data/lib/vagrant-libvirt/action/is_running.rb +2 -0
  16. data/lib/vagrant-libvirt/action/is_suspended.rb +2 -0
  17. data/lib/vagrant-libvirt/action/message_already_created.rb +2 -0
  18. data/lib/vagrant-libvirt/action/message_not_created.rb +2 -0
  19. data/lib/vagrant-libvirt/action/message_not_running.rb +2 -0
  20. data/lib/vagrant-libvirt/action/message_not_suspended.rb +2 -0
  21. data/lib/vagrant-libvirt/action/message_will_not_destroy.rb +2 -0
  22. data/lib/vagrant-libvirt/action/package_domain.rb +133 -68
  23. data/lib/vagrant-libvirt/action/prepare_nfs_settings.rb +2 -0
  24. data/lib/vagrant-libvirt/action/prepare_nfs_valid_ids.rb +2 -0
  25. data/lib/vagrant-libvirt/action/prune_nfs_exports.rb +2 -0
  26. data/lib/vagrant-libvirt/action/read_mac_addresses.rb +2 -0
  27. data/lib/vagrant-libvirt/action/remove_libvirt_image.rb +2 -0
  28. data/lib/vagrant-libvirt/action/remove_stale_volume.rb +2 -0
  29. data/lib/vagrant-libvirt/action/resume_domain.rb +2 -0
  30. data/lib/vagrant-libvirt/action/set_boot_order.rb +8 -2
  31. data/lib/vagrant-libvirt/action/set_name_of_domain.rb +3 -1
  32. data/lib/vagrant-libvirt/action/share_folders.rb +2 -0
  33. data/lib/vagrant-libvirt/action/shutdown_domain.rb +49 -0
  34. data/lib/vagrant-libvirt/action/start_domain.rb +26 -17
  35. data/lib/vagrant-libvirt/action/suspend_domain.rb +2 -0
  36. data/lib/vagrant-libvirt/action/wait_till_up.rb +2 -0
  37. data/lib/vagrant-libvirt/action.rb +34 -4
  38. data/lib/vagrant-libvirt/cap/mount_9p.rb +2 -0
  39. data/lib/vagrant-libvirt/cap/mount_virtiofs.rb +2 -0
  40. data/lib/vagrant-libvirt/cap/nic_mac_addresses.rb +2 -0
  41. data/lib/vagrant-libvirt/cap/public_address.rb +2 -0
  42. data/lib/vagrant-libvirt/cap/synced_folder_9p.rb +5 -2
  43. data/lib/vagrant-libvirt/cap/synced_folder_virtiofs.rb +5 -2
  44. data/lib/vagrant-libvirt/config.rb +58 -24
  45. data/lib/vagrant-libvirt/driver.rb +67 -12
  46. data/lib/vagrant-libvirt/errors.rb +2 -0
  47. data/lib/vagrant-libvirt/plugin.rb +2 -0
  48. data/lib/vagrant-libvirt/provider.rb +2 -0
  49. data/lib/vagrant-libvirt/templates/domain.xml.erb +4 -2
  50. data/lib/vagrant-libvirt/templates/public_interface.xml.erb +1 -0
  51. data/lib/vagrant-libvirt/util/byte_number.rb +71 -0
  52. data/lib/vagrant-libvirt/util/collection.rb +2 -0
  53. data/lib/vagrant-libvirt/util/erb_template.rb +2 -0
  54. data/lib/vagrant-libvirt/util/error_codes.rb +2 -0
  55. data/lib/vagrant-libvirt/util/network_util.rb +3 -0
  56. data/lib/vagrant-libvirt/util/nfs.rb +2 -0
  57. data/lib/vagrant-libvirt/util/storage_util.rb +1 -0
  58. data/lib/vagrant-libvirt/util/timer.rb +2 -0
  59. data/lib/vagrant-libvirt/util/ui.rb +1 -0
  60. data/lib/vagrant-libvirt/util.rb +2 -0
  61. data/lib/vagrant-libvirt/version +1 -1
  62. data/lib/vagrant-libvirt/version.rb +2 -0
  63. data/lib/vagrant-libvirt.rb +2 -0
  64. data/locales/en.yml +2 -0
  65. data/spec/spec_helper.rb +2 -0
  66. data/spec/support/binding_proc.rb +2 -0
  67. data/spec/support/environment_helper.rb +2 -0
  68. data/spec/support/libvirt_context.rb +2 -0
  69. data/spec/support/matchers/have_file_content.rb +2 -0
  70. data/spec/support/sharedcontext.rb +3 -0
  71. data/spec/support/temporary_dir.rb +12 -0
  72. data/spec/unit/action/clean_machine_folder_spec.rb +16 -4
  73. data/spec/unit/action/create_domain_spec/additional_disks_domain.xml +61 -0
  74. data/spec/unit/action/create_domain_spec/default_domain.xml +55 -0
  75. data/spec/unit/action/create_domain_spec.rb +68 -32
  76. data/spec/unit/action/create_domain_volume_spec/one_disk_in_storage.xml +1 -1
  77. data/spec/unit/action/create_domain_volume_spec/three_disks_in_storage_disk_0.xml +1 -1
  78. data/spec/unit/action/create_domain_volume_spec/three_disks_in_storage_disk_1.xml +1 -1
  79. data/spec/unit/action/create_domain_volume_spec/three_disks_in_storage_disk_2.xml +1 -1
  80. data/spec/unit/action/create_domain_volume_spec.rb +10 -4
  81. data/spec/unit/action/destroy_domain_spec.rb +8 -2
  82. data/spec/unit/action/forward_ports_spec.rb +2 -0
  83. data/spec/unit/action/halt_domain_spec.rb +30 -57
  84. data/spec/unit/action/handle_box_image_spec.rb +104 -24
  85. data/spec/unit/action/package_domain_spec.rb +304 -0
  86. data/spec/unit/action/set_name_of_domain_spec.rb +2 -0
  87. data/spec/unit/action/shutdown_domain_spec.rb +131 -0
  88. data/spec/unit/action/start_domain_spec/existing.xml +62 -0
  89. data/spec/unit/action/start_domain_spec.rb +18 -28
  90. data/spec/unit/action/wait_till_up_spec.rb +2 -0
  91. data/spec/unit/action_spec.rb +96 -0
  92. data/spec/unit/config_spec.rb +56 -3
  93. data/spec/unit/driver_spec.rb +155 -0
  94. data/spec/unit/templates/domain_all_settings.xml +4 -0
  95. data/spec/unit/templates/domain_spec.rb +2 -0
  96. data/spec/unit/util/byte_number_spec.rb +28 -0
  97. metadata +59 -38
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  require "rspec/expectations/version"
2
4
  #
3
5
  # Taken from https://github.com/cucumber/aruba/blob/main/lib/aruba/matchers/file/have_file_content.rb
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  require 'spec_helper'
2
4
 
3
5
  shared_context 'unit' do
@@ -7,6 +9,7 @@ shared_context 'unit' do
7
9
  let(:vagrantfile) do
8
10
  <<-EOF
9
11
  Vagrant.configure('2') do |config|
12
+ config.vm.box = "vagrant-libvirt/test"
10
13
  config.vm.define :test
11
14
  config.vm.provider :libvirt do |libvirt|
12
15
  #{vagrantfile_providerconfig}
@@ -0,0 +1,12 @@
1
+ # frozen_string_literal: true
2
+
3
+ shared_context 'temporary_dir' do
4
+ around do |example|
5
+ Dir.mktmpdir("rspec-") do |dir|
6
+ @temp_dir = dir
7
+ example.run
8
+ end
9
+ end
10
+
11
+ attr_reader :temp_dir
12
+ end
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  require 'spec_helper'
2
4
  require 'support/sharedcontext'
3
5
 
@@ -9,12 +11,18 @@ describe VagrantPlugins::ProviderLibvirt::Action::CleanMachineFolder do
9
11
  include_context 'unit'
10
12
 
11
13
  describe '#call' do
14
+ before do
15
+ FileUtils.touch(File.join(machine.data_dir, "box.meta"))
16
+ end
17
+
12
18
  context 'with default options' do
13
19
  it 'should verbosely remove the folder' do
14
20
  expect(ui).to receive(:info).with('Deleting the machine folder')
15
- expect(FileUtils).to receive(:rm_rf).with(machine.data_dir, {:secure => true})
16
21
 
17
22
  expect(subject.call(env)).to be_nil
23
+
24
+ expect(File.exists?(machine.data_dir)).to eq(true)
25
+ expect(Dir.entries(machine.data_dir)).to match_array([".", ".."])
18
26
  end
19
27
  end
20
28
 
@@ -22,15 +30,17 @@ describe VagrantPlugins::ProviderLibvirt::Action::CleanMachineFolder do
22
30
  before do
23
31
  Dir.mktmpdir do |d|
24
32
  # returns a temporary directory that has been already deleted when running
25
- expect(machine).to receive(:data_dir).and_return(d.to_s).exactly(2).times
33
+ expect(machine).to receive(:data_dir).and_return(d.to_s).exactly(3).times
26
34
  end
27
35
  end
28
36
 
29
37
  it 'should remove the folder' do
30
38
  expect(ui).to receive(:info).with('Deleting the machine folder')
31
- expect(FileUtils).to receive(:rm_rf).with(machine.data_dir, {:secure => true})
32
39
 
33
40
  expect(subject.call(env)).to be_nil
41
+
42
+ expect(File.exists?(machine.data_dir)).to eq(true)
43
+ expect(Dir.entries(machine.data_dir)).to match_array([".", ".."])
34
44
  end
35
45
  end
36
46
 
@@ -39,9 +49,11 @@ describe VagrantPlugins::ProviderLibvirt::Action::CleanMachineFolder do
39
49
 
40
50
  it 'should quietly remove the folder' do
41
51
  expect(ui).to_not receive(:info).with('Deleting the machine folder')
42
- expect(FileUtils).to receive(:rm_rf).with(machine.data_dir, {:secure => true})
43
52
 
44
53
  expect(subject.call(env)).to be_nil
54
+
55
+ expect(File.exists?(machine.data_dir)).to eq(true)
56
+ expect(Dir.entries(machine.data_dir)).to match_array([".", ".."])
45
57
  end
46
58
  end
47
59
  end
@@ -0,0 +1,61 @@
1
+ <domain type='kvm' xmlns:qemu='http://libvirt.org/schemas/domain/qemu/1.0'>
2
+ <name>vagrant-test_default</name>
3
+ <title></title>
4
+ <description>Source: /rootpath/Vagrantfile</description>
5
+ <uuid></uuid>
6
+ <memory>524288</memory>
7
+ <vcpu>1</vcpu>
8
+
9
+
10
+ <cpu mode='host-model'>
11
+ <model fallback='allow'></model>
12
+ </cpu>
13
+
14
+
15
+ <os>
16
+ <type>hvm</type>
17
+ <kernel></kernel>
18
+ <initrd></initrd>
19
+ <cmdline></cmdline>
20
+ </os>
21
+ <features>
22
+ <acpi/>
23
+ <apic/>
24
+ <pae/>
25
+ </features>
26
+ <clock offset='utc'>
27
+ </clock>
28
+ <devices>
29
+ <disk type='file' device='disk'>
30
+ <alias name='ua-box-volume-0'/>
31
+ <driver name='qemu' type='qcow2' cache='default'/>
32
+ <source file='/var/lib/libvirt/images/vagrant-test_default.img'/>
33
+ <target dev='vda' bus='virtio'/>
34
+ </disk>
35
+ <disk type='file' device='disk'>
36
+ <alias name='ua-disk-volume-0'/>
37
+ <driver name='qemu' type='qcow2' cache='default'/>
38
+ <source file='/var/lib/libvirt/images/vagrant-test_default-vdb.qcow2'/>
39
+ <target dev='vdb' bus='virtio'/>
40
+ </disk>
41
+
42
+
43
+ <serial type='pty'>
44
+ <target port='0'/>
45
+ </serial>
46
+ <console type='pty'>
47
+ <target port='0'/>
48
+ </console>
49
+
50
+
51
+ <input type='mouse' bus='ps2'/>
52
+
53
+ <graphics type='vnc' port='-1' autoport='yes' listen='127.0.0.1' keymap='en-us' />
54
+ <video>
55
+ <model type='cirrus' vram='9216' heads='1'/>
56
+ </video>
57
+
58
+
59
+ </devices>
60
+
61
+ </domain>
@@ -0,0 +1,55 @@
1
+ <domain type='kvm' xmlns:qemu='http://libvirt.org/schemas/domain/qemu/1.0'>
2
+ <name>vagrant-test_default</name>
3
+ <title></title>
4
+ <description>Source: /rootpath/Vagrantfile</description>
5
+ <uuid></uuid>
6
+ <memory>524288</memory>
7
+ <vcpu>1</vcpu>
8
+
9
+
10
+ <cpu mode='host-model'>
11
+ <model fallback='allow'></model>
12
+ </cpu>
13
+
14
+
15
+ <os>
16
+ <type>hvm</type>
17
+ <kernel></kernel>
18
+ <initrd></initrd>
19
+ <cmdline></cmdline>
20
+ </os>
21
+ <features>
22
+ <acpi/>
23
+ <apic/>
24
+ <pae/>
25
+ </features>
26
+ <clock offset='utc'>
27
+ </clock>
28
+ <devices>
29
+ <disk type='file' device='disk'>
30
+ <alias name='ua-box-volume-0'/>
31
+ <driver name='qemu' type='qcow2' cache='default'/>
32
+ <source file='/var/lib/libvirt/images/vagrant-test_default.img'/>
33
+ <target dev='vda' bus='virtio'/>
34
+ </disk>
35
+
36
+
37
+ <serial type='pty'>
38
+ <target port='0'/>
39
+ </serial>
40
+ <console type='pty'>
41
+ <target port='0'/>
42
+ </console>
43
+
44
+
45
+ <input type='mouse' bus='ps2'/>
46
+
47
+ <graphics type='vnc' port='-1' autoport='yes' listen='127.0.0.1' keymap='en-us' />
48
+ <video>
49
+ <model type='cirrus' vram='9216' heads='1'/>
50
+ </video>
51
+
52
+
53
+ </devices>
54
+
55
+ </domain>
@@ -1,8 +1,11 @@
1
+ # frozen_string_literal: true
2
+
1
3
  require 'spec_helper'
2
4
  require 'support/sharedcontext'
3
5
  require 'support/libvirt_context'
4
6
 
5
7
  require 'vagrant-libvirt/errors'
8
+ require 'vagrant-libvirt/util/byte_number'
6
9
  require 'vagrant-libvirt/action/create_domain'
7
10
 
8
11
  describe VagrantPlugins::ProviderLibvirt::Action::CreateDomain do
@@ -14,8 +17,10 @@ describe VagrantPlugins::ProviderLibvirt::Action::CreateDomain do
14
17
  let(:libvirt_client) { double('libvirt_client') }
15
18
  let(:servers) { double('servers') }
16
19
  let(:volumes) { double('volumes') }
20
+ let(:domain_volume) { double('domain_volume') }
17
21
 
18
- let(:storage_pool_xml) { File.read(File.join(File.dirname(__FILE__), File.basename(__FILE__, '.rb'), test_file)) }
22
+ let(:domain_xml) { File.read(File.join(File.dirname(__FILE__), File.basename(__FILE__, '.rb'), domain_xml_file)) }
23
+ let(:storage_pool_xml) { File.read(File.join(File.dirname(__FILE__), File.basename(__FILE__, '.rb'), storage_pool_xml_file)) }
19
24
  let(:libvirt_storage_pool) { double('storage_pool') }
20
25
 
21
26
  describe '#call' do
@@ -26,8 +31,15 @@ describe VagrantPlugins::ProviderLibvirt::Action::CreateDomain do
26
31
 
27
32
  allow(connection).to receive(:servers).and_return(servers)
28
33
  allow(connection).to receive(:volumes).and_return(volumes)
34
+ allow(volumes).to receive(:all).and_return([domain_volume])
35
+ allow(domain_volume).to receive(:pool_name).and_return('default')
36
+ allow(domain_volume).to receive(:[]).with('name').and_return('vagrant-test_default.img')
37
+ allow(domain_volume).to receive(:path).and_return('/var/lib/libvirt/images/vagrant-test_default.img')
38
+ allow(machine).to receive_message_chain("box.name") { 'vagrant-libvirt/test' }
29
39
 
30
40
  allow(logger).to receive(:info)
41
+ allow(logger).to receive(:debug)
42
+ allow(ui).to receive(:info)
31
43
 
32
44
  env[:domain_name] = "vagrant-test_default"
33
45
 
@@ -35,7 +47,7 @@ describe VagrantPlugins::ProviderLibvirt::Action::CreateDomain do
35
47
  env[:box_volumes].push({
36
48
  :path=>"/test/box.img",
37
49
  :name=>"test_vagrant_box_image_1.1.1_0.img",
38
- :virtual_size=>5
50
+ :virtual_size=> ByteNumber.new(5),
39
51
  })
40
52
  # should be ignored for system session and used for user session
41
53
  allow(Process).to receive(:uid).and_return(9999)
@@ -43,34 +55,44 @@ describe VagrantPlugins::ProviderLibvirt::Action::CreateDomain do
43
55
  end
44
56
 
45
57
  context 'connection => qemu:///system' do
46
- context 'default pool' do
47
- let(:test_file) { 'default_system_storage_pool.xml' }
58
+ let(:domain_xml_file) { 'default_domain.xml' }
48
59
 
60
+ context 'default pool' do
49
61
  it 'should execute correctly' do
50
- expect(libvirt_client).to receive(:lookup_storage_pool_by_name).and_return(libvirt_storage_pool)
51
- expect(libvirt_storage_pool).to receive(:xml_desc).and_return(storage_pool_xml)
52
- expect(servers).to receive(:create).and_return(machine)
62
+ expect(servers).to receive(:create).with(xml: domain_xml).and_return(machine)
53
63
  expect(volumes).to_not receive(:create) # additional disks only
54
64
 
55
65
  expect(subject.call(env)).to be_nil
56
66
  end
57
67
 
58
- context 'additional disks' do
68
+ context 'with no box' do
69
+ let(:storage_pool_xml_file) { 'default_system_storage_pool.xml' }
59
70
  let(:vagrantfile) do
60
71
  <<-EOF
61
72
  Vagrant.configure('2') do |config|
62
73
  config.vm.define :test
63
- config.vm.provider :libvirt do |libvirt|
64
- libvirt.storage :file, :size => '20G'
65
- end
66
74
  end
67
75
  EOF
68
76
  end
69
77
 
78
+ it 'should query for the storage pool path' do
79
+ expect(libvirt_client).to receive(:lookup_storage_pool_by_name).and_return(libvirt_storage_pool)
80
+ expect(libvirt_storage_pool).to receive(:xml_desc).and_return(storage_pool_xml)
81
+ expect(servers).to receive(:create).and_return(machine)
82
+
83
+ expect(subject.call(env)).to be_nil
84
+ end
85
+ end
86
+
87
+ context 'additional disks' do
88
+ let(:vagrantfile_providerconfig) do
89
+ <<-EOF
90
+ libvirt.storage :file, :size => '20G'
91
+ EOF
92
+ end
93
+
70
94
  context 'volume create failed' do
71
95
  it 'should raise an exception' do
72
- expect(libvirt_client).to receive(:lookup_storage_pool_by_name).and_return(libvirt_storage_pool)
73
- expect(libvirt_storage_pool).to receive(:xml_desc).and_return(storage_pool_xml)
74
96
  expect(volumes).to receive(:create).and_raise(Libvirt::Error)
75
97
 
76
98
  expect{ subject.call(env) }.to raise_error(VagrantPlugins::ProviderLibvirt::Errors::FogCreateDomainVolumeError)
@@ -78,9 +100,9 @@ describe VagrantPlugins::ProviderLibvirt::Action::CreateDomain do
78
100
  end
79
101
 
80
102
  context 'volume create succeeded' do
103
+ let(:domain_xml_file) { 'additional_disks_domain.xml' }
104
+
81
105
  it 'should complete' do
82
- expect(libvirt_client).to receive(:lookup_storage_pool_by_name).and_return(libvirt_storage_pool)
83
- expect(libvirt_storage_pool).to receive(:xml_desc).and_return(storage_pool_xml)
84
106
  expect(volumes).to receive(:create).with(
85
107
  hash_including(
86
108
  :path => "/var/lib/libvirt/images/vagrant-test_default-vdb.qcow2",
@@ -89,7 +111,7 @@ describe VagrantPlugins::ProviderLibvirt::Action::CreateDomain do
89
111
  :pool_name => "default",
90
112
  )
91
113
  )
92
- expect(servers).to receive(:create).and_return(machine)
114
+ expect(servers).to receive(:create).with(xml: domain_xml).and_return(machine)
93
115
 
94
116
  expect(subject.call(env)).to be_nil
95
117
  end
@@ -98,6 +120,14 @@ describe VagrantPlugins::ProviderLibvirt::Action::CreateDomain do
98
120
  end
99
121
 
100
122
  context 'no default pool' do
123
+ let(:vagrantfile) do
124
+ <<-EOF
125
+ Vagrant.configure('2') do |config|
126
+ config.vm.define :test
127
+ end
128
+ EOF
129
+ end
130
+
101
131
  it 'should raise an exception' do
102
132
  expect(libvirt_client).to receive(:lookup_storage_pool_by_name).and_return(nil)
103
133
 
@@ -107,45 +137,51 @@ describe VagrantPlugins::ProviderLibvirt::Action::CreateDomain do
107
137
  end
108
138
 
109
139
  context 'connection => qemu:///session' do
110
- let(:vagrantfile) do
140
+ let(:vagrantfile_providerconfig) do
111
141
  <<-EOF
112
- Vagrant.configure('2') do |config|
113
- config.vm.define :test
114
- config.vm.provider :libvirt do |libvirt|
115
- libvirt.qemu_use_session = true
116
- end
117
- end
142
+ libvirt.qemu_use_session = true
118
143
  EOF
119
144
  end
120
145
 
121
146
  context 'default pool' do
122
- let(:test_file) { 'default_user_storage_pool.xml' }
123
-
124
147
  it 'should execute correctly' do
125
- expect(libvirt_client).to receive(:lookup_storage_pool_by_name).and_return(libvirt_storage_pool)
126
- expect(libvirt_storage_pool).to receive(:xml_desc).and_return(storage_pool_xml)
127
148
  expect(servers).to receive(:create).and_return(machine)
128
149
 
129
150
  expect(subject.call(env)).to be_nil
130
151
  end
131
152
 
132
- context 'additional disks' do
153
+ context 'with no box' do
154
+ let(:storage_pool_xml_file) { 'default_user_storage_pool.xml' }
133
155
  let(:vagrantfile) do
134
156
  <<-EOF
135
157
  Vagrant.configure('2') do |config|
136
158
  config.vm.define :test
137
159
  config.vm.provider :libvirt do |libvirt|
138
- libvirt.qemu_use_session = true
139
- libvirt.storage :file, :size => '20G'
160
+ #{vagrantfile_providerconfig}
140
161
  end
141
162
  end
142
163
  EOF
143
164
  end
144
165
 
166
+ it 'should query for the storage pool path' do
167
+ expect(libvirt_client).to receive(:lookup_storage_pool_by_name).and_return(libvirt_storage_pool)
168
+ expect(libvirt_storage_pool).to receive(:xml_desc).and_return(storage_pool_xml)
169
+ expect(servers).to receive(:create).and_return(machine)
170
+
171
+ expect(subject.call(env)).to be_nil
172
+ end
173
+ end
174
+
175
+ context 'additional disks' do
176
+ let(:vagrantfile_providerconfig) do
177
+ <<-EOF
178
+ libvirt.qemu_use_session = true
179
+ libvirt.storage :file, :size => '20G'
180
+ EOF
181
+ end
182
+
145
183
  context 'volume create succeeded' do
146
184
  it 'should complete' do
147
- expect(libvirt_client).to receive(:lookup_storage_pool_by_name).and_return(libvirt_storage_pool)
148
- expect(libvirt_storage_pool).to receive(:xml_desc).and_return(storage_pool_xml)
149
185
  expect(volumes).to receive(:create).with(
150
186
  hash_including(
151
187
  :path => "/var/lib/libvirt/images/vagrant-test_default-vdb.qcow2",
@@ -1,6 +1,6 @@
1
1
  <volume>
2
2
  <name>test.img</name>
3
- <capacity unit="G">5</capacity>
3
+ <capacity unit="B">5368709120</capacity>
4
4
  <target>
5
5
  <format type="qcow2"></format>
6
6
  <permissions>
@@ -1,6 +1,6 @@
1
1
  <volume>
2
2
  <name>test.img</name>
3
- <capacity unit="G">5</capacity>
3
+ <capacity unit="B">5368709120</capacity>
4
4
  <target>
5
5
  <format type="qcow2"></format>
6
6
  <permissions>
@@ -1,6 +1,6 @@
1
1
  <volume>
2
2
  <name>test_1.img</name>
3
- <capacity unit="G">10</capacity>
3
+ <capacity unit="B">10737423360</capacity>
4
4
  <target>
5
5
  <format type="qcow2"></format>
6
6
  <permissions>
@@ -1,6 +1,6 @@
1
1
  <volume>
2
2
  <name>test_2.img</name>
3
- <capacity unit="G">20</capacity>
3
+ <capacity unit="B">21474836480</capacity>
4
4
  <target>
5
5
  <format type="qcow2"></format>
6
6
  <permissions>
@@ -1,8 +1,12 @@
1
+ # frozen_string_literal: true
2
+
1
3
  require 'spec_helper'
2
4
  require 'support/sharedcontext'
3
5
  require 'support/libvirt_context'
4
6
 
5
7
  require 'vagrant-libvirt/action/destroy_domain'
8
+ require 'vagrant-libvirt/util/byte_number'
9
+
6
10
 
7
11
  describe VagrantPlugins::ProviderLibvirt::Action::CreateDomainVolume do
8
12
  subject { described_class.new(app, env) }
@@ -30,6 +34,8 @@ describe VagrantPlugins::ProviderLibvirt::Action::CreateDomainVolume do
30
34
  allow(all).to receive(:first).and_return(box_volume)
31
35
  allow(box_volume).to receive(:id).and_return(nil)
32
36
  env[:domain_name] = 'test'
37
+
38
+ allow(logger).to receive(:debug)
33
39
  end
34
40
 
35
41
  context 'when one disk' do
@@ -38,7 +44,7 @@ describe VagrantPlugins::ProviderLibvirt::Action::CreateDomainVolume do
38
44
  env[:box_volumes] = [
39
45
  {
40
46
  :name=>"test_vagrant_box_image_1.1.1_0.img",
41
- :virtual_size=>5
47
+ :virtual_size=>ByteNumber.new(5368709120)
42
48
  }
43
49
  ]
44
50
  end
@@ -65,15 +71,15 @@ describe VagrantPlugins::ProviderLibvirt::Action::CreateDomainVolume do
65
71
  env[:box_volumes] = [
66
72
  {
67
73
  :name=>"test_vagrant_box_image_1.1.1_0.img",
68
- :virtual_size=>5
74
+ :virtual_size=>ByteNumber.new(5368709120)
69
75
  },
70
76
  {
71
77
  :name=>"test_vagrant_box_image_1.1.1_1.img",
72
- :virtual_size=>10
78
+ :virtual_size=>ByteNumber.new(10737423360)
73
79
  },
74
80
  {
75
81
  :name=>"test_vagrant_box_image_1.1.1_2.img",
76
- :virtual_size=>20
82
+ :virtual_size=>ByteNumber.new(21474836480)
77
83
  }
78
84
  ]
79
85
  end
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  require 'spec_helper'
2
4
  require 'support/sharedcontext'
3
5
  require 'support/libvirt_context'
@@ -10,14 +12,18 @@ describe VagrantPlugins::ProviderLibvirt::Action::DestroyDomain do
10
12
  include_context 'unit'
11
13
  include_context 'libvirt'
12
14
 
15
+ let(:driver) { double('driver') }
13
16
  let(:libvirt_domain) { double('libvirt_domain') }
14
17
  let(:libvirt_client) { double('libvirt_client') }
15
18
  let(:servers) { double('servers') }
16
19
 
20
+ before do
21
+ allow(machine.provider).to receive('driver').and_return(driver)
22
+ allow(driver).to receive(:connection).and_return(connection)
23
+ end
24
+
17
25
  describe '#call' do
18
26
  before do
19
- allow_any_instance_of(VagrantPlugins::ProviderLibvirt::Driver)
20
- .to receive(:connection).and_return(connection)
21
27
  allow(connection).to receive(:client).and_return(libvirt_client)
22
28
  allow(libvirt_client).to receive(:lookup_domain_by_uuid)
23
29
  .and_return(libvirt_domain)
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  require 'spec_helper'
2
4
  require 'support/sharedcontext'
3
5
  require 'support/libvirt_context'
@@ -1,7 +1,9 @@
1
+ # frozen_string_literal: true
2
+
1
3
  require 'spec_helper'
2
4
  require 'support/sharedcontext'
3
5
  require 'support/libvirt_context'
4
- require 'vagrant-libvirt/action/destroy_domain'
6
+ require 'vagrant-libvirt/action/halt_domain'
5
7
 
6
8
  describe VagrantPlugins::ProviderLibvirt::Action::HaltDomain do
7
9
  subject { described_class.new(app, env) }
@@ -9,81 +11,52 @@ describe VagrantPlugins::ProviderLibvirt::Action::HaltDomain do
9
11
  include_context 'unit'
10
12
  include_context 'libvirt'
11
13
 
14
+ let(:driver) { double('driver') }
12
15
  let(:libvirt_domain) { double('libvirt_domain') }
13
16
  let(:servers) { double('servers') }
14
17
 
18
+ before do
19
+ allow(machine.provider).to receive('driver').and_return(driver)
20
+ allow(driver).to receive(:created?).and_return(true)
21
+ allow(driver).to receive(:connection).and_return(connection)
22
+ end
23
+
15
24
  describe '#call' do
16
25
  before do
17
- allow_any_instance_of(VagrantPlugins::ProviderLibvirt::Driver)
18
- .to receive(:connection).and_return(connection)
19
26
  allow(connection).to receive(:servers).and_return(servers)
20
27
  allow(servers).to receive(:get).and_return(domain)
21
- # always see this at the start of #call
22
- expect(ui).to receive(:info).with('Halting domain...')
28
+ allow(ui).to receive(:info).with('Halting domain...')
23
29
  end
24
30
 
25
- context 'with graceful timeout' do
26
- it "should shutdown" do
27
- expect(guest).to receive(:capability).with(:halt).and_return(true)
28
- expect(domain).to receive(:wait_for).with(60).and_return(false)
29
- expect(subject.call(env)).to be_nil
30
- end
31
-
32
- context 'when halt fails' do
33
- before do
34
- expect(logger).to receive(:info).with('Trying Libvirt graceful shutdown.')
35
- expect(guest).to receive(:capability).with(:halt).and_raise(IOError)
36
- expect(domain).to receive(:state).and_return('running')
37
- end
31
+ context "when state is not running" do
32
+ before { expect(driver).to receive(:state).at_least(1).
33
+ and_return(:not_created) }
38
34
 
39
- it "should call shutdown" do
40
- expect(domain).to receive(:shutdown)
41
- expect(domain).to receive(:wait_for).with(60).and_return(false)
42
- expect(subject.call(env)).to be_nil
43
- end
44
-
45
- context 'when shutdown fails' do
46
- it "should call power off" do
47
- expect(logger).to receive(:error).with('Failed to shutdown cleanly. Calling force poweroff.')
48
- expect(domain).to receive(:shutdown).and_raise(IOError)
49
- expect(domain).to receive(:poweroff)
50
- expect(subject.call(env)).to be_nil
51
- end
52
- end
53
-
54
- context 'when shutdown exceeds the timeout' do
55
- it "should call poweroff" do
56
- expect(logger).to receive(:info).with('VM is still running. Calling force poweroff.')
57
- expect(domain).to receive(:shutdown).and_raise(Timeout::Error)
58
- expect(domain).to receive(:poweroff)
59
- expect(subject.call(env)).to be_nil
60
- end
61
- end
35
+ it "should not poweroff when state is not running" do
36
+ expect(domain).not_to receive(:poweroff)
37
+ subject.call(env)
62
38
  end
63
39
 
64
- context 'when halt exceeds the timeout' do
65
- before do
66
- expect(logger).to_not receive(:info).with('Trying Libvirt graceful shutdown.')
67
- expect(guest).to receive(:capability).with(:halt).and_raise(Timeout::Error)
68
- end
69
-
70
- it "should call poweroff" do
71
- expect(logger).to receive(:info).with('VM is still running. Calling force poweroff.')
72
- expect(domain).to receive(:poweroff)
73
- expect(subject.call(env)).to be_nil
74
- end
40
+ it "should not print halting message" do
41
+ expect(ui).not_to receive(:info)
42
+ subject.call(env)
75
43
  end
76
44
  end
77
45
 
78
- context 'with force halt enabled' do
46
+ context "when state is running" do
79
47
  before do
80
- allow(env).to receive(:[]).and_call_original
81
- expect(env).to receive(:[]).with(:force_halt).and_return(true)
48
+ expect(driver).to receive(:state).and_return(:running)
82
49
  end
83
50
 
84
- it "should just call poweroff" do
51
+ it "should poweroff" do
85
52
  expect(domain).to receive(:poweroff)
86
- expect(subject.call(env)).to be_nil
53
+ subject.call(env)
54
+ end
55
+
56
+ it "should print halting message" do
57
+ allow(domain).to receive(:poweroff)
58
+ expect(ui).to receive(:info).with('Halting domain...')
59
+ subject.call(env)
87
60
  end
88
61
  end
89
62
  end