mirror of https://github.com/apache/cloudstack.git
Refactoring the LibvirtComputingResource
- Adding LibvirtCreateVolumeFromSnapshotCommandWrapper, LibvirtFenceCommandWrapper and LibvirtSecurityGroupRulesCommandWrapper - 6 unit tests added - KVM hypervisor plugin with 17.2% coverage
This commit is contained in:
parent
3c8b217262
commit
ff7ae9ca0c
|
|
@ -53,9 +53,9 @@ public class SnapshotCommand extends Command {
|
|||
* is the value of that field If you have better ideas on how to
|
||||
* get it, you are welcome.
|
||||
*/
|
||||
public SnapshotCommand(StoragePool pool, String secondaryStorageUrl, String snapshotUuid, String snapshotName, Long dcId, Long accountId, Long volumeId) {
|
||||
// this.primaryStoragePoolNameLabel = pool.getUuid();
|
||||
//this.primaryPool = new StorageFilerTO(pool);
|
||||
public SnapshotCommand(final StoragePool pool, final String secondaryStorageUrl, final String snapshotUuid, final String snapshotName, final Long dcId, final Long accountId, final Long volumeId) {
|
||||
primaryStoragePoolNameLabel = pool.getUuid();
|
||||
primaryPool = new StorageFilerTO(pool);
|
||||
this.snapshotUuid = snapshotUuid;
|
||||
this.secondaryStorageUrl = secondaryStorageUrl;
|
||||
this.dcId = dcId;
|
||||
|
|
@ -112,7 +112,7 @@ public class SnapshotCommand extends Command {
|
|||
return volumePath;
|
||||
}
|
||||
|
||||
public void setVolumePath(String path) {
|
||||
public void setVolumePath(final String path) {
|
||||
volumePath = path;
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -44,10 +44,6 @@ import java.util.Properties;
|
|||
import java.util.Set;
|
||||
import java.util.UUID;
|
||||
import java.util.concurrent.ConcurrentHashMap;
|
||||
import java.util.concurrent.ExecutionException;
|
||||
import java.util.concurrent.ExecutorService;
|
||||
import java.util.concurrent.Executors;
|
||||
import java.util.concurrent.Future;
|
||||
import java.util.regex.Matcher;
|
||||
import java.util.regex.Pattern;
|
||||
|
||||
|
|
@ -88,10 +84,6 @@ import com.cloud.agent.api.BackupSnapshotCommand;
|
|||
import com.cloud.agent.api.Command;
|
||||
import com.cloud.agent.api.CreatePrivateTemplateFromSnapshotCommand;
|
||||
import com.cloud.agent.api.CreatePrivateTemplateFromVolumeCommand;
|
||||
import com.cloud.agent.api.CreateVolumeFromSnapshotAnswer;
|
||||
import com.cloud.agent.api.CreateVolumeFromSnapshotCommand;
|
||||
import com.cloud.agent.api.FenceAnswer;
|
||||
import com.cloud.agent.api.FenceCommand;
|
||||
import com.cloud.agent.api.HostVmStateReportEntry;
|
||||
import com.cloud.agent.api.ManageSnapshotAnswer;
|
||||
import com.cloud.agent.api.ManageSnapshotCommand;
|
||||
|
|
@ -103,8 +95,6 @@ import com.cloud.agent.api.PingRoutingWithNwGroupsCommand;
|
|||
import com.cloud.agent.api.PlugNicAnswer;
|
||||
import com.cloud.agent.api.PlugNicCommand;
|
||||
import com.cloud.agent.api.PvlanSetupCommand;
|
||||
import com.cloud.agent.api.SecurityGroupRuleAnswer;
|
||||
import com.cloud.agent.api.SecurityGroupRulesCmd;
|
||||
import com.cloud.agent.api.SetupGuestNetworkCommand;
|
||||
import com.cloud.agent.api.StartAnswer;
|
||||
import com.cloud.agent.api.StartCommand;
|
||||
|
|
@ -138,7 +128,6 @@ import com.cloud.dc.Vlan;
|
|||
import com.cloud.exception.InternalErrorException;
|
||||
import com.cloud.host.Host.Type;
|
||||
import com.cloud.hypervisor.Hypervisor.HypervisorType;
|
||||
import com.cloud.hypervisor.kvm.resource.KVMHABase.NfsStoragePool;
|
||||
import com.cloud.hypervisor.kvm.resource.LibvirtVMDef.ClockDef;
|
||||
import com.cloud.hypervisor.kvm.resource.LibvirtVMDef.ConsoleDef;
|
||||
import com.cloud.hypervisor.kvm.resource.LibvirtVMDef.CpuModeDef;
|
||||
|
|
@ -1284,14 +1273,8 @@ public class LibvirtComputingResource extends ServerResourceBase implements Serv
|
|||
return execute((ManageSnapshotCommand)cmd);
|
||||
} else if (cmd instanceof BackupSnapshotCommand) {
|
||||
return execute((BackupSnapshotCommand)cmd);
|
||||
} else if (cmd instanceof CreateVolumeFromSnapshotCommand) {
|
||||
return execute((CreateVolumeFromSnapshotCommand)cmd);
|
||||
} else if (cmd instanceof CreatePrivateTemplateFromSnapshotCommand) {
|
||||
return execute((CreatePrivateTemplateFromSnapshotCommand)cmd);
|
||||
} else if (cmd instanceof SecurityGroupRulesCmd) {
|
||||
return execute((SecurityGroupRulesCmd)cmd);
|
||||
} else if (cmd instanceof FenceCommand) {
|
||||
return execute((FenceCommand)cmd);
|
||||
} else if (cmd instanceof StartCommand) {
|
||||
return execute((StartCommand)cmd);
|
||||
} else if (cmd instanceof PlugNicCommand) {
|
||||
|
|
@ -1450,28 +1433,6 @@ public class LibvirtComputingResource extends ServerResourceBase implements Serv
|
|||
}
|
||||
}
|
||||
|
||||
protected FenceAnswer execute(final FenceCommand cmd) {
|
||||
final ExecutorService executors = Executors.newSingleThreadExecutor();
|
||||
final List<NfsStoragePool> pools = _monitor.getStoragePools();
|
||||
final KVMHAChecker ha = new KVMHAChecker(pools, cmd.getHostIp());
|
||||
final Future<Boolean> future = executors.submit(ha);
|
||||
try {
|
||||
final Boolean result = future.get();
|
||||
if (result) {
|
||||
return new FenceAnswer(cmd, false, "Heart is still beating...");
|
||||
} else {
|
||||
return new FenceAnswer(cmd);
|
||||
}
|
||||
} catch (final InterruptedException e) {
|
||||
s_logger.warn("Unable to fence", e);
|
||||
return new FenceAnswer(cmd, false, e.getMessage());
|
||||
} catch (final ExecutionException e) {
|
||||
s_logger.warn("Unable to fence", e);
|
||||
return new FenceAnswer(cmd, false, e.getMessage());
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
protected Storage.StorageResourceType getStorageResourceType() {
|
||||
return Storage.StorageResourceType.STORAGE_POOL;
|
||||
}
|
||||
|
|
@ -2265,25 +2226,6 @@ public class LibvirtComputingResource extends ServerResourceBase implements Serv
|
|||
return new BackupSnapshotAnswer(cmd, true, null, snapshotRelPath + File.separator + snapshotName, true);
|
||||
}
|
||||
|
||||
protected CreateVolumeFromSnapshotAnswer execute(final CreateVolumeFromSnapshotCommand cmd) {
|
||||
try {
|
||||
|
||||
String snapshotPath = cmd.getSnapshotUuid();
|
||||
final int index = snapshotPath.lastIndexOf("/");
|
||||
snapshotPath = snapshotPath.substring(0, index);
|
||||
final KVMStoragePool secondaryPool = _storagePoolMgr.getStoragePoolByURI(cmd.getSecondaryStorageUrl() + snapshotPath);
|
||||
final KVMPhysicalDisk snapshot = secondaryPool.getPhysicalDisk(cmd.getSnapshotName());
|
||||
|
||||
final String primaryUuid = cmd.getPrimaryStoragePoolNameLabel();
|
||||
final KVMStoragePool primaryPool = _storagePoolMgr.getStoragePool(cmd.getPool().getType(), primaryUuid);
|
||||
final String volUuid = UUID.randomUUID().toString();
|
||||
final KVMPhysicalDisk disk = _storagePoolMgr.copyPhysicalDisk(snapshot, volUuid, primaryPool, 0);
|
||||
return new CreateVolumeFromSnapshotAnswer(cmd, true, "", disk.getName());
|
||||
} catch (final CloudRuntimeException e) {
|
||||
return new CreateVolumeFromSnapshotAnswer(cmd, false, e.toString(), null);
|
||||
}
|
||||
}
|
||||
|
||||
protected CreatePrivateTemplateAnswer execute(final CreatePrivateTemplateFromSnapshotCommand cmd) {
|
||||
final String templateFolder = cmd.getAccountId() + File.separator + cmd.getNewTemplateId();
|
||||
final String templateInstallFolder = "template/tmpl/" + templateFolder;
|
||||
|
|
@ -2450,32 +2392,6 @@ public class LibvirtComputingResource extends ServerResourceBase implements Serv
|
|||
}
|
||||
}
|
||||
|
||||
private Answer execute(final SecurityGroupRulesCmd cmd) {
|
||||
String vif = null;
|
||||
String brname = null;
|
||||
try {
|
||||
final Connect conn = LibvirtConnection.getConnectionByVmName(cmd.getVmName());
|
||||
final List<InterfaceDef> nics = getInterfaces(conn, cmd.getVmName());
|
||||
vif = nics.get(0).getDevName();
|
||||
brname = nics.get(0).getBrName();
|
||||
} catch (final LibvirtException e) {
|
||||
return new SecurityGroupRuleAnswer(cmd, false, e.toString());
|
||||
}
|
||||
|
||||
final boolean result =
|
||||
add_network_rules(cmd.getVmName(), Long.toString(cmd.getVmId()), cmd.getGuestIp(), cmd.getSignature(), Long.toString(cmd.getSeqNum()), cmd.getGuestMac(),
|
||||
cmd.stringifyRules(), vif, brname, cmd.getSecIpsString());
|
||||
|
||||
if (!result) {
|
||||
s_logger.warn("Failed to program network rules for vm " + cmd.getVmName());
|
||||
return new SecurityGroupRuleAnswer(cmd, false, "programming network rules failed");
|
||||
} else {
|
||||
s_logger.debug("Programmed network rules for vm " + cmd.getVmName() + " guestIp=" + cmd.getGuestIp() + ",ingress numrules=" + cmd.getIngressRuleSet().length +
|
||||
",egress numrules=" + cmd.getEgressRuleSet().length);
|
||||
return new SecurityGroupRuleAnswer(cmd);
|
||||
}
|
||||
}
|
||||
|
||||
protected PowerState convertToPowerState(final DomainState ps) {
|
||||
final PowerState state = s_powerStatesTable.get(ps);
|
||||
return state == null ? PowerState.PowerUnknown : state;
|
||||
|
|
@ -4101,7 +4017,7 @@ public class LibvirtComputingResource extends ServerResourceBase implements Serv
|
|||
return true;
|
||||
}
|
||||
|
||||
private boolean add_network_rules(final String vmName, final String vmId, final String guestIP, final String sig, final String seq, final String mac, final String rules, final String vif, final String brname,
|
||||
public boolean addNetworkRules(final String vmName, final String vmId, final String guestIP, final String sig, final String seq, final String mac, final String rules, final String vif, final String brname,
|
||||
final String secIps) {
|
||||
if (!_canBridgeFirewall) {
|
||||
return false;
|
||||
|
|
|
|||
|
|
@ -0,0 +1,68 @@
|
|||
//
|
||||
// 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 java.util.UUID;
|
||||
|
||||
import com.cloud.agent.api.Answer;
|
||||
import com.cloud.agent.api.CreateVolumeFromSnapshotAnswer;
|
||||
import com.cloud.agent.api.CreateVolumeFromSnapshotCommand;
|
||||
import com.cloud.agent.api.to.StorageFilerTO;
|
||||
import com.cloud.hypervisor.kvm.resource.LibvirtComputingResource;
|
||||
import com.cloud.hypervisor.kvm.storage.KVMPhysicalDisk;
|
||||
import com.cloud.hypervisor.kvm.storage.KVMStoragePool;
|
||||
import com.cloud.hypervisor.kvm.storage.KVMStoragePoolManager;
|
||||
import com.cloud.resource.CommandWrapper;
|
||||
import com.cloud.utils.exception.CloudRuntimeException;
|
||||
|
||||
public final class LibvirtCreateVolumeFromSnapshotCommandWrapper extends CommandWrapper<CreateVolumeFromSnapshotCommand, Answer, LibvirtComputingResource> {
|
||||
|
||||
@Override
|
||||
public Answer execute(final CreateVolumeFromSnapshotCommand command, final LibvirtComputingResource libvirtComputingResource) {
|
||||
try {
|
||||
|
||||
String snapshotPath = command.getSnapshotUuid();
|
||||
final int index = snapshotPath.lastIndexOf("/");
|
||||
snapshotPath = snapshotPath.substring(0, index);
|
||||
|
||||
final KVMStoragePoolManager storagePoolMgr = libvirtComputingResource.getStoragePoolMgr();
|
||||
final KVMStoragePool secondaryPool = storagePoolMgr.getStoragePoolByURI(command.getSecondaryStorageUrl() + snapshotPath);
|
||||
final KVMPhysicalDisk snapshot = secondaryPool.getPhysicalDisk(command.getSnapshotName());
|
||||
|
||||
final String primaryUuid = command.getPrimaryStoragePoolNameLabel();
|
||||
|
||||
final StorageFilerTO pool = command.getPool();
|
||||
final KVMStoragePool primaryPool = storagePoolMgr.getStoragePool(pool.getType(), primaryUuid);
|
||||
|
||||
final String volUuid = UUID.randomUUID().toString();
|
||||
final KVMPhysicalDisk disk = storagePoolMgr.copyPhysicalDisk(snapshot, volUuid, primaryPool, 0);
|
||||
|
||||
if (disk == null) {
|
||||
throw new NullPointerException("Disk was not successfully copied to the new storage.");
|
||||
}
|
||||
|
||||
return new CreateVolumeFromSnapshotAnswer(command, true, "", disk.getName());
|
||||
} catch (final CloudRuntimeException e) {
|
||||
return new CreateVolumeFromSnapshotAnswer(command, false, e.toString(), null);
|
||||
} catch (final Exception e) {
|
||||
return new CreateVolumeFromSnapshotAnswer(command, false, e.toString(), null);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
@ -0,0 +1,67 @@
|
|||
//
|
||||
// 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 java.util.List;
|
||||
import java.util.concurrent.ExecutionException;
|
||||
import java.util.concurrent.ExecutorService;
|
||||
import java.util.concurrent.Executors;
|
||||
import java.util.concurrent.Future;
|
||||
|
||||
import org.apache.log4j.Logger;
|
||||
|
||||
import com.cloud.agent.api.Answer;
|
||||
import com.cloud.agent.api.FenceAnswer;
|
||||
import com.cloud.agent.api.FenceCommand;
|
||||
import com.cloud.hypervisor.kvm.resource.KVMHABase.NfsStoragePool;
|
||||
import com.cloud.hypervisor.kvm.resource.KVMHAChecker;
|
||||
import com.cloud.hypervisor.kvm.resource.KVMHAMonitor;
|
||||
import com.cloud.hypervisor.kvm.resource.LibvirtComputingResource;
|
||||
import com.cloud.resource.CommandWrapper;
|
||||
|
||||
public final class LibvirtFenceCommandWrapper extends CommandWrapper<FenceCommand, Answer, LibvirtComputingResource> {
|
||||
|
||||
private static final Logger s_logger = Logger.getLogger(LibvirtFenceCommandWrapper.class);
|
||||
|
||||
@Override
|
||||
public Answer execute(final FenceCommand command, final LibvirtComputingResource libvirtComputingResource) {
|
||||
final ExecutorService executors = Executors.newSingleThreadExecutor();
|
||||
final KVMHAMonitor monitor = libvirtComputingResource.getMonitor();
|
||||
|
||||
final List<NfsStoragePool> pools = monitor.getStoragePools();
|
||||
final KVMHAChecker ha = new KVMHAChecker(pools, command.getHostIp());
|
||||
|
||||
final Future<Boolean> future = executors.submit(ha);
|
||||
try {
|
||||
final Boolean result = future.get();
|
||||
if (result) {
|
||||
return new FenceAnswer(command, false, "Heart is still beating...");
|
||||
} else {
|
||||
return new FenceAnswer(command);
|
||||
}
|
||||
} catch (final InterruptedException e) {
|
||||
s_logger.warn("Unable to fence", e);
|
||||
return new FenceAnswer(command, false, e.getMessage());
|
||||
} catch (final ExecutionException e) {
|
||||
s_logger.warn("Unable to fence", e);
|
||||
return new FenceAnswer(command, false, e.getMessage());
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
@ -30,7 +30,9 @@ import com.cloud.agent.api.CheckVirtualMachineCommand;
|
|||
import com.cloud.agent.api.CleanupNetworkRulesCmd;
|
||||
import com.cloud.agent.api.Command;
|
||||
import com.cloud.agent.api.CreateStoragePoolCommand;
|
||||
import com.cloud.agent.api.CreateVolumeFromSnapshotCommand;
|
||||
import com.cloud.agent.api.DeleteStoragePoolCommand;
|
||||
import com.cloud.agent.api.FenceCommand;
|
||||
import com.cloud.agent.api.GetHostStatsCommand;
|
||||
import com.cloud.agent.api.GetStorageStatsCommand;
|
||||
import com.cloud.agent.api.GetVmDiskStatsCommand;
|
||||
|
|
@ -54,6 +56,7 @@ import com.cloud.agent.api.PrepareForMigrationCommand;
|
|||
import com.cloud.agent.api.ReadyCommand;
|
||||
import com.cloud.agent.api.RebootCommand;
|
||||
import com.cloud.agent.api.RebootRouterCommand;
|
||||
import com.cloud.agent.api.SecurityGroupRulesCmd;
|
||||
import com.cloud.agent.api.StopCommand;
|
||||
import com.cloud.agent.api.UpgradeSnapshotCommand;
|
||||
import com.cloud.agent.api.check.CheckSshCommand;
|
||||
|
|
@ -124,6 +127,9 @@ public class LibvirtRequestWrapper extends RequestWrapper {
|
|||
linbvirtCommands.put(OvsDestroyTunnelCommand.class, new LibvirtOvsDestroyTunnelCommandWrapper());
|
||||
linbvirtCommands.put(CheckOnHostCommand.class, new LibvirtCheckOnHostCommandWrapper());
|
||||
linbvirtCommands.put(OvsCreateTunnelCommand.class, new LibvirtOvsCreateTunnelCommandWrapper());
|
||||
linbvirtCommands.put(CreateVolumeFromSnapshotCommand.class, new LibvirtCreateVolumeFromSnapshotCommandWrapper());
|
||||
linbvirtCommands.put(FenceCommand.class, new LibvirtFenceCommandWrapper());
|
||||
linbvirtCommands.put(SecurityGroupRulesCmd.class, new LibvirtSecurityGroupRulesCommandWrapper());
|
||||
|
||||
resources.put(LibvirtComputingResource.class, linbvirtCommands);
|
||||
}
|
||||
|
|
|
|||
|
|
@ -0,0 +1,67 @@
|
|||
//
|
||||
// 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 java.util.List;
|
||||
|
||||
import org.apache.log4j.Logger;
|
||||
import org.libvirt.Connect;
|
||||
import org.libvirt.LibvirtException;
|
||||
|
||||
import com.cloud.agent.api.Answer;
|
||||
import com.cloud.agent.api.SecurityGroupRuleAnswer;
|
||||
import com.cloud.agent.api.SecurityGroupRulesCmd;
|
||||
import com.cloud.hypervisor.kvm.resource.LibvirtComputingResource;
|
||||
import com.cloud.hypervisor.kvm.resource.LibvirtVMDef.InterfaceDef;
|
||||
import com.cloud.resource.CommandWrapper;
|
||||
|
||||
public final class LibvirtSecurityGroupRulesCommandWrapper extends CommandWrapper<SecurityGroupRulesCmd, Answer, LibvirtComputingResource> {
|
||||
|
||||
private static final Logger s_logger = Logger.getLogger(LibvirtSecurityGroupRulesCommandWrapper.class);
|
||||
|
||||
@Override
|
||||
public Answer execute(final SecurityGroupRulesCmd command, final LibvirtComputingResource libvirtComputingResource) {
|
||||
String vif = null;
|
||||
String brname = null;
|
||||
try {
|
||||
final LibvirtConnectionWrapper libvirtConnectionWrapper = libvirtComputingResource.getLibvirtConnectionWrapper();
|
||||
|
||||
final Connect conn = libvirtConnectionWrapper.getConnectionByVmName(command.getVmName());
|
||||
final List<InterfaceDef> nics = libvirtComputingResource.getInterfaces(conn, command.getVmName());
|
||||
|
||||
vif = nics.get(0).getDevName();
|
||||
brname = nics.get(0).getBrName();
|
||||
} catch (final LibvirtException e) {
|
||||
return new SecurityGroupRuleAnswer(command, false, e.toString());
|
||||
}
|
||||
|
||||
final boolean result = libvirtComputingResource.addNetworkRules(command.getVmName(), Long.toString(command.getVmId()), command.getGuestIp(), command.getSignature(),
|
||||
Long.toString(command.getSeqNum()), command.getGuestMac(), command.stringifyRules(), vif, brname, command.getSecIpsString());
|
||||
|
||||
if (!result) {
|
||||
s_logger.warn("Failed to program network rules for vm " + command.getVmName());
|
||||
return new SecurityGroupRuleAnswer(command, false, "programming network rules failed");
|
||||
} else {
|
||||
s_logger.debug("Programmed network rules for vm " + command.getVmName() + " guestIp=" + command.getGuestIp() + ",ingress numrules="
|
||||
+ command.getIngressRuleSet().length + ",egress numrules=" + command.getEgressRuleSet().length);
|
||||
return new SecurityGroupRuleAnswer(command);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
@ -72,7 +72,9 @@ import com.cloud.agent.api.CheckOnHostCommand;
|
|||
import com.cloud.agent.api.CheckVirtualMachineCommand;
|
||||
import com.cloud.agent.api.CleanupNetworkRulesCmd;
|
||||
import com.cloud.agent.api.CreateStoragePoolCommand;
|
||||
import com.cloud.agent.api.CreateVolumeFromSnapshotCommand;
|
||||
import com.cloud.agent.api.DeleteStoragePoolCommand;
|
||||
import com.cloud.agent.api.FenceCommand;
|
||||
import com.cloud.agent.api.GetHostStatsCommand;
|
||||
import com.cloud.agent.api.GetStorageStatsCommand;
|
||||
import com.cloud.agent.api.GetVmDiskStatsCommand;
|
||||
|
|
@ -100,6 +102,8 @@ import com.cloud.agent.api.PrepareForMigrationCommand;
|
|||
import com.cloud.agent.api.ReadyCommand;
|
||||
import com.cloud.agent.api.RebootCommand;
|
||||
import com.cloud.agent.api.RebootRouterCommand;
|
||||
import com.cloud.agent.api.SecurityGroupRulesCmd;
|
||||
import com.cloud.agent.api.SecurityGroupRulesCmd.IpPortAndProto;
|
||||
import com.cloud.agent.api.StopCommand;
|
||||
import com.cloud.agent.api.UpgradeSnapshotCommand;
|
||||
import com.cloud.agent.api.VmStatsEntry;
|
||||
|
|
@ -117,6 +121,7 @@ import com.cloud.agent.api.to.VirtualMachineTO;
|
|||
import com.cloud.agent.api.to.VolumeTO;
|
||||
import com.cloud.agent.resource.virtualnetwork.VirtualRoutingResource;
|
||||
import com.cloud.exception.InternalErrorException;
|
||||
import com.cloud.hypervisor.kvm.resource.KVMHABase.NfsStoragePool;
|
||||
import com.cloud.hypervisor.kvm.resource.LibvirtVMDef.DiskDef;
|
||||
import com.cloud.hypervisor.kvm.resource.LibvirtVMDef.InterfaceDef;
|
||||
import com.cloud.hypervisor.kvm.resource.wrapper.LibvirtConnectionWrapper;
|
||||
|
|
@ -1863,17 +1868,17 @@ public class LibvirtComputingResourceTest {
|
|||
|
||||
@Test
|
||||
public void testUpgradeSnapshotCommand() {
|
||||
final StoragePool pool = null;
|
||||
final String secondaryStoragePoolURL = null;
|
||||
final Long dcId = null;
|
||||
final Long accountId = null;
|
||||
final Long volumeId = null;
|
||||
final Long templateId = null;
|
||||
final Long tmpltAccountId = null;
|
||||
final String volumePath = null;
|
||||
final String snapshotUuid = null;
|
||||
final String snapshotName = null;
|
||||
final String version = null;
|
||||
final StoragePool pool = Mockito.mock(StoragePool.class);;
|
||||
final String secondaryStoragePoolURL = "url";
|
||||
final Long dcId = 1l;
|
||||
final Long accountId = 1l;
|
||||
final Long volumeId = 1l;
|
||||
final Long templateId = 1l;
|
||||
final Long tmpltAccountId = 1l;
|
||||
final String volumePath = "/opt/path";
|
||||
final String snapshotUuid = "uuid:/8edb1156-a851-4914-afc6-468ee52ac861/";
|
||||
final String snapshotName = "uuid:/8edb1156-a851-4914-afc6-468ee52ac861/";
|
||||
final String version = "1";
|
||||
|
||||
final UpgradeSnapshotCommand command = new UpgradeSnapshotCommand(pool, secondaryStoragePoolURL, dcId, accountId, volumeId, templateId, tmpltAccountId, volumePath, snapshotUuid, snapshotName, version);
|
||||
|
||||
|
|
@ -2654,4 +2659,273 @@ public class LibvirtComputingResourceTest {
|
|||
verify(libvirtComputingResource, times(1)).configureTunnelNetwork(command.getNetworkId(), command.getFrom(),
|
||||
command.getNetworkName());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testCreateVolumeFromSnapshotCommand() {
|
||||
// This tests asserts to False because there will be a NPE due to UUID static method calls.
|
||||
|
||||
final StoragePool pool = Mockito.mock(StoragePool.class);
|
||||
final String secondaryStoragePoolURL = "/opt/storage/";
|
||||
final Long dcId = 1l;
|
||||
final Long accountId = 1l;
|
||||
final Long volumeId = 1l;
|
||||
final String backedUpSnapshotUuid = "uuid:/8edb1156-a851-4914-afc6-468ee52ac861/";
|
||||
final String backedUpSnapshotName = "uuid:/8edb1156-a851-4914-afc6-468ee52ac862/";
|
||||
final int wait = 0;
|
||||
|
||||
final CreateVolumeFromSnapshotCommand command = new CreateVolumeFromSnapshotCommand(pool, secondaryStoragePoolURL, dcId, accountId, volumeId, backedUpSnapshotUuid, backedUpSnapshotName, wait);
|
||||
|
||||
final KVMStoragePoolManager storagePoolMgr = Mockito.mock(KVMStoragePoolManager.class);
|
||||
final KVMStoragePool secondaryPool = Mockito.mock(KVMStoragePool.class);
|
||||
final KVMPhysicalDisk snapshot = Mockito.mock(KVMPhysicalDisk.class);
|
||||
final KVMStoragePool primaryPool = Mockito.mock(KVMStoragePool.class);
|
||||
|
||||
String snapshotPath = command.getSnapshotUuid();
|
||||
final int index = snapshotPath.lastIndexOf("/");
|
||||
snapshotPath = snapshotPath.substring(0, index);
|
||||
|
||||
final String primaryUuid = command.getPrimaryStoragePoolNameLabel();
|
||||
|
||||
when(libvirtComputingResource.getStoragePoolMgr()).thenReturn(storagePoolMgr);
|
||||
when(storagePoolMgr.getStoragePoolByURI(command.getSecondaryStorageUrl() + snapshotPath)).thenReturn(secondaryPool);
|
||||
when(secondaryPool.getPhysicalDisk(command.getSnapshotName())).thenReturn(snapshot);
|
||||
when(storagePoolMgr.getStoragePool(command.getPool().getType(), primaryUuid)).thenReturn(primaryPool);
|
||||
|
||||
//when(storagePoolMgr.copyPhysicalDisk(snapshot, volUuid, primaryPool, 0)).thenReturn(disk);
|
||||
|
||||
final LibvirtRequestWrapper wrapper = LibvirtRequestWrapper.getInstance();
|
||||
assertNotNull(wrapper);
|
||||
|
||||
final Answer answer = wrapper.execute(command, libvirtComputingResource);
|
||||
assertFalse(answer.getResult());
|
||||
|
||||
verify(libvirtComputingResource, times(1)).getStoragePoolMgr();
|
||||
verify(storagePoolMgr, times(1)).getStoragePoolByURI(command.getSecondaryStorageUrl() + snapshotPath);
|
||||
verify(secondaryPool, times(1)).getPhysicalDisk(command.getSnapshotName());
|
||||
verify(storagePoolMgr, times(1)).getStoragePool(command.getPool().getType(), primaryUuid);
|
||||
//verify(storagePoolMgr, times(1)).copyPhysicalDisk(snapshot, volUuid, primaryPool, 0);
|
||||
}
|
||||
|
||||
@SuppressWarnings("unchecked")
|
||||
@Test
|
||||
public void testCreateVolumeFromSnapshotCommandCloudException() {
|
||||
final StoragePool pool = Mockito.mock(StoragePool.class);
|
||||
final String secondaryStoragePoolURL = "/opt/storage/";
|
||||
final Long dcId = 1l;
|
||||
final Long accountId = 1l;
|
||||
final Long volumeId = 1l;
|
||||
final String backedUpSnapshotUuid = "uuid:/8edb1156-a851-4914-afc6-468ee52ac861/";
|
||||
final String backedUpSnapshotName = "uuid:/8edb1156-a851-4914-afc6-468ee52ac862/";
|
||||
final int wait = 0;
|
||||
|
||||
final CreateVolumeFromSnapshotCommand command = new CreateVolumeFromSnapshotCommand(pool, secondaryStoragePoolURL, dcId, accountId, volumeId, backedUpSnapshotUuid, backedUpSnapshotName, wait);
|
||||
|
||||
final KVMStoragePoolManager storagePoolMgr = Mockito.mock(KVMStoragePoolManager.class);
|
||||
final KVMStoragePool secondaryPool = Mockito.mock(KVMStoragePool.class);
|
||||
final KVMPhysicalDisk snapshot = Mockito.mock(KVMPhysicalDisk.class);
|
||||
|
||||
String snapshotPath = command.getSnapshotUuid();
|
||||
final int index = snapshotPath.lastIndexOf("/");
|
||||
snapshotPath = snapshotPath.substring(0, index);
|
||||
|
||||
final String primaryUuid = command.getPrimaryStoragePoolNameLabel();
|
||||
|
||||
when(libvirtComputingResource.getStoragePoolMgr()).thenReturn(storagePoolMgr);
|
||||
when(storagePoolMgr.getStoragePoolByURI(command.getSecondaryStorageUrl() + snapshotPath)).thenReturn(secondaryPool);
|
||||
when(secondaryPool.getPhysicalDisk(command.getSnapshotName())).thenReturn(snapshot);
|
||||
when(storagePoolMgr.getStoragePool(command.getPool().getType(), primaryUuid)).thenThrow(CloudRuntimeException.class);
|
||||
|
||||
final LibvirtRequestWrapper wrapper = LibvirtRequestWrapper.getInstance();
|
||||
assertNotNull(wrapper);
|
||||
|
||||
final Answer answer = wrapper.execute(command, libvirtComputingResource);
|
||||
assertFalse(answer.getResult());
|
||||
|
||||
verify(libvirtComputingResource, times(1)).getStoragePoolMgr();
|
||||
verify(storagePoolMgr, times(1)).getStoragePoolByURI(command.getSecondaryStorageUrl() + snapshotPath);
|
||||
verify(secondaryPool, times(1)).getPhysicalDisk(command.getSnapshotName());
|
||||
verify(storagePoolMgr, times(1)).getStoragePool(command.getPool().getType(), primaryUuid);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testFenceCommand() {
|
||||
final VirtualMachine vm = Mockito.mock(VirtualMachine.class);;
|
||||
final com.cloud.host.Host host = Mockito.mock(com.cloud.host.Host.class);
|
||||
|
||||
final FenceCommand command = new FenceCommand(vm, host);
|
||||
|
||||
final KVMHAMonitor monitor = Mockito.mock(KVMHAMonitor.class);
|
||||
|
||||
final NfsStoragePool storagePool = Mockito.mock(NfsStoragePool.class);
|
||||
final List<NfsStoragePool> pools = new ArrayList<NfsStoragePool>();
|
||||
pools.add(storagePool);
|
||||
|
||||
when(libvirtComputingResource.getMonitor()).thenReturn(monitor);
|
||||
when(monitor.getStoragePools()).thenReturn(pools);
|
||||
|
||||
final LibvirtRequestWrapper wrapper = LibvirtRequestWrapper.getInstance();
|
||||
assertNotNull(wrapper);
|
||||
|
||||
final Answer answer = wrapper.execute(command, libvirtComputingResource);
|
||||
assertFalse(answer.getResult());
|
||||
|
||||
verify(libvirtComputingResource, times(1)).getMonitor();
|
||||
verify(monitor, times(1)).getStoragePools();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testSecurityGroupRulesCmdFalse() {
|
||||
final String guestIp = "172.16.16.16";
|
||||
final String guestMac = "00:00:00:00";
|
||||
final String vmName = "Test";
|
||||
final Long vmId = 1l;
|
||||
final String signature = "signature";
|
||||
final Long seqNum = 1l;
|
||||
final IpPortAndProto[] ingressRuleSet = new IpPortAndProto[]{Mockito.mock(IpPortAndProto.class)};
|
||||
final IpPortAndProto[] egressRuleSet = new IpPortAndProto[]{Mockito.mock(IpPortAndProto.class)};
|
||||
|
||||
final SecurityGroupRulesCmd command = new SecurityGroupRulesCmd(guestIp, guestMac, vmName, vmId, signature, seqNum, ingressRuleSet, egressRuleSet);
|
||||
|
||||
final LibvirtConnectionWrapper libvirtConnectionWrapper = Mockito.mock(LibvirtConnectionWrapper.class);
|
||||
final Connect conn = Mockito.mock(Connect.class);
|
||||
|
||||
final List<InterfaceDef> nics = new ArrayList<InterfaceDef>();
|
||||
final InterfaceDef interfaceDef = Mockito.mock(InterfaceDef.class);
|
||||
nics.add(interfaceDef);
|
||||
|
||||
when(libvirtComputingResource.getLibvirtConnectionWrapper()).thenReturn(libvirtConnectionWrapper);
|
||||
when(libvirtComputingResource.getInterfaces(conn, command.getVmName())).thenReturn(nics);
|
||||
try {
|
||||
when(libvirtConnectionWrapper.getConnectionByVmName(command.getVmName())).thenReturn(conn);
|
||||
} catch (final LibvirtException e) {
|
||||
fail(e.getMessage());
|
||||
}
|
||||
|
||||
when(ingressRuleSet[0].getProto()).thenReturn("tcp");
|
||||
when(ingressRuleSet[0].getStartPort()).thenReturn(22);
|
||||
when(ingressRuleSet[0].getEndPort()).thenReturn(22);
|
||||
when(ingressRuleSet[0].getAllowedCidrs()).thenReturn(new String[]{"0.0.0.0/0"});
|
||||
|
||||
when(egressRuleSet[0].getProto()).thenReturn("tcp");
|
||||
when(egressRuleSet[0].getStartPort()).thenReturn(22);
|
||||
when(egressRuleSet[0].getEndPort()).thenReturn(22);
|
||||
when(egressRuleSet[0].getAllowedCidrs()).thenReturn(new String[]{"0.0.0.0/0"});
|
||||
|
||||
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.getVmName());
|
||||
} catch (final LibvirtException e) {
|
||||
fail(e.getMessage());
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testSecurityGroupRulesCmdTrue() {
|
||||
final String guestIp = "172.16.16.16";
|
||||
final String guestMac = "00:00:00:00";
|
||||
final String vmName = "Test";
|
||||
final Long vmId = 1l;
|
||||
final String signature = "signature";
|
||||
final Long seqNum = 1l;
|
||||
final IpPortAndProto[] ingressRuleSet = new IpPortAndProto[]{Mockito.mock(IpPortAndProto.class)};
|
||||
final IpPortAndProto[] egressRuleSet = new IpPortAndProto[]{Mockito.mock(IpPortAndProto.class)};
|
||||
|
||||
final SecurityGroupRulesCmd command = new SecurityGroupRulesCmd(guestIp, guestMac, vmName, vmId, signature, seqNum, ingressRuleSet, egressRuleSet);
|
||||
|
||||
final LibvirtConnectionWrapper libvirtConnectionWrapper = Mockito.mock(LibvirtConnectionWrapper.class);
|
||||
final Connect conn = Mockito.mock(Connect.class);
|
||||
|
||||
final List<InterfaceDef> nics = new ArrayList<InterfaceDef>();
|
||||
final InterfaceDef interfaceDef = Mockito.mock(InterfaceDef.class);
|
||||
nics.add(interfaceDef);
|
||||
|
||||
when(libvirtComputingResource.getLibvirtConnectionWrapper()).thenReturn(libvirtConnectionWrapper);
|
||||
when(libvirtComputingResource.getInterfaces(conn, command.getVmName())).thenReturn(nics);
|
||||
try {
|
||||
when(libvirtConnectionWrapper.getConnectionByVmName(command.getVmName())).thenReturn(conn);
|
||||
} catch (final LibvirtException e) {
|
||||
fail(e.getMessage());
|
||||
}
|
||||
|
||||
when(interfaceDef.getDevName()).thenReturn("eth0");
|
||||
when(interfaceDef.getBrName()).thenReturn("br0");
|
||||
|
||||
final String vif = nics.get(0).getDevName();
|
||||
final String brname = nics.get(0).getBrName();
|
||||
|
||||
when(ingressRuleSet[0].getProto()).thenReturn("tcp");
|
||||
when(ingressRuleSet[0].getStartPort()).thenReturn(22);
|
||||
when(ingressRuleSet[0].getEndPort()).thenReturn(22);
|
||||
when(ingressRuleSet[0].getAllowedCidrs()).thenReturn(new String[]{"0.0.0.0/0"});
|
||||
|
||||
when(egressRuleSet[0].getProto()).thenReturn("tcp");
|
||||
when(egressRuleSet[0].getStartPort()).thenReturn(22);
|
||||
when(egressRuleSet[0].getEndPort()).thenReturn(22);
|
||||
when(egressRuleSet[0].getAllowedCidrs()).thenReturn(new String[]{"0.0.0.0/0"});
|
||||
|
||||
when(libvirtComputingResource.addNetworkRules(command.getVmName(), Long.toString(command.getVmId()), command.getGuestIp(), command.getSignature(),
|
||||
Long.toString(command.getSeqNum()), command.getGuestMac(), command.stringifyRules(), vif, brname, command.getSecIpsString())).thenReturn(true);
|
||||
|
||||
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.getVmName());
|
||||
} catch (final LibvirtException e) {
|
||||
fail(e.getMessage());
|
||||
}
|
||||
}
|
||||
|
||||
@SuppressWarnings("unchecked")
|
||||
@Test
|
||||
public void testSecurityGroupRulesCmdException() {
|
||||
final String guestIp = "172.16.16.16";
|
||||
final String guestMac = "00:00:00:00";
|
||||
final String vmName = "Test";
|
||||
final Long vmId = 1l;
|
||||
final String signature = "signature";
|
||||
final Long seqNum = 1l;
|
||||
final IpPortAndProto[] ingressRuleSet = new IpPortAndProto[]{Mockito.mock(IpPortAndProto.class)};
|
||||
final IpPortAndProto[] egressRuleSet = new IpPortAndProto[]{Mockito.mock(IpPortAndProto.class)};
|
||||
|
||||
final SecurityGroupRulesCmd command = new SecurityGroupRulesCmd(guestIp, guestMac, vmName, vmId, signature, seqNum, ingressRuleSet, egressRuleSet);
|
||||
|
||||
final LibvirtConnectionWrapper libvirtConnectionWrapper = Mockito.mock(LibvirtConnectionWrapper.class);
|
||||
final Connect conn = Mockito.mock(Connect.class);
|
||||
|
||||
final List<InterfaceDef> nics = new ArrayList<InterfaceDef>();
|
||||
final InterfaceDef interfaceDef = Mockito.mock(InterfaceDef.class);
|
||||
nics.add(interfaceDef);
|
||||
|
||||
when(libvirtComputingResource.getLibvirtConnectionWrapper()).thenReturn(libvirtConnectionWrapper);
|
||||
when(libvirtComputingResource.getInterfaces(conn, command.getVmName())).thenReturn(nics);
|
||||
try {
|
||||
when(libvirtConnectionWrapper.getConnectionByVmName(command.getVmName())).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.getVmName());
|
||||
} catch (final LibvirtException e) {
|
||||
fail(e.getMessage());
|
||||
}
|
||||
}
|
||||
}
|
||||
Loading…
Reference in New Issue