UTI: Export results
[csit.git] / resources / tools / scripts / topo_reservation.py
index e0f39c7..0016ebc 100755 (executable)
@@ -1,6 +1,6 @@
 #!/usr/bin/env python3
 
-# Copyright (c) 2019 Cisco and/or its affiliates.
+# Copyright (c) 2021 Cisco and/or its affiliates.
 # Licensed under the Apache License, Version 2.0 (the "License");
 # you may not use this file except in compliance with the License.
 # You may obtain a copy of the License at:
@@ -24,13 +24,30 @@ import argparse
 import sys
 import yaml
 
-from resources.libraries.python.ssh import exec_cmd
+from resources.libraries.python.ssh import exec_cmd as _exec_cmd
 
 
 RESERVATION_DIR = u"/tmp/reservation_dir"
 RESERVATION_NODE = u"TG"
 
 
+def exec_cmd(node, cmd):
+    """A wrapper around ssh.exec_cmd with disabled JSON export.
+
+    Using this, maintainers can use "exec_cmd" without worrying
+    about interaction with json export.
+
+    TODO: Instead this, divide ssh module into reusable and robot-bound parts.
+
+    :param node: Node object as parsed from topology file to execute cmd on.
+    :param cmd: Command to execute.
+    :type node: dict
+    :type cmd: str
+    :returns: RC, Stdout, Stderr.
+    :rtype: Tuple[int, str, str]
+    """
+    return _exec_cmd(node, cmd, export=False)
+
 def diag_cmd(node, cmd):
     """Execute cmd, print cmd and stdout, ignore stderr and rc; return None.
 
@@ -100,9 +117,11 @@ def main():
     # store additional data (time, client_ID, ..) within reservation directory.
     if args.cancel:
         ret, _, err = exec_cmd(node, f"rm -r {RESERVATION_DIR}")
-        if ret:
+        # If connection is refused, ret==None.
+        if ret != 0:
             print(f"Cancellation unsuccessful:\n{err!r}")
-        return ret
+            return 1
+        return 0
     # Before critical section, output can be outdated already.
     print(u"Diagnostic commands:")
     # -d and * are to suppress "total <size>", see https://askubuntu.com/a/61190
@@ -111,7 +130,10 @@ def main():
     # Entering critical section.
     ret, _, _ = exec_cmd(node, f"mkdir '{RESERVATION_DIR}'")
     # Critical section is over.
-    if ret:
+    if ret is None:
+        print(u"Failed to connect to testbed.")
+        return 1
+    if ret != 0:
         _, stdo, _ = exec_cmd(node, f"ls '{RESERVATION_DIR}'/*")
         print(f"Testbed already reserved by:\n{stdo}")
         return 2
@@ -119,7 +141,7 @@ def main():
     print(u"Reservation success, writing additional info to reservation dir.")
     ret, _, err = exec_cmd(
         node, f"touch '{RESERVATION_DIR}/{args.runtag}'")
-    if ret:
+    if ret != 0:
         print(f"Writing test run info failed, but continuing anyway:\n{err!r}")
     return 0