| 1 |
from textwrap import dedent |
| 2 |
from unittest.mock import ( |
| 3 |
patch, Mock, call, mock_open |
| 4 |
) |
| 5 |
|
| 6 |
import kiwi |
| 7 |
|
| 8 |
from kiwi.defaults import Defaults |
| 9 |
from kiwi.storage.subformat.vagrant_libvirt import DiskFormatVagrantLibVirt |
| 10 |
|
| 11 |
|
| 12 |
class TestDiskFormatVagrantLibVirt: |
| 13 |
def setup(self): |
| 14 |
Defaults.set_platform_name('x86_64') |
| 15 |
xml_data = Mock() |
| 16 |
xml_data.get_name = Mock( |
| 17 |
return_value='some-disk-image' |
| 18 |
) |
| 19 |
self.xml_state = Mock() |
| 20 |
self.xml_state.xml_data = xml_data |
| 21 |
self.xml_state.get_image_version = Mock( |
| 22 |
return_value='1.2.3' |
| 23 |
) |
| 24 |
self.vagrantconfig = Mock() |
| 25 |
self.vagrantconfig.get_embedded_vagrantfile = Mock( |
| 26 |
return_value=None |
| 27 |
) |
| 28 |
self.vagrantconfig.get_virtualsize = Mock( |
| 29 |
return_value=42 |
| 30 |
) |
| 31 |
self.runtime_config = Mock() |
| 32 |
self.runtime_config.get_bundle_compression.return_value = False |
| 33 |
kiwi.storage.subformat.base.RuntimeConfig = Mock( |
| 34 |
return_value=self.runtime_config |
| 35 |
) |
| 36 |
self.disk_format = DiskFormatVagrantLibVirt( |
| 37 |
self.xml_state, 'root_dir', 'target_dir', |
| 38 |
{'vagrantconfig': self.vagrantconfig} |
| 39 |
) |
| 40 |
assert self.disk_format.image_format == 'vagrant.libvirt.box' |
| 41 |
assert self.disk_format.provider == 'libvirt' |
| 42 |
|
| 43 |
def setup_method(self, cls): |
| 44 |
self.setup() |
| 45 |
|
| 46 |
@patch('kiwi.storage.subformat.vagrant_libvirt.Command.run') |
| 47 |
@patch('kiwi.storage.subformat.vagrant_libvirt.DiskFormatQcow2') |
| 48 |
def test_create_box_img( |
| 49 |
self, mock_qcow, mock_command |
| 50 |
): |
| 51 |
qcow = Mock() |
| 52 |
qcow.image_format = 'qcow2' |
| 53 |
mock_qcow.return_value = qcow |
| 54 |
assert self.disk_format.create_box_img('tmpdir') == [ |
| 55 |
'tmpdir/box.img' |
| 56 |
] |
| 57 |
qcow.create_image_format.assert_called_once_with() |
| 58 |
mock_command.assert_called_once_with( |
| 59 |
[ |
| 60 |
'mv', 'target_dir/some-disk-image.x86_64-1.2.3.qcow2', |
| 61 |
'tmpdir/box.img' |
| 62 |
] |
| 63 |
) |
| 64 |
|
| 65 |
def test_get_additional_metadata(self): |
| 66 |
assert self.disk_format.get_additional_metadata() == { |
| 67 |
'format': 'qcow2', 'virtual_size': 42 |
| 68 |
} |
| 69 |
|
| 70 |
def test_get_additional_vagrant_config_settings(self): |
| 71 |
assert self.disk_format.get_additional_vagrant_config_settings() == \ |
| 72 |
dedent(''' |
| 73 |
config.vm.synced_folder ".", "/vagrant", type: "rsync" |
| 74 |
config.vm.provider :libvirt do |libvirt| |
| 75 |
libvirt.driver = "kvm" |
| 76 |
end |
| 77 |
''').strip() |
| 78 |
|
| 79 |
@patch('kiwi.storage.subformat.vagrant_base.Command.run') |
| 80 |
@patch('kiwi.storage.subformat.vagrant_base.Temporary') |
| 81 |
@patch.object(DiskFormatVagrantLibVirt, 'create_box_img') |
| 82 |
def test_create_image_format( |
| 83 |
self, mock_create_box_img, mock_Temporary, mock_command |
| 84 |
): |
| 85 |
mock_Temporary.return_value.new_dir.return_value.name = 'tmpdir' |
| 86 |
mock_create_box_img.return_value = ['arbitrary'] |
| 87 |
|
| 88 |
m_open = mock_open() |
| 89 |
with patch('builtins.open', m_open, create=True): |
| 90 |
self.disk_format.create_image_format() |
| 91 |
|
| 92 |
assert m_open.return_value.write.call_args_list[1] == call( |
| 93 |
dedent(''' |
| 94 |
Vagrant.configure("2") do |config| |
| 95 |
config.vm.synced_folder ".", "/vagrant", type: "rsync" |
| 96 |
config.vm.provider :libvirt do |libvirt| |
| 97 |
libvirt.driver = "kvm" |
| 98 |
end |
| 99 |
end |
| 100 |
''').strip() |
| 101 |
) |