FAFT: Issue warning when running incompatible EC tests

Some EC tests require certain functinoality of EC which might not exists
on all devices. In this case, we should issue warning and let the test
pass so as not to raise false alarm.

BUG=chrome-os-partner:11629
TEST=Check tests are gated by EC attributes.

Change-Id: Ie518271e2a21094b1f8876c9520d95678a7474e5
Reviewed-on: https://gerrit.chromium.org/gerrit/28355
Reviewed-by: Tom Wai-Hong Tam <waihong@chromium.org>
Tested-by: Vic Yang <victoryang@chromium.org>
Commit-Ready: Vic Yang <victoryang@chromium.org>
diff --git a/server/cros/faft_client_attribute.py b/server/cros/faft_client_attribute.py
index e13f090..6b6dc66 100644
--- a/server/cros/faft_client_attribute.py
+++ b/server/cros/faft_client_attribute.py
@@ -34,7 +34,7 @@
         if platform == 'Link':
             setting['ec_capability'] = ['adc_ectemp', 'battery', 'charging',
                                         'keyboard', 'lid', 'x86', 'thermal',
-                                        'usb']
+                                        'usb', 'peci']
         elif platform == 'Snow':
             setting['ec_capability'] = ['battery', 'charging', 'keyboard',
                                         'lid', 'arm']
diff --git a/server/cros/faftsequence.py b/server/cros/faftsequence.py
index adabd08..8a0bd905 100644
--- a/server/cros/faftsequence.py
+++ b/server/cros/faftsequence.py
@@ -402,6 +402,29 @@
         return result_list
 
 
