FAFT: Check for Chrome EC before trying to use it

Several FAFT tests perform operations on the Chrome EC by accessing
FirmwareTest.ec. This attribute only exists, and these operations only
make sense, on devices that actually have a Chrome EC. Skip these tests
as N/A on other devices.

BUG=b:125837981
TEST=Ran modified tests on Arcada

Change-Id: I1a9818a9e690cd518a258be428a4f10cfa0f0125
Reviewed-on: https://chromium-review.googlesource.com/1496156
Commit-Ready: Abe Levkoy <alevkoy@chromium.org>
Tested-by: Abe Levkoy <alevkoy@chromium.org>
Reviewed-by: Duncan Laurie <dlaurie@google.com>
Reviewed-by: Raul E Rangel <rrangel@chromium.org>
diff --git a/server/site_tests/firmware_ECCharging/firmware_ECCharging.py b/server/site_tests/firmware_ECCharging/firmware_ECCharging.py
index 4a7988d..3514b4f 100644
--- a/server/site_tests/firmware_ECCharging/firmware_ECCharging.py
+++ b/server/site_tests/firmware_ECCharging/firmware_ECCharging.py
@@ -18,6 +18,9 @@
 
     def initialize(self, host, cmdline_args):
         super(firmware_ECCharging, self).initialize(host, cmdline_args)
+        # Don't bother if there is no Chrome EC.
+        if not self.check_ec_capability():
+            raise error.TestNAError("Nothing needs to be tested on this device")
         # Only run in normal mode
         self.switcher.setup_mode('normal')
         self.ec.send_command("chan 0")
@@ -122,6 +125,8 @@
 
 
     def run_once(self):
+        """Execute the main body of the test.
+        """
         if not self.check_ec_capability(['battery', 'charging']):
             raise error.TestNAError("Nothing needs to be tested on this device")
         if self._get_battery_charge() == 100:
diff --git a/server/site_tests/firmware_ECPeci/firmware_ECPeci.py b/server/site_tests/firmware_ECPeci/firmware_ECPeci.py
index 4a096ba..4202db1 100644
--- a/server/site_tests/firmware_ECPeci/firmware_ECPeci.py
+++ b/server/site_tests/firmware_ECPeci/firmware_ECPeci.py
@@ -19,6 +19,9 @@
 
     def initialize(self, host, cmdline_args):
         super(firmware_ECPeci, self).initialize(host, cmdline_args)
+        # Don't bother if there is no Chrome EC.
+        if not self.check_ec_capability(['peci']):
+            raise error.TestNAError("Nothing needs to be tested on this device")
         self.ec.send_command("chan 0")
 
     def cleanup(self):
@@ -40,6 +43,8 @@
             raise error.TestFail("Abnormal CPU temperature %d K" % t)
 
     def run_once(self):
