blob: f012beef3a684f3bb8d5e988857881dc481b1571 [file] [log] [blame]
Kalin Stoyanov4afee9f2014-05-02 12:56:34 -07001# Copyright (c) 2014 The Chromium OS Authors. All rights reserved.
2# Use of this source code is governed by a BSD-style license that can be
3# found in the LICENSE file.
4
Kalin Stoyanovd592acd2014-05-06 18:58:55 -07005import logging, re, time
Kalin Stoyanovf64e2c92014-10-23 16:13:22 -07006from autotest_lib.server import autotest, test
Kalin Stoyanov4afee9f2014-05-02 12:56:34 -07007from autotest_lib.client.common_lib import error
8
9_CHARGING = 'CHARGING'
10_DISCHARGING = 'DISCHARGING'
Kalin Stoyanova69b9ae2014-10-02 15:34:11 -070011_WAIT_SECS_AFTER_SWITCH = 5
Kalin Stoyanovf64e2c92014-10-23 16:13:22 -070012_LONG_TIMEOUT = 300
13_CLIENT_LOGIN = 'desktopui_SimpleLogin'
Kalin Stoyanova69b9ae2014-10-02 15:34:11 -070014
Kalin Stoyanov4afee9f2014-05-02 12:56:34 -070015
16class platform_PowerStatusStress(test.test):
Kalin Stoyanov568436be2014-11-06 10:29:09 -080017 """Uses RPM and servo to test the power_supply_info output. """
Kalin Stoyanov4afee9f2014-05-02 12:56:34 -070018 version = 1
19
Kalin Stoyanovf64e2c92014-10-23 16:13:22 -070020
Kalin Stoyanovf64e2c92014-10-23 16:13:22 -070021 def action_login(self):
22 """Login i.e. runs running client test
23
24 @exception TestFail failed to login within timeout.
25
26 """
27 self.autotest_client.run_test(_CLIENT_LOGIN,
28 exit_without_logout=True)
Kalin Stoyanovf64e2c92014-10-23 16:13:22 -070029
30
31 def wait_to_suspend(self, suspend_timeout = _LONG_TIMEOUT):
32 """Wait for DUT to suspend.
33
34 @param suspend_timeout: Time in seconds to wait to disconnect
35
36 @exception TestFail if fail to suspend/disconnect in time
37 @returns time took to suspend/disconnect
38 """
39 start_time = int(time.time())
40 if not self.host.ping_wait_down(timeout=suspend_timeout):
41 raise error.TestFail("Unable to suspend in %s sec" %
42 suspend_timeout)
43 return int(time.time()) - start_time
44
45
46 def wait_to_come_up(self, resume_timeout):
47 """Wait for DUT to resume.
48
49 @param resume_timeout: Time in seconds to wait to come up
50
51 @exception TestFail if fail to come_up in time
52 @returns time took to come up
53 """
54 start_time = int(time.time())
55 if not self.host.wait_up(timeout=resume_timeout):
56 raise error.TestFail("Unable to resume in %s sec" %
57 resume_timeout)
58 return int(time.time()) - start_time
59
60
61
Kalin Stoyanov6b0c6732014-05-13 17:59:15 -070062 def do_suspend_resume(self, suspend_time):
Kalin Stoyanova69b9ae2014-10-02 15:34:11 -070063 """ Suspends the DUT through powerd_dbus_suspend
64
65 @param suspend_time: time in sec to suspend device for
66
67 """
Kalin Stoyanovf64e2c92014-10-23 16:13:22 -070068 #Suspend i.e. close lid
69 self.host.servo.lid_close()
70 stime = self.wait_to_suspend(_LONG_TIMEOUT)
Kalin Stoyanov568436be2014-11-06 10:29:09 -080071 logging.debug('Suspended in %d sec', stime)
Kalin Stoyanovf64e2c92014-10-23 16:13:22 -070072 #Suspended for suspend_time
73 time.sleep(suspend_time)
74 #Resume i.e. open lid
75 self.host.servo.lid_open()
76 rtime = self.wait_to_come_up(_LONG_TIMEOUT)
Kalin Stoyanov568436be2014-11-06 10:29:09 -080077 logging.debug('Resumed in %d sec', rtime)
Kalin Stoyanovf64e2c92014-10-23 16:13:22 -070078
Kalin Stoyanov4afee9f2014-05-02 12:56:34 -070079
Kalin Stoyanov4afee9f2014-05-02 12:56:34 -070080
Kalin Stoyanovd592acd2014-05-06 18:58:55 -070081 def cleanup(self):
Kalin Stoyanovf64e2c92014-10-23 16:13:22 -070082 """ Finish as powered on and lid open"""
Kalin Stoyanovd592acd2014-05-06 18:58:55 -070083 self.host.power_on()
Kalin Stoyanovf64e2c92014-10-23 16:13:22 -070084 self.host.servo.lid_open()
Kalin Stoyanova69b9ae2014-10-02 15:34:11 -070085
86
Kalin Stoyanovd592acd2014-05-06 18:58:55 -070087 def switch_power_and_verify(self, powered_on, expected):
Kalin Stoyanova69b9ae2014-10-02 15:34:11 -070088 """ Main action on switching the power state, and verifying status
89
90 @param powered_on: a boolean ON if True, OFF else
91 @param expected: touple of cmd and values to verify
92
93 @exception TestFail if line_power or battery state do not match
94 """
95 bat_state = _CHARGING if powered_on else _DISCHARGING,
Kalin Stoyanov568436be2014-11-06 10:29:09 -080096 logging.info('Switching status to %s ', bat_state)
Kalin Stoyanovd592acd2014-05-06 18:58:55 -070097 if powered_on:
98 self.host.power_on()
99 else:
100 self.host.power_off()
101 time.sleep(_WAIT_SECS_AFTER_SWITCH)
102
103 # Get power_supply_info output
104 psi_output = self.host.run('power_supply_info').stdout.strip()
105 psi_output = psi_output.replace('\n', '')
106
107 exp_psi_online, exp_psi_enum_type, exp_psi_bat_state = expected
108
109 is_psi_online = re.match(r'.+online:\s+%s.+' % exp_psi_online,
110 psi_output) is not None
111 is_psi_enum_type = re.match(r'.+enum type:\s+%s.+' % exp_psi_enum_type,
112 psi_output) is not None
113 is_psi_bat_state = re.match(r'.+state:\s+%s.+' % exp_psi_bat_state,
114 psi_output) is not None
115
116 if not all([is_psi_online, is_psi_enum_type, is_psi_bat_state]):
Kalin Stoyanova69b9ae2014-10-02 15:34:11 -0700117 raise error.TestFail('Bad %s state!' % bat_state)
Kalin Stoyanovd592acd2014-05-06 18:58:55 -0700118
119
Kalin Stoyanov6b0c6732014-05-13 17:59:15 -0700120 def run_once(self, host, loop_count, suspend_time):
Kalin Stoyanovd592acd2014-05-06 18:58:55 -0700121 self.host = host
Kalin Stoyanovf64e2c92014-10-23 16:13:22 -0700122 self.autotest_client = autotest.Autotest(self.host)
Kalin Stoyanovd592acd2014-05-06 18:58:55 -0700123
124 # Start as powered on
125 if self.host.has_power():
126 self.host.power_on()
Kalin Stoyanov4afee9f2014-05-02 12:56:34 -0700127 else:
128 raise error.TestFail('No RPM is setup to device')
Kalin Stoyanov568436be2014-11-06 10:29:09 -0800129 # Login to device
130 self.action_login()
Kalin Stoyanov4afee9f2014-05-02 12:56:34 -0700131
132 pdu_connected = True
Kalin Stoyanovd592acd2014-05-06 18:58:55 -0700133 for i in xrange(loop_count):
Kalin Stoyanov568436be2014-11-06 10:29:09 -0800134 logging.info('--- Iteration %d', (i + 1))
Kalin Stoyanov4afee9f2014-05-02 12:56:34 -0700135
Kalin Stoyanov6b0c6732014-05-13 17:59:15 -0700136 # Suspend/resume
Kalin Stoyanova69b9ae2014-10-02 15:34:11 -0700137 if suspend_time > 0:
Kalin Stoyanov6b0c6732014-05-13 17:59:15 -0700138 self.do_suspend_resume(suspend_time)
Kalin Stoyanov6b0c6732014-05-13 17:59:15 -0700139
Kalin Stoyanova69b9ae2014-10-02 15:34:11 -0700140 # Charging state - it could be any of the three below
141 expected = ('yes', 'AC', '(Charging|Fully charged|Discharging)')
Kalin Stoyanovd592acd2014-05-06 18:58:55 -0700142 self.switch_power_and_verify(True, expected)
Kalin Stoyanov4afee9f2014-05-02 12:56:34 -0700143
Kalin Stoyanovd592acd2014-05-06 18:58:55 -0700144 # Discharging state
145 expected = ('no', 'Disconnected', 'Discharging')
Kalin Stoyanov6b0c6732014-05-13 17:59:15 -0700146 self.switch_power_and_verify(False, expected)