vagrant-libvirt 0.8.2 → 0.10.1

Sign up to get free protection for your applications and to get access to all the features.
Files changed (88) hide show
  1. checksums.yaml +4 -4
  2. data/README.md +51 -2079
  3. data/lib/vagrant-libvirt/action/create_domain.rb +57 -94
  4. data/lib/vagrant-libvirt/action/create_network_interfaces.rb +1 -1
  5. data/lib/vagrant-libvirt/action/create_networks.rb +3 -3
  6. data/lib/vagrant-libvirt/action/destroy_domain.rb +21 -5
  7. data/lib/vagrant-libvirt/action/destroy_networks.rb +2 -2
  8. data/lib/vagrant-libvirt/action/handle_box_image.rb +3 -1
  9. data/lib/vagrant-libvirt/action/package_domain.rb +1 -5
  10. data/lib/vagrant-libvirt/action/remove_libvirt_image.rb +3 -1
  11. data/lib/vagrant-libvirt/action/resolve_disk_settings.rb +181 -0
  12. data/lib/vagrant-libvirt/action/snapshot_delete.rb +26 -0
  13. data/lib/vagrant-libvirt/action/snapshot_restore.rb +22 -0
  14. data/lib/vagrant-libvirt/action/snapshot_save.rb +27 -0
  15. data/lib/vagrant-libvirt/action/start_domain.rb +80 -11
  16. data/lib/vagrant-libvirt/action.rb +53 -1
  17. data/lib/vagrant-libvirt/cap/snapshots.rb +12 -0
  18. data/lib/vagrant-libvirt/cap/synced_folder_9p.rb +4 -4
  19. data/lib/vagrant-libvirt/cap/synced_folder_virtiofs.rb +4 -4
  20. data/lib/vagrant-libvirt/config.rb +104 -6
  21. data/lib/vagrant-libvirt/driver.rb +108 -46
  22. data/lib/vagrant-libvirt/errors.rb +23 -3
  23. data/lib/vagrant-libvirt/plugin.rb +7 -3
  24. data/lib/vagrant-libvirt/provider.rb +1 -1
  25. data/lib/vagrant-libvirt/templates/domain.xml.erb +32 -6
  26. data/lib/vagrant-libvirt/util/byte_number.rb +0 -1
  27. data/lib/vagrant-libvirt/util/compat.rb +23 -0
  28. data/lib/vagrant-libvirt/util/domain_flags.rb +15 -0
  29. data/lib/vagrant-libvirt/util/unindent.rb +7 -0
  30. data/lib/vagrant-libvirt/util.rb +1 -0
  31. data/lib/vagrant-libvirt/version +1 -1
  32. data/locales/en.yml +28 -4
  33. data/spec/acceptance/additional_storage_spec.rb +32 -0
  34. data/spec/acceptance/package_domain_spec.rb +90 -0
  35. data/spec/acceptance/provider_settings_spec.rb +54 -0
  36. data/spec/acceptance/simple_vm_provision_via_shell_spec.rb +31 -0
  37. data/spec/acceptance/snapshots_spec.rb +41 -0
  38. data/spec/acceptance/support-skeletons/package_complex/Vagrantfile.testbox +14 -0
  39. data/spec/acceptance/support-skeletons/package_complex/scripts/sysprep.sh +32 -0
  40. data/spec/acceptance/support-skeletons/package_simple/Vagrantfile.testbox +10 -0
  41. data/spec/acceptance/two_disks_spec.rb +29 -0
  42. data/spec/acceptance/use_qemu_agent_for_connectivity_spec.rb +35 -0
  43. data/spec/spec_helper.rb +3 -0
  44. data/spec/support/acceptance/configuration.rb +21 -0
  45. data/spec/support/acceptance/context.rb +70 -0
  46. data/spec/support/acceptance/isolated_environment.rb +41 -0
  47. data/spec/support/libvirt_acceptance_context.rb +64 -0
  48. data/spec/support/libvirt_context.rb +4 -0
  49. data/spec/support/sharedcontext.rb +1 -0
  50. data/spec/unit/action/cleanup_on_failure_spec.rb +0 -2
  51. data/spec/unit/action/create_domain_spec/sysinfo.xml +66 -0
  52. data/spec/unit/action/create_domain_spec/sysinfo_only_required.xml +49 -0
  53. data/spec/unit/action/create_domain_spec.rb +188 -140
  54. data/spec/unit/action/create_domain_volume_spec.rb +0 -3
  55. data/spec/unit/action/destroy_domain_spec.rb +43 -10
  56. data/spec/unit/action/forward_ports_spec.rb +0 -1
  57. data/spec/unit/action/handle_box_image_spec.rb +31 -14
  58. data/spec/unit/action/package_domain_spec.rb +0 -5
  59. data/spec/unit/action/remove_libvirt_image_spec.rb +43 -0
  60. data/spec/unit/action/resolve_disk_settings_spec/default_domain.xml +43 -0
  61. data/spec/unit/action/resolve_disk_settings_spec/default_no_aliases.xml +42 -0
  62. data/spec/unit/action/{create_domain_spec → resolve_disk_settings_spec}/default_system_storage_pool.xml +0 -0
  63. data/spec/unit/action/resolve_disk_settings_spec/multi_volume_box.xml +55 -0
  64. data/spec/unit/action/resolve_disk_settings_spec/multi_volume_box_additional_and_custom_no_aliases.xml +67 -0
  65. data/spec/unit/action/resolve_disk_settings_spec/multi_volume_box_additional_storage.xml +67 -0
  66. data/spec/unit/action/resolve_disk_settings_spec.rb +385 -0
  67. data/spec/unit/action/start_domain_spec/clock_timer_removed.xml +38 -0
  68. data/spec/unit/action/start_domain_spec/clock_timer_rtc_tsc.xml +39 -0
  69. data/spec/unit/action/start_domain_spec/existing_added_nvram.xml +62 -0
  70. data/spec/unit/action/start_domain_spec/nvram_domain.xml +64 -0
  71. data/spec/unit/action/start_domain_spec/nvram_domain_other_setting.xml +64 -0
  72. data/spec/unit/action/start_domain_spec/nvram_domain_removed.xml +64 -0
  73. data/spec/unit/action/start_domain_spec.rb +122 -22
  74. data/spec/unit/action/wait_till_up_spec.rb +0 -2
  75. data/spec/unit/action_spec.rb +88 -3
  76. data/spec/unit/cap/synced_folder_9p_spec.rb +120 -0
  77. data/spec/unit/cap/synced_folder_virtiofs_spec.rb +120 -0
  78. data/spec/unit/config_spec.rb +153 -6
  79. data/spec/unit/driver_spec.rb +3 -1
  80. data/spec/unit/plugin_spec.rb +42 -0
  81. data/spec/unit/templates/domain_all_settings.xml +15 -6
  82. data/spec/unit/templates/domain_scsi_bus_storage.xml +44 -0
  83. data/spec/unit/templates/domain_scsi_device_storage.xml +44 -0
  84. data/spec/unit/templates/domain_scsi_multiple_controllers_storage.xml +130 -0
  85. data/spec/unit/templates/domain_spec.rb +105 -21
  86. data/spec/unit/util/byte_number_spec.rb +1 -1
  87. metadata +169 -79
  88. data/spec/unit/provider_spec.rb +0 -11
