Skip to content

Commit b997dd1

Browse files
authored
Fix/issue 317 (#323)
* adding test to cover the failed scenario * fixing test * raising exception when install message has an error * linting * fixing unit test
1 parent 2e320b4 commit b997dd1

File tree

2 files changed

+29
-1
lines changed

2 files changed

+29
-1
lines changed

pyntc/devices/ios_device.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -718,7 +718,10 @@ def install_os(self, image_name, install_mode=False, read_timeout=2000, **vendor
718718
)
719719
# Set a higher read_timeout and send it in
720720
try:
721-
self.show(command, read_timeout=read_timeout)
721+
install_message = self.show(command, read_timeout=read_timeout)
722+
if install_message.startswith("FAILED:"):
723+
log.error("Host %s: OS install error for image %s", self.host, image_name)
724+
raise OSInstallError(hostname=self.hostname, desired_boot=image_name)
722725
except IOError:
723726
log.error("Host %s: IO error for image %s", self.host, image_name)
724727
except CommandError:

tests/unit/test_devices/test_ios_device.py

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -389,6 +389,30 @@ def test_install_os_error(self, mock_wait, mock_reboot, mock_set_boot, mock_imag
389389
mock_raw_version_data.return_value = DEVICE_FACTS
390390
self.assertRaises(ios_module.OSInstallError, self.device.install_os, BOOT_IMAGE)
391391

392+
@mock.patch.object(IOSDevice, "os_version", new_callable=mock.PropertyMock)
393+
@mock.patch.object(IOSDevice, "_image_booted", side_effect=[False, True])
394+
@mock.patch.object(IOSDevice, "set_boot_options")
395+
@mock.patch.object(IOSDevice, "show")
396+
@mock.patch.object(IOSDevice, "reboot")
397+
@mock.patch.object(IOSDevice, "_wait_for_device_reboot")
398+
@mock.patch.object(IOSDevice, "_raw_version_data")
399+
def test_install_os_not_enough_space(
400+
self,
401+
mock_raw_version_data,
402+
mock_wait,
403+
mock_reboot,
404+
mock_show,
405+
mock_set_boot,
406+
mock_image_booted,
407+
mock_os_version,
408+
):
409+
mock_raw_version_data.return_value = DEVICE_FACTS
410+
mock_os_version.return_value = "17.4.3"
411+
mock_show.return_value = "FAILED: There is not enough free disk available to perform this operation on switch 1. At least 1276287 KB of free disk is required"
412+
self.assertRaises(ios_module.OSInstallError, self.device.install_os, image_name=BOOT_IMAGE, install_mode=True)
413+
mock_wait.assert_not_called()
414+
mock_reboot.assert_not_called()
415+
392416

393417
if __name__ == "__main__":
394418
unittest.main()
@@ -1262,6 +1286,7 @@ def test_install_os_install_mode_with_retries(
12621286
mock_has_reload_happened_recently.side_effect = [False, False, True]
12631287
mock_image_booted.side_effect = [False, True]
12641288
mock_sleep.return_value = None
1289+
mock_show.return_value = "show must go on"
12651290
# Call the install os function
12661291
actual = ios_device.install_os(image_name, install_mode=True)
12671292

0 commit comments

Comments
 (0)