Search in sources :

Example 6 with UpdateHostPasswordCommand

use of com.cloud.agent.api.UpdateHostPasswordCommand in project cosmic by MissionCriticalCloud.

the class NotAValidCommand method testUpdateHostPasswordCommand.

@Test
public void testUpdateHostPasswordCommand() {
    final XenServerUtilitiesHelper xenServerUtilitiesHelper = Mockito.mock(XenServerUtilitiesHelper.class);
    final Pair<Boolean, String> result = Mockito.mock(Pair.class);
    final UpdateHostPasswordCommand updatePwd = new UpdateHostPasswordCommand("test", "123", "127.0.0.1");
    when(citrixResourceBase.getPwdFromQueue()).thenReturn("password");
    final String hostIp = updatePwd.getHostIp();
    final String username = updatePwd.getUsername();
    final String hostPasswd = citrixResourceBase.getPwdFromQueue();
    final String newPassword = updatePwd.getNewPassword();
    final StringBuilder cmdLine = new StringBuilder();
    cmdLine.append(XenServerUtilitiesHelper.SCRIPT_CMD_PATH).append(VRScripts.UPDATE_HOST_PASSWD).append(' ').append(username).append(' ').append(newPassword);
    when(citrixResourceBase.getXenServerUtilitiesHelper()).thenReturn(xenServerUtilitiesHelper);
    when(xenServerUtilitiesHelper.buildCommandLine(XenServerUtilitiesHelper.SCRIPT_CMD_PATH, VRScripts.UPDATE_HOST_PASSWD, username, newPassword)).thenReturn(cmdLine.toString());
    try {
        when(xenServerUtilitiesHelper.executeSshWrapper(hostIp, 22, username, null, hostPasswd, cmdLine.toString())).thenReturn(result);
        when(result.first()).thenReturn(true);
        when(result.second()).thenReturn("");
    } catch (final Exception e) {
        fail(e.getMessage());
    }
    final CitrixRequestWrapper wrapper = CitrixRequestWrapper.getInstance();
    assertNotNull(wrapper);
    final Answer answer = wrapper.execute(updatePwd, citrixResourceBase);
    verify(citrixResourceBase, times(2)).getPwdFromQueue();
    verify(citrixResourceBase, times(1)).getXenServerUtilitiesHelper();
    verify(xenServerUtilitiesHelper, times(1)).buildCommandLine(XenServerUtilitiesHelper.SCRIPT_CMD_PATH, VRScripts.UPDATE_HOST_PASSWD, username, newPassword);
    try {
        verify(xenServerUtilitiesHelper, times(1)).executeSshWrapper(hostIp, 22, username, null, hostPasswd, cmdLine.toString());
    } catch (final Exception e) {
        fail(e.getMessage());
    }
    verify(result, times(1)).first();
    verify(result, times(1)).second();
    assertTrue(answer.getResult());
}
Also used : RebootAnswer(com.cloud.agent.api.RebootAnswer) CreateAnswer(com.cloud.agent.api.storage.CreateAnswer) Answer(com.cloud.agent.api.Answer) AttachAnswer(com.cloud.storage.command.AttachAnswer) UpdateHostPasswordCommand(com.cloud.agent.api.UpdateHostPasswordCommand) XenAPIException(com.xensource.xenapi.Types.XenAPIException) XmlRpcException(org.apache.xmlrpc.XmlRpcException) Test(org.junit.Test) PrepareForTest(org.powermock.core.classloader.annotations.PrepareForTest)

Example 7 with UpdateHostPasswordCommand

use of com.cloud.agent.api.UpdateHostPasswordCommand in project cosmic by MissionCriticalCloud.

the class NotAValidCommand method testUpdateHostPasswordCommandException.

