blob: 2712089e3b619cf0e8be755364b6aa2dd1e75013 [file] [log] [blame]
Craig Harrison2b6c6fc2011-06-23 10:34:02 -07001# 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
5import subprocess
6
Chrome Bot9a1137d2011-07-19 14:35:00 -07007from autotest_lib.client.common_lib import error
8from autotest_lib.server import test
Craig Harrison2b6c6fc2011-06-23 10:34:02 -07009import autotest_lib.server.cros.servo
10
11class 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 Broch5fd6bc02011-07-20 15:53:37 -070021 def initialize(self, host, servo_port, xml_config='servo.xml',
22 servo_vid=None, servo_pid=None, servo_serial=None):
Craig Harrison2b6c6fc2011-06-23 10:34:02 -070023 """Create a Servo object."""
Chrome Bot9a1137d2011-07-19 14:35:00 -070024 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 Harrison2b6c6fc2011-06-23 10:34:02 -070036 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 Bot9a1137d2011-07-19 14:35:00 -070062 if self.servo: del self.servo