Search in sources :

Example 1 with UpdateHostPasswordCommand

use of com.cloud.legacymodel.communication.command.UpdateHostPasswordCommand in project cosmic by MissionCriticalCloud.

the class LibvirtComputingResourceTest method testUpdateHostPasswordCommand.

@Test
public void testUpdateHostPasswordCommand() {
    final LibvirtUtilitiesHelper libvirtUtilitiesHelper = Mockito.mock(LibvirtUtilitiesHelper.class);
    final Script script = Mockito.mock(Script.class);
    final String hostIp = "127.0.0.1";
    final String username = "root";
    final String newPassword = "password";
    final UpdateHostPasswordCommand command = new UpdateHostPasswordCommand(username, newPassword, hostIp);
    when(this.libvirtComputingResource.getLibvirtUtilitiesHelper()).thenReturn(libvirtUtilitiesHelper);
    when(this.libvirtComputingResource.getUpdateHostPasswdPath()).thenReturn("/tmp");
    when(libvirtUtilitiesHelper.buildScript(this.libvirtComputingResource.getUpdateHostPasswdPath())).thenReturn(script);
    when(script.execute()).thenReturn(null);
    final LibvirtRequestWrapper wrapper = LibvirtRequestWrapper.getInstance();
    assertNotNull(wrapper);
    final Answer answer = wrapper.execute(command, this.libvirtComputingResource);
    assertTrue(answer.getResult());
}
Also used : Script(com.cloud.utils.script.Script) Answer(com.cloud.legacymodel.communication.answer.Answer) CheckRouterAnswer(com.cloud.legacymodel.communication.answer.CheckRouterAnswer) AttachAnswer(com.cloud.legacymodel.communication.answer.AttachAnswer) LibvirtRequestWrapper(com.cloud.agent.resource.kvm.wrapper.LibvirtRequestWrapper) UpdateHostPasswordCommand(com.cloud.legacymodel.communication.command.UpdateHostPasswordCommand) LibvirtUtilitiesHelper(com.cloud.agent.resource.kvm.wrapper.LibvirtUtilitiesHelper) Test(org.junit.Test)

Example 2 with UpdateHostPasswordCommand

use of com.cloud.legacymodel.communication.command.UpdateHostPasswordCommand in project cosmic by MissionCriticalCloud.

the class NotAValidCommand method testUpdateHostPasswordCommandFail.