@@ -0,0 +1,64 @@
1
+ <domain type='qemu'>
2
+ <name>vagrant-libvirt_default</name>
3
+ <uuid>881a931b-0110-4d10-81aa-47a1a19f5726</uuid>
4
+ <description>Source: /home/test/vagrant-libvirt/Vagrantfile</description>
5
+ <memory unit='KiB'>2097152</memory>
6
+ <currentMemory unit='KiB'>2097152</currentMemory>
7
+ <vcpu placement='static'>2</vcpu>
8
+ <os>
9
+ <type arch='x86_64' machine='pc-i440fx-6.0'>hvm</type>
10
+
11
+
12
+ <boot dev='hd'/>
13
+ </os>
14
+ <features>
15
+ <acpi/>
16
+ <apic/>
17
+ <pae/>
18
+ </features>
19
+ <cpu check='partial' mode='host-model'/>
20
+ <clock offset='utc'/>
21
+ <on_poweroff>destroy</on_poweroff>
22
+ <on_reboot>restart</on_reboot>
23
+ <on_crash>destroy</on_crash>
24
+ <devices>
25
+ <emulator>/usr/bin/qemu-system-x86_64</emulator>
26
+ <disk device='disk' type='file'>
27
+ <driver name='qemu' type='qcow2'/>
28
+ <source file='/var/lib/libvirt/images/vagrant-libvirt_default.img'/>
29
+ <target bus='virtio' dev='vda'/>
30
+ <address bus='0x00' domain='0x0000' function='0x0' slot='0x03' type='pci'/>
31
+ </disk>
32
+ <controller index='0' model='piix3-uhci' type='usb'>
33
+ <address bus='0x00' domain='0x0000' function='0x2' slot='0x01' type='pci'/>
34
+ </controller>
35
+ <controller index='0' model='pci-root' type='pci'/>
36
+ <interface type='network'>
37
+ <mac address='52:54:00:7d:14:0e'/>
38
+ <source network='vagrant-libvirt'/>
39
+ <model type='virtio'/>
40
+ <address bus='0x00' domain='0x0000' function='0x0' slot='0x05' type='pci'/>
41
+ </interface>
42
+ <serial type='pty'>
43
+ <target port='0' type='isa-serial'>
44
+ <model name='isa-serial'/>
45
+ </target>
46
+ </serial>
47
+ <console type='pty'>
48
+ <target port='0' type='serial'/>
49
+ </console>
50
+ <input bus='ps2' type='mouse'/>
51
+ <input bus='ps2' type='keyboard'/>
52
+ <graphics autoport='yes' keymap='en-us' listen='127.0.0.1' port='-1' type='vnc'>
53
+ <listen address='127.0.0.1' type='address'/>
54
+ </graphics>
55
+ <audio id='1' type='none'/>
56
+ <video>
57
+ <model heads='1' primary='yes' type='cirrus' vram='16384'/>
58
+ <address bus='0x00' domain='0x0000' function='0x0' slot='0x02' type='pci'/>
59
+ </video>
60
+ <memballoon model='virtio'>
61
+ <address bus='0x00' domain='0x0000' function='0x0' slot='0x04' type='pci'/>
62
+ </memballoon>
63
+ </devices>
64
+ </domain>
@@ -13,19 +13,19 @@ describe VagrantPlugins::ProviderLibvirt::Action::StartDomain do
13
13
  include_context 'unit'
