Scott Zawalski | 201d6be | 2012-09-21 15:56:25 -0400 | [diff] [blame] | 1 | #!/usr/bin/python |
Simran Basi | e644932 | 2012-06-15 15:03:49 -0700 | [diff] [blame] | 2 | # Copyright (c) 2012 The Chromium OS Authors. All rights reserved. |
| 3 | # Use of this source code is governed by a BSD-style license that can be |
| 4 | # found in the LICENSE file. |
| 5 | |
Simran Basi | e644932 | 2012-06-15 15:03:49 -0700 | [diff] [blame] | 6 | import mox |
| 7 | import pexpect |
Simran Basi | 7498d20 | 2012-07-10 15:21:28 -0700 | [diff] [blame] | 8 | import unittest |
Simran Basi | e644932 | 2012-06-15 15:03:49 -0700 | [diff] [blame] | 9 | |
| 10 | import dli |
| 11 | |
| 12 | import rpm_controller |
| 13 | |
| 14 | |
Dan Shi | 6d46b56 | 2014-03-07 15:00:15 -0800 | [diff] [blame^] | 15 | class TestRPMControllerQueue(mox.MoxTestBase): |
| 16 | """Test request can be queued and processed in controller. |
| 17 | """ |
| 18 | |
| 19 | def setUp(self): |
| 20 | super(TestRPMControllerQueue, self).setUp() |
| 21 | self.rpm = rpm_controller.SentryRPMController('chromeos-rack1-host8') |
| 22 | |
| 23 | |
| 24 | def testQueueRequest(self): |
| 25 | """Should create a new process to handle request.""" |
| 26 | dut_hostname = 'chromos-rack1-host8' |
| 27 | new_state = 'ON' |
| 28 | process = self.mox.CreateMockAnything() |
| 29 | rpm_controller.multiprocessing.Process = self.mox.CreateMockAnything() |
| 30 | rpm_controller.multiprocessing.Process(target=mox.IgnoreArg(), |
| 31 | args=mox.IgnoreArg()).AndReturn(process) |
| 32 | process.start() |
| 33 | process.join() |
| 34 | self.mox.ReplayAll() |
| 35 | self.assertFalse(self.rpm.queue_request(dut_hostname, new_state)) |
| 36 | self.mox.VerifyAll() |
| 37 | |
| 38 | |
Simran Basi | e644932 | 2012-06-15 15:03:49 -0700 | [diff] [blame] | 39 | class TestSentryRPMController(mox.MoxTestBase): |
Dan Shi | 6d46b56 | 2014-03-07 15:00:15 -0800 | [diff] [blame^] | 40 | """Test SentryRPMController.""" |
Simran Basi | e644932 | 2012-06-15 15:03:49 -0700 | [diff] [blame] | 41 | |
| 42 | |
| 43 | def setUp(self): |
| 44 | super(TestSentryRPMController, self).setUp() |
Simran Basi | aba8b53 | 2012-08-09 14:58:35 -0700 | [diff] [blame] | 45 | self.ssh = self.mox.CreateMockAnything() |
| 46 | rpm_controller.pexpect.spawn = self.mox.CreateMockAnything() |
| 47 | rpm_controller.pexpect.spawn(mox.IgnoreArg()).AndReturn(self.ssh) |
| 48 | self.rpm = rpm_controller.SentryRPMController('chromeos-rack1-host8') |
Simran Basi | e644932 | 2012-06-15 15:03:49 -0700 | [diff] [blame] | 49 | |
| 50 | |
| 51 | def testSuccessfullyChangeOutlet(self): |
| 52 | """Should return True if change was successful.""" |
| 53 | prompt = 'Switched CDU:' |
| 54 | password = 'admn' |
| 55 | dut_hostname = 'chromos-rack1-host8' |
| 56 | new_state = 'ON' |
| 57 | self.ssh.expect('Password:', timeout=60) |
| 58 | self.ssh.sendline(password) |
| 59 | self.ssh.expect(prompt, timeout=60) |
| 60 | self.ssh.sendline('%s %s' % (new_state, dut_hostname)) |
| 61 | self.ssh.expect('Command successful', timeout=60) |
| 62 | self.ssh.sendline('logout') |
Fang Deng | 71c4b1f | 2013-05-20 09:55:04 -0700 | [diff] [blame] | 63 | self.ssh.close(force=True) |
Simran Basi | e644932 | 2012-06-15 15:03:49 -0700 | [diff] [blame] | 64 | self.mox.ReplayAll() |
Dan Shi | 6d46b56 | 2014-03-07 15:00:15 -0800 | [diff] [blame^] | 65 | self.assertTrue(self.rpm.set_power_state(dut_hostname, new_state)) |
Simran Basi | e644932 | 2012-06-15 15:03:49 -0700 | [diff] [blame] | 66 | self.mox.VerifyAll() |
| 67 | |
| 68 | |
| 69 | def testUnsuccessfullyChangeOutlet(self): |
| 70 | """Should return False if change was unsuccessful.""" |
| 71 | prompt = 'Switched CDU:' |
| 72 | password = 'admn' |
| 73 | dut_hostname = 'chromos-rack1-host8' |
| 74 | new_state = 'ON' |
| 75 | self.ssh.expect('Password:', timeout=60) |
| 76 | self.ssh.sendline(password) |
| 77 | self.ssh.expect(prompt, timeout=60) |
| 78 | self.ssh.sendline('%s %s' % (new_state, dut_hostname)) |
| 79 | self.ssh.expect('Command successful', |
| 80 | timeout=60).AndRaise(pexpect.TIMEOUT('Timed Out')) |
| 81 | self.ssh.sendline('logout') |
Fang Deng | 71c4b1f | 2013-05-20 09:55:04 -0700 | [diff] [blame] | 82 | self.ssh.close(force=True) |
Simran Basi | e644932 | 2012-06-15 15:03:49 -0700 | [diff] [blame] | 83 | self.mox.ReplayAll() |
Dan Shi | 6d46b56 | 2014-03-07 15:00:15 -0800 | [diff] [blame^] | 84 | self.assertFalse(self.rpm.set_power_state(dut_hostname, new_state)) |
Simran Basi | e644932 | 2012-06-15 15:03:49 -0700 | [diff] [blame] | 85 | self.mox.VerifyAll() |
| 86 | |
| 87 | |
| 88 | class TestWebPoweredRPMController(mox.MoxTestBase): |
Dan Shi | 6d46b56 | 2014-03-07 15:00:15 -0800 | [diff] [blame^] | 89 | """Test WebPoweredRPMController.""" |
Simran Basi | e644932 | 2012-06-15 15:03:49 -0700 | [diff] [blame] | 90 | |
| 91 | |
| 92 | def setUp(self): |
| 93 | super(TestWebPoweredRPMController, self).setUp() |
| 94 | self.dli_ps = self.mox.CreateMock(dli.powerswitch) |
| 95 | hostname = 'chromeos-rack8a-rpm1' |
| 96 | self.web_rpm = rpm_controller.WebPoweredRPMController(hostname, |
| 97 | self.dli_ps) |
| 98 | outlet = 8 |
| 99 | dut = 'chromeos-rack8a-host8' |
| 100 | # Outlet statuses are in the format "u'ON'" |
| 101 | initial_state = 'u\'ON\'' |
| 102 | self.test_status_list_initial = [[outlet, dut, initial_state]] |
| 103 | |
| 104 | |
| 105 | def testSuccessfullyChangeOutlet(self): |
| 106 | """Should return True if change was successful.""" |
Fang Deng | 71c4b1f | 2013-05-20 09:55:04 -0700 | [diff] [blame] | 107 | test_status_list_final = [[8, 'chromeos-rack8a-host8','u\'OFF\'']] |
Simran Basi | e644932 | 2012-06-15 15:03:49 -0700 | [diff] [blame] | 108 | self.dli_ps.statuslist().AndReturn(self.test_status_list_initial) |
| 109 | self.dli_ps.off(8) |
| 110 | self.dli_ps.statuslist().AndReturn(test_status_list_final) |
| 111 | self.mox.ReplayAll() |
Dan Shi | 6d46b56 | 2014-03-07 15:00:15 -0800 | [diff] [blame^] | 112 | self.assertTrue(self.web_rpm.set_power_state('chromeos-rack8a-host8', |
Simran Basi | e644932 | 2012-06-15 15:03:49 -0700 | [diff] [blame] | 113 | 'OFF')) |
| 114 | self.mox.VerifyAll() |
| 115 | |
| 116 | |
| 117 | def testUnsuccessfullyChangeOutlet(self): |
| 118 | """Should return False if Outlet State does not change.""" |
Fang Deng | 71c4b1f | 2013-05-20 09:55:04 -0700 | [diff] [blame] | 119 | test_status_list_final = [[8, 'chromeos-rack8a-host8','u\'ON\'']] |
Simran Basi | e644932 | 2012-06-15 15:03:49 -0700 | [diff] [blame] | 120 | self.dli_ps.statuslist().AndReturn(self.test_status_list_initial) |
| 121 | self.dli_ps.off(8) |
| 122 | self.dli_ps.statuslist().AndReturn(test_status_list_final) |
| 123 | self.mox.ReplayAll() |
Dan Shi | 6d46b56 | 2014-03-07 15:00:15 -0800 | [diff] [blame^] | 124 | self.assertFalse(self.web_rpm.set_power_state('chromeos-rack8a-host8', |
Simran Basi | e644932 | 2012-06-15 15:03:49 -0700 | [diff] [blame] | 125 | 'OFF')) |
| 126 | self.mox.VerifyAll() |
| 127 | |
| 128 | |
| 129 | def testDutNotOnRPM(self): |
| 130 | """Should return False if DUT hostname is not on the RPM device.""" |
| 131 | self.dli_ps.statuslist().AndReturn(self.test_status_list_initial) |
| 132 | self.mox.ReplayAll() |
Dan Shi | 6d46b56 | 2014-03-07 15:00:15 -0800 | [diff] [blame^] | 133 | self.assertFalse(self.web_rpm.set_power_state('chromeos-rack8a-host1', |
Simran Basi | e644932 | 2012-06-15 15:03:49 -0700 | [diff] [blame] | 134 | 'OFF')) |
| 135 | self.mox.VerifyAll() |
| 136 | |
| 137 | |
Fang Deng | 71c4b1f | 2013-05-20 09:55:04 -0700 | [diff] [blame] | 138 | class TestCiscoPOEController(mox.MoxTestBase): |
| 139 | """Test CiscoPOEController.""" |
| 140 | |
| 141 | |
| 142 | STREAM_WELCOME = 'This is a POE switch.\n\nUser Name:' |
| 143 | STREAM_PWD = 'Password:' |
| 144 | STREAM_DEVICE = '\nchromeos2-poe-sw8#' |
| 145 | STREAM_CONFIG = 'chromeos2-poe-sw8(config)#' |
| 146 | STREAM_CONFIG_IF = 'chromeos2-poe-sw8(config-if)#' |
| 147 | STREAM_STATUS = ('\n ' |
| 148 | 'Flow Link Back Mdix\n' |
| 149 | 'Port Type Duplex Speed Neg ' |
| 150 | 'ctrl State Pressure Mode\n' |
| 151 | '-------- ------------ ------ ----- -------- ' |
| 152 | '---- ----------- -------- -------\n' |
| 153 | 'fa32 100M-Copper Full 100 Enabled ' |
| 154 | 'Off Up Disabled Off\n') |
| 155 | SERVO = 'chromeos1-rack3-host12-servo' |
| 156 | SWITCH = 'chromeos2-poe-switch8' |
| 157 | PORT = 'fa32' |
| 158 | |
| 159 | |
| 160 | def setUp(self): |
| 161 | super(TestCiscoPOEController, self).setUp() |
| 162 | self.mox.StubOutWithMock(pexpect.spawn, '_spawn') |
| 163 | self.mox.StubOutWithMock(pexpect.spawn, 'read_nonblocking') |
| 164 | self.mox.StubOutWithMock(pexpect.spawn, 'sendline') |
| 165 | servo_interface = {self.SERVO:(self.SWITCH, self.PORT)} |
| 166 | self.poe = rpm_controller.CiscoPOEController( |
| 167 | self.SWITCH, servo_interface) |
| 168 | pexpect.spawn._spawn(mox.IgnoreArg(), mox.IgnoreArg()) |
| 169 | pexpect.spawn.read_nonblocking( |
| 170 | mox.IgnoreArg(), mox.IgnoreArg()).AndReturn(self.STREAM_WELCOME) |
| 171 | pexpect.spawn.sendline(self.poe._username) |
| 172 | pexpect.spawn.read_nonblocking( |
| 173 | mox.IgnoreArg(), mox.IgnoreArg()).AndReturn(self.STREAM_PWD) |
| 174 | pexpect.spawn.sendline(self.poe._password) |
| 175 | pexpect.spawn.read_nonblocking( |
| 176 | mox.IgnoreArg(), mox.IgnoreArg()).AndReturn(self.STREAM_DEVICE) |
| 177 | |
| 178 | |
| 179 | def testLogin(self): |
| 180 | """Test we can log into the switch.""" |
| 181 | self.mox.ReplayAll() |
| 182 | self.assertNotEqual(self.poe._login(), None) |
| 183 | self.mox.VerifyAll() |
| 184 | |
| 185 | |
| 186 | def _EnterConfigurationHelper(self, success=True): |
| 187 | """A helper function for testing entering configuration terminal. |
| 188 | |
| 189 | @param success: True if we want the process to pass, False if we |
| 190 | want it to fail. |
| 191 | """ |
| 192 | pexpect.spawn.sendline('configure terminal') |
| 193 | pexpect.spawn.read_nonblocking( |
| 194 | mox.IgnoreArg(), mox.IgnoreArg()).AndReturn(self.STREAM_CONFIG) |
| 195 | pexpect.spawn.sendline('interface %s' % self.PORT) |
| 196 | if success: |
| 197 | pexpect.spawn.read_nonblocking( |
| 198 | mox.IgnoreArg(), |
| 199 | mox.IgnoreArg()).AndReturn(self.STREAM_CONFIG_IF) |
| 200 | else: |
| 201 | self.mox.StubOutWithMock(pexpect.spawn, '__str__') |
| 202 | exception = pexpect.TIMEOUT( |
| 203 | 'Could not enter configuration terminal.') |
| 204 | pexpect.spawn.read_nonblocking( |
| 205 | mox.IgnoreArg(), |
| 206 | mox.IgnoreArg()).MultipleTimes().AndRaise(exception) |
| 207 | pexpect.spawn.__str__().AndReturn('A pexpect.spawn object.') |
| 208 | pexpect.spawn.sendline('end') |
| 209 | |
| 210 | |
| 211 | def testSuccessfullyChangeOutlet(self): |
| 212 | """Should return True if change was successful.""" |
| 213 | self._EnterConfigurationHelper() |
| 214 | pexpect.spawn.sendline('power inline auto') |
| 215 | pexpect.spawn.sendline('end') |
| 216 | pexpect.spawn.read_nonblocking( |
| 217 | mox.IgnoreArg(), mox.IgnoreArg()).AndReturn(self.STREAM_DEVICE) |
| 218 | pexpect.spawn.sendline('show interface status %s' % self.PORT) |
| 219 | pexpect.spawn.read_nonblocking( |
| 220 | mox.IgnoreArg(), mox.IgnoreArg()).AndReturn(self.STREAM_STATUS) |
| 221 | pexpect.spawn.read_nonblocking( |
| 222 | mox.IgnoreArg(), mox.IgnoreArg()).AndReturn(self.STREAM_DEVICE) |
| 223 | pexpect.spawn.sendline('exit') |
| 224 | self.mox.ReplayAll() |
Dan Shi | 6d46b56 | 2014-03-07 15:00:15 -0800 | [diff] [blame^] | 225 | self.assertTrue(self.poe.set_power_state(self.SERVO, 'ON')) |
Fang Deng | 71c4b1f | 2013-05-20 09:55:04 -0700 | [diff] [blame] | 226 | self.mox.VerifyAll() |
| 227 | |
| 228 | |
| 229 | def testUnableToEnterConfigurationTerminal(self): |
| 230 | """Should return False if unable to enter configuration terminal.""" |
| 231 | self._EnterConfigurationHelper(success=False) |
| 232 | pexpect.spawn.sendline('exit') |
| 233 | self.mox.ReplayAll() |
Dan Shi | 6d46b56 | 2014-03-07 15:00:15 -0800 | [diff] [blame^] | 234 | self.assertFalse(self.poe.set_power_state(self.SERVO, 'ON')) |
Fang Deng | 71c4b1f | 2013-05-20 09:55:04 -0700 | [diff] [blame] | 235 | self.mox.VerifyAll() |
| 236 | |
| 237 | |
| 238 | def testUnableToExitConfigurationTerminal(self): |
| 239 | """Should return False if unable to exit configuration terminal.""" |
| 240 | self.mox.StubOutWithMock(pexpect.spawn, '__str__') |
| 241 | self.mox.StubOutWithMock(rpm_controller.CiscoPOEController, |
| 242 | '_enter_configuration_terminal') |
| 243 | self.poe._enter_configuration_terminal( |
| 244 | mox.IgnoreArg(), mox.IgnoreArg()).AndReturn(True) |
| 245 | pexpect.spawn.sendline('power inline auto') |
| 246 | pexpect.spawn.sendline('end') |
| 247 | exception = pexpect.TIMEOUT('Could not exit configuration terminal.') |
| 248 | pexpect.spawn.read_nonblocking( |
| 249 | mox.IgnoreArg(), |
| 250 | mox.IgnoreArg()).MultipleTimes().AndRaise(exception) |
| 251 | pexpect.spawn.__str__().AndReturn('A pexpect.spawn object.') |
| 252 | pexpect.spawn.sendline('exit') |
| 253 | self.mox.ReplayAll() |
Dan Shi | 6d46b56 | 2014-03-07 15:00:15 -0800 | [diff] [blame^] | 254 | self.assertFalse(self.poe.set_power_state(self.SERVO, 'ON')) |
Fang Deng | 71c4b1f | 2013-05-20 09:55:04 -0700 | [diff] [blame] | 255 | self.mox.VerifyAll() |
| 256 | |
| 257 | |
| 258 | def testUnableToVerifyState(self): |
| 259 | """Should return False if unable to verify current state.""" |
| 260 | self.mox.StubOutWithMock(pexpect.spawn, '__str__') |
| 261 | self.mox.StubOutWithMock(rpm_controller.CiscoPOEController, |
| 262 | '_enter_configuration_terminal') |
| 263 | self.mox.StubOutWithMock(rpm_controller.CiscoPOEController, |
| 264 | '_exit_configuration_terminal') |
| 265 | self.poe._enter_configuration_terminal( |
| 266 | mox.IgnoreArg(), mox.IgnoreArg()).AndReturn(True) |
| 267 | pexpect.spawn.sendline('power inline auto') |
| 268 | self.poe._exit_configuration_terminal(mox.IgnoreArg()).AndReturn(True) |
| 269 | pexpect.spawn.sendline('show interface status %s' % self.PORT) |
| 270 | exception = pexpect.TIMEOUT('Could not verify state.') |
| 271 | pexpect.spawn.read_nonblocking( |
| 272 | mox.IgnoreArg(), |
| 273 | mox.IgnoreArg()).MultipleTimes().AndRaise(exception) |
| 274 | pexpect.spawn.__str__().AndReturn('A pexpect.spawn object.') |
| 275 | pexpect.spawn.sendline('exit') |
| 276 | self.mox.ReplayAll() |
Dan Shi | 6d46b56 | 2014-03-07 15:00:15 -0800 | [diff] [blame^] | 277 | self.assertFalse(self.poe.set_power_state(self.SERVO, 'ON')) |
Fang Deng | 71c4b1f | 2013-05-20 09:55:04 -0700 | [diff] [blame] | 278 | self.mox.VerifyAll() |
| 279 | |
| 280 | |
Simran Basi | e644932 | 2012-06-15 15:03:49 -0700 | [diff] [blame] | 281 | if __name__ == "__main__": |
| 282 | unittest.main() |