Debian Codegraph @

test/unit/storage/subformat/vagrant_base_test.py

raw
1 import io
2 from pytest import raises
3 from unittest.mock import (
4 call, patch, mock_open
5 )
6 from unittest.mock import (
7 Mock, MagicMock
8 )
9
10 import kiwi
11
12 from kiwi.defaults import Defaults
13 from kiwi.exceptions import KiwiFormatSetupError
14 from kiwi.storage.subformat.vagrant_base import DiskFormatVagrantBase
15
16 from textwrap import dedent
17
18
19 class TestDiskFormatVagrantBase:
20 def setup(self):
21 Defaults.set_platform_name('x86_64')
22 xml_data = Mock()
23 xml_data.get_name = Mock(
24 return_value='some-disk-image'
25 )
26 xml_data.description_dir = './'
27 self.xml_state = Mock()
28 self.xml_state.xml_data = xml_data
29 self.xml_state.get_image_version = Mock(
30 return_value='1.2.3'
31 )
32 self.vagrantconfig = Mock()
33 self.vagrantconfig.get_embedded_vagrantfile = Mock(
34 return_value=None
35 )
36 self.vagrantconfig.get_virtualsize = Mock(
37 return_value=42
38 )
39 self.runtime_config = Mock()
40 self.runtime_config.get_bundle_compression.return_value = False
41 kiwi.storage.subformat.base.RuntimeConfig = Mock(
42 return_value=self.runtime_config
43 )
44 self.disk_format = DiskFormatVagrantBase(
45 self.xml_state, 'root_dir', 'target_dir',
46 {'vagrantconfig': self.vagrantconfig}
47 )
48 assert self.disk_format.image_format == ''
49 assert self.disk_format.provider is None
50
51 def setup_method(self, cls):
52 self.setup()
53
54 def test_create_box_img_not_implemented(self):
55 with raises(NotImplementedError):
56 self.disk_format.create_box_img('arbitrary')
57
58 def test_post_init_missing_custom_arguments(self):
59 with raises(KiwiFormatSetupError):
60 self.disk_format.post_init(custom_args=None)
61
62 def test_post_init_missing_vagrantconfig(self):
63 with raises(KiwiFormatSetupError):
64 self.disk_format.post_init({'vagrantconfig': None})
65
66 def test_create_image_format_missing_provider_setup(self):
67 with raises(NotImplementedError):
68 self.disk_format.create_image_format()
69
70 def test_store_to_result_missing_provider_setup(self):
71 with raises(NotImplementedError):
72 self.disk_format.store_to_result(Mock())
73
74 @patch('kiwi.storage.subformat.vagrant_base.Command.run')
75 @patch('kiwi.storage.subformat.vagrant_base.Temporary')
76 @patch.object(DiskFormatVagrantBase, 'create_box_img')
77 def test_create_image_format(
78 self, mock_create_box_img, mock_Temporary, mock_command
79 ):
80 # select an example provider
81 self.disk_format.image_format = 'vagrant.libvirt.box'
82 self.disk_format.provider = 'libvirt'
83
84 metadata_json = dedent('''
85 {
86 "provider": "libvirt"
87 }
88 ''').strip()
89
90 expected_vagrantfile = dedent('''
91 Vagrant.configure("2") do |config|
92 end
93 ''').strip()
94
95 mock_Temporary.return_value.new_dir.return_value.name = 'tmpdir'
96
97 with patch('builtins.open', create=True) as mock_file:
98 mock_file.return_value = MagicMock(spec=io.IOBase)
99 file_handle = mock_file.return_value.__enter__.return_value
100
101 self.disk_format.create_image_format()
102
103 assert mock_file.call_args_list == [
104 call('tmpdir/metadata.json', 'w'),
105 call('tmpdir/Vagrantfile', 'w')
106 ]
107 assert file_handle.write.call_args_list == [
108 call(metadata_json), call(expected_vagrantfile)
109 ]
110
111 mock_command.assert_called_once_with(
112 [
113 'tar', '-C', 'tmpdir', '-czf',
114 'target_dir/some-disk-image.x86_64-1.2.3.vagrant.libvirt.box',
115 'metadata.json', 'Vagrantfile'
116 ]
117 )
118
119 @patch('kiwi.storage.subformat.vagrant_base.Command.run')
120 @patch('kiwi.storage.subformat.vagrant_base.Temporary')
121 @patch.object(DiskFormatVagrantBase, 'create_box_img')
122 def test_user_provided_vagrantfile(
123 self, mock_create_box_img, mock_Temporary, mock_cmd_run
124 ):
125 # select an example provider
126 self.disk_format.image_format = 'vagrant.virtualbox.box'
127 self.disk_format.provider = 'virtualbox'
128
129 # deterministic tempdir:
130 mock_Temporary.return_value.new_dir.return_value.name = 'tmpdir'
131
132 self.vagrantconfig.get_embedded_vagrantfile = Mock(
133 return_value='example_Vagrantfile'
134 )
135
136 expected_vagrantfile = r"it's something"
137
138 with patch(
139 'builtins.open', mock_open(read_data=expected_vagrantfile)
140 ) as mock_file:
141 self.disk_format.create_image_format()
142 file_handle = mock_file.return_value.__enter__.return_value
143
144 assert mock_file.call_args_list == [
145 call('tmpdir/metadata.json', 'w'),
146 call('tmpdir/Vagrantfile', 'w'),
147 call('./example_Vagrantfile', 'r')
148 ]
149 assert file_handle.write.call_args_list[1] == call(
150 expected_vagrantfile
151 )
152
153 def test_get_additional_vagrant_config_settings(self):
154 assert self.disk_format.get_additional_vagrant_config_settings() == ''
155
156 def test_get_additional_metadata(self):
157 assert self.disk_format.get_additional_metadata() == {}
158
159 def test_store_to_result(self):
160 # select an example provider
161 self.disk_format.image_format = 'vagrant.libvirt.box'
162 result = Mock()
163 self.disk_format.store_to_result(result)
164 result.add.assert_called_once_with(
165 compress=False,
166 filename='target_dir/'
167 'some-disk-image.x86_64-1.2.3.vagrant.libvirt.box',
168 key='disk_format_image',
169 shasum=True,
170 use_for_bundle=True
171 )