Mike Frysinger | d03e6b5 | 2019-08-03 12:49:01 -0400 | [diff] [blame] | 1 | #!/usr/bin/python2 |
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 | |
Fang Deng | c30c4bf | 2014-08-13 17:21:21 -0700 | [diff] [blame] | 14 | import common |
| 15 | from autotest_lib.site_utils.rpm_control_system import utils |
| 16 | |
Simran Basi | e644932 | 2012-06-15 15:03:49 -0700 | [diff] [blame] | 17 | |
Dan Shi | 6d46b56 | 2014-03-07 15:00:15 -0800 | [diff] [blame] | 18 | class TestRPMControllerQueue(mox.MoxTestBase): |
Abe Smith | 2b48c2a | 2019-01-28 18:18:06 -0500 | [diff] [blame] | 19 | """Test request can be queued and processed in controller.""" |
Dan Shi | 6d46b56 | 2014-03-07 15:00:15 -0800 | [diff] [blame] | 20 | |
| 21 | def setUp(self): |
| 22 | super(TestRPMControllerQueue, self).setUp() |
| 23 | self.rpm = rpm_controller.SentryRPMController('chromeos-rack1-host8') |
Fang Deng | c30c4bf | 2014-08-13 17:21:21 -0700 | [diff] [blame] | 24 | self.powerunit_info = utils.PowerUnitInfo( |
| 25 | device_hostname='chromos-rack1-host8', |
| 26 | powerunit_hostname='chromeos-rack1-rpm1', |
| 27 | powerunit_type=utils.PowerUnitInfo.POWERUNIT_TYPES.RPM, |
| 28 | outlet='.A100', |
| 29 | hydra_hostname=None) |
Dan Shi | 6d46b56 | 2014-03-07 15:00:15 -0800 | [diff] [blame] | 30 | |
| 31 | |
| 32 | def testQueueRequest(self): |
| 33 | """Should create a new process to handle request.""" |
Dan Shi | 6d46b56 | 2014-03-07 15:00:15 -0800 | [diff] [blame] | 34 | new_state = 'ON' |
| 35 | process = self.mox.CreateMockAnything() |
| 36 | rpm_controller.multiprocessing.Process = self.mox.CreateMockAnything() |
| 37 | rpm_controller.multiprocessing.Process(target=mox.IgnoreArg(), |
| 38 | args=mox.IgnoreArg()).AndReturn(process) |
| 39 | process.start() |
| 40 | process.join() |
| 41 | self.mox.ReplayAll() |
Fang Deng | c30c4bf | 2014-08-13 17:21:21 -0700 | [diff] [blame] | 42 | self.assertFalse(self.rpm.queue_request(self.powerunit_info, new_state)) |
Dan Shi | 6d46b56 | 2014-03-07 15:00:15 -0800 | [diff] [blame] | 43 | self.mox.VerifyAll() |
| 44 | |
| 45 | |
Simran Basi | e644932 | 2012-06-15 15:03:49 -0700 | [diff] [blame] | 46 | class TestSentryRPMController(mox.MoxTestBase): |
Dan Shi | 6d46b56 | 2014-03-07 15:00:15 -0800 | [diff] [blame] | 47 | """Test SentryRPMController.""" |
Simran Basi | e644932 | 2012-06-15 15:03:49 -0700 | [diff] [blame] | 48 | |
| 49 | |
| 50 | def setUp(self): |
| 51 | super(TestSentryRPMController, self).setUp() |
Simran Basi | aba8b53 | 2012-08-09 14:58:35 -0700 | [diff] [blame] | 52 | self.ssh = self.mox.CreateMockAnything() |
| 53 | rpm_controller.pexpect.spawn = self.mox.CreateMockAnything() |
| 54 | rpm_controller.pexpect.spawn(mox.IgnoreArg()).AndReturn(self.ssh) |
| 55 | self.rpm = rpm_controller.SentryRPMController('chromeos-rack1-host8') |
Fang Deng | c30c4bf | 2014-08-13 17:21:21 -0700 | [diff] [blame] | 56 | self.powerunit_info = utils.PowerUnitInfo( |
| 57 | device_hostname='chromos-rack1-host8', |
| 58 | powerunit_hostname='chromeos-rack1-rpm1', |
| 59 | powerunit_type=utils.PowerUnitInfo.POWERUNIT_TYPES.RPM, |
| 60 | outlet='.A100', |
| 61 | hydra_hostname=None) |
Simran Basi | e644932 | 2012-06-15 15:03:49 -0700 | [diff] [blame] | 62 | |
| 63 | |
| 64 | def testSuccessfullyChangeOutlet(self): |
| 65 | """Should return True if change was successful.""" |
Abe Smith | 2b48c2a | 2019-01-28 18:18:06 -0500 | [diff] [blame] | 66 | prompt = ['Switched CDU:', 'Switched PDU:'] |
Aviv Keshet | 4d7bbc8 | 2018-11-01 17:55:53 -0700 | [diff] [blame] | 67 | password = 'admn' |
Simran Basi | e644932 | 2012-06-15 15:03:49 -0700 | [diff] [blame] | 68 | new_state = 'ON' |
| 69 | self.ssh.expect('Password:', timeout=60) |
| 70 | self.ssh.sendline(password) |
| 71 | self.ssh.expect(prompt, timeout=60) |
Fang Deng | c30c4bf | 2014-08-13 17:21:21 -0700 | [diff] [blame] | 72 | self.ssh.sendline('%s %s' % (new_state, self.powerunit_info.outlet)) |
Simran Basi | e644932 | 2012-06-15 15:03:49 -0700 | [diff] [blame] | 73 | self.ssh.expect('Command successful', timeout=60) |
| 74 | self.ssh.sendline('logout') |
Fang Deng | 71c4b1f | 2013-05-20 09:55:04 -0700 | [diff] [blame] | 75 | self.ssh.close(force=True) |
Simran Basi | e644932 | 2012-06-15 15:03:49 -0700 | [diff] [blame] | 76 | self.mox.ReplayAll() |
Fang Deng | c30c4bf | 2014-08-13 17:21:21 -0700 | [diff] [blame] | 77 | self.assertTrue(self.rpm.set_power_state( |
| 78 | self.powerunit_info, new_state)) |
Simran Basi | e644932 | 2012-06-15 15:03:49 -0700 | [diff] [blame] | 79 | self.mox.VerifyAll() |
| 80 | |
| 81 | |
| 82 | def testUnsuccessfullyChangeOutlet(self): |
| 83 | """Should return False if change was unsuccessful.""" |
Abe Smith | 2b48c2a | 2019-01-28 18:18:06 -0500 | [diff] [blame] | 84 | prompt = ['Switched CDU:', 'Switched PDU:'] |
Aviv Keshet | 4d7bbc8 | 2018-11-01 17:55:53 -0700 | [diff] [blame] | 85 | password = 'admn' |
Simran Basi | e644932 | 2012-06-15 15:03:49 -0700 | [diff] [blame] | 86 | new_state = 'ON' |
| 87 | self.ssh.expect('Password:', timeout=60) |
| 88 | self.ssh.sendline(password) |
| 89 | self.ssh.expect(prompt, timeout=60) |
Fang Deng | c30c4bf | 2014-08-13 17:21:21 -0700 | [diff] [blame] | 90 | self.ssh.sendline('%s %s' % (new_state, self.powerunit_info.outlet)) |
Simran Basi | e644932 | 2012-06-15 15:03:49 -0700 | [diff] [blame] | 91 | self.ssh.expect('Command successful', |
| 92 | timeout=60).AndRaise(pexpect.TIMEOUT('Timed Out')) |
| 93 | self.ssh.sendline('logout') |
Fang Deng | 71c4b1f | 2013-05-20 09:55:04 -0700 | [diff] [blame] | 94 | self.ssh.close(force=True) |
Simran Basi | e644932 | 2012-06-15 15:03:49 -0700 | [diff] [blame] | 95 | self.mox.ReplayAll() |
Abe Smith | 2b48c2a | 2019-01-28 18:18:06 -0500 | [diff] [blame] | 96 | self.assertFalse( |
| 97 | self.rpm.set_power_state(self.powerunit_info, new_state)) |
Simran Basi | e644932 | 2012-06-15 15:03:49 -0700 | [diff] [blame] | 98 | self.mox.VerifyAll() |
| 99 | |
| 100 | |
| 101 | class TestWebPoweredRPMController(mox.MoxTestBase): |
Dan Shi | 6d46b56 | 2014-03-07 15:00:15 -0800 | [diff] [blame] | 102 | """Test WebPoweredRPMController.""" |
Simran Basi | e644932 | 2012-06-15 15:03:49 -0700 | [diff] [blame] | 103 | |
| 104 | |
| 105 | def setUp(self): |
| 106 | super(TestWebPoweredRPMController, self).setUp() |
| 107 | self.dli_ps = self.mox.CreateMock(dli.powerswitch) |
| 108 | hostname = 'chromeos-rack8a-rpm1' |
| 109 | self.web_rpm = rpm_controller.WebPoweredRPMController(hostname, |
| 110 | self.dli_ps) |
| 111 | outlet = 8 |
| 112 | dut = 'chromeos-rack8a-host8' |
| 113 | # Outlet statuses are in the format "u'ON'" |
| 114 | initial_state = 'u\'ON\'' |
| 115 | self.test_status_list_initial = [[outlet, dut, initial_state]] |
Fang Deng | c30c4bf | 2014-08-13 17:21:21 -0700 | [diff] [blame] | 116 | self.powerunit_info = utils.PowerUnitInfo( |
| 117 | device_hostname=dut, |
| 118 | powerunit_hostname=hostname, |
| 119 | powerunit_type=utils.PowerUnitInfo.POWERUNIT_TYPES.RPM, |
| 120 | outlet=outlet, |
| 121 | hydra_hostname=None) |
Simran Basi | e644932 | 2012-06-15 15:03:49 -0700 | [diff] [blame] | 122 | |
| 123 | |
| 124 | def testSuccessfullyChangeOutlet(self): |
| 125 | """Should return True if change was successful.""" |
Abe Smith | 2b48c2a | 2019-01-28 18:18:06 -0500 | [diff] [blame] | 126 | test_status_list_final = [[8, 'chromeos-rack8a-host8', 'u\'OFF\'']] |
Simran Basi | e644932 | 2012-06-15 15:03:49 -0700 | [diff] [blame] | 127 | self.dli_ps.off(8) |
| 128 | self.dli_ps.statuslist().AndReturn(test_status_list_final) |
| 129 | self.mox.ReplayAll() |
Fang Deng | c30c4bf | 2014-08-13 17:21:21 -0700 | [diff] [blame] | 130 | self.assertTrue(self.web_rpm.set_power_state( |
| 131 | self.powerunit_info, 'OFF')) |
Simran Basi | e644932 | 2012-06-15 15:03:49 -0700 | [diff] [blame] | 132 | self.mox.VerifyAll() |
| 133 | |
| 134 | |
| 135 | def testUnsuccessfullyChangeOutlet(self): |
| 136 | """Should return False if Outlet State does not change.""" |
Abe Smith | 2b48c2a | 2019-01-28 18:18:06 -0500 | [diff] [blame] | 137 | test_status_list_final = [[8, 'chromeos-rack8a-host8', 'u\'ON\'']] |
Simran Basi | e644932 | 2012-06-15 15:03:49 -0700 | [diff] [blame] | 138 | self.dli_ps.off(8) |
| 139 | self.dli_ps.statuslist().AndReturn(test_status_list_final) |
| 140 | self.mox.ReplayAll() |
Fang Deng | c30c4bf | 2014-08-13 17:21:21 -0700 | [diff] [blame] | 141 | self.assertFalse(self.web_rpm.set_power_state( |
| 142 | self.powerunit_info, 'OFF')) |
Simran Basi | e644932 | 2012-06-15 15:03:49 -0700 | [diff] [blame] | 143 | self.mox.VerifyAll() |
| 144 | |
| 145 | |
Fang Deng | c30c4bf | 2014-08-13 17:21:21 -0700 | [diff] [blame] | 146 | def testNoOutlet(self): |
Simran Basi | e644932 | 2012-06-15 15:03:49 -0700 | [diff] [blame] | 147 | """Should return False if DUT hostname is not on the RPM device.""" |
Fang Deng | c30c4bf | 2014-08-13 17:21:21 -0700 | [diff] [blame] | 148 | self.powerunit_info.outlet=None |
| 149 | self.assertFalse(self.web_rpm.set_power_state( |
| 150 | self.powerunit_info, 'OFF')) |
Simran Basi | e644932 | 2012-06-15 15:03:49 -0700 | [diff] [blame] | 151 | |
| 152 | |
Fang Deng | 71c4b1f | 2013-05-20 09:55:04 -0700 | [diff] [blame] | 153 | class TestCiscoPOEController(mox.MoxTestBase): |
| 154 | """Test CiscoPOEController.""" |
| 155 | |
Abe Smith | 2b48c2a | 2019-01-28 18:18:06 -0500 | [diff] [blame] | 156 | DEVICE = 'chromeos2-poe-sw8#' |
| 157 | MATCHER = 'Port\\s+.*%s(\\s+(\\S+)){6,6}.*%s' |
| 158 | PORT = 'fa32' |
| 159 | PWD = 'Password:' |
Fang Deng | 71c4b1f | 2013-05-20 09:55:04 -0700 | [diff] [blame] | 160 | SERVO = 'chromeos1-rack3-host12-servo' |
| 161 | SWITCH = 'chromeos2-poe-switch8' |
Fang Deng | c30c4bf | 2014-08-13 17:21:21 -0700 | [diff] [blame] | 162 | POWERUNIT_INFO = utils.PowerUnitInfo( |
| 163 | device_hostname=PORT, |
| 164 | powerunit_hostname=SERVO, |
| 165 | powerunit_type=utils.PowerUnitInfo.POWERUNIT_TYPES.POE, |
| 166 | outlet=PORT, |
| 167 | hydra_hostname=None) |
Fang Deng | 71c4b1f | 2013-05-20 09:55:04 -0700 | [diff] [blame] | 168 | |
Fang Deng | 71c4b1f | 2013-05-20 09:55:04 -0700 | [diff] [blame] | 169 | def testLogin(self): |
| 170 | """Test we can log into the switch.""" |
Abe Smith | 2b48c2a | 2019-01-28 18:18:06 -0500 | [diff] [blame] | 171 | rpm_controller.pexpect.spawn = self.mox.CreateMockAnything() |
| 172 | mock_ssh = self.mox.CreateMockAnything() |
| 173 | rpm_controller.pexpect.spawn(mox.IgnoreArg()).AndReturn(mock_ssh) |
| 174 | sut = rpm_controller.CiscoPOEController(self.SWITCH) |
| 175 | mock_ssh.expect(sut.POE_USERNAME_PROMPT, timeout=sut.LOGIN_TIMEOUT) |
| 176 | mock_ssh.sendline(mox.IgnoreArg()) |
| 177 | mock_ssh.expect(self.PWD, timeout=sut.LOGIN_TIMEOUT) |
| 178 | mock_ssh.sendline(mox.IgnoreArg()) |
| 179 | mock_ssh.expect(self.DEVICE, timeout=sut.LOGIN_TIMEOUT) |
Fang Deng | 71c4b1f | 2013-05-20 09:55:04 -0700 | [diff] [blame] | 180 | self.mox.ReplayAll() |
Abe Smith | 2b48c2a | 2019-01-28 18:18:06 -0500 | [diff] [blame] | 181 | self.assertIsNotNone(sut._login()) |
Fang Deng | 71c4b1f | 2013-05-20 09:55:04 -0700 | [diff] [blame] | 182 | self.mox.VerifyAll() |
| 183 | |
Abe Smith | 2b48c2a | 2019-01-28 18:18:06 -0500 | [diff] [blame] | 184 | def testSuccessfullyChangePowerState(self): |
Fang Deng | 71c4b1f | 2013-05-20 09:55:04 -0700 | [diff] [blame] | 185 | """Should return True if change was successful.""" |
Abe Smith | 2b48c2a | 2019-01-28 18:18:06 -0500 | [diff] [blame] | 186 | sut = rpm_controller.CiscoPOEController(self.SWITCH) |
| 187 | mock_ssh = self.mox.CreateMockAnything() |
| 188 | self.mox.StubOutWithMock(sut, '_login') |
| 189 | sut._login().AndReturn(mock_ssh) |
| 190 | self.mox.StubOutWithMock(sut, '_verify_state') |
| 191 | sut._verify_state(self.PORT, 'ON', mock_ssh).AndReturn(True) |
| 192 | # _enter_configuration_terminal |
| 193 | mock_ssh.sendline(sut.CONFIG) |
| 194 | mock_ssh.expect(sut.config_prompt, timeout=sut.CMD_TIMEOUT) |
| 195 | mock_ssh.sendline(sut.CONFIG_IF % self.PORT) |
| 196 | mock_ssh.expect(sut.config_if_prompt, timeout=sut.CMD_TIMEOUT) |
| 197 | # _change_state |
| 198 | mock_ssh.sendline(sut.SET_STATE_ON) |
| 199 | # _exit_configuration_terminal |
| 200 | mock_ssh.sendline(sut.END_CMD) |
| 201 | mock_ssh.expect(sut.poe_prompt, timeout=sut.CMD_TIMEOUT) |
| 202 | # exit |
| 203 | mock_ssh.sendline(sut.EXIT_CMD) |
| 204 | mock_ssh.close(force=True) |
Fang Deng | 71c4b1f | 2013-05-20 09:55:04 -0700 | [diff] [blame] | 205 | self.mox.ReplayAll() |
Abe Smith | 2b48c2a | 2019-01-28 18:18:06 -0500 | [diff] [blame] | 206 | self.assertTrue(sut.set_power_state(self.POWERUNIT_INFO, 'ON')) |
Fang Deng | 71c4b1f | 2013-05-20 09:55:04 -0700 | [diff] [blame] | 207 | self.mox.VerifyAll() |
| 208 | |
Fang Deng | 71c4b1f | 2013-05-20 09:55:04 -0700 | [diff] [blame] | 209 | def testUnableToEnterConfigurationTerminal(self): |
| 210 | """Should return False if unable to enter configuration terminal.""" |
Abe Smith | 2b48c2a | 2019-01-28 18:18:06 -0500 | [diff] [blame] | 211 | exception = pexpect.TIMEOUT('Could not enter configuration terminal.') |
| 212 | sut = rpm_controller.CiscoPOEController(self.SWITCH) |
| 213 | timeout = sut.CMD_TIMEOUT |
| 214 | mock_ssh = self.mox.CreateMockAnything() |
| 215 | self.mox.StubOutWithMock(sut, '_login') |
| 216 | sut._login().AndReturn(mock_ssh) |
| 217 | mock_ssh.sendline(sut.CONFIG) |
| 218 | mock_ssh.expect(sut.config_prompt, timeout=timeout) |
| 219 | mock_ssh.sendline(sut.CONFIG_IF % self.PORT) |
| 220 | config_if_prompt = sut.config_if_prompt |
| 221 | mock_ssh.expect(config_if_prompt, timeout=timeout).AndRaise(exception) |
| 222 | mock_ssh.sendline(sut.END_CMD) |
| 223 | mock_ssh.sendline(sut.EXIT_CMD) |
| 224 | mock_ssh.close(force=True) |
Fang Deng | 71c4b1f | 2013-05-20 09:55:04 -0700 | [diff] [blame] | 225 | self.mox.ReplayAll() |
Abe Smith | 2b48c2a | 2019-01-28 18:18:06 -0500 | [diff] [blame] | 226 | self.assertFalse(sut.set_power_state(self.POWERUNIT_INFO, mock_ssh)) |
Fang Deng | 71c4b1f | 2013-05-20 09:55:04 -0700 | [diff] [blame] | 227 | self.mox.VerifyAll() |
| 228 | |
Fang Deng | 71c4b1f | 2013-05-20 09:55:04 -0700 | [diff] [blame] | 229 | def testUnableToExitConfigurationTerminal(self): |
| 230 | """Should return False if unable to exit configuration terminal.""" |
Fang Deng | 71c4b1f | 2013-05-20 09:55:04 -0700 | [diff] [blame] | 231 | exception = pexpect.TIMEOUT('Could not exit configuration terminal.') |
Abe Smith | 2b48c2a | 2019-01-28 18:18:06 -0500 | [diff] [blame] | 232 | sut = rpm_controller.CiscoPOEController(self.SWITCH) |
| 233 | mock_ssh = self.mox.CreateMockAnything() |
| 234 | self.mox.StubOutWithMock(sut, '_login') |
| 235 | self.mox.StubOutWithMock(sut, '_enter_configuration_terminal') |
| 236 | sut._login().AndReturn(mock_ssh) |
| 237 | sut._enter_configuration_terminal(self.PORT, mock_ssh).AndReturn(True) |
| 238 | mock_ssh.sendline(sut.SET_STATE_ON) |
| 239 | mock_ssh.sendline(sut.END_CMD) |
| 240 | mock_ssh.expect( |
| 241 | self.DEVICE, timeout=sut.CMD_TIMEOUT).AndRaise(exception) |
| 242 | mock_ssh.sendline(sut.EXIT_CMD) |
| 243 | mock_ssh.close(force=True) |
Fang Deng | 71c4b1f | 2013-05-20 09:55:04 -0700 | [diff] [blame] | 244 | self.mox.ReplayAll() |
Abe Smith | 2b48c2a | 2019-01-28 18:18:06 -0500 | [diff] [blame] | 245 | self.assertFalse(sut.set_power_state(self.POWERUNIT_INFO, 'ON')) |
Fang Deng | 71c4b1f | 2013-05-20 09:55:04 -0700 | [diff] [blame] | 246 | self.mox.VerifyAll() |
| 247 | |
Fang Deng | 71c4b1f | 2013-05-20 09:55:04 -0700 | [diff] [blame] | 248 | def testUnableToVerifyState(self): |
| 249 | """Should return False if unable to verify current state.""" |
Abe Smith | 2b48c2a | 2019-01-28 18:18:06 -0500 | [diff] [blame] | 250 | sut = rpm_controller.CiscoPOEController(self.SWITCH) |
| 251 | mock_ssh = self.mox.CreateMockAnything() |
| 252 | self.mox.StubOutWithMock(sut, '_login') |
| 253 | self.mox.StubOutWithMock(sut, '_enter_configuration_terminal') |
| 254 | self.mox.StubOutWithMock(sut, '_exit_configuration_terminal') |
| 255 | sut._login().AndReturn(mock_ssh) |
| 256 | sut._enter_configuration_terminal(self.PORT, mock_ssh).AndReturn(True) |
| 257 | sut._exit_configuration_terminal(mock_ssh).AndReturn(True) |
| 258 | mock_ssh.sendline(sut.SET_STATE_ON) |
| 259 | mock_ssh.sendline(sut.CHECK_INTERFACE_STATE % self.PORT) |
Fang Deng | 71c4b1f | 2013-05-20 09:55:04 -0700 | [diff] [blame] | 260 | exception = pexpect.TIMEOUT('Could not verify state.') |
Abe Smith | 2b48c2a | 2019-01-28 18:18:06 -0500 | [diff] [blame] | 261 | matcher = self.MATCHER % (self.PORT, self.DEVICE) |
| 262 | mock_ssh.expect(matcher, timeout=sut.CMD_TIMEOUT).AndRaise(exception) |
| 263 | mock_ssh.sendline(sut.EXIT_CMD) |
| 264 | mock_ssh.close(force=True) |
Fang Deng | 71c4b1f | 2013-05-20 09:55:04 -0700 | [diff] [blame] | 265 | self.mox.ReplayAll() |
Abe Smith | 2b48c2a | 2019-01-28 18:18:06 -0500 | [diff] [blame] | 266 | self.assertFalse(sut.set_power_state(self.POWERUNIT_INFO, 'ON')) |
Fang Deng | 71c4b1f | 2013-05-20 09:55:04 -0700 | [diff] [blame] | 267 | self.mox.VerifyAll() |
| 268 | |
| 269 | |
Simran Basi | e644932 | 2012-06-15 15:03:49 -0700 | [diff] [blame] | 270 | if __name__ == "__main__": |
| 271 | unittest.main() |