|
42 | 42 | import com.cloud.user.Account; |
43 | 43 | import com.cloud.user.AccountManager; |
44 | 44 | import com.cloud.user.User; |
| 45 | +import com.cloud.user.UserDataVO; |
| 46 | +import com.cloud.user.dao.UserDataDao; |
45 | 47 | import com.cloud.utils.Pair; |
46 | 48 | import com.cloud.utils.net.NetUtils; |
47 | 49 | import com.cloud.vm.VMInstanceVO; |
48 | 50 | import com.cloud.vm.dao.VMInstanceDao; |
49 | 51 | import org.apache.cloudstack.api.BaseCmd; |
50 | 52 | import org.apache.cloudstack.api.command.user.kubernetes.cluster.AddVirtualMachinesToKubernetesClusterCmd; |
| 53 | +import org.apache.cloudstack.api.command.user.kubernetes.cluster.CreateKubernetesClusterCmd; |
51 | 54 | import org.apache.cloudstack.api.command.user.kubernetes.cluster.RemoveVirtualMachinesFromKubernetesClusterCmd; |
52 | 55 | import org.apache.cloudstack.context.CallContext; |
53 | 56 | import org.apache.cloudstack.framework.config.ConfigKey; |
@@ -103,10 +106,70 @@ public class KubernetesClusterManagerImplTest { |
103 | 106 | @Mock |
104 | 107 | private ServiceOfferingDao serviceOfferingDao; |
105 | 108 |
|
| 109 | + @Mock |
| 110 | + private UserDataDao userDataDao; |
| 111 | + |
106 | 112 | @Spy |
107 | 113 | @InjectMocks |
108 | 114 | KubernetesClusterManagerImpl kubernetesClusterManager; |
109 | 115 |
|
| 116 | + @Test |
| 117 | + public void testValidateCniConfigurationNotSpecified() { |
| 118 | + kubernetesClusterManager.validateCniConfiguration(null); |
| 119 | + |
| 120 | + Mockito.verifyNoInteractions(userDataDao); |
| 121 | + } |
| 122 | + |
| 123 | + @Test |
| 124 | + public void testValidateCniConfigurationAccessible() { |
| 125 | + UserDataVO userData = new UserDataVO(); |
| 126 | + userData.setAccountId(CallContext.current().getCallingAccount().getId()); |
| 127 | + Mockito.when(userDataDao.findById(1L)).thenReturn(userData); |
| 128 | + |
| 129 | + kubernetesClusterManager.validateCniConfiguration(1L); |
| 130 | + |
| 131 | + Mockito.verify(accountManager).checkAccess(CallContext.current().getCallingAccount(), null, false, userData); |
| 132 | + } |
| 133 | + |
| 134 | + @Test |
| 135 | + public void testCreateManagedClusterRejectsInaccessibleCniConfiguration() { |
| 136 | + CreateKubernetesClusterCmd cmd = Mockito.mock(CreateKubernetesClusterCmd.class); |
| 137 | + Mockito.when(cmd.getCniConfigId()).thenReturn(1L); |
| 138 | + UserDataVO userData = new UserDataVO(); |
| 139 | + userData.setAccountId(CallContext.current().getCallingAccount().getId() + 1); |
| 140 | + Mockito.when(userDataDao.findById(1L)).thenReturn(userData); |
| 141 | + PermissionDeniedException denied = new PermissionDeniedException("UserData access denied"); |
| 142 | + Mockito.doThrow(denied).when(accountManager).checkAccess( |
| 143 | + CallContext.current().getCallingAccount(), null, false, userData); |
| 144 | + |
| 145 | + try { |
| 146 | + kubernetesClusterManager.createManagedKubernetesCluster(cmd); |
| 147 | + Assert.fail("Expected UserData access denial"); |
| 148 | + } catch (PermissionDeniedException expected) { |
| 149 | + Assert.assertSame(denied, expected); |
| 150 | + } |
| 151 | + |
| 152 | + Mockito.verifyNoInteractions(kubernetesClusterDao); |
| 153 | + Mockito.verify(cmd, Mockito.never()).getZoneId(); |
| 154 | + Mockito.verify(cmd, Mockito.never()).getCniConfigDetails(); |
| 155 | + } |
| 156 | + |
| 157 | + @Test |
| 158 | + public void testCreateManagedClusterRejectsMissingCniConfiguration() { |
| 159 | + CreateKubernetesClusterCmd cmd = Mockito.mock(CreateKubernetesClusterCmd.class); |
| 160 | + Mockito.when(cmd.getCniConfigId()).thenReturn(1L); |
| 161 | + |
| 162 | + try { |
| 163 | + kubernetesClusterManager.createManagedKubernetesCluster(cmd); |
| 164 | + Assert.fail("Expected missing UserData rejection"); |
| 165 | + } catch (InvalidParameterValueException expected) { |
| 166 | + Assert.assertEquals("Unable to find user data with the specified ID.", expected.getMessage()); |
| 167 | + } |
| 168 | + |
| 169 | + Mockito.verifyNoInteractions(kubernetesClusterDao); |
| 170 | + Mockito.verify(cmd, Mockito.never()).getZoneId(); |
| 171 | + } |
| 172 | + |
110 | 173 | @Test |
111 | 174 | public void testValidateVpcTierAllocated() { |
112 | 175 | Network network = Mockito.mock(Network.class); |
|
0 commit comments