@Test
public void testUpdateHostPasswordCommandException() {
    final XenServerUtilitiesHelper xenServerUtilitiesHelper = Mockito.mock(XenServerUtilitiesHelper.class);
    final UpdateHostPasswordCommand updatePwd = new UpdateHostPasswordCommand("test", "123", "127.0.0.1");
    when(citrixResourceBase.getPwdFromQueue()).thenReturn("password");
    final String hostIp = updatePwd.getHostIp();
    final String username = updatePwd.getUsername();
    final String hostPasswd = citrixResourceBase.getPwdFromQueue();
    final String newPassword = updatePwd.getNewPassword();
    final StringBuilder cmdLine = new StringBuilder();
    cmdLine.append(XenServerUtilitiesHelper.SCRIPT_CMD_PATH).append(VRScripts.UPDATE_HOST_PASSWD).append(' ').append(username).append(' ').append(newPassword);
    when(citrixResourceBase.getXenServerUtilitiesHelper()).thenReturn(xenServerUtilitiesHelper);
    when(xenServerUtilitiesHelper.buildCommandLine(XenServerUtilitiesHelper.SCRIPT_CMD_PATH, VRScripts.UPDATE_HOST_PASSWD, username, newPassword)).thenReturn(cmdLine.toString());
    try {
        when(xenServerUtilitiesHelper.executeSshWrapper(hostIp, 22, username, null, hostPasswd, cmdLine.toString())).thenThrow(new Exception("testing failure"));
    } catch (final Exception e) {
        fail(e.getMessage());
    }
    final CitrixRequestWrapper wrapper = CitrixRequestWrapper.getInstance();
    assertNotNull(wrapper);
    final Answer answer = wrapper.execute(updatePwd, citrixResourceBase);
    verify(citrixResourceBase, times(2)).getPwdFromQueue();
    verify(citrixResourceBase, times(1)).getXenServerUtilitiesHelper();
    verify(xenServerUtilitiesHelper, times(1)).buildCommandLine(XenServerUtilitiesHelper.SCRIPT_CMD_PATH, VRScripts.UPDATE_HOST_PASSWD, username, newPassword);
    try {
        verify(xenServerUtilitiesHelper, times(1)).executeSshWrapper(hostIp, 22, username, null, hostPasswd, cmdLine.toString());
    } catch (final Exception e) {
        fail(e.getMessage());
    }
    assertFalse(answer.getResult());
}
Also used : RebootAnswer(com.cloud.agent.api.RebootAnswer) CreateAnswer(com.cloud.agent.api.storage.CreateAnswer) Answer(com.cloud.agent.api.Answer) AttachAnswer(com.cloud.storage.command.AttachAnswer) UpdateHostPasswordCommand(com.cloud.agent.api.UpdateHostPasswordCommand) XenAPIException(com.xensource.xenapi.Types.XenAPIException) XmlRpcException(org.apache.xmlrpc.XmlRpcException) Test(org.junit.Test) PrepareForTest(org.powermock.core.classloader.annotations.PrepareForTest)

Example 8 with UpdateHostPasswordCommand

use of com.cloud.agent.api.UpdateHostPasswordCommand in project cloudstack by apache.

the class RequestTest method testSerDeser.

public void testSerDeser() {
    s_logger.info("Testing serializing and deserializing works as expected");
    s_logger.info("UpdateHostPasswordCommand should have two parameters that doesn't show in logging");
    UpdateHostPasswordCommand cmd1 = new UpdateHostPasswordCommand("abc", "def");
    s_logger.info("SecStorageFirewallCfgCommand has a context map that shouldn't show up in debug level");
    SecStorageFirewallCfgCommand cmd2 = new SecStorageFirewallCfgCommand();
    s_logger.info("GetHostStatsCommand should not show up at all in debug level");
    GetHostStatsCommand cmd3 = new GetHostStatsCommand("hostguid", "hostname", 101);
    cmd2.addPortConfig("abc", "24", true, "eth0");
    cmd2.addPortConfig("127.0.0.1", "44", false, "eth1");
    Request sreq = new Request(2, 3, new Command[] { cmd1, cmd2, cmd3 }, true, true);
    sreq.setSequence(892403717);
    Logger logger = Logger.getLogger(GsonHelper.class);
    Level level = logger.getLevel();
    logger.setLevel(Level.DEBUG);
    String log = sreq.log("Debug", true, Level.DEBUG);
    assert (log.contains(UpdateHostPasswordCommand.class.getSimpleName()));
    assert (log.contains(SecStorageFirewallCfgCommand.class.getSimpleName()));
    assert (!log.contains(GetHostStatsCommand.class.getSimpleName()));
    assert (!log.contains("username"));
    assert (!log.contains("password"));
    logger.setLevel(Level.TRACE);
    log = sreq.log("Trace", true, Level.TRACE);
    assert (log.contains(UpdateHostPasswordCommand.class.getSimpleName()));
    assert (log.contains(SecStorageFirewallCfgCommand.class.getSimpleName()));
    assert (log.contains(GetHostStatsCommand.class.getSimpleName()));
    assert (!log.contains("username"));
    assert (!log.contains("password"));
    logger.setLevel(Level.INFO);
    log = sreq.log("Info", true, Level.INFO);
    assert (log == null);
    logger.setLevel(level);
    byte[] bytes = sreq.getBytes();
    assert Request.getSequence(bytes) == 892403717;
    assert Request.getManagementServerId(bytes) == 3;
    assert Request.getAgentId(bytes) == 2;
    assert Request.getViaAgentId(bytes) == 2;
    Request creq = null;
    try {
        creq = Request.parse(bytes);
    } catch (ClassNotFoundException e) {
        s_logger.error("Unable to parse bytes: ", e);
    } catch (UnsupportedVersionException e) {
        s_logger.error("Unable to parse bytes: ", e);
    }
    assert creq != null : "Couldn't get the request back";
    compareRequest(creq, sreq);
    Answer ans = new Answer(cmd1, true, "No Problem");
    Response cresp = new Response(creq, ans);
    bytes = cresp.getBytes();
    Response sresp = null;
    try {
        sresp = Response.parse(bytes);
    } catch (ClassNotFoundException e) {
        s_logger.error("Unable to parse bytes: ", e);
    } catch (UnsupportedVersionException e) {
        s_logger.error("Unable to parse bytes: ", e);
    }
    assert sresp != null : "Couldn't get the response back";
    compareRequest(cresp, sresp);
}
Also used : DownloadAnswer(com.cloud.agent.api.storage.DownloadAnswer) Answer(com.cloud.agent.api.Answer) UpdateHostPasswordCommand(com.cloud.agent.api.UpdateHostPasswordCommand) SecStorageFirewallCfgCommand(com.cloud.agent.api.SecStorageFirewallCfgCommand) Level(org.apache.log4j.Level) Logger(org.apache.log4j.Logger) GetHostStatsCommand(com.cloud.agent.api.GetHostStatsCommand) UnsupportedVersionException(com.cloud.exception.UnsupportedVersionException)

