Reland "autotest: Set NOT_CONNECTED/WRONG_CONFIG/BROKEN as result of creating ServoHost"

This is a reland of 2c01d45b6c83bf63dfeefcd3b8ba274e8c96b024

BUG=chromium:1059391
TEST=unittest,presubmit

Original change's description:
> autotest: Set NOT_CONNECTED/WRONG_CONFIG/BROKEN as result of creating ServoHost
>
> Servo repair/verify can set WORKING/BROKEN/NOT_CONNECTED/WRONG_CONFIG as servo_state value.
>
> BUG=chrominum:1012504
> TEST=unittest
>
> Change-Id: I01ffc1544da49b7a39c63ec9686b378d00bebd01
> Reviewed-on: https://chromium-review.googlesource.com/c/chromiumos/third_party/autotest/+/2076696
> Tested-by: Otabek Kasimov <otabek@google.com>
> Auto-Submit: Otabek Kasimov <otabek@google.com>
> Reviewed-by: Garry Wang <xianuowang@chromium.org>
> Commit-Queue: Otabek Kasimov <otabek@google.com>

Bug: chrominum:1012504
Change-Id: Iae9bfb77653bb6b0558f47ad5c3c55d900bf0ac8
Reviewed-on: https://chromium-review.googlesource.com/c/chromiumos/third_party/autotest/+/2092210
Tested-by: Otabek Kasimov <otabek@google.com>
Reviewed-by: Brent Peterson <brentpeterson@chromium.org>
Reviewed-by: Garry Wang <xianuowang@chromium.org>
Commit-Queue: Otabek Kasimov <otabek@google.com>
Auto-Submit: Otabek Kasimov <otabek@google.com>
diff --git a/server/hosts/servo_host.py b/server/hosts/servo_host.py
index 767629c..f675e4e 100644
--- a/server/hosts/servo_host.py
+++ b/server/hosts/servo_host.py
@@ -69,8 +69,10 @@
 SERVO_STATE_WORKING = 'WORKING'
 SERVO_STATE_BROKEN = 'BROKEN'
 SERVO_STATE_NOT_CONNECTED = 'NOT_CONNECTED'
+SERVO_STATE_WRONG_CONFIG = 'WRONG_CONFIG'
 SERVO_STATE_UNKNOWN = 'UNKNOWN'
 
+
 class ServoHost(base_servohost.BaseServoHost):
     """Host class for a servo host(e.g. beaglebone, labstation)
      that with a servo instance for a specific port.
@@ -661,7 +663,9 @@
 
 
     def get_servo_state(self):
-        return SERVO_STATE_BROKEN if self._servo_state is None else self._servo_state
+        if self._servo_state is None:
+            return SERVO_STATE_UNKNOWN
+        return self._servo_state
 
 
 def make_servo_hostname(dut_hostname):
@@ -825,15 +829,28 @@
 
     if servo_args is None:
         logging.debug('No servo_args provided, and failed to find overrides.')
-        return None
-    if SERVO_HOST_ATTR not in servo_args:
-        logging.debug('%s attribute missing from servo_args: %s',
-                      SERVO_HOST_ATTR, servo_args)
-        return None
+        if try_lab_servo or servo_dependency:
+            return None, SERVO_STATE_NOT_CONNECTED
+        else:
+            # For regular test case which not required the servo
+            return None, None
+
+    servo_hostname = servo_args.get(SERVO_HOST_ATTR)
+    servo_port = servo_args.get(SERVO_PORT_ATTR)
+    if not _is_servo_host_information_exist(servo_hostname, servo_port):
+        logging.debug(
+            'Servo connection info missed hostname: %s , port: %s',
+            servo_hostname, servo_port)
+        return None, SERVO_STATE_NOT_CONNECTED
+    if not is_servo_host_information_valid(servo_hostname, servo_port):
+        logging.debug(
+            'Servo connection info is incorrect hostname: %s , port: %s',
+            servo_hostname, servo_port)
+        return None, SERVO_STATE_WRONG_CONFIG
     if (not servo_dependency and not try_servo_repair and
-            not servo_host_is_up(servo_args[SERVO_HOST_ATTR])):
+            not servo_host_is_up(servo_hostname)):
         logging.debug('ServoHost is not up.')
-        return None
+        return None, SERVO_STATE_BROKEN
 
     newhost = ServoHost(**servo_args)
     try:
@@ -866,7 +883,7 @@
     # we don't need both.
     if servo_dependency:
         newhost.repair(silent=True)
-        return newhost
+        return newhost, newhost.get_servo_state()
 
     if try_servo_repair:
         try:
@@ -878,24 +895,31 @@
             newhost.verify()
         except Exception:
             logging.exception('servo verify failed for %s', newhost.hostname)
-    return newhost
+    return newhost, newhost.get_servo_state()
 
 
-def _is_servo_host_information_exist(hostname, port_int):
+def _is_servo_host_information_exist(hostname, port):
     if hostname is None or len(hostname.strip()) == 0:
         return False
-    if port_int is None or not type(port_int) is int:
+    if port is None:
         return False
+    if not type(port) is int:
+        try:
+            int(port)
+        except ValueError:
+            return False
+
     return True
 
 
-def is_servo_host_information_valid(hostname, port_int):
-    if not _is_servo_host_information_exist(hostname, port_int):
+def is_servo_host_information_valid(hostname, port):
+    if not _is_servo_host_information_exist(hostname, port):
         return False
     # checking range and correct of the port
+    port_int = int(port)
     if port_int < 1 or port_int > 65000:
         return False
     # we expecting host contain only latters, digits and '-' or '_'
-    if not re.match('[a-zA-Z0-9-_]*$', hostname) or len(hostname) < 5:
+    if not re.match('[a-zA-Z0-9-_\.]*$', hostname) or len(hostname) < 5:
         return False
     return True