+    def check_ec_capability(self, required_cap=[]):
+        """Check if current platform has required EC capabilities.
+
+        Args:
+          required_cap: A list containing required EC capabilities. Pass in
+            None to only check for presence of Chrome EC.
+
+        Returns:
+          True if requirements are met. Otherwise, False.
+        """
+        if not self.client_attr.chrome_ec:
+            logging.warn('Requires Chrome EC to run this test.')
+            return False
+
+        for cap in required_cap:
+            if cap not in self.client_attr.ec_capability:
+                logging.warn('Requires EC capability "%s" to run this test.' %
+                             cap)
+                return False
+
+        return True
+
+
     def _parse_crossystem_output(self, lines):
         """Parse the crossystem output into a dict.
 
diff --git a/server/site_tests/firmware_ECAdc/firmware_ECAdc.py b/server/site_tests/firmware_ECAdc/firmware_ECAdc.py
index 1b75107..74bf597 100644
--- a/server/site_tests/firmware_ECAdc/firmware_ECAdc.py
+++ b/server/site_tests/firmware_ECAdc/firmware_ECAdc.py
@@ -33,6 +33,8 @@
 
 
     def run_once(self, host=None):
+        if not self.check_ec_capability(['adc_ectemp']):
+            return
         logging.info("Reading EC internal temperature for %d times." %
                      self.READ_COUNT)
         for i in xrange(self.READ_COUNT):
diff --git a/server/site_tests/firmware_ECBattery/firmware_ECBattery.py b/server/site_tests/firmware_ECBattery/firmware_ECBattery.py
index 4742503..570e251 100644
--- a/server/site_tests/firmware_ECBattery/firmware_ECBattery.py
+++ b/server/site_tests/firmware_ECBattery/firmware_ECBattery.py
@@ -89,6 +89,8 @@
 
 
     def run_once(self, host=None):
+        if not self.check_ec_capability(['battery']):
+            return
         logging.info("Checking battery current reading...")
         self._check_current_match()
 
diff --git a/server/site_tests/firmware_ECBootTime/firmware_ECBootTime.py b/server/site_tests/firmware_ECBootTime/firmware_ECBootTime.py
index f53b80d..4d152de 100644
--- a/server/site_tests/firmware_ECBootTime/firmware_ECBootTime.py
+++ b/server/site_tests/firmware_ECBootTime/firmware_ECBootTime.py
@@ -32,6 +32,8 @@
             raise error.TestFail("Boot time longer than 1 second.")
 
     def run_once(self, host=None):
+        if not self.check_ec_capability():
+            return
         self.register_faft_sequence((
             {   # Step 1, Reboot and check EC cold boot time and host boot time
                 'reboot_action': self.check_boot_time,
diff --git a/server/site_tests/firmware_ECCharging/firmware_ECCharging.py b/server/site_tests/firmware_ECCharging/firmware_ECCharging.py
index e720268..971da7a 100644
--- a/server/site_tests/firmware_ECCharging/firmware_ECCharging.py
+++ b/server/site_tests/firmware_ECCharging/firmware_ECCharging.py
@@ -107,6 +107,8 @@
 
 
     def run_once(self, host=None):
+        if not self.check_ec_capability(['battery', 'charging']):
+            return
         if self._get_battery_charge() == 100:
             logging.info("Battery is full. Unable to test.")
             return
diff --git a/server/site_tests/firmware_ECCorruptFwBodyA/firmware_ECCorruptFwBodyA.py b/server/site_tests/firmware_ECCorruptFwBodyA/firmware_ECCorruptFwBodyA.py
index 904aa8f..e59c1a3 100644
--- a/server/site_tests/firmware_ECCorruptFwBodyA/firmware_ECCorruptFwBodyA.py
+++ b/server/site_tests/firmware_ECCorruptFwBodyA/firmware_ECCorruptFwBodyA.py
@@ -40,6 +40,8 @@
 
 
     def run_once(self, host=None):
+        if not self.check_ec_capability():
+            return
         self.register_faft_sequence((
             {   # Step 1, corrupt firmware body A
                 'state_checker': (self.ec_act_copy_checker, 'A'),
diff --git a/server/site_tests/firmware_ECCorruptFwSigA/firmware_ECCorruptFwSigA.py b/server/site_tests/firmware_ECCorruptFwSigA/firmware_ECCorruptFwSigA.py
index c7d7da9..41a5caf 100644
--- a/server/site_tests/firmware_ECCorruptFwSigA/firmware_ECCorruptFwSigA.py
+++ b/server/site_tests/firmware_ECCorruptFwSigA/firmware_ECCorruptFwSigA.py
@@ -40,6 +40,8 @@
 
 
     def run_once(self, host=None):
+        if not self.check_ec_capability():
+            return
         self.register_faft_sequence((
             {   # Step 1, corrupt firmware signature A
                 'state_checker': (self.ec_act_copy_checker, 'A'),
diff --git a/server/site_tests/firmware_ECKeyboard/firmware_ECKeyboard.py b/server/site_tests/firmware_ECKeyboard/firmware_ECKeyboard.py
index 3d2d77c..bed4b8c 100644
--- a/server/site_tests/firmware_ECKeyboard/firmware_ECKeyboard.py
+++ b/server/site_tests/firmware_ECKeyboard/firmware_ECKeyboard.py
@@ -104,6 +104,8 @@
 
 
     def run_once(self, host=None):
+        if not self.check_ec_capability(['keyboard']):
+            return
         self.register_faft_sequence((
             {   # Step 1, use key press simulation to issue reboot command
                 'reboot_action': self.reboot_by_keyboard,
diff --git a/server/site_tests/firmware_ECLidSwitch/firmware_ECLidSwitch.py b/server/site_tests/firmware_ECLidSwitch/firmware_ECLidSwitch.py
index 26ebd13..2977020 100644
--- a/server/site_tests/firmware_ECLidSwitch/firmware_ECLidSwitch.py
+++ b/server/site_tests/firmware_ECLidSwitch/firmware_ECLidSwitch.py
@@ -185,6 +185,8 @@
 
 
     def run_once(self, host=None):
+        if not self.check_ec_capability(['lid']):
+            return
         self.register_faft_sequence((
             {   # Step 1, shutdown and long delayed wake
                 'reboot_action': (self.shutdown_and_wake,
diff --git a/server/site_tests/firmware_ECPeci/firmware_ECPeci.py b/server/site_tests/firmware_ECPeci/firmware_ECPeci.py
index ba47e1c..a069943 100644
--- a/server/site_tests/firmware_ECPeci/firmware_ECPeci.py
+++ b/server/site_tests/firmware_ECPeci/firmware_ECPeci.py
@@ -33,6 +33,8 @@
 
 
     def run_once(self, host=None):
+        if not self.check_ec_capability(['peci']):
+            return
         logging.info("Reading PECI CPU temperature for %d times." %
                      self.READ_COUNT)
         for i in xrange(self.READ_COUNT):
diff --git a/server/site_tests/firmware_ECPowerButton/firmware_ECPowerButton.py b/server/site_tests/firmware_ECPowerButton/firmware_ECPowerButton.py
index d4871a8..2eb7296 100644
--- a/server/site_tests/firmware_ECPowerButton/firmware_ECPowerButton.py
+++ b/server/site_tests/firmware_ECPowerButton/firmware_ECPowerButton.py
@@ -67,6 +67,8 @@
 
 
     def run_once(self, host=None):
+        if not self.check_ec_capability():
+            return
         self.register_faft_sequence((
             {   # Step 1, Shutdown when powerd is still running and wake from S5
                 #         with short power button press.
diff --git a/server/site_tests/firmware_ECPowerG3/firmware_ECPowerG3.py b/server/site_tests/firmware_ECPowerG3/firmware_ECPowerG3.py
index 6573ffe..f07bb41 100644
--- a/server/site_tests/firmware_ECPowerG3/firmware_ECPowerG3.py
+++ b/server/site_tests/firmware_ECPowerG3/firmware_ECPowerG3.py
@@ -60,6 +60,8 @@
 
 
     def run_once(self, host=None):
+        if not self.check_ec_capability(['x86']):
+            return
         self.register_faft_sequence((
             {   # Step 1, power off and check if system drop into G3 correctly
                 'reboot_action': self.check_G3,
diff --git a/server/site_tests/firmware_ECThermal/firmware_ECThermal.py b/server/site_tests/firmware_ECThermal/firmware_ECThermal.py
index 68c9ee7..d03eb82 100644
--- a/server/site_tests/firmware_ECThermal/firmware_ECThermal.py
+++ b/server/site_tests/firmware_ECThermal/firmware_ECThermal.py
@@ -404,6 +404,8 @@
 
 
     def run_once(self, host=None):
+        if not self.check_ec_capability(['thermal']):
+            return
         logging.info("Checking host temperature report.")
         self.check_temp_report()
 
diff --git a/server/site_tests/firmware_ECUsbPorts/firmware_ECUsbPorts.py b/server/site_tests/firmware_ECUsbPorts/firmware_ECUsbPorts.py
index 835a3d0..28a5ecc 100644
--- a/server/site_tests/firmware_ECUsbPorts/firmware_ECUsbPorts.py
+++ b/server/site_tests/firmware_ECUsbPorts/firmware_ECUsbPorts.py
@@ -107,6 +107,8 @@
 
 
     def run_once(self, host=None):
+        if not self.check_ec_capability(['usb']):
+            return
         self.register_faft_sequence((
             {   # Step 1, turn off all USB ports and then turn them on again
                 'reboot_action': self.fake_reboot_by_usb_mode_change,
diff --git a/server/site_tests/firmware_ECWakeSource/firmware_ECWakeSource.py b/server/site_tests/firmware_ECWakeSource/firmware_ECWakeSource.py
index 51e50a8..a47c23a 100644
--- a/server/site_tests/firmware_ECWakeSource/firmware_ECWakeSource.py
+++ b/server/site_tests/firmware_ECWakeSource/firmware_ECWakeSource.py
@@ -77,6 +77,9 @@
 
 
     def run_once(self, host=None):
+        # TODO(victoryang): make this test run on both x86 and arm
+        if not self.check_ec_capability(['x86', 'lid']):
+            return
         self.register_faft_sequence((
             {   # Step 1, suspend and wake by power button
                 'reboot_action': (self.suspend_as_reboot,
diff --git a/server/site_tests/firmware_ECWatchdog/firmware_ECWatchdog.py b/server/site_tests/firmware_ECWatchdog/firmware_ECWatchdog.py
index 6bb94bb..a990fbf 100644
--- a/server/site_tests/firmware_ECWatchdog/firmware_ECWatchdog.py
+++ b/server/site_tests/firmware_ECWatchdog/firmware_ECWatchdog.py
@@ -31,6 +31,8 @@
 
 
     def run_once(self, host=None):
+        if not self.check_ec_capability():
+            return
         self.register_faft_sequence((
             {   # Step 1, trigger a watchdog reset and power on system again.
                 'reboot_action': self.reboot_by_watchdog,