Example 9 with UpdateHostPasswordCommand

use of com.cloud.agent.api.UpdateHostPasswordCommand in project cloudstack by apache.

the class ResourceManagerImpl method doUpdateHostPassword.

private boolean doUpdateHostPassword(final long hostId) {
    if (!_agentMgr.isAgentAttached(hostId)) {
        return false;
    }
    DetailVO nv = _hostDetailsDao.findDetail(hostId, ApiConstants.USERNAME);
    final String username = nv.getValue();
    nv = _hostDetailsDao.findDetail(hostId, ApiConstants.PASSWORD);
    final String password = nv.getValue();
    final HostVO host = _hostDao.findById(hostId);
    final String hostIpAddress = host.getPrivateIpAddress();
    final UpdateHostPasswordCommand cmd = new UpdateHostPasswordCommand(username, password, hostIpAddress);
    final Answer answer = _agentMgr.easySend(hostId, cmd);
    s_logger.info("Result returned from update host password ==> " + answer.getDetails());
    return answer.getResult();
}
Also used : UnsupportedAnswer(com.cloud.agent.api.UnsupportedAnswer) GetGPUStatsAnswer(com.cloud.agent.api.GetGPUStatsAnswer) MaintainAnswer(com.cloud.agent.api.MaintainAnswer) GetHostStatsAnswer(com.cloud.agent.api.GetHostStatsAnswer) GetVncPortAnswer(com.cloud.agent.api.GetVncPortAnswer) Answer(com.cloud.agent.api.Answer) DetailVO(com.cloud.host.DetailVO) UpdateHostPasswordCommand(com.cloud.agent.api.UpdateHostPasswordCommand) StoragePoolHostVO(com.cloud.storage.StoragePoolHostVO) HostVO(com.cloud.host.HostVO)

Example 10 with UpdateHostPasswordCommand

use of com.cloud.agent.api.UpdateHostPasswordCommand in project cloudstack by apache.

the class NotAValidCommand method testUpdateHostPasswordCommand.

