Skip to content

Commit a9797c9

Browse files
committed
changes
Signed-off-by: Abhishek Kumar <abhishek.mrt22@gmail.com>
1 parent e021864 commit a9797c9

6 files changed

Lines changed: 139 additions & 15 deletions

File tree

framework/cluster/src/main/java/com/cloud/cluster/dao/ManagementServerHostDao.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,4 +61,6 @@ public interface ManagementServerHostDao extends GenericDao<ManagementServerHost
6161
ManagementServerHostVO findOneInUpState(Filter filter);
6262

6363
ManagementServerHostVO findOneByLongestRuntime();
64+
65+
List<ManagementServerHostVO> listUpByIds(List<Long> ids);
6466
}

framework/cluster/src/main/java/com/cloud/cluster/dao/ManagementServerHostDaoImpl.java

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -24,12 +24,11 @@
2424
import java.util.List;
2525
import java.util.TimeZone;
2626

27-
27+
import org.apache.cloudstack.management.ManagementServerHost;
28+
import org.apache.cloudstack.management.ManagementServerHost.State;
2829
import org.apache.commons.collections.CollectionUtils;
2930

3031
import com.cloud.cluster.ClusterInvalidSessionException;
31-
import org.apache.cloudstack.management.ManagementServerHost;
32-
import org.apache.cloudstack.management.ManagementServerHost.State;
3332
import com.cloud.cluster.ManagementServerHostVO;
3433
import com.cloud.utils.DateUtil;
3534
import com.cloud.utils.db.DB;
@@ -318,4 +317,18 @@ public ManagementServerHostVO findOneByLongestRuntime() {
318317
return CollectionUtils.isNotEmpty(msHosts) ? msHosts.get(0) : null;
319318
}
320319

