Debian Codegraph @

test/unit/storage/subformat/gce_test.py

raw
1 from unittest.mock import (
2 patch, call, mock_open
3 )
4 import unittest.mock as mock
5 import kiwi
6
7 from kiwi.defaults import Defaults
8 from kiwi.storage.subformat.gce import DiskFormatGce
9
10
11 class TestDiskFormatGce:
12 def setup(self):
13 Defaults.set_platform_name('x86_64')
14 xml_data = mock.Mock()
15 xml_data.get_name = mock.Mock(
16 return_value='some-disk-image'
17 )
18 self.xml_state = mock.Mock()
19 self.xml_state.xml_data = xml_data
20 self.xml_state.get_image_version = mock.Mock(
21 return_value='0.8.15'
22 )
23 self.runtime_config = mock.Mock()
24 self.runtime_config.get_bundle_compression.return_value = True
25 kiwi.storage.subformat.base.RuntimeConfig = mock.Mock(
26 return_value=self.runtime_config
27 )
28 self.disk_format = DiskFormatGce(
29 self.xml_state, 'root_dir', 'target_dir'
30 )
31
32 def setup_method(self, cls):
33 self.setup()
34
35 def test_post_init(self):
36 self.disk_format.post_init({'option': 'value', '--tag': 'tag'})
37 assert self.disk_format.tag == 'tag'
38
39 def test_store_to_result(self):
40 result = mock.Mock()
41 self.disk_format.store_to_result(result)
42 result.add.assert_called_once_with(
43 compress=False,
44 filename='target_dir/some-disk-image.x86_64-0.8.15.tar.gz',
45 key='disk_format_image',
46 shasum=True,
47 use_for_bundle=True
48 )
49
50 @patch('kiwi.storage.subformat.gce.Command.run')
51 @patch('kiwi.storage.subformat.gce.ArchiveTar')
52 @patch('kiwi.storage.subformat.gce.Temporary')
53 def test_create_image_format(
54 self, mock_Temporary, mock_archive, mock_command
55 ):
56 mock_Temporary.return_value.new_dir.return_value.name = 'tmpdir'
57 archive = mock.Mock()
58 mock_archive.return_value = archive
59 self.disk_format.tag = 'gce-license'
60
61 m_open = mock_open()
62 with patch('builtins.open', m_open, create=True):
63 self.disk_format.create_image_format()
64
65 mock_command.assert_called_once_with(
66 [
67 'cp', 'target_dir/some-disk-image.x86_64-0.8.15.raw',
68 'tmpdir/disk.raw'
69 ]
70 )
71 assert m_open.call_args_list == [
72 call('tmpdir/manifest.json', 'w')
73 ]
74 assert m_open.return_value.write.call_args_list == [
75 call('{"licenses": ["gce-license"]}')
76 ]
77 mock_archive.assert_called_once_with(
78 filename='target_dir/some-disk-image.x86_64-0.8.15.tar',
79 file_list=['manifest.json', 'disk.raw']
80 )
81 archive.create_gnu_gzip_compressed.assert_called_once_with(
82 'tmpdir'
83 )
84 assert self.disk_format.get_target_file_path_for_format('gce') == \
85 'target_dir/some-disk-image.x86_64-0.8.15.tar.gz'