1616# under the License.
1717
1818import mock
19+ import os
1920import shutil
2021import tempfile
2122
@@ -38,20 +39,41 @@ def setUp(self):
3839 super (TestImageExtension , self ).setUp ()
3940 self .agent_extension = image .ImageExtension ()
4041 self .fake_dev = '/dev/fake'
42+ self .fake_efi_system_part = '/dev/fake1'
4143 self .fake_root_part = '/dev/fake2'
4244 self .fake_root_uuid = '11111111-2222-3333-4444-555555555555'
45+ self .fake_efi_system_part_uuid = '45AB-2312'
4346 self .fake_dir = '/tmp/fake-dir'
4447
4548 @mock .patch .object (image , '_install_grub2' )
46- def test_install_bootloader (self , mock_grub2 , mock_execute , mock_dispatch ):
49+ def test_install_bootloader_bios (self , mock_grub2 , mock_execute ,
50+ mock_dispatch ):
4751 mock_dispatch .return_value = self .fake_dev
4852 self .agent_extension .install_bootloader (root_uuid = self .fake_root_uuid )
4953 mock_dispatch .assert_called_once_with ('get_os_install_device' )
50- mock_grub2 .assert_called_once_with (self .fake_dev , self .fake_root_uuid )
54+ mock_grub2 .assert_called_once_with (
55+ self .fake_dev , root_uuid = self .fake_root_uuid ,
56+ efi_system_part_uuid = None )
5157
52- @mock .patch .object (image , '_get_root_partition' )
53- def test__install_grub2 (self , mock_get_root , mock_execute , mock_dispatch ):
54- mock_get_root .return_value = self .fake_root_part
58+ @mock .patch .object (image , '_install_grub2' )
59+ def test_install_bootloader_uefi (self , mock_grub2 , mock_execute ,
60+ mock_dispatch ):
61+ mock_dispatch .return_value = self .fake_dev
62+ self .agent_extension .install_bootloader (
63+ root_uuid = self .fake_root_uuid ,
64+ efi_system_part_uuid = self .fake_efi_system_part_uuid )
65+ mock_dispatch .assert_called_once_with ('get_os_install_device' )
66+ mock_grub2 .assert_called_once_with (
67+ self .fake_dev ,
68+ root_uuid = self .fake_root_uuid ,
69+ efi_system_part_uuid = self .fake_efi_system_part_uuid )
70+
71+ @mock .patch .object (os , 'environ' )
72+ @mock .patch .object (image , '_get_partition' )
73+ def test__install_grub2 (self , mock_get_part_uuid , environ_mock ,
74+ mock_execute , mock_dispatch ):
75+ mock_get_part_uuid .return_value = self .fake_root_part
76+ environ_mock .get .return_value = '/sbin'
5577 image ._install_grub2 (self .fake_dev , self .fake_root_uuid )
5678
5779 expected = [mock .call ('mount' , '/dev/fake2' , self .fake_dir ),
@@ -63,11 +85,13 @@ def test__install_grub2(self, mock_get_root, mock_execute, mock_dispatch):
6385 self .fake_dir + '/proc' ),
6486 mock .call (('chroot %s /bin/bash -c '
6587 '"/usr/sbin/grub-install %s"' %
66- (self .fake_dir , self .fake_dev )), shell = True ),
88+ (self .fake_dir , self .fake_dev )), shell = True ,
89+ env_variables = {'PATH' : '/sbin:/bin' }),
6790 mock .call (('chroot %s /bin/bash -c '
6891 '"/usr/sbin/grub-mkconfig -o '
6992 '/boot/grub/grub.cfg"' % self .fake_dir ),
70- shell = True ),
93+ shell = True ,
94+ env_variables = {'PATH' : '/sbin:/bin' }),
7195 mock .call ('umount' , self .fake_dir + '/dev' ,
7296 attempts = 3 , delay_on_retry = True ),
7397 mock .call ('umount' , self .fake_dir + '/sys' ,
@@ -77,30 +101,123 @@ def test__install_grub2(self, mock_get_root, mock_execute, mock_dispatch):
77101 mock .call ('umount' , self .fake_dir , attempts = 3 ,
78102 delay_on_retry = True )]
79103 mock_execute .assert_has_calls (expected )
80- mock_get_root .assert_called_once_with (self .fake_dev ,
81- self .fake_root_uuid )
104+ mock_get_part_uuid .assert_called_once_with (self .fake_dev ,
105+ uuid = self .fake_root_uuid )
106+ self .assertFalse (mock_dispatch .called )
107+
108+ @mock .patch .object (os , 'environ' )
109+ @mock .patch .object (os , 'makedirs' )
110+ @mock .patch .object (image , '_get_partition' )
111+ def test__install_grub2_uefi (self , mock_get_part_uuid , mkdir_mock ,
112+ environ_mock , mock_execute ,
113+ mock_dispatch ):
114+ mock_get_part_uuid .side_effect = [self .fake_root_part ,
115+ self .fake_efi_system_part ]
116+ environ_mock .get .return_value = '/sbin'
117+
118+ image ._install_grub2 (
119+ self .fake_dev , root_uuid = self .fake_root_uuid ,
120+ efi_system_part_uuid = self .fake_efi_system_part_uuid )
121+
122+ expected = [mock .call ('mount' , '/dev/fake2' , self .fake_dir ),
123+ mock .call ('mount' , '-o' , 'bind' , '/dev' ,
124+ self .fake_dir + '/dev' ),
125+ mock .call ('mount' , '-o' , 'bind' , '/sys' ,
126+ self .fake_dir + '/sys' ),
127+ mock .call ('mount' , '-o' , 'bind' , '/proc' ,
128+ self .fake_dir + '/proc' ),
129+ mock .call ('mount' , self .fake_efi_system_part ,
130+ self .fake_dir + '/boot/efi' ),
131+ mock .call (('chroot %s /bin/bash -c '
132+ '"/usr/sbin/grub-install %s"' %
133+ (self .fake_dir , self .fake_dev )), shell = True ,
134+ env_variables = {'PATH' : '/sbin:/bin' }),
135+ mock .call (('chroot %s /bin/bash -c '
136+ '"/usr/sbin/grub-mkconfig -o '
137+ '/boot/grub/grub.cfg"' % self .fake_dir ),
138+ shell = True ,
139+ env_variables = {'PATH' : '/sbin:/bin' }),
140+ mock .call ('umount' , self .fake_dir + '/boot/efi' ,
141+ attempts = 3 , delay_on_retry = True ),
142+ mock .call ('umount' , self .fake_dir + '/dev' ,
143+ attempts = 3 , delay_on_retry = True ),
144+ mock .call ('umount' , self .fake_dir + '/sys' ,
145+ attempts = 3 , delay_on_retry = True ),
146+ mock .call ('umount' , self .fake_dir + '/proc' ,
147+ attempts = 3 , delay_on_retry = True ),
148+ mock .call ('umount' , self .fake_dir , attempts = 3 ,
149+ delay_on_retry = True )]
150+ mkdir_mock .assert_called_once_with (self .fake_dir + '/boot/efi' )
151+ mock_execute .assert_has_calls (expected )
152+ mock_get_part_uuid .assert_any_call (self .fake_dev ,
153+ uuid = self .fake_root_uuid )
154+ mock_get_part_uuid .assert_any_call (self .fake_dev ,
155+ uuid = self .fake_efi_system_part_uuid )
82156 self .assertFalse (mock_dispatch .called )
83157
84- @mock .patch .object (image , '_get_root_partition' )
85- def test__install_grub2_command_fail (self , mock_get_root , mock_execute ,
158+ @mock .patch .object (os , 'environ' )
159+ @mock .patch .object (os , 'makedirs' )
160+ @mock .patch .object (image , '_get_partition' )
161+ def test__install_grub2_uefi_umount_fails (
162+ self , mock_get_part_uuid , mkdir_mock , environ_mock ,
163+ mock_execute , mock_dispatch ):
164+ mock_get_part_uuid .side_effect = [self .fake_root_part ,
165+ self .fake_efi_system_part ]
166+
167+ def umount_raise_func (* args , ** kwargs ):
168+ if args [0 ] == 'umount' :
169+ raise processutils .ProcessExecutionError ('error' )
170+
171+ mock_execute .side_effect = umount_raise_func
172+ environ_mock .get .return_value = '/sbin'
173+ self .assertRaises (errors .CommandExecutionError ,
174+ image ._install_grub2 ,
175+ self .fake_dev , root_uuid = self .fake_root_uuid ,
176+ efi_system_part_uuid = self .fake_efi_system_part_uuid )
177+
178+ expected = [mock .call ('mount' , '/dev/fake2' , self .fake_dir ),
179+ mock .call ('mount' , '-o' , 'bind' , '/dev' ,
180+ self .fake_dir + '/dev' ),
181+ mock .call ('mount' , '-o' , 'bind' , '/sys' ,
182+ self .fake_dir + '/sys' ),
183+ mock .call ('mount' , '-o' , 'bind' , '/proc' ,
184+ self .fake_dir + '/proc' ),
185+ mock .call ('mount' , self .fake_efi_system_part ,
186+ self .fake_dir + '/boot/efi' ),
187+ mock .call (('chroot %s /bin/bash -c '
188+ '"/usr/sbin/grub-install %s"' %
189+ (self .fake_dir , self .fake_dev )), shell = True ,
190+ env_variables = {'PATH' : '/sbin:/bin' }),
191+ mock .call (('chroot %s /bin/bash -c '
192+ '"/usr/sbin/grub-mkconfig -o '
193+ '/boot/grub/grub.cfg"' % self .fake_dir ),
194+ shell = True ,
195+ env_variables = {'PATH' : '/sbin:/bin' }),
196+ mock .call ('umount' , self .fake_dir + '/boot/efi' ,
197+ attempts = 3 , delay_on_retry = True )]
198+ mock_execute .assert_has_calls (expected )
199+
200+ @mock .patch .object (image , '_get_partition' )
201+ def test__install_grub2_command_fail (self , mock_get_part_uuid ,
202+ mock_execute ,
86203 mock_dispatch ):
87- mock_get_root .return_value = self .fake_root_part
204+ mock_get_part_uuid .return_value = self .fake_root_part
88205 mock_execute .side_effect = processutils .ProcessExecutionError ('boom' )
89206
90207 self .assertRaises (errors .CommandExecutionError , image ._install_grub2 ,
91208 self .fake_dev , self .fake_root_uuid )
92209
93- mock_get_root .assert_called_once_with (self .fake_dev ,
94- self .fake_root_uuid )
210+ mock_get_part_uuid .assert_called_once_with (self .fake_dev ,
211+ uuid = self .fake_root_uuid )
95212 self .assertFalse (mock_dispatch .called )
96213
97- def test__get_root_partition (self , mock_execute , mock_dispatch ):
214+ def test__get_partition (self , mock_execute , mock_dispatch ):
98215 lsblk_output = ('''KNAME="test" UUID="" TYPE="disk"
99216 KNAME="test1" UUID="256a39e3-ca3c-4fb8-9cc2-b32eec441f47" TYPE="part"
100217 KNAME="test2" UUID="%s" TYPE="part"''' % self .fake_root_uuid )
101218 mock_execute .side_effect = (None , [lsblk_output ])
102219
103- root_part = image ._get_root_partition (self .fake_dev ,
220+ root_part = image ._get_partition (self .fake_dev ,
104221 self .fake_root_uuid )
105222 self .assertEqual ('/dev/test2' , root_part )
106223 expected = [mock .call ('partx' , '-u' , self .fake_dev , attempts = 3 ,
@@ -109,28 +226,28 @@ def test__get_root_partition(self, mock_execute, mock_dispatch):
109226 mock_execute .assert_has_calls (expected )
110227 self .assertFalse (mock_dispatch .called )
111228
112- def test__get_root_partition_no_device_found (self , mock_execute ,
229+ def test__get_partition_no_device_found (self , mock_execute ,
113230 mock_dispatch ):
114231 lsblk_output = ('''KNAME="test" UUID="" TYPE="disk"
115232 KNAME="test1" UUID="256a39e3-ca3c-4fb8-9cc2-b32eec441f47" TYPE="part"
116233 KNAME="test2" UUID="" TYPE="part"''' )
117234 mock_execute .side_effect = (None , [lsblk_output ])
118235
119236 self .assertRaises (errors .DeviceNotFound ,
120- image ._get_root_partition , self .fake_dev ,
237+ image ._get_partition , self .fake_dev ,
121238 self .fake_root_uuid )
122239 expected = [mock .call ('partx' , '-u' , self .fake_dev , attempts = 3 ,
123240 delay_on_retry = True ),
124241 mock .call ('lsblk' , '-PbioKNAME,UUID,TYPE' , self .fake_dev )]
125242 mock_execute .assert_has_calls (expected )
126243 self .assertFalse (mock_dispatch .called )
127244
128- def test__get_root_partition_command_fail (self , mock_execute ,
245+ def test__get_partition_command_fail (self , mock_execute ,
129246 mock_dispatch ):
130247 mock_execute .side_effect = (None ,
131248 processutils .ProcessExecutionError ('boom' ))
132249 self .assertRaises (errors .CommandExecutionError ,
133- image ._get_root_partition , self .fake_dev ,
250+ image ._get_partition , self .fake_dev ,
134251 self .fake_root_uuid )
135252
136253 expected = [mock .call ('partx' , '-u' , self .fake_dev , attempts = 3 ,
0 commit comments