def testLogin(self): """Test we can log into the switch.""" rpm_controller.pexpect.spawn = self.mox.CreateMockAnything() mock_ssh = self.mox.CreateMockAnything() rpm_controller.pexpect.spawn(mox.IgnoreArg()).AndReturn(mock_ssh) sut = rpm_controller.CiscoPOEController(self.SWITCH) mock_ssh.expect(sut.POE_USERNAME_PROMPT, timeout=sut.LOGIN_TIMEOUT) mock_ssh.sendline(mox.IgnoreArg()) mock_ssh.expect(self.PWD, timeout=sut.LOGIN_TIMEOUT) mock_ssh.sendline(mox.IgnoreArg()) mock_ssh.expect(self.DEVICE, timeout=sut.LOGIN_TIMEOUT) self.mox.ReplayAll() self.assertIsNotNone(sut._login()) self.mox.VerifyAll()
def setUp(self): super(TestCiscoPOEController, self).setUp() self.mox.StubOutWithMock(pexpect.spawn, '_spawn') self.mox.StubOutWithMock(pexpect.spawn, 'read_nonblocking') self.mox.StubOutWithMock(pexpect.spawn, 'sendline') self.poe = rpm_controller.CiscoPOEController(self.SWITCH) pexpect.spawn._spawn(mox.IgnoreArg(), mox.IgnoreArg()) pexpect.spawn.read_nonblocking( mox.IgnoreArg(), mox.IgnoreArg()).AndReturn(self.STREAM_WELCOME) pexpect.spawn.sendline(self.poe._username) pexpect.spawn.read_nonblocking( mox.IgnoreArg(), mox.IgnoreArg()).AndReturn(self.STREAM_PWD) pexpect.spawn.sendline(self.poe._password) pexpect.spawn.read_nonblocking( mox.IgnoreArg(), mox.IgnoreArg()).AndReturn(self.STREAM_DEVICE)
def testUnableToExitConfigurationTerminal(self): """Should return False if unable to exit configuration terminal.""" exception = pexpect.TIMEOUT('Could not exit configuration terminal.') sut = rpm_controller.CiscoPOEController(self.SWITCH) mock_ssh = self.mox.CreateMockAnything() self.mox.StubOutWithMock(sut, '_login') self.mox.StubOutWithMock(sut, '_enter_configuration_terminal') sut._login().AndReturn(mock_ssh) sut._enter_configuration_terminal(self.PORT, mock_ssh).AndReturn(True) mock_ssh.sendline(sut.SET_STATE_ON) mock_ssh.sendline(sut.END_CMD) mock_ssh.expect(self.DEVICE, timeout=sut.CMD_TIMEOUT).AndRaise(exception) mock_ssh.sendline(sut.EXIT_CMD) mock_ssh.close(force=True) self.mox.ReplayAll() self.assertFalse(sut.set_power_state(self.POWERUNIT_INFO, 'ON')) self.mox.VerifyAll()
def testUnableToEnterConfigurationTerminal(self): """Should return False if unable to enter configuration terminal.""" exception = pexpect.TIMEOUT('Could not enter configuration terminal.') sut = rpm_controller.CiscoPOEController(self.SWITCH) timeout = sut.CMD_TIMEOUT mock_ssh = self.mox.CreateMockAnything() self.mox.StubOutWithMock(sut, '_login') sut._login().AndReturn(mock_ssh) mock_ssh.sendline(sut.CONFIG) mock_ssh.expect(sut.config_prompt, timeout=timeout) mock_ssh.sendline(sut.CONFIG_IF % self.PORT) config_if_prompt = sut.config_if_prompt mock_ssh.expect(config_if_prompt, timeout=timeout).AndRaise(exception) mock_ssh.sendline(sut.END_CMD) mock_ssh.sendline(sut.EXIT_CMD) mock_ssh.close(force=True) self.mox.ReplayAll() self.assertFalse(sut.set_power_state(self.POWERUNIT_INFO, mock_ssh)) self.mox.VerifyAll()
def testUnableToVerifyState(self): """Should return False if unable to verify current state.""" sut = rpm_controller.CiscoPOEController(self.SWITCH) mock_ssh = self.mox.CreateMockAnything() self.mox.StubOutWithMock(sut, '_login') self.mox.StubOutWithMock(sut, '_enter_configuration_terminal') self.mox.StubOutWithMock(sut, '_exit_configuration_terminal') sut._login().AndReturn(mock_ssh) sut._enter_configuration_terminal(self.PORT, mock_ssh).AndReturn(True) sut._exit_configuration_terminal(mock_ssh).AndReturn(True) mock_ssh.sendline(sut.SET_STATE_ON) mock_ssh.sendline(sut.CHECK_INTERFACE_STATE % self.PORT) exception = pexpect.TIMEOUT('Could not verify state.') matcher = self.MATCHER % (self.PORT, self.DEVICE) mock_ssh.expect(matcher, timeout=sut.CMD_TIMEOUT).AndRaise(exception) mock_ssh.sendline(sut.EXIT_CMD) mock_ssh.close(force=True) self.mox.ReplayAll() self.assertFalse(sut.set_power_state(self.POWERUNIT_INFO, 'ON')) self.mox.VerifyAll()
def _create_rpm_controller(self, rpm_hostname, hydra_hostname): """ Determines the type of RPMController required and initializes it. @param rpm_hostname: Hostname of the RPM we need to communicate with. @return: RPMController instance responsible for this RPM. """ hostname_elements = rpm_hostname.split('-') if hostname_elements[-2] == 'poe': # POE switch hostname looks like 'chromeos2-poe-switch1'. logging.info('The controller is a Cisco POE switch.') return rpm_controller.CiscoPOEController(rpm_hostname) else: # The device is an RPM. rack_id = hostname_elements[-2] rpm_typechecker = re.compile('rack[0-9]+[a-z]+') if rpm_typechecker.match(rack_id): logging.info('RPM is a webpowered device.') return rpm_controller.WebPoweredRPMController(rpm_hostname) else: logging.info('RPM is a Sentry CDU device.') return rpm_controller.SentryRPMController( hostname=rpm_hostname, hydra_hostname=hydra_hostname)
def testSuccessfullyChangePowerState(self): """Should return True if change was successful.""" sut = rpm_controller.CiscoPOEController(self.SWITCH) mock_ssh = self.mox.CreateMockAnything() self.mox.StubOutWithMock(sut, '_login') sut._login().AndReturn(mock_ssh) self.mox.StubOutWithMock(sut, '_verify_state') sut._verify_state(self.PORT, 'ON', mock_ssh).AndReturn(True) # _enter_configuration_terminal mock_ssh.sendline(sut.CONFIG) mock_ssh.expect(sut.config_prompt, timeout=sut.CMD_TIMEOUT) mock_ssh.sendline(sut.CONFIG_IF % self.PORT) mock_ssh.expect(sut.config_if_prompt, timeout=sut.CMD_TIMEOUT) # _change_state mock_ssh.sendline(sut.SET_STATE_ON) # _exit_configuration_terminal mock_ssh.sendline(sut.END_CMD) mock_ssh.expect(sut.poe_prompt, timeout=sut.CMD_TIMEOUT) # exit mock_ssh.sendline(sut.EXIT_CMD) mock_ssh.close(force=True) self.mox.ReplayAll() self.assertTrue(sut.set_power_state(self.POWERUNIT_INFO, 'ON')) self.mox.VerifyAll()