320+
@Override
321+
public List<ManagementServerHostVO> listUpByIds(List<Long> ids) {
322+
if (CollectionUtils.isEmpty(ids)) {
323+
return new ArrayList<>();
324+
}
325+
SearchBuilder<ManagementServerHostVO> sb = createSearchBuilder();
326+
sb.and("ids", sb.entity().getId(), SearchCriteria.Op.IN);
327+
sb.and("state", sb.entity().getState(), SearchCriteria.Op.EQ);
328+
sb.done();
329+
SearchCriteria<ManagementServerHostVO> sc = sb.create();
330+
sc.setParameters("ids", ids.toArray());
331+
sc.setParameters("state", ManagementServerHost.State.Up);
332+
return listBy(sc);
333+
}
321334
}
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+
18+
package com.cloud.cluster.dao;
19+
20+
import static org.junit.Assert.assertEquals;
21+
import static org.junit.Assert.assertNotNull;
22+
import static org.junit.Assert.assertTrue;
23+
import static org.mockito.ArgumentMatchers.any;
24+
import static org.mockito.ArgumentMatchers.anyLong;
25+
import static org.mockito.ArgumentMatchers.eq;
26+
import static org.mockito.ArgumentMatchers.nullable;
27+
import static org.mockito.Mockito.doReturn;
28+
import static org.mockito.Mockito.mock;
29+
import static org.mockito.Mockito.verify;
30+
import static org.mockito.Mockito.when;
31+
32+
import java.util.Collections;
33+
import java.util.List;
34+
35+
import org.apache.cloudstack.management.ManagementServerHost;
36+
import org.junit.Test;
37+
import org.junit.runner.RunWith;
38+
import org.mockito.InjectMocks;
39+
import org.mockito.Spy;
40+
import org.mockito.junit.MockitoJUnitRunner;
41+
42+
import com.cloud.cluster.ManagementServerHostVO;
43+
import com.cloud.utils.db.SearchBuilder;
44+
import com.cloud.utils.db.SearchCriteria;
45+
46+
@RunWith(MockitoJUnitRunner.class)
47+
public class ManagementServerHostDaoImplTest {
48+
49+
@Spy
50+
@InjectMocks
51+
private ManagementServerHostDaoImpl managementServerHostDao;
52+
53+
@Test
54+
public void listUpByIdsReturnsEmptyListWhenInputIsEmpty() {
55+
List<ManagementServerHostVO> result = managementServerHostDao.listUpByIds(Collections.emptyList());
56+
assertNotNull(result);
57+
assertTrue(result.isEmpty());
58+
}
59+
60+
@Test
61+
public void listUpByIdsReturnsEmptyListWhenNoMatchingIds() {;
62+
doReturn(Collections.emptyList()).when(managementServerHostDao).listBy(any(SearchCriteria.class));
63+
SearchBuilder<ManagementServerHostVO> mockSb = mock(SearchBuilder.class);
64+
SearchCriteria<ManagementServerHostVO> mocSC = mock(SearchCriteria.class);
65+
when(mockSb.entity()).thenReturn(mock(ManagementServerHostVO.class));
66+
when(mockSb.create()).thenReturn(mocSC);
67+
doReturn(mockSb).when(managementServerHostDao).createSearchBuilder();
68+
doReturn(Collections.emptyList()).when(managementServerHostDao).listBy(any(SearchCriteria.class));
69+
List<ManagementServerHostVO> result = managementServerHostDao.listUpByIds(List.of(1L, 2L));
70+
assertNotNull(result);
71+
assertTrue(result.isEmpty());
72+
verify(managementServerHostDao).createSearchBuilder();
73+
verify(mockSb).and(eq("ids"), anyLong(), eq(SearchCriteria.Op.IN));
74+
verify(mockSb).and(eq("state"), nullable(ManagementServerHost.State.class), eq(SearchCriteria.Op.EQ));
75+
verify(mockSb).done();
76+
verify(mocSC).setParameters(eq("ids"), anyLong(), anyLong());
77+
verify(mocSC).setParameters("state", ManagementServerHost.State.Up);
78+
}
79+
80+
@Test
81+
public void listUpByIdsReturnsMatchingHostsWhenIdsAreValid() {
82+
ManagementServerHostVO host1 = mock(ManagementServerHostVO.class);
83+
ManagementServerHostVO host2 = mock(ManagementServerHostVO.class);
84+
doReturn(List.of(host1, host2)).when(managementServerHostDao).listBy(any(SearchCriteria.class));
85+
List<ManagementServerHostVO> result = managementServerHostDao.listUpByIds(List.of(1L, 2L));
86+
assertNotNull(result);
87+
assertEquals(2, result.size());
88+
assertTrue(result.contains(host1));
89+
assertTrue(result.contains(host2));
90+
}
91+
}