+        """Execute the main body of the test.
+        """
         if not self.check_ec_capability(['peci']):
             raise error.TestNAError("Nothing needs to be tested on this device")
         logging.info("Reading PECI CPU temperature for %d times.",
diff --git a/server/site_tests/firmware_ECPowerG3/firmware_ECPowerG3.py b/server/site_tests/firmware_ECPowerG3/firmware_ECPowerG3.py
index 1c95513..139e403 100644
--- a/server/site_tests/firmware_ECPowerG3/firmware_ECPowerG3.py
+++ b/server/site_tests/firmware_ECPowerG3/firmware_ECPowerG3.py
@@ -22,6 +22,9 @@
 
     def initialize(self, host, cmdline_args):
         super(firmware_ECPowerG3, self).initialize(host, cmdline_args)
+        # Don't bother if there is no Chrome EC.
+        if not self.check_ec_capability():
+            raise error.TestNAError("Nothing needs to be tested on this device")
         # Only run in normal mode
         self.switcher.setup_mode('normal')
         self.ec.send_command("chan 0")
@@ -46,6 +49,8 @@
         return not self._failed
 
     def run_once(self):
+        """Execute the main body of the test.
+        """
         if not self.check_ec_capability(['x86']):
             raise error.TestNAError("Nothing needs to be tested on this device")
 
diff --git a/server/site_tests/firmware_ECSharedMem/firmware_ECSharedMem.py b/server/site_tests/firmware_ECSharedMem/firmware_ECSharedMem.py
index 28fd745..86ec199 100644
--- a/server/site_tests/firmware_ECSharedMem/firmware_ECSharedMem.py
+++ b/server/site_tests/firmware_ECSharedMem/firmware_ECSharedMem.py
@@ -17,6 +17,9 @@
 
     def initialize(self, host, cmdline_args):
         super(firmware_ECSharedMem, self).initialize(host, cmdline_args)
+        # Don't bother if there is no Chrome EC.
+        if not self.check_ec_capability():
+            raise error.TestNAError("Nothing needs to be tested on this device")
         # Only run in normal mode
         self.switcher.setup_mode('normal')
         self.ec.send_command("chan 0")
@@ -29,6 +32,8 @@
         super(firmware_ECSharedMem, self).cleanup()
 
     def shared_mem_checker(self):
+        """Return whether there is still EC shared memory available.
+        """
         match = self.ec.send_command_get_output("shmem",
                                                 ["Size:\s+([0-9-]+)\r"])[0]
         shmem_size = int(match[1])
@@ -40,11 +45,15 @@
         return True
 
     def jump_checker(self):
+        """Check for available EC shared memory after jumping to RW image.
+        """
         self.ec.send_command("sysjump RW")
         time.sleep(self.faft_config.ec_boot_to_console)
         return self.shared_mem_checker()
 
     def run_once(self):
+        """Execute the main body of the test.
+        """
         if not self.check_ec_capability():
             raise error.TestNAError("Nothing needs to be tested on this device")
 
diff --git a/server/site_tests/firmware_ECThermal/firmware_ECThermal.py b/server/site_tests/firmware_ECThermal/firmware_ECThermal.py
index a70a524..97bfdf3 100644
--- a/server/site_tests/firmware_ECThermal/firmware_ECThermal.py
+++ b/server/site_tests/firmware_ECThermal/firmware_ECThermal.py
@@ -140,6 +140,9 @@
 
     def initialize(self, host, cmdline_args):
         super(firmware_ECThermal, self).initialize(host, cmdline_args)
+        # Don't bother if there is no Chrome EC.
+        if not self.check_ec_capability():
+            raise error.TestNAError("Nothing needs to be tested on this device")
         self.ec.send_command("chan 0")
         try:
             self.faft_client.system.run_shell_command('stop temp_metrics')
@@ -419,6 +422,8 @@
 
 
     def run_once(self):
+        """Execute the main body of the test.
+        """
         if not self.check_ec_capability(['thermal']):
             raise error.TestNAError("Nothing needs to be tested on this device")
         logging.info("Checking host temperature report.")
diff --git a/server/site_tests/firmware_ECUpdateId/firmware_ECUpdateId.py b/server/site_tests/firmware_ECUpdateId/firmware_ECUpdateId.py
index 5d6e48f..71e4fb1 100644
--- a/server/site_tests/firmware_ECUpdateId/firmware_ECUpdateId.py
+++ b/server/site_tests/firmware_ECUpdateId/firmware_ECUpdateId.py
@@ -20,7 +20,9 @@
         # If EC isn't write-protected, it won't do EFS. Should enable WP.
         super(firmware_ECUpdateId, self).initialize(host, cmdline_args,
                                                     ec_wp=True)
-        # NA error checkpoint for this test
+        # Don't bother if there is no Chrome EC or if the EC is non-EFS.
+        if not self.check_ec_capability():
+            raise error.TestNAError("Nothing needs to be tested on this device")
         if not self.faft_client.ec.is_efs():
             raise error.TestNAError("Nothing needs to be tested for non-EFS")
         # In order to test software sync, it must be enabled.
@@ -104,6 +106,8 @@
             time.sleep(self.faft_config.software_sync_update)
 
     def run_once(self):
+        """Execute the main body of the test.
+        """
         logging.info("Check the current state and record hash.")
         self.check_state((self.active_copy_checker, 'RW'))
         original_hash = self.get_active_hash()
diff --git a/server/site_tests/firmware_ECUsbPorts/firmware_ECUsbPorts.py b/server/site_tests/firmware_ECUsbPorts/firmware_ECUsbPorts.py
index 6d3ab8a..d116f73 100644
--- a/server/site_tests/firmware_ECUsbPorts/firmware_ECUsbPorts.py
+++ b/server/site_tests/firmware_ECUsbPorts/firmware_ECUsbPorts.py
@@ -33,6 +33,9 @@
 
     def initialize(self, host, cmdline_args):
         super(firmware_ECUsbPorts, self).initialize(host, cmdline_args)
+        # Don't bother if there is no Chrome EC.
+        if not self.check_ec_capability(['usb']):
+            raise error.TestNAError("Nothing needs to be tested on this device")
         # Only run in normal mode
         self.switcher.setup_mode('normal')
         self.ec.send_command("chan 0")
@@ -140,9 +143,8 @@
 
 
     def run_once(self):
-        if not self.check_ec_capability(['usb']):
-            raise error.TestNAError("Nothing needs to be tested on this device")
-
+        """Execute the main body of the test.
+        """
         self._smart_usb_charge = (
             'smart_usb_charge' in self.faft_config.ec_capability)
         self._port_count = self.get_port_count()
diff --git a/server/site_tests/firmware_ECWriteProtect/firmware_ECWriteProtect.py b/server/site_tests/firmware_ECWriteProtect/firmware_ECWriteProtect.py
index 0aea62b..0ffd667 100644
--- a/server/site_tests/firmware_ECWriteProtect/firmware_ECWriteProtect.py
+++ b/server/site_tests/firmware_ECWriteProtect/firmware_ECWriteProtect.py
@@ -33,6 +33,9 @@
     def initialize(self, host, cmdline_args, dev_mode=False):
         super(firmware_ECWriteProtect, self).initialize(host, cmdline_args,
                                                         ec_wp=False)
+        # Don't bother if there is no Chrome EC.
+        if not self.check_ec_capability():
+            raise error.TestNAError("Nothing needs to be tested on this device")
         self.backup_firmware()
         self.switcher.setup_mode('dev' if dev_mode else 'normal')
         self.ec.send_command("chan 0")
@@ -46,6 +49,8 @@
         super(firmware_ECWriteProtect, self).cleanup()
 
     def run_once(self):
+        """Execute the main body of the test.
+        """
         flags = self.faft_client.bios.get_preamble_flags('a')
         if flags & vboot.PREAMBLE_USE_RO_NORMAL == 0:
             logging.info('The firmware USE_RO_NORMAL flag is disabled.')