14
14
  include_context 'libvirt'
15
15
 
16
- let(:libvirt_domain) { double('libvirt_domain') }
17
- let(:libvirt_client) { double('libvirt_client') }
18
16
  let(:servers) { double('servers') }
19
17
 
20
18
  let(:domain_xml) { File.read(File.join(File.dirname(__FILE__), File.basename(__FILE__, '.rb'), test_file)) }
21
19
  let(:updated_domain_xml) { File.read(File.join(File.dirname(__FILE__), File.basename(__FILE__, '.rb'), updated_test_file)) }
22
20
 
21
+ before do
22
+ allow(driver).to receive(:created?).and_return(true)
23
+ end
24
+
23
25
  describe '#call' do
24
26
  let(:test_file) { 'default.xml' }
25
27
 
26
28
  before do
27
- allow_any_instance_of(VagrantPlugins::ProviderLibvirt::Driver)
28
- .to receive(:connection).and_return(connection)
29
29
  allow(connection).to receive(:client).and_return(libvirt_client)
30
30
  allow(libvirt_client).to receive(:lookup_domain_by_uuid).and_return(libvirt_domain)
31
31
 
@@ -33,8 +33,7 @@ describe VagrantPlugins::ProviderLibvirt::Action::StartDomain do
33
33
  allow(servers).to receive(:get).and_return(domain)
34
34
 
35
35
  allow(logger).to receive(:debug)
36
- expect(logger).to receive(:info)
37
- expect(ui).to_not receive(:error)
36
+ allow(logger).to receive(:info)
38
37
 
39
38
  allow(libvirt_domain).to receive(:xml_desc).and_return(domain_xml)
40
39
 
@@ -43,23 +42,112 @@ describe VagrantPlugins::ProviderLibvirt::Action::StartDomain do
43
42
  end
44
43
 
45
44
  it 'should execute without changing' do
46
- allow(libvirt_domain).to receive(:undefine)
45
+ expect(ui).to_not receive(:error)
46
+ expect(libvirt_client).to_not receive(:define_domain_xml)
47
47
  expect(libvirt_domain).to receive(:autostart=)
48
48
  expect(domain).to receive(:start)
49
49
 
50
50
  expect(subject.call(env)).to be_nil
51
51
  end
52
52
 
53
- context 'when previously running default config' do
54
- let(:test_file) { 'existing.xml' }
53
+ context 'when any setting changed' do
54
+ let(:vagrantfile_providerconfig) do
55
+ <<-EOF
56
+ libvirt.cpus = 2
57
+ EOF
58
+ end
59
+
60
+ let(:updated_domain_xml) {
61
+ new_xml = domain_xml.dup
62
+ new_xml['<vcpu>1</vcpu>'] = '<vcpu>2</vcpu>'
63
+ new_xml
64
+ }
55
65
 
56
- it 'should execute without changing' do
57
- allow(libvirt_domain).to receive(:undefine)
66
+ it 'should update the domain' do
67
+ expect(ui).to_not receive(:error)
58
68
  expect(libvirt_domain).to receive(:autostart=)
69
+ expect(connection).to receive(:define_domain).and_return(libvirt_domain)
70
+ expect(libvirt_domain).to receive(:xml_desc).and_return(domain_xml, updated_domain_xml)
59
71
  expect(domain).to receive(:start)
