Simran Basi | e644932 | 2012-06-15 15:03:49 -0700 | [diff] [blame] | 1 | # Copyright (c) 2012 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 logging |
| 6 | import mox |
| 7 | import pexpect |
| 8 | import time |
Simran Basi | 7498d20 | 2012-07-10 15:21:28 -0700 | [diff] [blame^] | 9 | import unittest |
Simran Basi | e644932 | 2012-06-15 15:03:49 -0700 | [diff] [blame] | 10 | |
| 11 | import dli |
| 12 | |
| 13 | import rpm_controller |
| 14 | |
| 15 | |
| 16 | class TestSentryRPMController(mox.MoxTestBase): |
| 17 | |
| 18 | |
| 19 | def setUp(self): |
| 20 | super(TestSentryRPMController, self).setUp() |
| 21 | self.ssh = self.mox.CreateMock(pexpect.spawn) |
| 22 | self.rpm = rpm_controller.SentryRPMController('chromeos-rack1-host8', |
| 23 | self.ssh) |
| 24 | |
| 25 | |
| 26 | def testSuccessfullyChangeOutlet(self): |
| 27 | """Should return True if change was successful.""" |
| 28 | prompt = 'Switched CDU:' |
| 29 | password = 'admn' |
| 30 | dut_hostname = 'chromos-rack1-host8' |
| 31 | new_state = 'ON' |
| 32 | self.ssh.expect('Password:', timeout=60) |
| 33 | self.ssh.sendline(password) |
| 34 | self.ssh.expect(prompt, timeout=60) |
| 35 | self.ssh.sendline('%s %s' % (new_state, dut_hostname)) |
| 36 | self.ssh.expect('Command successful', timeout=60) |
| 37 | self.ssh.sendline('logout') |
| 38 | self.mox.ReplayAll() |
| 39 | self.assertTrue(self.rpm.queue_request(dut_hostname, new_state)) |
| 40 | self.mox.VerifyAll() |
| 41 | |
| 42 | |
| 43 | def testUnsuccessfullyChangeOutlet(self): |
| 44 | """Should return False if change was unsuccessful.""" |
| 45 | prompt = 'Switched CDU:' |
| 46 | password = 'admn' |
| 47 | dut_hostname = 'chromos-rack1-host8' |
| 48 | new_state = 'ON' |
| 49 | self.ssh.expect('Password:', timeout=60) |
| 50 | self.ssh.sendline(password) |
| 51 | self.ssh.expect(prompt, timeout=60) |
| 52 | self.ssh.sendline('%s %s' % (new_state, dut_hostname)) |
| 53 | self.ssh.expect('Command successful', |
| 54 | timeout=60).AndRaise(pexpect.TIMEOUT('Timed Out')) |
| 55 | self.ssh.sendline('logout') |
| 56 | self.mox.ReplayAll() |
| 57 | self.assertFalse(self.rpm.queue_request(dut_hostname, new_state)) |
| 58 | self.mox.VerifyAll() |
| 59 | |
| 60 | |
| 61 | class TestWebPoweredRPMController(mox.MoxTestBase): |
| 62 | |
| 63 | |
| 64 | def setUp(self): |
| 65 | super(TestWebPoweredRPMController, self).setUp() |
| 66 | self.dli_ps = self.mox.CreateMock(dli.powerswitch) |
| 67 | hostname = 'chromeos-rack8a-rpm1' |
| 68 | self.web_rpm = rpm_controller.WebPoweredRPMController(hostname, |
| 69 | self.dli_ps) |
| 70 | outlet = 8 |
| 71 | dut = 'chromeos-rack8a-host8' |
| 72 | # Outlet statuses are in the format "u'ON'" |
| 73 | initial_state = 'u\'ON\'' |
| 74 | self.test_status_list_initial = [[outlet, dut, initial_state]] |
| 75 | |
| 76 | |
| 77 | def testSuccessfullyChangeOutlet(self): |
| 78 | """Should return True if change was successful.""" |
| 79 | test_status_list_final = [[8,'chromeos-rack8a-host8','u\'OFF\'']] |
| 80 | self.dli_ps.statuslist().AndReturn(self.test_status_list_initial) |
| 81 | self.dli_ps.off(8) |
| 82 | self.dli_ps.statuslist().AndReturn(test_status_list_final) |
| 83 | self.mox.ReplayAll() |
| 84 | self.assertTrue(self.web_rpm.queue_request('chromeos-rack8a-host8', |
| 85 | 'OFF')) |
| 86 | self.mox.VerifyAll() |
| 87 | |
| 88 | |
| 89 | def testUnsuccessfullyChangeOutlet(self): |
| 90 | """Should return False if Outlet State does not change.""" |
| 91 | test_status_list_final = [[8,'chromeos-rack8a-host8','u\'ON\'']] |
| 92 | self.dli_ps.statuslist().AndReturn(self.test_status_list_initial) |
| 93 | self.dli_ps.off(8) |
| 94 | self.dli_ps.statuslist().AndReturn(test_status_list_final) |
| 95 | self.mox.ReplayAll() |
| 96 | self.assertFalse(self.web_rpm.queue_request('chromeos-rack8a-host8', |
| 97 | 'OFF')) |
| 98 | self.mox.VerifyAll() |
| 99 | |
| 100 | |
| 101 | def testDutNotOnRPM(self): |
| 102 | """Should return False if DUT hostname is not on the RPM device.""" |
| 103 | self.dli_ps.statuslist().AndReturn(self.test_status_list_initial) |
| 104 | self.mox.ReplayAll() |
| 105 | self.assertFalse(self.web_rpm.queue_request('chromeos-rack8a-host1', |
| 106 | 'OFF')) |
| 107 | self.mox.VerifyAll() |
| 108 | |
| 109 | |
| 110 | if __name__ == "__main__": |
| 111 | unittest.main() |