• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1# 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.
4import logging
5import time
6
7from autotest_lib.client.common_lib import error
8from autotest_lib.server import test
9
10
11class platform_ServoPowerStateController(test.test):
12    """Test servo can power on and off DUT in recovery and non-recovery mode."""
13    version = 1
14
15
16    def initialize(self, host):
17        """Initialize DUT for testing."""
18        pass
19
20
21    def cleanup(self):
22        """Clean up DUT after servo actions."""
23        if not self.host.ssh_ping():
24            # Power off, then power on DUT from internal storage.
25            self.controller.power_off()
26            self.host.servo.switch_usbkey('off')
27            self.controller.power_on(self.controller.REC_OFF)
28
29
30    def assert_dut_on(self, rec_on=False):
31        """Confirm DUT is powered on, claim test failure if DUT is off.
32
33        @param rec_on: True if DUT should boot from external USB stick as in
34                       recovery mode.
35
36        @raise TestFail: If DUT is off or DUT boot from wrong source.
37        """
38        if not self.host.wait_up(timeout=300):
39            raise error.TestFail('power_state:%s did not turn DUT on.' %
40                                 ('rec' if rec_on else 'on'))
41
42        # Check boot source. Raise TestFail if DUT boot from wrong source.
43        boot_from_usb = self.host.is_boot_from_usb()
44        if boot_from_usb != rec_on:
45            boot_source = ('USB' if boot_from_usb else
46                           'non-removable storage')
47            raise error.TestFail('power_state:%s booted from %s.' %
48                                 ('rec' if rec_on else 'on', boot_source))
49
50
51    def assert_dut_off(self, error_message):
52        """Confirm DUT is off and does not turn back on after 30 seconds.
53
54        @param error_message: Error message to raise if DUT stays on.
55        @raise TestFail: If DUT stays on.
56        """
57        if not self.host.ping_wait_down(timeout=10):
58            raise error.TestFail(error_message)
59
60        if self.host.ping_wait_up(timeout=30):
61            raise error.TestFail('%s. %s' % (error_message, 'DUT turns back on'
62                                             ' after it is turned off.'))
63
64
65    def test_with_usb_plugged_in(self):
66        """Run test when USB stick is plugged in to servo."""
67        logging.info('Power off DUT')
68        self.controller.power_off()
69        self.assert_dut_off('power_state:off did not turn DUT off.')
70
71        logging.info('Power DUT on in recovery mode, DUT shall boot from USB.')
72        self.host.servo.switch_usbkey('off')
73        self.controller.power_on(self.controller.REC_ON)
74        self.assert_dut_off('power_state:rec didn\'t stay at recovery screen.')
75
76        self.host.servo.switch_usbkey('dut')
77        time.sleep(30)
78        self.assert_dut_on(rec_on=True)
79
80        logging.info('Power off DUT which is up in recovery mode.')
81        self.controller.power_off()
82        self.assert_dut_off('power_state:off failed after boot from external '
83                            'USB stick.')
84
85        logging.info('Power DUT off in recovery mode without booting.')
86        self.host.servo.switch_usbkey('off')
87        self.controller.power_on(self.controller.REC_ON)
88        time.sleep(10)
89        self.controller.power_off()
90        self.assert_dut_off('power_state:off failed at recovery screen ')
91
92        # Power DUT on in non-recovery mode with USB stick plugged in.
93        # DUT shall boot from internal storage.
94        logging.info('Power on DUT in non-recovery mode.')
95        self.host.servo.switch_usbkey('dut')
96        self.controller.power_on(self.controller.REC_OFF)
97        self.assert_dut_on()
98        self.host.servo.switch_usbkey('off')
99
100
101    def test_with_usb_unplugged(self):
102        """Run test when USB stick is not plugged in servo."""
103        # Power off DUT regardless its current status.
104        logging.info('Power off DUT.')
105        self.controller.power_off()
106        self.assert_dut_off('power_state:off did not turn DUT off.')
107
108        # Try to power off the DUT again, make sure the DUT stays off.
109        logging.info('Power off DUT which is already off.')
110        self.controller.power_off()
111        self.assert_dut_off('power_state:off turned DUT on.')
112
113        # USB stick should be unplugged before the test.
114        self.host.servo.switch_usbkey('off')
115
116        logging.info('Power on in non-recovery mode.')
117        self.controller.power_on(self.controller.REC_OFF)
118        self.assert_dut_on(rec_on=False)
119
120        logging.info('Power DUT off and on without delay. DUT should be '
121                     'on after power_on is completed.')
122        self.controller.power_off()
123        self.controller.power_on(self.controller.REC_OFF)
124        self.assert_dut_on(rec_on=False)
125
126        logging.info('Power off DUT which is up in non-recovery mode.')
127        self.controller.power_off()
128        self.assert_dut_off('power_state:off failed after boot from '
129                            'internal storage.')
130
131        logging.info('Power DUT off and reset. DUT should be on after '
132                     'reset is completed.')
133        self.controller.reset()
134        self.assert_dut_on(rec_on=False)
135
136        logging.info('Reset DUT when it\'s on. DUT should be on after '
137                     'reset is completed.')
138        boot_id = self.host.get_boot_id()
139        self.controller.reset()
140        self.assert_dut_on(rec_on=False)
141        new_boot_id = self.host.get_boot_id()
142        if not new_boot_id or boot_id == new_boot_id:
143            raise error.TestFail('power_state:reset failed to reboot DUT.')
144
145
146    def run_once(self, host, usb_available=True):
147        """Run the test.
148
149        @param host: host object of tested DUT.
150        @param usb_plugged_in: True if USB stick is plugged in servo.
151        """
152        self.host = host
153        self.controller = host.servo.get_power_state_controller()
154
155        self.test_with_usb_unplugged()
156        if usb_available:
157            self.test_with_usb_plugged_in()
158