Refactoring the LibvirtComputingResource

- Adding LibvirtGetVncPortCommandWrapper
  - 2 unit tests added
  - KVM hypervisor plugin with 12.1% coverage
This commit is contained in:
wilderrodrigues 2015-04-24 13:17:58 +02:00
parent 7319a12600
commit f14c7c2074
4 changed files with 105 additions and 15 deletions

View File

@ -100,8 +100,6 @@ import com.cloud.agent.api.FenceAnswer;
import com.cloud.agent.api.FenceCommand;
import com.cloud.agent.api.GetStorageStatsAnswer;
import com.cloud.agent.api.GetStorageStatsCommand;
import com.cloud.agent.api.GetVncPortAnswer;
import com.cloud.agent.api.GetVncPortCommand;
import com.cloud.agent.api.HostVmStateReportEntry;
import com.cloud.agent.api.MaintainAnswer;
import com.cloud.agent.api.MaintainCommand;
@ -1300,9 +1298,7 @@ public class LibvirtComputingResource extends ServerResourceBase implements Serv
}
try {
if (cmd instanceof GetVncPortCommand) {
return execute((GetVncPortCommand)cmd);
} else if (cmd instanceof ModifySshKeysCommand) {
if (cmd instanceof ModifySshKeysCommand) {
return execute((ModifySshKeysCommand)cmd);
} else if (cmd instanceof MaintainCommand) {
return execute((MaintainCommand)cmd);
@ -2865,16 +2861,6 @@ public class LibvirtComputingResource extends ServerResourceBase implements Serv
return new Answer(cmd, result, "");
}
protected GetVncPortAnswer execute(final GetVncPortCommand cmd) {
try {
final Connect conn = LibvirtConnection.getConnectionByVmName(cmd.getName());
final Integer vncPort = getVncPort(conn, cmd.getName());
return new GetVncPortAnswer(cmd, _privateIp, 5900 + vncPort);
} catch (final LibvirtException e) {
return new GetVncPortAnswer(cmd, e.toString());
}
}
protected MaintainAnswer execute(final MaintainCommand cmd) {
return new MaintainAnswer(cmd);
}

View File

@ -0,0 +1,45 @@
//
// Licensed to the Apache Software Foundation (ASF) under one
// or more contributor license agreements. See the NOTICE file
// distributed with this work for additional information
// regarding copyright ownership. The ASF licenses this file
// to you under the Apache License, Version 2.0 (the
// "License"); you may not use this file except in compliance
// with the License. You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing,
// software distributed under the License is distributed on an
// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
// KIND, either express or implied. See the License for the
// specific language governing permissions and limitations
// under the License.
//
package com.cloud.hypervisor.kvm.resource.wrapper;
import org.libvirt.Connect;
import org.libvirt.LibvirtException;
import com.cloud.agent.api.Answer;
import com.cloud.agent.api.GetVncPortAnswer;
import com.cloud.agent.api.GetVncPortCommand;
import com.cloud.hypervisor.kvm.resource.LibvirtComputingResource;
import com.cloud.resource.CommandWrapper;
public final class LibvirtGetVncPortCommandWrapper extends CommandWrapper<GetVncPortCommand, Answer, LibvirtComputingResource> {
@Override
public Answer execute(final GetVncPortCommand command, final LibvirtComputingResource libvirtComputingResource) {
try {
final LibvirtConnectionWrapper libvirtConnectionWrapper = libvirtComputingResource.getLibvirtConnectionWrapper();
final Connect conn = libvirtConnectionWrapper.getConnectionByVmName(command.getName());
final Integer vncPort = libvirtComputingResource.getVncPort(conn, command.getName());
return new GetVncPortAnswer(command, libvirtComputingResource.getPrivateIp(), 5900 + vncPort);
} catch (final LibvirtException e) {
return new GetVncPortAnswer(command, e.toString());
}
}
}

View File

@ -29,6 +29,7 @@ import com.cloud.agent.api.Command;
import com.cloud.agent.api.GetHostStatsCommand;
import com.cloud.agent.api.GetVmDiskStatsCommand;
import com.cloud.agent.api.GetVmStatsCommand;
import com.cloud.agent.api.GetVncPortCommand;
import com.cloud.agent.api.MigrateCommand;
import com.cloud.agent.api.PingTestCommand;
import com.cloud.agent.api.PrepareForMigrationCommand;
@ -76,6 +77,7 @@ public class LibvirtRequestWrapper extends RequestWrapper {
linbvirtCommands.put(AttachVolumeCommand.class, new LibvirtAttachVolumeCommandWrapper());
linbvirtCommands.put(WatchConsoleProxyLoadCommand.class, new LibvirtWatchConsoleProxyLoadCommandWrapper());
linbvirtCommands.put(CheckConsoleProxyLoadCommand.class, new LibvirtCheckConsoleProxyLoadCommandWrapper());
linbvirtCommands.put(GetVncPortCommand.class, new LibvirtGetVncPortCommandWrapper());
resources.put(LibvirtComputingResource.class, linbvirtCommands);
}

View File

@ -71,6 +71,7 @@ import com.cloud.agent.api.CheckVirtualMachineCommand;
import com.cloud.agent.api.GetHostStatsCommand;
import com.cloud.agent.api.GetVmDiskStatsCommand;
import com.cloud.agent.api.GetVmStatsCommand;
import com.cloud.agent.api.GetVncPortCommand;
import com.cloud.agent.api.MigrateCommand;
import com.cloud.agent.api.PingTestCommand;
import com.cloud.agent.api.PrepareForMigrationCommand;
@ -1144,4 +1145,60 @@ public class LibvirtComputingResourceTest {
final Answer answer = wrapper.execute(command, libvirtComputingResource);
assertFalse(answer.getResult());
}
@Test
public void testGetVncPortCommand() {
final Connect conn = Mockito.mock(Connect.class);
final LibvirtConnectionWrapper libvirtConnectionWrapper = Mockito.mock(LibvirtConnectionWrapper.class);
final GetVncPortCommand command = new GetVncPortCommand(1l, "host");
when(libvirtComputingResource.getLibvirtConnectionWrapper()).thenReturn(libvirtConnectionWrapper);
try {
when(libvirtConnectionWrapper.getConnectionByVmName(command.getName())).thenReturn(conn);
} catch (final LibvirtException e) {
fail(e.getMessage());
}
final LibvirtRequestWrapper wrapper = LibvirtRequestWrapper.getInstance();
assertNotNull(wrapper);
final Answer answer = wrapper.execute(command, libvirtComputingResource);
assertTrue(answer.getResult());
verify(libvirtComputingResource, times(1)).getLibvirtConnectionWrapper();
try {
verify(libvirtConnectionWrapper, times(1)).getConnectionByVmName(command.getName());
} catch (final LibvirtException e) {
fail(e.getMessage());
}
}
@SuppressWarnings("unchecked")
@Test
public void testGetVncPortCommandLibvirtException() {
final LibvirtConnectionWrapper libvirtConnectionWrapper = Mockito.mock(LibvirtConnectionWrapper.class);
final GetVncPortCommand command = new GetVncPortCommand(1l, "host");
when(libvirtComputingResource.getLibvirtConnectionWrapper()).thenReturn(libvirtConnectionWrapper);
try {
when(libvirtConnectionWrapper.getConnectionByVmName(command.getName())).thenThrow(LibvirtException.class);
} catch (final LibvirtException e) {
fail(e.getMessage());
}
final LibvirtRequestWrapper wrapper = LibvirtRequestWrapper.getInstance();
assertNotNull(wrapper);
final Answer answer = wrapper.execute(command, libvirtComputingResource);
assertFalse(answer.getResult());
verify(libvirtComputingResource, times(1)).getLibvirtConnectionWrapper();
try {
verify(libvirtConnectionWrapper, times(1)).getConnectionByVmName(command.getName());
} catch (final LibvirtException e) {
fail(e.getMessage());
}
}
}