Craig Harrison | 2b6c6fc | 2011-06-23 10:34:02 -0700 | [diff] [blame] | 1 | # Copyright (c) 2011 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 | |
| 5 | import subprocess |
| 6 | |
Chrome Bot | 9a1137d | 2011-07-19 14:35:00 -0700 | [diff] [blame^] | 7 | from autotest_lib.client.common_lib import error |
| 8 | from autotest_lib.server import test |
Craig Harrison | 2b6c6fc | 2011-06-23 10:34:02 -0700 | [diff] [blame] | 9 | import autotest_lib.server.cros.servo |
| 10 | |
| 11 | class ServoTest(test.test): |
| 12 | """AutoTest test class that creates and destroys a servo object. |
| 13 | |
| 14 | Servo-based server side AutoTests can inherit from this object. |
| 15 | """ |
| 16 | version = 1 |
| 17 | servo = None |
| 18 | _ip = None |
| 19 | |
| 20 | |
Todd Broch | 5fd6bc0 | 2011-07-20 15:53:37 -0700 | [diff] [blame] | 21 | def initialize(self, host, servo_port, xml_config='servo.xml', |
| 22 | servo_vid=None, servo_pid=None, servo_serial=None): |
Craig Harrison | 2b6c6fc | 2011-06-23 10:34:02 -0700 | [diff] [blame] | 23 | """Create a Servo object.""" |
Chrome Bot | 9a1137d | 2011-07-19 14:35:00 -0700 | [diff] [blame^] | 24 | self.servo = autotest_lib.server.cros.servo.Servo( |
| 25 | servo_port, xml_config, servo_vid, servo_pid, servo_serial) |
| 26 | |
| 27 | # Initializes dut, may raise AssertionError if pre-defined gpio |
| 28 | # sequence to set GPIO's fail. Autotest does not handle exception |
| 29 | # throwing in initialize and will cause a test to hang. |
| 30 | try: |
| 31 | self.servo.initialize_dut() |
| 32 | except AssertionError as e: |
| 33 | del self.servo |
| 34 | raise error.TestFail(e) |
| 35 | |
Craig Harrison | 2b6c6fc | 2011-06-23 10:34:02 -0700 | [diff] [blame] | 36 | self._ip = host.ip |
| 37 | |
| 38 | |
| 39 | def assert_ping(self): |
| 40 | """Ping to assert that the device is up.""" |
| 41 | assert self.ping_test(self._ip) |
| 42 | |
| 43 | |
| 44 | def assert_pingfail(self): |
| 45 | """Ping to assert that the device is down.""" |
| 46 | assert not self.ping_test(self._ip) |
| 47 | |
| 48 | |
| 49 | def ping_test(self, hostname, timeout=5): |
| 50 | """Verify whether a host responds to a ping. |
| 51 | |
| 52 | Args: |
| 53 | hostname: Hostname to ping. |
| 54 | timeout: Time in seconds to wait for a response. |
| 55 | """ |
| 56 | return subprocess.call(['ping', '-c', '1', '-W', |
| 57 | str(timeout), hostname]) == 0 |
| 58 | |
| 59 | |
| 60 | def cleanup(self): |
| 61 | """Delete the Servo object.""" |
Chrome Bot | 9a1137d | 2011-07-19 14:35:00 -0700 | [diff] [blame^] | 62 | if self.servo: del self.servo |