From 00b36ec5f9d8631230fd478ff6e7481fb7783cdc Mon Sep 17 00:00:00 2001 From: Nitin Kumar Date: Mon, 21 Oct 2019 14:08:31 +0530 Subject: [PATCH 1/2] check for any pending install --- lib/jnpr/junos/utils/sw.py | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) diff --git a/lib/jnpr/junos/utils/sw.py b/lib/jnpr/junos/utils/sw.py index f2d19eca6..0f90dc815 100644 --- a/lib/jnpr/junos/utils/sw.py +++ b/lib/jnpr/junos/utils/sw.py @@ -814,6 +814,23 @@ def _progress(report): self.log = _progress + # --------------------------------------------------------------------- + # Before doing anything, Do check if any pending install exists. + # --------------------------------------------------------------------- + try: + pending_install = self._dev.rpc.request_package_check_pending_install() + msg = pending_install.text + if msg and msg.strip() != '' and pending_install.getparent().findtext( + 'package-result').strip() == '1': + _progress(msg) + return False + except RpcError: + _progress("request-package-check-pending-install rpc is not " + "supported on given device") + except Exception as ex: + _progress("check pending install failed with exception: %s" % ex) + # Continue with software installation + # --------------------------------------------------------------------- # perform a 'safe-copy' of the image to the remote device # --------------------------------------------------------------------- From 35a389f24cd0ad8d2ae1d811132bfb35fbd04af5 Mon Sep 17 00:00:00 2001 From: Nitin Kumar Date: Mon, 21 Oct 2019 15:47:43 +0530 Subject: [PATCH 2/2] unit tests for check pending commits --- lib/jnpr/junos/utils/sw.py | 2 +- ...st-package-check-pending-install-error.xml | 10 +++++++ .../request-package-check-pending-install.xml | 7 +++++ tests/unit/utils/test_sw.py | 27 +++++++++++++++++++ 4 files changed, 45 insertions(+), 1 deletion(-) create mode 100644 tests/unit/utils/rpc-reply/request-package-check-pending-install-error.xml create mode 100644 tests/unit/utils/rpc-reply/request-package-check-pending-install.xml diff --git a/lib/jnpr/junos/utils/sw.py b/lib/jnpr/junos/utils/sw.py index 0f90dc815..cc3ac3c70 100644 --- a/lib/jnpr/junos/utils/sw.py +++ b/lib/jnpr/junos/utils/sw.py @@ -818,7 +818,7 @@ def _progress(report): # Before doing anything, Do check if any pending install exists. # --------------------------------------------------------------------- try: - pending_install = self._dev.rpc.request_package_check_pending_install() + pending_install = self._dev.rpc.request_package_checks_pending_install() msg = pending_install.text if msg and msg.strip() != '' and pending_install.getparent().findtext( 'package-result').strip() == '1': diff --git a/tests/unit/utils/rpc-reply/request-package-check-pending-install-error.xml b/tests/unit/utils/rpc-reply/request-package-check-pending-install-error.xml new file mode 100644 index 000000000..a743a0297 --- /dev/null +++ b/tests/unit/utils/rpc-reply/request-package-check-pending-install-error.xml @@ -0,0 +1,10 @@ + + +There is already an install pending. + Use the 'request system reboot' command to complete the install, + or the 'request system software rollback' command to back it out. + + +1 + + \ No newline at end of file diff --git a/tests/unit/utils/rpc-reply/request-package-check-pending-install.xml b/tests/unit/utils/rpc-reply/request-package-check-pending-install.xml new file mode 100644 index 000000000..d2e701dff --- /dev/null +++ b/tests/unit/utils/rpc-reply/request-package-check-pending-install.xml @@ -0,0 +1,7 @@ + + + + +0 + + \ No newline at end of file diff --git a/tests/unit/utils/test_sw.py b/tests/unit/utils/test_sw.py index f91e86e8e..717a77604 100644 --- a/tests/unit/utils/test_sw.py +++ b/tests/unit/utils/test_sw.py @@ -844,6 +844,17 @@ def test_sw_poweroff_multi_re_vc(self, mock_execute): self.sw._multi_VC = False self.assertTrue('Shutdown NOW' in self.sw.poweroff()) + @patch('jnpr.junos.Device.execute') + def test_sw_check_pending_install(self, mock_execute): + mock_execute.side_effect = self._mock_manager + package = 'test.tgz' + self.assertFalse(self.sw.install(package)) + + @patch('jnpr.junos.utils.sw.SW.pkgadd') + def test_sw_check_pending_install_RpcError_continue(self, mock_pkgadd): + mock_pkgadd.return_value = True + self.assertTrue(self.sw.install('test.tgz', no_copy=True)) + def _myprogress(self, dev, report): pass @@ -889,6 +900,22 @@ def _mock_manager(self, *args, **kwargs): elif args: if args[0].find('at') is not None: return self._read_file('request-reboot-at.xml') + elif self._testMethodName == 'test_sw_check_pending_install': + if args[0].text == 'request-package-check-pending-install': + return self._read_file( + 'request-package-check-pending-install-error.xml') + elif self._testMethodName == 'test_sw_check_pending_install_RpcError_continue': + if args[0].text == 'request-package-check-pending-install': + xml = ''' + protocol + operation-failed + error + syntax error + + request-package-check-pendings-install + + ''' + return RpcError(rsp=etree.fromstring(xml)) else: return self._read_file(args[0].tag + '.xml')