Fixing some of the scary bugs possible null pointer: created testSetNicDevIdIfCorrectVifIsNotNull

Signed-off-by: Hugo Trippaers <htrippaers@schubergphilis.com>
This commit is contained in:
Sander Botman 2014-02-11 17:45:00 +01:00 committed by Hugo Trippaers
parent e171cb181c
commit 16aa73c2c8
4 changed files with 73 additions and 10 deletions

View File

@ -43,7 +43,6 @@ import com.cloud.vm.VirtualMachine;
@Local(value = FenceBuilder.class)
public class XenServerFencer extends AdapterBase implements FenceBuilder {
private static final Logger s_logger = Logger.getLogger(XenServerFencer.class);
String _name;
@Inject
HostDao _hostDao;

View File

@ -7297,14 +7297,7 @@ public abstract class CitrixResourceBase implements ServerResource, HypervisorRe
VM router = getVM(conn, routerName);
VIF correctVif = getVifByMac(conn, router, ip.getVifMacAddress());
if (correctVif == null) {
if (ip.isAdd()) {
throw new InternalErrorException("Failed to find DomR VIF to associate IP with.");
} else {
s_logger.debug("VIF to deassociate IP with does not exist, return success");
}
}
ip.setNicDevId(Integer.valueOf(correctVif.getDevice(conn)));
setNicDevIdIfCorrectVifIsNotNull(conn, ip, correctVif);
}
} catch (Exception e) {
s_logger.error("Ip Assoc failure on applying one ip due to exception: ", e);
@ -7314,6 +7307,20 @@ public abstract class CitrixResourceBase implements ServerResource, HypervisorRe
return new ExecutionResult(true, null);
}
protected void setNicDevIdIfCorrectVifIsNotNull(Connection conn,
IpAddressTO ip, VIF correctVif) throws InternalErrorException,
BadServerResponse, XenAPIException, XmlRpcException {
if (correctVif == null) {
if (ip.isAdd()) {
throw new InternalErrorException("Failed to find DomR VIF to associate IP with.");
} else {
s_logger.debug("VIF to deassociate IP with does not exist, return success");
}
} else {
ip.setNicDevId(Integer.valueOf(correctVif.getDevice(conn)));
}
}
protected ExecutionResult prepareNetworkElementCommand(SetSourceNatCommand cmd) {
Connection conn = getConnection();
String routerName = cmd.getAccessDetail(NetworkElementCommand.ROUTER_NAME);

View File

@ -0,0 +1,39 @@
// 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.ha;
import static org.junit.Assert.assertEquals;
import org.junit.Test;
public class XenServerFencerTest {
@Test
public void testSetAndGetName() throws Exception {
XenServerFencer xenServerFencer = new XenServerFencer();
String name = "name";
xenServerFencer.setName(name);
String actual = xenServerFencer.getName();
assertEquals(name, actual);
}
}

View File

@ -19,12 +19,14 @@
package com.cloud.hypervisor.xen.resource;
import static org.junit.Assert.fail;
import static org.mockito.Mockito.doNothing;
import static org.mockito.Mockito.doReturn;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.times;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;
import static org.mockito.Matchers.any;
import java.util.HashMap;
import java.util.Iterator;
@ -41,11 +43,12 @@ import org.mockito.Spy;
import com.xensource.xenapi.Connection;
import com.xensource.xenapi.Host;
import com.xensource.xenapi.Types;
import com.xensource.xenapi.VIF;
import com.xensource.xenapi.VM;
import com.xensource.xenapi.XenAPIObject;
import com.cloud.agent.api.ScaleVmAnswer;
import com.cloud.agent.api.ScaleVmCommand;
import com.cloud.agent.api.to.IpAddressTO;
import com.cloud.agent.api.to.VirtualMachineTO;
import com.cloud.hypervisor.xen.resource.CitrixResourceBase.XsHost;
@ -171,4 +174,19 @@ public class CitrixResourceBaseTest {
verify(_resource, times(1)).callHostPlugin(conn, "vmops", "add_to_VCPUs_params_live", "key", "weight", "value", "253", "vmname", "i-2-3-VM");
verify(_resource, times(1)).callHostPlugin(conn, "vmops", "add_to_VCPUs_params_live", "key", "cap", "value", "99", "vmname", "i-2-3-VM");
}
@Test
public void testSetNicDevIdIfCorrectVifIsNotNull() throws Exception {
IpAddressTO ip = mock (IpAddressTO.class);
when (ip.isAdd()).thenReturn(false);
VIF correctVif = null;
try {
_resource.setNicDevIdIfCorrectVifIsNotNull(conn, ip, correctVif);
}
catch (NullPointerException e)
{
fail("this test is meant to show that null pointer is not thrown");
}
}
}