Skip to content

Commit db97e96

Browse files
committed
Fix unit test
1 parent bcf7b6d commit db97e96

File tree

1 file changed

+20
-1
lines changed

1 file changed

+20
-1
lines changed

plugins/integrations/kubernetes-service/src/test/java/com/cloud/kubernetes/cluster/actionworkers/KubernetesClusterScaleWorkerTest.java

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,10 +18,14 @@
1818

1919
import com.cloud.kubernetes.cluster.KubernetesCluster;
2020
import com.cloud.kubernetes.cluster.KubernetesClusterManagerImpl;
21+
import com.cloud.kubernetes.cluster.KubernetesClusterVmMapVO;
22+
import com.cloud.kubernetes.cluster.dao.KubernetesClusterVmMapDao;
2123
import com.cloud.offering.ServiceOffering;
2224
import com.cloud.service.ServiceOfferingVO;
2325
import com.cloud.service.dao.ServiceOfferingDao;
2426
import com.cloud.utils.Pair;
27+
import com.cloud.vm.UserVmVO;
28+
import com.cloud.vm.dao.UserVmDao;
2529
import org.junit.Assert;
2630
import org.junit.Before;
2731
import org.junit.Test;
@@ -30,6 +34,8 @@
3034
import org.mockito.Mockito;
3135
import org.mockito.junit.MockitoJUnitRunner;
3236

37+
import java.util.List;
38+
3339
import static com.cloud.kubernetes.cluster.KubernetesServiceHelper.KubernetesClusterNodeType.DEFAULT;
3440
import static com.cloud.kubernetes.cluster.KubernetesServiceHelper.KubernetesClusterNodeType.CONTROL;
3541

@@ -42,6 +48,10 @@ public class KubernetesClusterScaleWorkerTest {
4248
private KubernetesClusterManagerImpl clusterManager;
4349
@Mock
4450
private ServiceOfferingDao serviceOfferingDao;
51+
@Mock
52+
private KubernetesClusterVmMapDao kubernetesClusterVmMapDao;
53+
@Mock
54+
private UserVmDao userVmDao;
4555

4656
private KubernetesClusterScaleWorker worker;
4757

@@ -51,6 +61,8 @@ public class KubernetesClusterScaleWorkerTest {
5161
public void setUp() {
5262
worker = new KubernetesClusterScaleWorker(kubernetesCluster, clusterManager);
5363
worker.serviceOfferingDao = serviceOfferingDao;
64+
worker.kubernetesClusterVmMapDao = kubernetesClusterVmMapDao;
65+
worker.userVmDao = userVmDao;
5466
}
5567

5668
@Test
@@ -78,6 +90,8 @@ public void testCalculateNewClusterCountAndCapacityAllNodesScaleSize() {
7890
@Test
7991
public void testCalculateNewClusterCountAndCapacityNodeTypeScaleControlOffering() {
8092
long controlNodes = 2L;
93+
long kubernetesClusterId = 10L;
94+
Mockito.when(kubernetesCluster.getId()).thenReturn(kubernetesClusterId);
8195
Mockito.when(kubernetesCluster.getControlNodeCount()).thenReturn(controlNodes);
8296

8397
ServiceOfferingVO existingOffering = Mockito.mock(ServiceOfferingVO.class);
@@ -90,7 +104,6 @@ public void testCalculateNewClusterCountAndCapacityNodeTypeScaleControlOffering(
90104
Mockito.when(kubernetesCluster.getCores()).thenReturn(remainingClusterCpu + (controlNodes * existingCores));
91105
Mockito.when(kubernetesCluster.getMemory()).thenReturn(remainingClusterMemory + (controlNodes * existingMemory));
92106

93-
Mockito.when(kubernetesCluster.getControlServiceOfferingId()).thenReturn(1L);
94107
Mockito.when(serviceOfferingDao.findById(1L)).thenReturn(existingOffering);
95108

96109
ServiceOfferingVO newOffering = Mockito.mock(ServiceOfferingVO.class);
@@ -99,6 +112,12 @@ public void testCalculateNewClusterCountAndCapacityNodeTypeScaleControlOffering(
99112
Mockito.when(newOffering.getCpu()).thenReturn(newCores);
100113
Mockito.when(newOffering.getRamSize()).thenReturn(newMemory);
101114

115+
KubernetesClusterVmMapVO controlNodeVM1 = Mockito.mock(KubernetesClusterVmMapVO.class);
116+
Mockito.when(controlNodeVM1.getVmId()).thenReturn(10L);
117+
UserVmVO userVmVO = Mockito.mock(UserVmVO.class);
118+
Mockito.when(userVmVO.getServiceOfferingId()).thenReturn(defaultOfferingId);
119+
Mockito.when(userVmDao.findById(10L)).thenReturn(userVmVO);
120+
Mockito.when(kubernetesClusterVmMapDao.listByClusterIdAndVmType(kubernetesClusterId, CONTROL)).thenReturn(List.of(controlNodeVM1));
102121
Pair<Long, Long> newClusterCapacity = worker.calculateNewClusterCountAndCapacity(null, CONTROL, newOffering);
103122

104123
long expectedCores = remainingClusterCpu + (controlNodes * newCores);

0 commit comments

Comments
 (0)