|
26 | 26 | import com.cloud.exception.InvalidParameterValueException; |
27 | 27 | import com.cloud.exception.PermissionDeniedException; |
28 | 28 | import com.cloud.kubernetes.cluster.actionworkers.KubernetesClusterActionWorker; |
| 29 | +import com.cloud.kubernetes.cluster.dao.KubernetesClusterAffinityGroupMapDao; |
29 | 30 | import com.cloud.kubernetes.cluster.dao.KubernetesClusterDao; |
30 | 31 | import com.cloud.kubernetes.cluster.dao.KubernetesClusterVmMapDao; |
31 | 32 | import com.cloud.kubernetes.version.KubernetesSupportedVersion; |
|
46 | 47 | import com.cloud.utils.net.NetUtils; |
47 | 48 | import com.cloud.vm.VMInstanceVO; |
48 | 49 | import com.cloud.vm.dao.VMInstanceDao; |
| 50 | +import org.apache.cloudstack.affinity.AffinityGroupVO; |
| 51 | +import org.apache.cloudstack.affinity.dao.AffinityGroupDao; |
49 | 52 | import org.apache.cloudstack.api.BaseCmd; |
50 | 53 | import org.apache.cloudstack.api.command.user.kubernetes.cluster.AddVirtualMachinesToKubernetesClusterCmd; |
51 | 54 | import org.apache.cloudstack.api.command.user.kubernetes.cluster.RemoveVirtualMachinesFromKubernetesClusterCmd; |
| 55 | +import org.apache.cloudstack.api.response.KubernetesClusterResponse; |
52 | 56 | import org.apache.cloudstack.context.CallContext; |
53 | 57 | import org.apache.cloudstack.framework.config.ConfigKey; |
54 | 58 | import org.apache.commons.collections.MapUtils; |
@@ -103,6 +107,12 @@ public class KubernetesClusterManagerImplTest { |
103 | 107 | @Mock |
104 | 108 | private ServiceOfferingDao serviceOfferingDao; |
105 | 109 |
|
| 110 | + @Mock |
| 111 | + private KubernetesClusterAffinityGroupMapDao kubernetesClusterAffinityGroupMapDao; |
| 112 | + |
| 113 | + @Mock |
| 114 | + private AffinityGroupDao affinityGroupDao; |
| 115 | + |
106 | 116 | @Spy |
107 | 117 | @InjectMocks |
108 | 118 | KubernetesClusterManagerImpl kubernetesClusterManager; |
@@ -441,4 +451,128 @@ public void testGetCksClusterPreferredArchSameArch() { |
441 | 451 | String cksClusterPreferredArch = kubernetesClusterManager.getCksClusterPreferredArch(systemVMArch, cksIso); |
442 | 452 | Assert.assertEquals(CPU.CPUArch.amd64.getType(), cksClusterPreferredArch); |
443 | 453 | } |
| 454 | + |
| 455 | + @Test |
| 456 | + public void testSetAffinityGroupResponseForNodeTypeControl() { |
| 457 | + KubernetesClusterResponse response = new KubernetesClusterResponse(); |
| 458 | + long clusterId = 1L; |
| 459 | + |
| 460 | + AffinityGroupVO ag1 = Mockito.mock(AffinityGroupVO.class); |
| 461 | + AffinityGroupVO ag2 = Mockito.mock(AffinityGroupVO.class); |
| 462 | + Mockito.when(ag1.getUuid()).thenReturn("uuid-1"); |
| 463 | + Mockito.when(ag1.getName()).thenReturn("affinity-group-1"); |
| 464 | + Mockito.when(ag2.getUuid()).thenReturn("uuid-2"); |
| 465 | + Mockito.when(ag2.getName()).thenReturn("affinity-group-2"); |
| 466 | + |
| 467 | + Mockito.when(kubernetesClusterAffinityGroupMapDao.listAffinityGroupIdsByClusterIdAndNodeType(clusterId, CONTROL.name())) |
| 468 | + .thenReturn(Arrays.asList(1L, 2L)); |
| 469 | + Mockito.when(affinityGroupDao.findById(1L)).thenReturn(ag1); |
| 470 | + Mockito.when(affinityGroupDao.findById(2L)).thenReturn(ag2); |
| 471 | + |
| 472 | + kubernetesClusterManager.setAffinityGroupResponseForNodeType(response, clusterId, CONTROL.name()); |
| 473 | + |
| 474 | + Mockito.verify(kubernetesClusterAffinityGroupMapDao).listAffinityGroupIdsByClusterIdAndNodeType(clusterId, CONTROL.name()); |
| 475 | + Mockito.verify(affinityGroupDao).findById(1L); |
| 476 | + Mockito.verify(affinityGroupDao).findById(2L); |
| 477 | + } |
| 478 | + |
| 479 | + @Test |
| 480 | + public void testSetAffinityGroupResponseForNodeTypeWorker() { |
| 481 | + KubernetesClusterResponse response = new KubernetesClusterResponse(); |
| 482 | + long clusterId = 1L; |
| 483 | + |
| 484 | + AffinityGroupVO ag = Mockito.mock(AffinityGroupVO.class); |
| 485 | + Mockito.when(ag.getUuid()).thenReturn("worker-uuid"); |
| 486 | + Mockito.when(ag.getName()).thenReturn("worker-affinity"); |
| 487 | + |
| 488 | + Mockito.when(kubernetesClusterAffinityGroupMapDao.listAffinityGroupIdsByClusterIdAndNodeType(clusterId, WORKER.name())) |
| 489 | + .thenReturn(Arrays.asList(10L)); |
| 490 | + Mockito.when(affinityGroupDao.findById(10L)).thenReturn(ag); |
| 491 | + |
| 492 | + kubernetesClusterManager.setAffinityGroupResponseForNodeType(response, clusterId, WORKER.name()); |
| 493 | + |
| 494 | + Mockito.verify(kubernetesClusterAffinityGroupMapDao).listAffinityGroupIdsByClusterIdAndNodeType(clusterId, WORKER.name()); |
| 495 | + Mockito.verify(affinityGroupDao).findById(10L); |
| 496 | + } |
| 497 | + |
| 498 | + @Test |
| 499 | + public void testSetAffinityGroupResponseForNodeTypeEtcd() { |
| 500 | + KubernetesClusterResponse response = new KubernetesClusterResponse(); |
| 501 | + long clusterId = 1L; |
| 502 | + |
| 503 | + AffinityGroupVO ag = Mockito.mock(AffinityGroupVO.class); |
| 504 | + Mockito.when(ag.getUuid()).thenReturn("etcd-uuid"); |
| 505 | + Mockito.when(ag.getName()).thenReturn("etcd-affinity"); |
| 506 | + |
| 507 | + Mockito.when(kubernetesClusterAffinityGroupMapDao.listAffinityGroupIdsByClusterIdAndNodeType(clusterId, ETCD.name())) |
| 508 | + .thenReturn(Arrays.asList(20L)); |
| 509 | + Mockito.when(affinityGroupDao.findById(20L)).thenReturn(ag); |
| 510 | + |
| 511 | + kubernetesClusterManager.setAffinityGroupResponseForNodeType(response, clusterId, ETCD.name()); |
| 512 | + |
| 513 | + Mockito.verify(kubernetesClusterAffinityGroupMapDao).listAffinityGroupIdsByClusterIdAndNodeType(clusterId, ETCD.name()); |
| 514 | + Mockito.verify(affinityGroupDao).findById(20L); |
| 515 | + } |
| 516 | + |
| 517 | + @Test |
| 518 | + public void testSetAffinityGroupResponseForNodeTypeEmptyList() { |
| 519 | + KubernetesClusterResponse response = new KubernetesClusterResponse(); |
| 520 | + long clusterId = 1L; |
| 521 | + |
| 522 | + Mockito.when(kubernetesClusterAffinityGroupMapDao.listAffinityGroupIdsByClusterIdAndNodeType(clusterId, CONTROL.name())) |
| 523 | + .thenReturn(Collections.emptyList()); |
| 524 | + |
| 525 | + kubernetesClusterManager.setAffinityGroupResponseForNodeType(response, clusterId, CONTROL.name()); |
| 526 | + |
| 527 | + Mockito.verify(affinityGroupDao, Mockito.never()).findById(Mockito.anyLong()); |
| 528 | + } |
| 529 | + |
| 530 | + @Test |
| 531 | + public void testSetAffinityGroupResponseForNodeTypeNullList() { |
| 532 | + KubernetesClusterResponse response = new KubernetesClusterResponse(); |
| 533 | + long clusterId = 1L; |
| 534 | + |
| 535 | + Mockito.when(kubernetesClusterAffinityGroupMapDao.listAffinityGroupIdsByClusterIdAndNodeType(clusterId, ETCD.name())) |
| 536 | + .thenReturn(null); |
| 537 | + |
| 538 | + kubernetesClusterManager.setAffinityGroupResponseForNodeType(response, clusterId, ETCD.name()); |
| 539 | + |
| 540 | + Mockito.verify(affinityGroupDao, Mockito.never()).findById(Mockito.anyLong()); |
| 541 | + } |
| 542 | + |
| 543 | + @Test |
| 544 | + public void testSetAffinityGroupResponseForNodeTypeNullAffinityGroup() { |
| 545 | + KubernetesClusterResponse response = new KubernetesClusterResponse(); |
| 546 | + long clusterId = 1L; |
| 547 | + |
| 548 | + AffinityGroupVO ag1 = Mockito.mock(AffinityGroupVO.class); |
| 549 | + Mockito.when(ag1.getUuid()).thenReturn("uuid-1"); |
| 550 | + Mockito.when(ag1.getName()).thenReturn("affinity-group-1"); |
| 551 | + |
| 552 | + Mockito.when(kubernetesClusterAffinityGroupMapDao.listAffinityGroupIdsByClusterIdAndNodeType(clusterId, CONTROL.name())) |
| 553 | + .thenReturn(Arrays.asList(1L, 2L)); |
| 554 | + Mockito.when(affinityGroupDao.findById(1L)).thenReturn(ag1); |
| 555 | + Mockito.when(affinityGroupDao.findById(2L)).thenReturn(null); |
| 556 | + |
| 557 | + kubernetesClusterManager.setAffinityGroupResponseForNodeType(response, clusterId, CONTROL.name()); |
| 558 | + |
| 559 | + Mockito.verify(affinityGroupDao).findById(1L); |
| 560 | + Mockito.verify(affinityGroupDao).findById(2L); |
| 561 | + } |
| 562 | + |
| 563 | + @Test |
| 564 | + public void testSetNodeTypeAffinityGroupResponse() { |
| 565 | + KubernetesClusterResponse response = new KubernetesClusterResponse(); |
| 566 | + long clusterId = 1L; |
| 567 | + |
| 568 | + Mockito.when(kubernetesClusterAffinityGroupMapDao.listAffinityGroupIdsByClusterIdAndNodeType(Mockito.eq(clusterId), Mockito.anyString())) |
| 569 | + .thenReturn(Collections.emptyList()); |
| 570 | + |
| 571 | + kubernetesClusterManager.setNodeTypeAffinityGroupResponse(response, clusterId); |
| 572 | + |
| 573 | + Mockito.verify(kubernetesClusterAffinityGroupMapDao).listAffinityGroupIdsByClusterIdAndNodeType(clusterId, CONTROL.name()); |
| 574 | + Mockito.verify(kubernetesClusterAffinityGroupMapDao).listAffinityGroupIdsByClusterIdAndNodeType(clusterId, WORKER.name()); |
| 575 | + Mockito.verify(kubernetesClusterAffinityGroupMapDao).listAffinityGroupIdsByClusterIdAndNodeType(clusterId, ETCD.name()); |
| 576 | + } |
| 577 | + |
444 | 578 | } |
0 commit comments