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 | |
| 7 | from autotest_lib.server import test, autotest |
| 8 | import autotest_lib.server.cros.servo |
| 9 | |
| 10 | class ServoTest(test.test): |
| 11 | """AutoTest test class that creates and destroys a servo object. |
| 12 | |
| 13 | Servo-based server side AutoTests can inherit from this object. |
| 14 | """ |
| 15 | version = 1 |
| 16 | servo = None |
| 17 | _ip = None |
| 18 | |
| 19 | |
| 20 | def initialize(self, host, servo_port, xml_config='servo.xml'): |
| 21 | """Create a Servo object.""" |
| 22 | self.servo = autotest_lib.server.cros.servo.Servo(servo_port, |
| 23 | xml_config) |
| 24 | self._ip = host.ip |
| 25 | |
| 26 | |
| 27 | def assert_ping(self): |
| 28 | """Ping to assert that the device is up.""" |
| 29 | assert self.ping_test(self._ip) |
| 30 | |
| 31 | |
| 32 | def assert_pingfail(self): |
| 33 | """Ping to assert that the device is down.""" |
| 34 | assert not self.ping_test(self._ip) |
| 35 | |
| 36 | |
| 37 | def ping_test(self, hostname, timeout=5): |
| 38 | """Verify whether a host responds to a ping. |
| 39 | |
| 40 | Args: |
| 41 | hostname: Hostname to ping. |
| 42 | timeout: Time in seconds to wait for a response. |
| 43 | """ |
| 44 | return subprocess.call(['ping', '-c', '1', '-W', |
| 45 | str(timeout), hostname]) == 0 |
| 46 | |
| 47 | |
| 48 | def cleanup(self): |
| 49 | """Delete the Servo object.""" |
| 50 | del self.servo |