framework/extensions/src/main/java/org/apache/cloudstack/framework/extensions/manager/ExtensionsManagerImpl.java

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1676,14 +1676,14 @@ public boolean syncExtension(SyncExtensionCmd cmd) {
16761676
}
16771677
List<ManagementServerHost> targetManagementServers = new ArrayList<>();
16781678
if (CollectionUtils.isNotEmpty(targetManagementServerIds)) {
1679-
for (Long targetMsId : targetManagementServerIds) {
1680-
ManagementServerHostVO targetMs = managementServerHostDao.findById(targetMsId);
1681-
if (targetMs == null || !ManagementServerHost.State.Up.equals(targetMs.getState())) {
1682-
throw new InvalidParameterValueException(
1683-
String.format("Unable to find active target management server with id: %d", targetMsId));
1684-
}
1685-
targetManagementServers.add(targetMs);
1679+
if (targetManagementServerIds.contains(sourceManagementServerId)) {
1680+
throw new InvalidParameterValueException("Source management server cannot be specified as target");
1681+
}
1682+
List<ManagementServerHostVO> msList = managementServerHostDao.listUpByIds(targetManagementServerIds);
1683+
if (msList.size() != targetManagementServerIds.size()) {
1684+
throw new InvalidParameterValueException("Some of the specified target management servers are not found or not in Up state");
16861685
}
1686+
targetManagementServers.addAll(msList);
16871687
} else {
16881688
List<ManagementServerHostVO> msList = managementServerHostDao.listBy(ManagementServerHost.State.Up);
16891689
msList.removeIf(ms -> ms.getId() == sourceManagementServerId);

framework/extensions/src/test/java/org/apache/cloudstack/framework/extensions/manager/ExtensionsManagerImplTest.java

Lines changed: 22 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2261,22 +2261,40 @@ public void syncExtensionThrowsWhenInvalidFilesProvided() {
22612261
extensionsManager.syncExtension(cmd);
22622262
}
22632263

2264+
@Test(expected = InvalidParameterValueException.class)
2265+
public void syncExtensionReturnsTrueWhenNotAllTargetsInUpOrFound() {
2266+
SyncExtensionCmd cmd = mock(SyncExtensionCmd.class);
2267+
when(cmd.getId()).thenReturn(1L);
2268+
when(cmd.getSourceManagementServerId()).thenReturn(2L);
2269+
List<Long> targetIds = List.of(3L, 4L);
2270+
when(cmd.getTargetManagementServerIds()).thenReturn(targetIds);
2271+
when(cmd.getFiles()).thenReturn(List.of("validFile"));
2272+
ExtensionVO extension = mock(ExtensionVO.class);
2273+
when(extensionDao.findById(1L)).thenReturn(extension);
2274+
ManagementServerHostVO sourceServer = mock(ManagementServerHostVO.class);
2275+
when(sourceServer.getState()).thenReturn(ManagementServerHost.State.Up);
2276+
when(sourceServer.getMsid()).thenReturn(100L);
2277+
when(managementServerHostDao.findById(2L)).thenReturn(sourceServer);
2278+
when(managementServerHostDao.listUpByIds(targetIds)).thenReturn(List.of(mock(ManagementServerHostVO.class)));
2279+
extensionsManager.syncExtension(cmd);
2280+
}
2281+
22642282
@Test
22652283
public void syncExtensionReturnsTrueWhenSyncSucceeds() {
22662284
SyncExtensionCmd cmd = mock(SyncExtensionCmd.class);
22672285
when(cmd.getId()).thenReturn(1L);
22682286
when(cmd.getSourceManagementServerId()).thenReturn(2L);
2269-
when(cmd.getTargetManagementServerIds()).thenReturn(List.of(3L));
2287+
List<Long> targetIds = List.of(3L, 4L);
2288+
when(cmd.getTargetManagementServerIds()).thenReturn(targetIds);
22702289
when(cmd.getFiles()).thenReturn(List.of("validFile"));
22712290
ExtensionVO extension = mock(ExtensionVO.class);
22722291
when(extensionDao.findById(1L)).thenReturn(extension);
22732292
ManagementServerHostVO sourceServer = mock(ManagementServerHostVO.class);
22742293
when(sourceServer.getState()).thenReturn(ManagementServerHost.State.Up);
22752294
when(sourceServer.getMsid()).thenReturn(100L);
22762295
when(managementServerHostDao.findById(2L)).thenReturn(sourceServer);
2277-
ManagementServerHostVO targetServer = mock(ManagementServerHostVO.class);
2278-
when(targetServer.getState()).thenReturn(ManagementServerHost.State.Up);
2279-
when(managementServerHostDao.findById(3L)).thenReturn(targetServer);
2296+
when(managementServerHostDao.listUpByIds(targetIds)).thenReturn(List.of(mock(ManagementServerHostVO.class),
2297+
mock(ManagementServerHostVO.class)));
22802298
doNothing().when(extensionsFilesystemManager).validateExtensionFiles(extension, List.of("validFile"));
22812299
doNothing().when(extensionsManager).checkExtensionPathState(extension);
22822300
try (MockedStatic<ManagementServerNode> mocked = Mockito.mockStatic(ManagementServerNode.class)) {

framework/extensions/src/test/java/org/apache/cloudstack/framework/extensions/manager/ExtensionsShareManagerImplTest.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -785,4 +785,4 @@ public void syncExtensionReturnsFailureWhenTargetSyncFails() throws Exception {
785785
assertFalse(result.first());
786786
assertEquals("Sync failed on management server: targetServer", result.second());
787787
}
788-
}
788+
}

0 commit comments

Comments
 (0)