@SuppressWarnings("unchecked")
@Test
public void testUpdateHostPasswordCommand() {
    final XenServerUtilitiesHelper xenServerUtilitiesHelper = Mockito.mock(XenServerUtilitiesHelper.class);
    final Pair<Boolean, String> result = Mockito.mock(Pair.class);
    final UpdateHostPasswordCommand updatePwd = new UpdateHostPasswordCommand("test", "123", "127.0.0.1");
    when(citrixResourceBase.getPwdFromQueue()).thenReturn("password");
    final String hostIp = updatePwd.getHostIp();
    final String username = updatePwd.getUsername();
    final String hostPasswd = citrixResourceBase.getPwdFromQueue();
    final String newPassword = updatePwd.getNewPassword();
    final StringBuilder cmdLine = new StringBuilder();
    cmdLine.append(XenServerUtilitiesHelper.SCRIPT_CMD_PATH).append(VRScripts.UPDATE_HOST_PASSWD).append(' ').append(username).append(' ').append(newPassword);
    when(citrixResourceBase.getXenServerUtilitiesHelper()).thenReturn(xenServerUtilitiesHelper);
    when(xenServerUtilitiesHelper.buildCommandLine(XenServerUtilitiesHelper.SCRIPT_CMD_PATH, VRScripts.UPDATE_HOST_PASSWD, username, newPassword)).thenReturn(cmdLine.toString());
    try {
        when(xenServerUtilitiesHelper.executeSshWrapper(hostIp, 22, username, null, hostPasswd, cmdLine.toString())).thenReturn(result);
        when(result.first()).thenReturn(true);
        when(result.second()).thenReturn("");
    } catch (final Exception e) {
        fail(e.getMessage());
    }
    final CitrixRequestWrapper wrapper = CitrixRequestWrapper.getInstance();
    assertNotNull(wrapper);
    final Answer answer = wrapper.execute(updatePwd, citrixResourceBase);
    verify(citrixResourceBase, times(2)).getPwdFromQueue();
    verify(citrixResourceBase, times(1)).getXenServerUtilitiesHelper();
    verify(xenServerUtilitiesHelper, times(1)).buildCommandLine(XenServerUtilitiesHelper.SCRIPT_CMD_PATH, VRScripts.UPDATE_HOST_PASSWD, username, newPassword);
    try {
        verify(xenServerUtilitiesHelper, times(1)).executeSshWrapper(hostIp, 22, username, null, hostPasswd, cmdLine.toString());
    } catch (final Exception e) {
        fail(e.getMessage());
    }
    verify(result, times(1)).first();
    verify(result, times(1)).second();
    assertTrue(answer.getResult());
}
Also used : RebootAnswer(com.cloud.agent.api.RebootAnswer) CreateAnswer(com.cloud.agent.api.storage.CreateAnswer) AttachAnswer(org.apache.cloudstack.storage.command.AttachAnswer) Answer(com.cloud.agent.api.Answer) UpdateHostPasswordCommand(com.cloud.agent.api.UpdateHostPasswordCommand) XenAPIException(com.xensource.xenapi.Types.XenAPIException) XmlRpcException(org.apache.xmlrpc.XmlRpcException) Test(org.junit.Test) PrepareForTest(org.powermock.core.classloader.annotations.PrepareForTest)

Aggregations

Answer (com.cloud.agent.api.Answer)14 UpdateHostPasswordCommand (com.cloud.agent.api.UpdateHostPasswordCommand)14 Test (org.junit.Test)11 PrepareForTest (org.powermock.core.classloader.annotations.PrepareForTest)8 RebootAnswer (com.cloud.agent.api.RebootAnswer)6 CreateAnswer (com.cloud.agent.api.storage.CreateAnswer)6 XenAPIException (com.xensource.xenapi.Types.XenAPIException)6 XmlRpcException (org.apache.xmlrpc.XmlRpcException)6 AttachAnswer (com.cloud.storage.command.AttachAnswer)5 AttachAnswer (org.apache.cloudstack.storage.command.AttachAnswer)5 CheckRouterAnswer (com.cloud.agent.api.CheckRouterAnswer)4 UnsupportedAnswer (com.cloud.agent.api.UnsupportedAnswer)4 LibvirtRequestWrapper (com.cloud.hypervisor.kvm.resource.wrapper.LibvirtRequestWrapper)4 LibvirtUtilitiesHelper (com.cloud.hypervisor.kvm.resource.wrapper.LibvirtUtilitiesHelper)4 Script (com.cloud.utils.script.Script)4 GetGPUStatsAnswer (com.cloud.agent.api.GetGPUStatsAnswer)2 GetHostStatsAnswer (com.cloud.agent.api.GetHostStatsAnswer)2 GetHostStatsCommand (com.cloud.agent.api.GetHostStatsCommand)2 MaintainAnswer (com.cloud.agent.api.MaintainAnswer)2 SecStorageFirewallCfgCommand (com.cloud.agent.api.SecStorageFirewallCfgCommand)2