op-test icon indicating copy to clipboard operation
op-test copied to clipboard

OpTestSystem: Add state names for readability

Open debmc opened this issue 6 years ago • 3 comments

For consumers of the debug files add the state names to aide in debug log analysis.

Signed-off-by: Deb McLemore [email protected]

debmc avatar Dec 12 '18 18:12 debmc

Sam Mendoza-Jonas [email protected] writes:

sammj commented on this pull request.

@@ -328,10 +328,12 @@ def goto_state(self, state): raise unittest.SkipTest("OpTestSystem running QEMU/Mambo so skipping OpSystemState.OS test") if (self.state == OpSystemState.UNKNOWN): log.debug("OpTestSystem CHECKING CURRENT STATE and TRANSITIONING for TARGET STATE: %s" % (state))

  •      log.debug(" *** UNKNOWN=0 OFF=1 IPLing=2 PETITBOOT=3 PETITBOOT_SHELL=4 BOOTING=5 OS=6 POWERING_OFF=7 UNKNOWN_BAD=8")
    

Could we instead have a little helper that turns state numbers into strings and do log.debug("OpTestSystem CHECKING CURRENT STATE and TRANSITIONING for TARGET STATE: {} ({})".format(state, state_name(state))?

We should be able to do that by using the object-as-an-enum trick and having a str method in it.

-- Stewart Smith OPAL Architect, IBM.

ghost avatar Dec 13 '18 03:12 ghost

We should be able to do that by using the object-as-an-enum trick and having a str method in it.

I think I have a combo solution, alternative suggestions welcomed !

debmc avatar Dec 17 '18 01:12 debmc

comedy option:

diff --git a/common/OpTestSystem.py b/common/OpTestSystem.py
index 712057704edf..ddb8f8c67de5 100644
--- a/common/OpTestSystem.py
+++ b/common/OpTestSystem.py
@@ -55,22 +55,28 @@ import logging
 import OpTestLogger
 log = OpTestLogger.optest_logger_glob.get_logger(__name__)
 
-
 class OpSystemState():
-    '''
-    This class is used as an enum as to what state op-test *thinks* the host is in.
-    These states are used to drive a state machine in OpTestSystem.
-    '''
-    UNKNOWN = 0
-    OFF = 1
-    IPLing = 2
-    PETITBOOT = 3
-    PETITBOOT_SHELL = 4
-    BOOTING = 5
-    OS = 6
-    POWERING_OFF = 7
-    UNKNOWN_BAD = 8  # special case, use set_state to place system in hold for later goto
-
+    # dumb wrapper that'll print out the state name when printed / casted
+    # to a string. Otherwise it acts like an int.
+    class OpSysState(int):
+        state_names = ["UNKNOWN", "OFF", "IPL", "PETITBOOT", "PETITBOOT_SHELL",
+                       "BOOTING", "OS", "POWERING_OFF", "UNKNOWN_BAD"]
+        def __str__(self):
+            return self.state_names[self]
+        def __format__(self, fmt):
+            if fmt == "s":
+                return str(self)
+            return int(self).__format__(fmt)
+
+    UNKNOWN         = OpSysState(0)
+    OFF             = OpSysState(1)
+    IPLing          = OpSysState(2)
+    PETITBOOT       = OpSysState(3)
+    PETITBOOT_SHELL = OpSysState(4)
+    BOOTING         = OpSysState(5)
+    OS              = OpSysState(6)
+    POWERING_OFF    = OpSysState(7)
+    UNKNOWN_BAD     = OpSysState(8)  # special case, use set_state to place system in hold for later goto
 
 class OpTestSystem(object):
 

Whenever we print the state we'd get the string name, but in most situations it's treated as an integer would be. We might be better off using the enum class rather though.

oohal avatar Nov 28 '19 06:11 oohal