Debian Codegraph @

test/unit/storage/subformat/vhdfixed_test.py

raw
1 import sys
2 from builtins import bytes
3 from unittest.mock import (
4 call, patch, Mock, mock_open
5 )
6 from pytest import raises
7
8 import kiwi
9
10 from kiwi.defaults import Defaults
11 from kiwi.storage.subformat.vhdfixed import DiskFormatVhdFixed
12
13 from kiwi.exceptions import KiwiVhdTagError
14
15
16 class TestDiskFormatVhdFixed:
17 def setup(self):
18 Defaults.set_platform_name('x86_64')
19 xml_data = Mock()
20 xml_data.get_name = Mock(
21 return_value='some-disk-image'
22 )
23 self.xml_state = Mock()
24 self.xml_state.xml_data = xml_data
25 self.xml_state.get_image_version = Mock(
26 return_value='1.2.3'
27 )
28 self.xml_state.get_luks_credentials = Mock(
29 return_value=None
30 )
31 self.runtime_config = Mock()
32 self.runtime_config.get_bundle_compression.return_value = True
33 kiwi.storage.subformat.base.RuntimeConfig = Mock(
34 return_value=self.runtime_config
35 )
36 self.disk_format = DiskFormatVhdFixed(
37 self.xml_state, 'root_dir', 'target_dir', {'force_size': None}
38 )
39
40 def setup_method(self, cls):
41 self.setup()
42
43 def test_post_init(self):
44 self.disk_format.post_init({'option': 'value', '--tag': 'tag'})
45 assert self.disk_format.options == [
46 '-o', 'option=value', '-o', 'subformat=fixed'
47 ]
48 assert self.disk_format.tag == 'tag'
49
50 @patch('kiwi.storage.subformat.vhdfixed.Command.run')
51 def test_create_image_format_invalid_tag(self, mock_command):
52 self.disk_format.tag = 'invalid'
53 with raises(KiwiVhdTagError):
54 self.disk_format.create_image_format()
55
56 @patch('kiwi.storage.subformat.vhdfixed.Command.run')
57 def test_create_image_format(self, mock_command):
58 self.disk_format.tag = '12345678-1234-1234-1234-123456789999'
59
60 m_open = mock_open(read_data='dev_zero_data')
61 with patch('builtins.open', m_open, create=True):
62 self.disk_format.create_image_format()
63
64 mock_command.assert_called_once_with(
65 [
66 'qemu-img', 'convert', '-f', 'raw',
67 'target_dir/some-disk-image.x86_64-1.2.3.raw', '-O', 'vpc',
68 '-o', 'force_size', '-o', 'subformat=fixed',
69 'target_dir/some-disk-image.x86_64-1.2.3.vhdfixed'
70 ]
71 )
72 assert m_open.call_args_list == [
73 call('target_dir/some-disk-image.x86_64-1.2.3.vhdfixed', 'r+b'),
74 call('/dev/zero', 'rb'),
75 call('target_dir/some-disk-image.x86_64-1.2.3.vhdfixed', 'r+b')
76 ]
77 assert m_open.return_value.write.call_args_list[0] == call(
78 'dev_zero_data'
79 )
80 if sys.byteorder == 'little':
81 # on little endian machines
82 assert m_open.return_value.write.call_args_list[1] == call(
83 bytes(b'xV4\x124\x124\x12\x124\x124Vx\x99\x99')
84 )
85 else:
86 # on big endian machines
87 assert m_open.return_value.write.call_args_list[1] == call(
88 bytes(b'\x124Vx\x124\x124\x124\x124Vx\x99\x99')
89 )
90 assert m_open.return_value.seek.call_args_list == [
91 call(65536, 0), call(0, 2),
92 call(65536, 0), call(0, 2)
93 ]
94
95 def test_store_to_result_default(self):
96 result = Mock()
97 self.disk_format.store_to_result(result)
98 result.add.assert_called_once_with(
99 compress=True,
100 filename='target_dir/some-disk-image.x86_64-1.2.3.vhdfixed',
101 key='disk_format_image',
102 shasum=True,
103 use_for_bundle=True
104 )
105
106 def test_store_to_result_with_luks(self):
107 result = Mock()
108 self.xml_state.get_luks_credentials = Mock(return_value='foo')
109 self.disk_format.store_to_result(result)
110 result.add.assert_called_once_with(
111 compress=False,
112 filename='target_dir/some-disk-image.x86_64-1.2.3.vhdfixed',
113 key='disk_format_image',
114 shasum=True,
115 use_for_bundle=True
116 )
117 self.xml_state.get_luks_credentials = Mock(return_value=None)