60
72
 
61
73
  expect(subject.call(env)).to be_nil
62
74
  end
75
+
76
+ context 'when there is an error during update' do
77
+ it 'should skip attempting to start' do
78
+ expect(ui).to receive(:error)
79
+ expect(connection).to receive(:define_domain).and_raise(::Libvirt::Error)
80
+
81
+ expect { subject.call(env) }.to raise_error(VagrantPlugins::ProviderLibvirt::Errors::VagrantLibvirtError)
82
+ end
83
+ end
84
+
85
+ context 'when there is an interrupt' do
86
+ it 'should skip attempting to start' do
87
+ expect(connection).to receive(:define_domain).and_raise(Interrupt)
88
+
89
+ expect { subject.call(env) }.to raise_error(Interrupt)
90
+ end
91
+ end
92
+ end
93
+
94
+ context 'nvram' do
95
+ context 'when being added to existing' do
96
+ let(:vagrantfile_providerconfig) do
97
+ <<-EOF
98
+ libvirt.loader = "/path/to/loader/file"
99
+ libvirt.nvram = "/path/to/nvram/file"
100
+ EOF
101
+ end
102
+ let(:test_file) { 'existing.xml' }
103
+ let(:updated_test_file) { 'existing_added_nvram.xml' }
104
+
105
+ it 'should add the nvram element' do
106
+ expect(ui).to_not receive(:error)
107
+ expect(connection).to receive(:define_domain).with(updated_domain_xml).and_return(libvirt_domain)
108
+ expect(libvirt_domain).to receive(:xml_desc).and_return(domain_xml, updated_domain_xml)
109
+ expect(libvirt_domain).to receive(:autostart=)
110
+ expect(domain).to receive(:start)
111
+
112
+ expect(subject.call(env)).to be_nil
113
+ end
114
+ end
115
+
116
+ context 'when it was already in use' do
117
+ let(:vagrantfile_providerconfig) do
118
+ <<-EOF
119
+ libvirt.loader = "/path/to/loader/file"
120
+ libvirt.nvram = "/path/to/nvram/file1"
121
+ EOF
122
+ end
123
+ let(:test_file) { 'nvram_domain.xml' }
124
+ let(:updated_test_file) { 'nvram_domain_other_setting.xml' }
125
+
126
+ it 'should keep the XML element' do
127
+ expect(ui).to_not receive(:error)
128
+ expect(connection).to receive(:define_domain).with(updated_domain_xml).and_return(libvirt_domain)
129
+ expect(libvirt_domain).to receive(:xml_desc).and_return(domain_xml, updated_domain_xml)
130
+ expect(libvirt_domain).to receive(:autostart=)
131
+ expect(domain).to receive(:start)
132
+
133
+ expect(subject.call(env)).to be_nil
134
+ end
135
+
136
+ context 'when it is being disabled' do
137
+ let(:vagrantfile_providerconfig) { }
138
+ let(:updated_test_file) { 'nvram_domain_removed.xml' }
139
+
140
+ it 'should delete the XML element' do
141
+ expect(ui).to_not receive(:error)
142
+ expect(connection).to receive(:define_domain).with(updated_domain_xml).and_return(libvirt_domain)
143
+ expect(libvirt_domain).to receive(:xml_desc).and_return(domain_xml, updated_domain_xml)
144
+ expect(libvirt_domain).to receive(:autostart=)
145
+ expect(domain).to receive(:start)
146
+
147
+ expect(subject.call(env)).to be_nil
148
+ end
149
+ end
150
+ end
63
151
  end
64
152
 
65
153
  context 'tpm' do
@@ -74,9 +162,10 @@ describe VagrantPlugins::ProviderLibvirt::Action::StartDomain do
74
162
  end
75
163
 
76
164
  it 'should modify the domain tpm_path' do
77
- expect(libvirt_domain).to receive(:undefine)
165
+ expect(ui).to_not receive(:error)
78
166
  expect(logger).to receive(:debug).with('tpm config changed')
79
- expect(servers).to receive(:create).with(xml: updated_domain_xml)
167
+ expect(connection).to receive(:define_domain).with(updated_domain_xml).and_return(libvirt_domain)
168
+ expect(libvirt_domain).to receive(:xml_desc).and_return(domain_xml, updated_domain_xml)
80
169
  expect(libvirt_domain).to receive(:autostart=)
81
170
  expect(domain).to receive(:start)
82
171
 
@@ -95,9 +184,10 @@ describe VagrantPlugins::ProviderLibvirt::Action::StartDomain do
95
184
  end
96
185
 
97
186
  it 'should modify the domain tpm_path' do
98
- expect(libvirt_domain).to receive(:undefine)
187
+ expect(ui).to_not receive(:error)
99
188
  expect(logger).to receive(:debug).with('tpm config changed')
