platform_PowerStatusStress: create power status stress test. Include in stress suite.

BUG=chromium:358927
TEST=Tested against a squawks device in lab

Change-Id: Ie5e5869df7ba1850e11defff1f380c6b8db79955
Reviewed-on: https://chromium-review.googlesource.com/198032
Reviewed-by: Yusuf Mohsinally <mohsinally@chromium.org>
Commit-Queue: Kalin Stoyanov <kalin@chromium.org>
Tested-by: Kalin Stoyanov <kalin@chromium.org>
diff --git a/server/site_tests/platform_PowerStatusStress/control b/server/site_tests/platform_PowerStatusStress/control
new file mode 100644
index 0000000..a575915
--- /dev/null
+++ b/server/site_tests/platform_PowerStatusStress/control
@@ -0,0 +1,36 @@
+# Copyright (c) 2014 The Chromium OS Authors. All rights reserved.
+# Use of this source code is governed by a BSD-style license that can be
+# found in the LICENSE file.
+
+from autotest_lib.server import utils
+
+AUTHOR = "Chromium OS Team"
+NAME = "PowerStatusStress"
+PURPOSE = "Checks the Power line / Battery status when AC is plugged/unplugged"
+TIME = "LONG"
+TEST_CATEGORY = "Stress"
+TEST_CLASS = "platform"
+TEST_TYPE = "server"
+SUITE = "stress"
+
+DOC = """
+Power stress test would be:
+-Connect power adapter to device
+-Check powerd that the device is charging
+-Remove power adapter
+-Check powerd that the device is discharging
+-Repeat
+The test should fail if the powerd status does not correspond to the RPM/PDU
+status.
+"""
+
+args_dict = utils.args_to_dict(args)
+
+def run(machine):
+    host = hosts.create_host(machine)
+    loop_count = int(args_dict.get('loop_count', 20))
+    job.run_test("platform_PowerStatusStress", host=host,
+                 disable_sysinfo=True,
+                 loop_count=loop_count)
+
+parallel_simple(run, machines)
\ No newline at end of file
diff --git a/server/site_tests/platform_PowerStatusStress/platform_PowerStatusStress.py b/server/site_tests/platform_PowerStatusStress/platform_PowerStatusStress.py
new file mode 100644
index 0000000..e30cea0
--- /dev/null
+++ b/server/site_tests/platform_PowerStatusStress/platform_PowerStatusStress.py
@@ -0,0 +1,71 @@
+# Copyright (c) 2014 The Chromium OS Authors. All rights reserved.
+# Use of this source code is governed by a BSD-style license that can be
+# found in the LICENSE file.
+
+import re, time
+from autotest_lib.server import test
+from autotest_lib.client.common_lib import error
+
+_CHARGING = 'CHARGING'
+_DISCHARGING = 'DISCHARGING'
+
+class platform_PowerStatusStress(test.test):
+    version = 1
+
+    def suspend_resume(self):
+        pass
+
+    def run_once(self, host, loop_count):
+
+        #Start as powered on
+        if host.has_power():
+            host.power_on()
+        else:
+            raise error.TestFail('No RPM is setup to device')
+
+        pdu_connected = True
+
+        for i in xrange(loop_count * 2):
+            time.sleep(1)
+            iteration = i/2 + 1
+
+            # Get power_supply_info output
+            psi_output = host.run('power_supply_info').stdout.strip()
+            psi_output = psi_output.replace('\n', '')
+
+            if pdu_connected:
+                expected_psi_online = 'yes'
+                expected_psi_enum_type = 'AC'
+                expected_psi_bat_state = '(Charging|Fully charged)'
+            else:
+                expected_psi_online = 'no'
+                expected_psi_enum_type = 'Disconnected'
+                expected_psi_bat_state = 'Discharging'
+
+            is_psi_online = re.match(r'.+online:\s+%s.+' % expected_psi_online,
+                                     psi_output) is not None
+            is_psi_enum_type = re.match(r'.+enum type:\s+%s.+' %
+                expected_psi_enum_type, psi_output) is not None
+            is_psi_bat_state = re.match(r'.+state:\s+%s.+' %
+                expected_psi_bat_state, psi_output) is not None
+
+            if not all([is_psi_online,
+                       is_psi_enum_type,
+                       is_psi_bat_state]):
+                host.power_on()
+                raise error.TestFail('Bad %s state at iteration %d: %s' %
+                    (_CHARGING if pdu_connected else _DISCHARGING,
+                     iteration, psi_output))
+
+            if pdu_connected:
+                host.power_off()
+                pdu_connected = False
+            else:
+                host.power_on()
+                pdu_connected = True
+
+            #TODO(kalin@): Add suspend/resume
+            self.suspend_resume()
+
+        #Finish as powered on
+        host.power_on()