Debian Codegraph @

test/unit/storage/subformat/init_test.py

raw
1 from unittest.mock import (
2 patch, Mock
3 )
4 from pytest import raises
5
6 from kiwi.storage.subformat import DiskFormat
7
8 from kiwi.exceptions import KiwiDiskFormatSetupError
9
10
11 class TestDiskFormat:
12 def setup(self):
13 self.xml_state = Mock()
14 self.xml_state.get_build_type_format_options.return_value = {}
15
16 def setup_method(self, cls):
17 self.setup()
18
19 def test_format_not_implemented(self):
20 with raises(KiwiDiskFormatSetupError):
21 DiskFormat.new('foo', self.xml_state, 'root_dir', 'target_dir')
22
23 def test_disk_format_vagrant_not_implemented(self):
24 self.xml_state.get_build_type_vagrant_config_section = Mock(
25 return_value=None
26 )
27 with raises(KiwiDiskFormatSetupError):
28 DiskFormat.new(
29 'vagrant', self.xml_state, 'root_dir', 'target_dir'
30 )
31
32 @patch('kiwi.storage.subformat.qcow2.DiskFormatQcow2')
33 def test_disk_format_qcow2(self, mock_qcow2):
34 DiskFormat.new('qcow2', self.xml_state, 'root_dir', 'target_dir')
35 mock_qcow2.assert_called_once_with(
36 self.xml_state, 'root_dir', 'target_dir', {}
37 )
38
39 @patch('kiwi.storage.subformat.vdi.DiskFormatVdi')
40 def test_disk_format_vdi(self, mock_vdi):
41 DiskFormat.new('vdi', self.xml_state, 'root_dir', 'target_dir')
42 mock_vdi.assert_called_once_with(
43 self.xml_state, 'root_dir', 'target_dir', {}
44 )
45
46 @patch('kiwi.storage.subformat.vhd.DiskFormatVhd')
47 def test_disk_format_vhd(self, mock_vhd):
48 DiskFormat.new('vhd', self.xml_state, 'root_dir', 'target_dir')
49 mock_vhd.assert_called_once_with(
50 self.xml_state, 'root_dir', 'target_dir', {}
51 )
52
53 @patch('kiwi.storage.subformat.vhdx.DiskFormatVhdx')
54 def test_disk_format_vhdx(self, mock_vhdx):
55 DiskFormat.new('vhdx', self.xml_state, 'root_dir', 'target_dir')
56 mock_vhdx.assert_called_once_with(
57 self.xml_state, 'root_dir', 'target_dir', {}
58 )
59
60 @patch('kiwi.storage.subformat.vhdfixed.DiskFormatVhdFixed')
61 def test_disk_format_vhdfixed(self, mock_vhdfixed):
62 self.xml_state.build_type.get_vhdfixedtag = Mock(
63 return_value='disk-tag'
64 )
65 DiskFormat.new('vhd-fixed', self.xml_state, 'root_dir', 'target_dir')
66 mock_vhdfixed.assert_called_once_with(
67 self.xml_state, 'root_dir', 'target_dir', {'--tag': 'disk-tag'}
68 )
69
70 @patch('kiwi.storage.subformat.gce.DiskFormatGce')
71 def test_disk_format_gce(self, mock_gce):
72 self.xml_state.build_type.get_gcelicense = Mock(
73 return_value='gce_license_tag'
74 )
75 DiskFormat.new('gce', self.xml_state, 'root_dir', 'target_dir')
76 mock_gce.assert_called_once_with(
77 self.xml_state, 'root_dir', 'target_dir',
78 {'--tag': 'gce_license_tag'}
79 )
80
81 @patch('kiwi.storage.subformat.vmdk.DiskFormatVmdk')
82 def test_disk_format_vmdk(self, mock_vmdk):
83 vmdisk = Mock()
84 vmdisk.get_controller = Mock(
85 return_value='controller'
86 )
87 vmdisk.get_diskmode = Mock(
88 return_value='disk-mode'
89 )
90 self.xml_state.get_build_type_vmdisk_section = Mock(
91 return_value=vmdisk
92 )
93 DiskFormat.new('vmdk', self.xml_state, 'root_dir', 'target_dir')
94 mock_vmdk.assert_called_once_with(
95 self.xml_state, 'root_dir', 'target_dir',
96 {'adapter_type=controller': None, 'subformat=disk-mode': None}
97 )
98
99 @patch('kiwi.storage.subformat.ova.DiskFormatOva')
100 def test_disk_format_ova(self, mock_ova):
101 vmdisk = Mock()
102 vmdisk.get_controller = Mock(
103 return_value='controller'
104 )
105 vmdisk.get_diskmode = Mock(
106 return_value='disk-mode'
107 )
108 self.xml_state.get_build_type_vmdisk_section = Mock(
109 return_value=vmdisk
110 )
111 DiskFormat.new('ova', self.xml_state, 'root_dir', 'target_dir')
112 mock_ova.assert_called_once_with(
113 self.xml_state, 'root_dir', 'target_dir',
114 {'adapter_type=controller': None, 'subformat=disk-mode': None}
115 )
116
117 @patch('kiwi.storage.subformat.oci.DiskFormatOci')
118 def test_disk_format_oci(self, mock_oci):
119 DiskFormat.new(
120 'oci:raw', self.xml_state, 'root_dir', 'target_dir'
121 )
122 mock_oci.assert_called_once_with(
123 self.xml_state, 'root_dir', 'target_dir',
124 {'base_format': 'raw', 'container_format': 'oci'}
125 )
126 mock_oci.reset_mock()
127 DiskFormat.new(
128 'docker:raw', self.xml_state, 'root_dir', 'target_dir'
129 )
130 mock_oci.assert_called_once_with(
131 self.xml_state, 'root_dir', 'target_dir',
132 {'base_format': 'raw', 'container_format': 'docker'}
133 )
134
135 @patch('kiwi.storage.subformat.vagrant_virtualbox.DiskFormatVagrantVirtualBox')
136 @patch('kiwi.storage.subformat.vagrant_libvirt.DiskFormatVagrantLibVirt')
137 def test_disk_format_vagrant_libvirt(
138 self, mock_vagrant_libvirt, mock_vagrant_virt_box
139 ):
140 for provider_name, provider_mock in (
141 ('libvirt', mock_vagrant_libvirt),
142 ('virtualbox', mock_vagrant_virt_box)
143 ):
144 vagrant_config = Mock()
145 vagrant_config.get_provider = Mock(
146 return_value=provider_name
147 )
148 self.xml_state.get_build_type_vagrant_config_section = Mock(
149 return_value=vagrant_config
150 )
151 DiskFormat.new(
152 'vagrant', self.xml_state, 'root_dir', 'target_dir'
153 )
154 provider_mock.assert_called_once_with(
155 self.xml_state, 'root_dir', 'target_dir',
156 {'vagrantconfig': vagrant_config}
157 )
158
159 @patch('kiwi.storage.subformat.base.DiskFormatBase')
160 def test_disk_format_base(self, mock_base):
161 DiskFormat.new('raw', self.xml_state, 'root_dir', 'target_dir')
162 mock_base.assert_called_once_with(
163 self.xml_state, 'root_dir', 'target_dir', {}
164 )