100
- expect(servers).to receive(:create).with(xml: updated_domain_xml)
189
+ expect(connection).to receive(:define_domain).with(updated_domain_xml).and_return(libvirt_domain)
190
+ expect(libvirt_domain).to receive(:xml_desc).and_return(domain_xml, updated_domain_xml)
101
191
  expect(libvirt_domain).to receive(:autostart=)
102
192
  expect(domain).to receive(:start)
103
193
 
@@ -117,6 +207,7 @@ describe VagrantPlugins::ProviderLibvirt::Action::StartDomain do
117
207
  end
118
208
 
119
209
  it 'should execute without changing' do
210
+ expect(ui).to_not receive(:error)
120
211
  expect(libvirt_domain).to receive(:autostart=)
121
212
  expect(domain).to receive(:start)
122
213
 
@@ -136,6 +227,7 @@ describe VagrantPlugins::ProviderLibvirt::Action::StartDomain do
136
227
  end
137
228
 
138
229
  it 'should execute without changing' do
230
+ expect(ui).to_not receive(:error)
139
231
  expect(libvirt_domain).to receive(:autostart=)
140
232
  expect(domain).to receive(:start)
141
233
 
@@ -155,9 +247,10 @@ describe VagrantPlugins::ProviderLibvirt::Action::StartDomain do
155
247
  end
156
248
 
157
249
  it 'should modify the domain' do
158
- expect(libvirt_domain).to receive(:undefine)
250
+ expect(ui).to_not receive(:error)
159
251
  expect(logger).to receive(:debug).with('tpm config changed')
160
- expect(servers).to receive(:create).with(xml: updated_domain_xml)
252
+ expect(connection).to receive(:define_domain).with(updated_domain_xml).and_return(libvirt_domain)
253
+ expect(libvirt_domain).to receive(:xml_desc).and_return(domain_xml, updated_domain_xml)
161
254
  expect(libvirt_domain).to receive(:autostart=)
162
255
  expect(domain).to receive(:start)
163
256
 
@@ -177,8 +270,9 @@ describe VagrantPlugins::ProviderLibvirt::Action::StartDomain do
177
270
  end
178
271
 
179
272
  it 'should not modify the domain' do
273
+ expect(ui).to_not receive(:error)
180
274
  expect(logger).to_not receive(:debug).with('clock timers config changed')
181
- expect(servers).to_not receive(:create)
275
+ expect(connection).to_not receive(:define_domain)
182
276
  expect(libvirt_domain).to receive(:autostart=)
183
277
  expect(domain).to receive(:start)
184
278
 
@@ -194,10 +288,13 @@ describe VagrantPlugins::ProviderLibvirt::Action::StartDomain do
194
288
  EOF
195
289
  end
196
290
 
291
+ let(:updated_test_file) { 'clock_timer_rtc_tsc.xml' }
292
+
197
293
  it 'should modify the domain' do
198
- expect(libvirt_domain).to receive(:undefine)
294
+ expect(ui).to_not receive(:error)
199
295
  expect(logger).to receive(:debug).with('clock timers config changed')
200
- expect(servers).to receive(:create).with(xml: match(/<clock offset='utc'>\s*<timer name='rtc'\/>\s*<timer name='tsc'\/>\s*<\/clock>/))
296
+ expect(connection).to receive(:define_domain).with(match(/<clock offset='utc'>\s*<timer name='rtc'\/>\s*<timer name='tsc'\/>\s*<\/clock>/)).and_return(libvirt_domain)
297
+ expect(libvirt_domain).to receive(:xml_desc).and_return(domain_xml, updated_domain_xml)
201
298
  expect(libvirt_domain).to receive(:autostart=)
202
299
  expect(domain).to receive(:start)
203
300
 
@@ -206,10 +303,13 @@ describe VagrantPlugins::ProviderLibvirt::Action::StartDomain do
206
303
  end
207
304
 
208
305
  context 'timers removed' do
306
+ let(:updated_test_file) { 'clock_timer_removed.xml' }
307
+
209
308
  it 'should modify the domain' do
210
- expect(libvirt_domain).to receive(:undefine)
309
+ expect(ui).to_not receive(:error)
211
310
  expect(logger).to receive(:debug).with('clock timers config changed')
212
- expect(servers).to receive(:create).with(xml: match(/<clock offset='utc'>\s*<\/clock>/))
311
+ expect(connection).to receive(:define_domain).with(match(/<clock offset='utc'>\s*<\/clock>/)).and_return(libvirt_domain)
312
+ expect(libvirt_domain).to receive(:xml_desc).and_return(domain_xml, updated_domain_xml)
213
313
  expect(libvirt_domain).to receive(:autostart=)