@Test
public void testUpdateHostPasswordCommandFail() {
    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(this.citrixResourceBase.getPwdFromQueue()).thenReturn("password");
    final String hostIp = updatePwd.getHostIp();
    final String username = updatePwd.getUsername();
    final String hostPasswd = this.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(this.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(false);
        when(result.second()).thenReturn("");
    } catch (final Exception e) {
        fail(e.getMessage());
    }
    final CitrixRequestWrapper wrapper = CitrixRequestWrapper.getInstance();
    assertNotNull(wrapper);
    final Answer answer = wrapper.execute(updatePwd, this.citrixResourceBase);
    verify(this.citrixResourceBase, times(2)).getPwdFromQueue();
    verify(this.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();
    assertFalse(answer.getResult());
}
Also used : RebootAnswer(com.cloud.legacymodel.communication.answer.RebootAnswer) Answer(com.cloud.legacymodel.communication.answer.Answer) CreateAnswer(com.cloud.legacymodel.communication.answer.CreateAnswer) AttachAnswer(com.cloud.legacymodel.communication.answer.AttachAnswer) UpdateHostPasswordCommand(com.cloud.legacymodel.communication.command.UpdateHostPasswordCommand) XenAPIException(com.xensource.xenapi.Types.XenAPIException) XmlRpcException(org.apache.xmlrpc.XmlRpcException) Test(org.junit.Test) PrepareForTest(org.powermock.core.classloader.annotations.PrepareForTest)

Example 3 with UpdateHostPasswordCommand

use of com.cloud.legacymodel.communication.command.UpdateHostPasswordCommand in project cosmic by MissionCriticalCloud.

the class LibvirtComputingResourceTest method testUpdateHostPasswordCommandFail.

@Test
public void testUpdateHostPasswordCommandFail() {
    final LibvirtUtilitiesHelper libvirtUtilitiesHelper = Mockito.mock(LibvirtUtilitiesHelper.class);
    final Script script = Mockito.mock(Script.class);
    final String hostIp = "127.0.0.1";
    final String username = "root";
    final String newPassword = "password";
    final UpdateHostPasswordCommand command = new UpdateHostPasswordCommand(username, newPassword, hostIp);
    when(this.libvirtComputingResource.getLibvirtUtilitiesHelper()).thenReturn(libvirtUtilitiesHelper);
    when(this.libvirtComputingResource.getUpdateHostPasswdPath()).thenReturn("/tmp");
    when(libvirtUtilitiesHelper.buildScript(this.libvirtComputingResource.getUpdateHostPasswdPath())).thenReturn(script);
    when(script.execute()).thenReturn("#FAIL");
    final LibvirtRequestWrapper wrapper = LibvirtRequestWrapper.getInstance();
    assertNotNull(wrapper);
    final Answer answer = wrapper.execute(command, this.libvirtComputingResource);
    assertFalse(answer.getResult());
}
Also used : Script(com.cloud.utils.script.Script) Answer(com.cloud.legacymodel.communication.answer.Answer) CheckRouterAnswer(com.cloud.legacymodel.communication.answer.CheckRouterAnswer) AttachAnswer(com.cloud.legacymodel.communication.answer.AttachAnswer) LibvirtRequestWrapper(com.cloud.agent.resource.kvm.wrapper.LibvirtRequestWrapper) UpdateHostPasswordCommand(com.cloud.legacymodel.communication.command.UpdateHostPasswordCommand) LibvirtUtilitiesHelper(com.cloud.agent.resource.kvm.wrapper.LibvirtUtilitiesHelper) Test(org.junit.Test)

Example 4 with UpdateHostPasswordCommand

use of com.cloud.legacymodel.communication.command.UpdateHostPasswordCommand in project cosmic by MissionCriticalCloud.

the class RequestTest method testSerDeser.

@Test
@Ignore
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");
    final UpdateHostPasswordCommand cmd1 = new UpdateHostPasswordCommand("abc", "def");
    s_logger.info("SecStorageFirewallCfgCommand has a context map that shouldn't show up in debug level");
    final SecStorageFirewallCfgCommand cmd2 = new SecStorageFirewallCfgCommand();
    s_logger.info("GetHostStatsCommand should not show up at all in debug level");
    final GetHostStatsCommand cmd3 = new GetHostStatsCommand("hostguid", "hostname", 101);
    cmd2.addPortConfig("abc", "24", true, "eth0");
    cmd2.addPortConfig("127.0.0.1", "44", false, "eth1");
    final Request sreq = new Request(2, 3, new Command[] { cmd1, cmd2, cmd3 }, true, true);
    sreq.setSequence(892403717);
    final Logger logger = LoggerFactory.getLogger(GsonHelper.class);
    // logger.setLevel(Level.DEBUG);
    String log = sreq.log("Debug", true);
    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);
    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);
    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 (final ClassNotFoundException e) {
        s_logger.error("Unable to parse bytes: ", e);
    } catch (final UnsupportedVersionException e) {
        s_logger.error("Unable to parse bytes: ", e);
    }
    assert creq != null : "Couldn't get the request back";
    compareRequest(creq, sreq);
    final Answer ans = new Answer(cmd1, true, "No Problem");
    final Response cresp = new Response(creq, ans);
    bytes = cresp.getBytes();
    Response sresp = null;
    try {
        sresp = Response.parse(bytes);
    } catch (final ClassNotFoundException e) {
        s_logger.error("Unable to parse bytes: ", e);
    } catch (final UnsupportedVersionException e) {
        s_logger.error("Unable to parse bytes: ", e);
    }
    assert sresp != null : "Couldn't get the response back";
    compareRequest(cresp, sresp);
}
Also used : Answer(com.cloud.legacymodel.communication.answer.Answer) UpdateHostPasswordCommand(com.cloud.legacymodel.communication.command.UpdateHostPasswordCommand) SecStorageFirewallCfgCommand(com.cloud.legacymodel.communication.command.SecStorageFirewallCfgCommand) Logger(org.slf4j.Logger) GetHostStatsCommand(com.cloud.legacymodel.communication.command.GetHostStatsCommand) UnsupportedVersionException(com.cloud.legacymodel.exceptions.UnsupportedVersionException) Ignore(org.junit.Ignore) Test(org.junit.Test)

Example 5 with UpdateHostPasswordCommand

use of com.cloud.legacymodel.communication.command.UpdateHostPasswordCommand in project cosmic by MissionCriticalCloud.

the class ResourceManagerImpl method doUpdateHostPassword.

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

Aggregations

Answer (com.cloud.legacymodel.communication.answer.Answer)7 UpdateHostPasswordCommand (com.cloud.legacymodel.communication.command.UpdateHostPasswordCommand)7 Test (org.junit.Test)6 AttachAnswer (com.cloud.legacymodel.communication.answer.AttachAnswer)5 CreateAnswer (com.cloud.legacymodel.communication.answer.CreateAnswer)3 RebootAnswer (com.cloud.legacymodel.communication.answer.RebootAnswer)3 XenAPIException (com.xensource.xenapi.Types.XenAPIException)3 XmlRpcException (org.apache.xmlrpc.XmlRpcException)3 PrepareForTest (org.powermock.core.classloader.annotations.PrepareForTest)3 LibvirtRequestWrapper (com.cloud.agent.resource.kvm.wrapper.LibvirtRequestWrapper)2 LibvirtUtilitiesHelper (com.cloud.agent.resource.kvm.wrapper.LibvirtUtilitiesHelper)2 CheckRouterAnswer (com.cloud.legacymodel.communication.answer.CheckRouterAnswer)2 Script (com.cloud.utils.script.Script)2 DetailVO (com.cloud.host.DetailVO)1 HostVO (com.cloud.host.HostVO)1 GetGPUStatsAnswer (com.cloud.legacymodel.communication.answer.GetGPUStatsAnswer)1 GetHostStatsAnswer (com.cloud.legacymodel.communication.answer.GetHostStatsAnswer)1 MaintainAnswer (com.cloud.legacymodel.communication.answer.MaintainAnswer)1 UnsupportedAnswer (com.cloud.legacymodel.communication.answer.UnsupportedAnswer)1 GetHostStatsCommand (com.cloud.legacymodel.communication.command.GetHostStatsCommand)1