|
26 | 26 | import org.apache.cloudstack.api.response.AccountResponse; |
27 | 27 | import org.apache.cloudstack.api.response.DomainResponse; |
28 | 28 | import org.apache.cloudstack.api.response.TaggedResourceLimitAndCountResponse; |
| 29 | +import org.apache.cloudstack.context.CallContext; |
29 | 30 | import org.apache.cloudstack.framework.config.ConfigKey; |
30 | 31 | import org.apache.cloudstack.reservation.dao.ReservationDao; |
31 | 32 | import org.apache.cloudstack.resourcelimit.Reserver; |
|
57 | 58 | import com.cloud.domain.Domain; |
58 | 59 | import com.cloud.domain.DomainVO; |
59 | 60 | import com.cloud.domain.dao.DomainDao; |
| 61 | +import com.cloud.exception.PermissionDeniedException; |
60 | 62 | import com.cloud.exception.ResourceAllocationException; |
61 | 63 | import com.cloud.offering.DiskOffering; |
62 | 64 | import com.cloud.offering.ServiceOffering; |
|
73 | 75 | import com.cloud.user.AccountManager; |
74 | 76 | import com.cloud.user.AccountVO; |
75 | 77 | import com.cloud.user.ResourceLimitService; |
| 78 | +import com.cloud.user.User; |
76 | 79 | import com.cloud.user.dao.AccountDao; |
77 | 80 | import com.cloud.utils.Pair; |
| 81 | +import com.cloud.utils.db.EntityManager; |
78 | 82 | import com.cloud.vm.VirtualMachine; |
79 | 83 | import com.cloud.vm.VirtualMachineManager; |
80 | 84 | import com.cloud.vm.dao.UserVmDao; |
@@ -123,6 +127,8 @@ public class ResourceLimitManagerImplTest extends TestCase { |
123 | 127 | UserVmDao userVmDao; |
124 | 128 | @Mock |
125 | 129 | SnapshotDataStoreDao snapshotDataStoreDao; |
| 130 | + @Mock |
| 131 | + EntityManager entityManager; |
126 | 132 |
|
127 | 133 | private List<String> hostTags = List.of("htag1", "htag2", "htag3"); |
128 | 134 | private List<String> storageTags = List.of("stag1", "stag2"); |
@@ -1178,4 +1184,46 @@ public void testDecrementVmMemoryResourceCount() { |
1178 | 1184 | Mockito.verify(resourceLimitManager, Mockito.times(1)) |
1179 | 1185 | .decrementResourceCountWithTag(accountId, Resource.ResourceType.memory, tag, Long.valueOf(memory)); |
1180 | 1186 | } |
| 1187 | + |
| 1188 | + private Account mockResourceDomainAdmin(long domainId) { |
| 1189 | + Account resourceDomainAdmin = Mockito.mock(Account.class); |
| 1190 | + Mockito.when(resourceDomainAdmin.getDomainId()).thenReturn(domainId); |
| 1191 | + Mockito.when(resourceDomainAdmin.getType()).thenReturn(Account.Type.RESOURCE_DOMAIN_ADMIN); |
| 1192 | + return resourceDomainAdmin; |
| 1193 | + } |
| 1194 | + |
| 1195 | + @Test |
| 1196 | + public void updateResourceLimitAllowsAResourceDomainAdminToUpdateASubdomain() { |
| 1197 | + long ownDomainId = 5L; |
| 1198 | + long subdomainId = 7L; |
| 1199 | + Domain subdomain = Mockito.mock(Domain.class); |
| 1200 | + Mockito.when(subdomain.getParent()).thenReturn(ownDomainId); |
| 1201 | + Mockito.when(entityManager.findById(Domain.class, subdomainId)).thenReturn(subdomain); |
| 1202 | + DomainVO ownDomain = Mockito.mock(DomainVO.class); |
| 1203 | + Mockito.when(domainDao.findById(ownDomainId)).thenReturn(ownDomain); |
| 1204 | + Mockito.doReturn((long) Resource.RESOURCE_UNLIMITED).when(resourceLimitManager) |
| 1205 | + .findCorrectResourceLimitForDomain(ownDomain, Resource.ResourceType.user_vm, null); |
| 1206 | + |
| 1207 | + CallContext.register(Mockito.mock(User.class), mockResourceDomainAdmin(ownDomainId)); |
| 1208 | + try { |
| 1209 | + resourceLimitManager.updateResourceLimit(null, subdomainId, Resource.ResourceType.user_vm.getOrdinal(), 10L, null); |
| 1210 | + } finally { |
| 1211 | + CallContext.unregister(); |
| 1212 | + } |
| 1213 | + Mockito.verify(resourceLimitDao).persist(Mockito.any(ResourceLimitVO.class)); |
| 1214 | + } |
| 1215 | + |
| 1216 | + @Test |
| 1217 | + public void updateResourceLimitDeniesAResourceDomainAdminOnTheirOwnDomain() { |
| 1218 | + long ownDomainId = 5L; |
| 1219 | + Mockito.when(entityManager.findById(Domain.class, ownDomainId)).thenReturn(Mockito.mock(Domain.class)); |
| 1220 | + |
| 1221 | + CallContext.register(Mockito.mock(User.class), mockResourceDomainAdmin(ownDomainId)); |
| 1222 | + try { |
| 1223 | + Assert.assertThrows(PermissionDeniedException.class, () -> |
| 1224 | + resourceLimitManager.updateResourceLimit(null, ownDomainId, Resource.ResourceType.user_vm.getOrdinal(), 10L, null)); |
| 1225 | + } finally { |
| 1226 | + CallContext.unregister(); |
| 1227 | + } |
| 1228 | + } |
1181 | 1229 | } |
0 commit comments