214
314
  expect(domain).to receive(:start)
215
315
 
@@ -18,8 +18,6 @@ describe VagrantPlugins::ProviderLibvirt::Action::WaitTillUp do
18
18
 
19
19
  describe '#call' do
20
20
  before do
21
- allow_any_instance_of(VagrantPlugins::ProviderLibvirt::Provider).to receive(:driver)
22
- .and_return(driver)
23
21
  allow(driver).to receive(:get_domain).and_return(domain)
24
22
  allow(driver).to receive(:state).and_return(:running)
25
23
  # return some information for domain when needed
@@ -14,13 +14,10 @@ describe VagrantPlugins::ProviderLibvirt::Action do
14
14
  include_context 'libvirt'
15
15
  include_context 'unit'
16
16
 
17
- let(:libvirt_domain) { double('libvirt_domain') }
18
17
  let(:runner) { Vagrant::Action::Runner.new(env) }
19
18
  let(:state) { double('state') }
20
19
 
21
20
  before do
22
- allow_any_instance_of(VagrantPlugins::ProviderLibvirt::Driver)
23
- .to receive(:connection).and_return(connection)
24
21
  allow(machine).to receive(:id).and_return('test-machine-id')
25
22
  allow(machine).to receive(:state).and_return(state)
26
23
 
@@ -173,4 +170,92 @@ describe VagrantPlugins::ProviderLibvirt::Action do
173
170
  end
174
171
  end
175
172
  end
173
+
174
+ describe '#action_snapshot_delete' do
175
+ context 'when not created' do
176
+ before do
177
+ allow_action_env_result(VagrantPlugins::ProviderLibvirt::Action::IsCreated, false)
178
+ end
179
+
180
+ it 'should cause an error' do
181
+ expect{ machine.action(:snapshot_delete, snapshot_opts: {})}.to raise_error(Vagrant::Errors::VMNotCreatedError)
182
+ end
183
+ end
184
+
185
+ context 'when created' do
186
+ before do
187
+ allow_action_env_result(VagrantPlugins::ProviderLibvirt::Action::IsCreated, true)
188
+ end
189
+
190
+ context 'when running' do
191
+ before do
192
+ allow_action_env_result(VagrantPlugins::ProviderLibvirt::Action::IsRunning, true)
193
+ end
194
+
195
+ it 'should call SnapshotDelete' do
196
+ expect_any_instance_of(VagrantPlugins::ProviderLibvirt::Action::SnapshotDelete).to receive(:call).and_return(0)
197
+ expect(machine.action(:snapshot_delete, snapshot_opts: {})).to match(hash_including({:action_name => :machine_action_snapshot_delete}))
198
+ end
199
+ end
200
+ end
201
+ end
202
+
203
+
204
+ describe '#action_snapshot_restore' do
205
+ context 'when not created' do
206
+ before do
207
+ allow_action_env_result(VagrantPlugins::ProviderLibvirt::Action::IsCreated, false)
208
+ end
209
+
210
+ it 'should cause an error' do
211
+ expect{ machine.action(:snapshot_restore, snapshot_opts: {})}.to raise_error(Vagrant::Errors::VMNotCreatedError)
212
+ end
213
+ end
214
+
215
+ context 'when created' do
216
+ before do
217
+ allow_action_env_result(VagrantPlugins::ProviderLibvirt::Action::IsCreated, true)
218
+ end
219
+
220
+ context 'when running' do
221
+ before do
222
+ allow_action_env_result(VagrantPlugins::ProviderLibvirt::Action::IsRunning, true)
223
+ end
224
+
225
+ it 'should call SnapshotRestore' do
226
+ expect_any_instance_of(VagrantPlugins::ProviderLibvirt::Action::SnapshotRestore).to receive(:call).and_return(0)
227
+ expect(machine.action(:snapshot_restore, snapshot_opts: {})).to match(hash_including({:action_name => :machine_action_snapshot_restore}))
228
+ end
229
+ end
230
+ end
231
+ end
232
+
233
+ describe '#action_snapshot_save' do
234
+ context 'when not created' do
235
+ before do
236
+ allow_action_env_result(VagrantPlugins::ProviderLibvirt::Action::IsCreated, false)
237
+ end
238
+
239
+ it 'should cause an error' do
240
+ expect{ machine.action(:snapshot_save, snapshot_opts: {})}.to raise_error(Vagrant::Errors::VMNotCreatedError)
241
+ end
242
+ end
243
+
244
+ context 'when created' do
245
+ before do
246
+ allow_action_env_result(VagrantPlugins::ProviderLibvirt::Action::IsCreated, true)
247
+ end
248
+
249
+ context 'when running' do
250
+ before do
251
+ allow_action_env_result(VagrantPlugins::ProviderLibvirt::Action::IsRunning, true)
252
+ end
253
+
254
+ it 'should call SnapshotSave' do
255
+ expect_any_instance_of(VagrantPlugins::ProviderLibvirt::Action::SnapshotSave).to receive(:call).and_return(0)
256
+ expect(machine.action(:snapshot_save, snapshot_opts: {})).to match(hash_including({:action_name => :machine_action_snapshot_save}))
257
+ end
258
+ end
259
+ end
260
+ end
176
261
  end
