Skip to content

Commit

Permalink
Merge pull request #1087 from rahkumar651991/zeroize_warning
Browse files Browse the repository at this point in the history
Zeroize may return warning only in the rpc-reply
  • Loading branch information
rahkumar651991 authored Oct 28, 2020
2 parents 5254d69 + 99f4968 commit 5095bf7
Show file tree
Hide file tree
Showing 3 changed files with 77 additions and 9 deletions.
51 changes: 44 additions & 7 deletions lib/jnpr/junos/utils/sw.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@
from jnpr.junos.utils.ftp import FTP
from jnpr.junos.utils.start_shell import StartShell
from jnpr.junos.exception import SwRollbackError, RpcTimeoutError, RpcError
from ncclient.xml_ import NCElement
from jnpr.junos import jxml as JXML

"""
Software Installation Utilities
Expand Down Expand Up @@ -1247,18 +1249,53 @@ def zeroize(self, all_re=False, media=None):
if media is True:
cmd = E("media")

# initialize an empty output message
output_msg = ""

try:
# For zeroize we don't get a response similar to reboot, shutdown.
# In ansible it was passed even if rpc-reply was not coming.
# Code is added here to reply the message else pass an empty string.
rsp = self.rpc(cmd, ignore_warning=True, normalize=True)
output_msg = "\n".join(
[i.text for i in rsp.xpath("//message") if i.text is not None]
)
return output_msg
# The response may come as a warning message only.
# Code is added here to extract the warning message and append it.
# Don't pass ignore warning true and handle the warning here.
rsp = self.rpc(cmd, normalize=True)
except RpcError as ex:
if hasattr(ex, "xml"):
if hasattr(ex, "errs"):
errors = ex.errs
else:
errors = [ex]
for err in errors:
if err.get("severity", "") != "warning":
# Not a warning (probably an error).
raise ex
output_msg += err.get("message", "") + "\n"
rsp = ex.xml.getroottree().getroot()
# 1) A normal response has been run through the XSLT
# transformation, but ex.xml has not. Do that now.
encode = None if sys.version < "3" else "unicode"
rsp = NCElement(
etree.tostring(rsp, encoding=encode), self._dev.transform()
)._NCElement__doc
# 2) Now remove all of the <rpc-error> elements from
# the response. We've already confirmed they are all warnings
rsp = etree.fromstring(str(JXML.strip_rpc_error_transform(rsp)))
else:
# ignore_warning was false, or an RPCError which doesn't have
# an XML attribute. Raise it up for the caller to deal with.
raise ex
except Exception as err:
raise err

# safety check added in case the rpc-reply for zeroize doesn't have message
# This scenario is not expected.
if isinstance(rsp, bool):
return "zeroize initiated with no message"

output_msg += "\n".join(
[i.text for i in rsp.xpath("//message") if i.text is not None]
)
return output_msg

# -------------------------------------------------------------------------
# rollback - clears the install request
# -------------------------------------------------------------------------
Expand Down
30 changes: 30 additions & 0 deletions tests/unit/utils/rpc-reply/request-zeroize.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
<rpc-reply>
<rpc-error>
<error-severity>warning</error-severity>
<error-message>
ipsec-key-management subsystem not running - not needed by configuration.
</error-message>
</rpc-error>
<warning xmlns="http://xml.juniper.net/xnm/1.1/xnm" xmlns:xnm="http://xml.juniper.net/xnm/1.1/xnm">
<message>
ipsec-key-management subsystem not running - not needed by configuration.
</message>
<reason>
<daemon>
ipsec-key-management
</daemon>
<process-not-configured/>
</reason>
</warning>
<warning xmlns="http://xml.juniper.net/xnm/1.1/xnm" xmlns:xnm="http://xml.juniper.net/xnm/1.1/xnm">
<message>
zeroizing re1
</message>
</warning>
<rpc-error>
<error-severity>warning</error-severity>
<error-message>
zeroizing re0
</error-message>
</rpc-error>
</rpc-reply>
5 changes: 3 additions & 2 deletions tests/unit/utils/test_sw.py
Original file line number Diff line number Diff line change
Expand Up @@ -942,8 +942,7 @@ def test_sw_halt_multi_re_vc(self, mock_execute):
def test_sw_zeroize(self, mock_execute):
mock_execute.side_effect = self._mock_manager
self.sw._multi_MX = True
test_str = "System will be rebooted and may not boot without configuration"
self.assertTrue(test_str in self.sw.zeroize())
self.assertTrue("zeroizing" in self.sw.zeroize())

@patch("jnpr.junos.Device.execute")
def test_sw_zeroize_exception(self, mock_execute):
Expand Down Expand Up @@ -1002,6 +1001,8 @@ def _mock_manager(self, *args, **kwargs):
if "path" in kwargs:
if kwargs["path"] == "/packages":
return self._read_file("file-list_dir.xml")
if args and self._testMethodName == "test_sw_zeroize":
return self._read_file("request-zeroize.xml")
device_params = kwargs["device_params"]
device_handler = make_device_handler(device_params)
session = SSHSession(device_handler)
Expand Down

0 comments on commit 5095bf7

Please sign in to comment.