Skip to content

Commit c01c73e

Browse files
committed
CLOUDSTACK-9051: add unit tests for UpdateVmNicIp
1 parent b79d338 commit c01c73e

3 files changed

Lines changed: 338 additions & 5 deletions

File tree

Lines changed: 91 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,91 @@
1+
// Licensed to the Apache Software Foundation (ASF) under one
2+
// or more contributor license agreements. See the NOTICE file
3+
// distributed with this work for additional information
4+
// regarding copyright ownership. The ASF licenses this file
5+
// to you under the Apache License, Version 2.0 (the
6+
// "License"); you may not use this file except in compliance
7+
// with the License. You may obtain a copy of the License at
8+
//
9+
// http://www.apache.org/licenses/LICENSE-2.0
10+
//
11+
// Unless required by applicable law or agreed to in writing,
12+
// software distributed under the License is distributed on an
13+
// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
14+
// KIND, either express or implied. See the License for the
15+
// specific language governing permissions and limitations
16+
// under the License.
17+
package org.apache.cloudstack.api.command.test;
18+
19+
import junit.framework.Assert;
20+
import junit.framework.TestCase;
21+
22+
import org.junit.Before;
23+
import org.junit.Test;
24+
import org.mockito.Mockito;
25+
26+
import java.util.LinkedList;
27+
import java.util.List;
28+
29+
import org.apache.cloudstack.api.ResponseGenerator;
30+
import org.apache.cloudstack.api.ServerApiException;
31+
import org.apache.cloudstack.api.ResponseObject.ResponseView;
32+
import org.apache.cloudstack.api.command.user.vm.UpdateVmNicIpCmd;
33+
import org.apache.cloudstack.api.response.UserVmResponse;
34+
35+
import com.cloud.exception.ConcurrentOperationException;
36+
import com.cloud.exception.InsufficientCapacityException;
37+
import com.cloud.exception.ResourceAllocationException;
38+
import com.cloud.exception.ResourceUnavailableException;
39+
import com.cloud.uservm.UserVm;
40+
import com.cloud.vm.UserVmService;
41+
42+
public class UpdateVmNicIpTest extends TestCase {
43+
44+
private UpdateVmNicIpCmd updateVmNicIpCmd;
45+
private ResponseGenerator responseGenerator;
46+
47+
@Override
48+
@Before
49+
public void setUp() {
50+
51+
}
52+
53+
@Test
54+
public void testSuccess() throws ResourceAllocationException, ResourceUnavailableException, ConcurrentOperationException, InsufficientCapacityException {
55+
56+
UserVmService userVmService = Mockito.mock(UserVmService.class);
57+
updateVmNicIpCmd = Mockito.mock(UpdateVmNicIpCmd.class);
58+
UserVm userVm = Mockito.mock(UserVm.class);
59+
60+
Mockito.when(userVmService.updateNicIpForVirtualMachine(Mockito.any(UpdateVmNicIpCmd.class))).thenReturn(userVm);
61+
62+
updateVmNicIpCmd._userVmService = userVmService;
63+
responseGenerator = Mockito.mock(ResponseGenerator.class);
64+
65+
List<UserVmResponse> list = new LinkedList<UserVmResponse>();
66+
UserVmResponse userVmResponse = Mockito.mock(UserVmResponse.class);
67+
list.add(userVmResponse);
68+
Mockito.when(responseGenerator.createUserVmResponse(ResponseView.Restricted, "virtualmachine", userVm)).thenReturn(list);
69+
70+
updateVmNicIpCmd._responseGenerator = responseGenerator;
71+
updateVmNicIpCmd.execute();
72+
}
73+
74+
@Test
75+
public void testFailure() throws ResourceAllocationException, ResourceUnavailableException, ConcurrentOperationException, InsufficientCapacityException {
76+
UserVmService userVmService = Mockito.mock(UserVmService.class);
77+
updateVmNicIpCmd = Mockito.mock(UpdateVmNicIpCmd.class);
78+
79+
Mockito.when(userVmService.updateNicIpForVirtualMachine(Mockito.any(UpdateVmNicIpCmd.class))).thenReturn(null);
80+
81+
updateVmNicIpCmd._userVmService = userVmService;
82+
83+
updateVmNicIpCmd._responseGenerator = responseGenerator;
84+
try {
85+
updateVmNicIpCmd.execute();
86+
} catch (ServerApiException exception) {
87+
Assert.assertEquals("Failed to update ip address on vm NIC. Refer to server logs for details.", exception.getDescription());
88+
}
89+
}
90+
91+
}

server/src/com/cloud/vm/UserVmManagerImpl.java

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1433,9 +1433,6 @@ public UserVm updateNicIpForVirtualMachine(UpdateVmNicIpCmd cmd) {
14331433
if (vm == null) {
14341434
throw new InvalidParameterValueException("There is no vm with the nic");
14351435
}
1436-
if (vm.getState() != State.Stopped) {
1437-
throw new InvalidParameterValueException("The vm is not Stopped, please stop it before update Vm nic Ip");
1438-
}
14391436

14401437
if (!_networkModel.listNetworkOfferingServices(nicVO.getNetworkId()).isEmpty() && vm.getState() != State.Stopped) {
14411438
InvalidParameterValueException ex = new InvalidParameterValueException(
@@ -1482,8 +1479,8 @@ public UserVm updateNicIpForVirtualMachine(UpdateVmNicIpCmd cmd) {
14821479
Transaction.execute(new TransactionCallbackNoReturn() {
14831480
@Override
14841481
public void doInTransactionWithoutResult(TransactionStatus status) {
1485-
_ipAddrMgr.markIpAsUnavailable(ip.getId());
1486-
_ipAddressDao.unassignIpAddress(ip.getId());
1482+
_ipAddrMgr.markIpAsUnavailable(ip.getId());
1483+
_ipAddressDao.unassignIpAddress(ip.getId());
14871484
}
14881485
});
14891486
}

0 commit comments

Comments
 (0)