@@ -0,0 +1,120 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'spec_helper'
4
+ require 'support/sharedcontext'
5
+
6
+ require 'vagrant-libvirt/cap/synced_folder_9p'
7
+ require 'vagrant-libvirt/util/unindent'
8
+
9
+ describe VagrantPlugins::SyncedFolder9P::SyncedFolder do
10
+ include_context 'unit'
11
+ include_context 'libvirt'
12
+
13
+ subject { described_class.new }
14
+
15
+ describe '#prepare' do
16
+ let(:synced_folders) { {
17
+ "/vagrant" => {
18
+ :hostpath => '/home/test/default',
19
+ :disabled=>false,
20
+ :guestpath=>'/vagrant',
21
+ :type => :"9p",
22
+ },
23
+ } }
24
+
25
+ let(:expected_xml) {
26
+ <<-EOF.unindent
27
+ <filesystem type="mount" accessmode="passthrough">
28
+ <driver type="path" wrpolicy="immediate"></driver>
29
+ <source dir="/home/test/default"></source>
30
+ <target dir="1ef53e1c9b6a5a0c695c80665331652\"></target>
31
+ </filesystem>
32
+ EOF
33
+ }
34
+
35
+ before do
36
+ allow(libvirt_client).to receive(:lookup_domain_by_uuid).and_return(libvirt_domain)
37
+ allow(logger).to receive(:debug)
38
+ end
39
+
40
+ it 'should attach device xml' do
41
+ expect(libvirt_domain).to receive(:attach_device).with(expected_xml, 0)
42
+
43
+ subject.prepare(machine, synced_folders, {})
44
+ end
45
+
46
+ context 'multiple folders' do
47
+ let(:synced_folders) { {
48
+ "/vagrant" => {
49
+ :hostpath => '/home/test/default',
50
+ :disabled=>false,
51
+ :guestpath=>'/vagrant',
52
+ :type => :"9p",
53
+ },
54
+ "/custom" => {
55
+ :hostpath => '/home/test/custom',
56
+ :disabled=>false,
57
+ :guestpath=>'/custom',
58
+ :type => :"9p",
59
+ },
60
+ } }
61
+
62
+ let(:expected_xml) {
63
+ [ <<-XML1.unindent, <<-XML2.unindent ]
64
+ <filesystem type="mount" accessmode="passthrough">
65
+ <driver type="path" wrpolicy="immediate"></driver>
66
+ <source dir="/home/test/default"></source>
67
+ <target dir="1ef53e1c9b6a5a0c695c80665331652\"></target>
68
+ </filesystem>
69
+ XML1
70
+ <filesystem type="mount" accessmode="passthrough">
71
+ <driver type="path" wrpolicy="immediate"></driver>
72
+ <source dir="/home/test/custom"></source>
73
+ <target dir=\"a2a1a8b6d98be8f790f3c987e006d13\"></target>
74
+ </filesystem>
75
+ XML2
76
+ }
77
+
78
+ it 'should attach all device xml' do
79
+ expect(libvirt_domain).to receive(:attach_device).with(expected_xml[0], 0)
80
+ expect(libvirt_domain).to receive(:attach_device).with(expected_xml[1], 0)
81
+ expect(ui).to receive(:info).with(/Configuring 9p devices for shared folders/).once
82
+
83
+ subject.prepare(machine, synced_folders, {})
84
+ end
85
+ end
86
+ end
87
+
88
+ describe '#usable?' do
89
+ context 'with libvirt provider' do
90
+ before do
91
+ allow(machine).to receive(:provider_name).and_return(:libvirt)
92
+ allow(libvirt_client).to receive(:libversion).and_return(8002000)
93
+ end
94
+
95
+ it 'should be' do
96
+ expect(subject).to be_usable(machine)
97
+ end
98
+
99
+ context 'with version less than 1.2.2' do
100
+ before do
101
+ allow(libvirt_client).to receive(:libversion).and_return(1002001)
102
+ end
103
+
104
+ it 'should not be' do
105
+ expect(subject).to_not be_usable(machine)
106
+ end
107
+ end
108
+ end
109
+
110
+ context 'with other provider' do
111
+ before do
112
+ allow(machine).to receive(:provider_name).and_return(:virtualbox)
113
+ end
114
+
115
+ it 'should not be' do
116
+ expect(subject).to_not be_usable(machine)
117
+ end
118
+ end
119
+ end
120
+ end
@@ -0,0 +1,120 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'spec_helper'
4
+ require 'support/sharedcontext'
5
+
6
+ require 'vagrant-libvirt/cap/synced_folder_virtiofs'
7
+ require 'vagrant-libvirt/util/unindent'
8
+
9
+ describe VagrantPlugins::SyncedFolderVirtioFS::SyncedFolder do
10
+ include_context 'unit'
11
+ include_context 'libvirt'
12
+
13
+ subject { described_class.new }
14
+
15
+ describe '#prepare' do
16
+ let(:synced_folders) { {
17
+ "/vagrant" => {
18
+ :hostpath => '/home/test/default',
19
+ :disabled=>false,
20
+ :guestpath=>'/vagrant',
21
+ :type => :virtiofs,
22
+ },
23
+ } }
24
+
25
+ let(:expected_xml) {
26
+ <<-EOF.unindent
27
+ <filesystem type="mount" accessmode="passthrough">
28
+ <driver type="virtiofs"></driver>
29
+ <source dir="/home/test/default"></source>
30
+ <target dir="1ef53e1c9b6a5a0c695c80665331652\"></target>
31
+ </filesystem>
32
+ EOF
33
+ }
34
+
35
+ before do
36
+ allow(libvirt_client).to receive(:lookup_domain_by_uuid).and_return(libvirt_domain)
37
+ allow(logger).to receive(:debug)
38
+ end
39
+
40
+ it 'should attach device xml' do
41
+ expect(libvirt_domain).to receive(:attach_device).with(expected_xml, 0)
42
+
43
+ subject.prepare(machine, synced_folders, {})
44
+ end
45
+
46
+ context 'multiple folders' do
47
+ let(:synced_folders) { {
48
+ "/vagrant" => {
49
+ :hostpath => '/home/test/default',
50
+ :disabled=>false,
51
+ :guestpath=>'/vagrant',
52
+ :type => :virtiofs,
53
+ },
54
+ "/custom" => {
55
+ :hostpath => '/home/test/custom',
56
+ :disabled=>false,
57
+ :guestpath=>'/custom',
58
+ :type => :virtiofs,
59
+ },
60
+ } }
61
+
62
+ let(:expected_xml) {
63
+ [ <<-XML1.unindent, <<-XML2.unindent ]
64
+ <filesystem type="mount" accessmode="passthrough">
65
+ <driver type="virtiofs"></driver>
66
+ <source dir="/home/test/default"></source>
67
+ <target dir="1ef53e1c9b6a5a0c695c80665331652\"></target>
68
+ </filesystem>
69
+ XML1
70
+ <filesystem type="mount" accessmode="passthrough">
71
+ <driver type="virtiofs"></driver>
72
+ <source dir="/home/test/custom"></source>
73
+ <target dir=\"a2a1a8b6d98be8f790f3c987e006d13\"></target>
74
+ </filesystem>
75
+ XML2
76
+ }
77
+
78
+ it 'should attach all device xml' do
79
+ expect(libvirt_domain).to receive(:attach_device).with(expected_xml[0], 0)
80
+ expect(libvirt_domain).to receive(:attach_device).with(expected_xml[1], 0)
81
+ expect(ui).to receive(:info).with(/Configuring virtiofs devices for shared folders/).once
82
+
83
+ subject.prepare(machine, synced_folders, {})
84
+ end
85
+ end
86
+ end
87
+
88
+ describe '#usable?' do
89
+ context 'with libvirt provider' do
90
+ before do
91
+ allow(machine).to receive(:provider_name).and_return(:libvirt)
92
+ allow(libvirt_client).to receive(:libversion).and_return(8002000)
93
+ end
94
+
95
+ it 'should be' do
96
+ expect(subject).to be_usable(machine)
97
+ end
98
+
99
+ context 'with version less than 6.2.0' do
100
+ before do
101
+ allow(libvirt_client).to receive(:libversion).and_return(6001000)
102
+ end
103
+
104
+ it 'should not be' do
105
+ expect(subject).to_not be_usable(machine)
106
+ end
107
+ end
108
+ end
109
+
110
+ context 'with other provider' do
111
+ before do
112
+ allow(machine).to receive(:provider_name).and_return(:virtualbox)
113
+ end
114
+
115
+ it 'should not be' do
116
+ expect(subject).to_not be_usable(machine)
117
+ end
118
+ end
119
+ end
120
+ end