|
| 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 | +package com.cloud.api; |
| 18 | + |
| 19 | +import static org.mockito.Mockito.mock; |
| 20 | +import static org.mockito.Mockito.when; |
| 21 | + |
| 22 | +import org.junit.After; |
| 23 | +import org.junit.Assert; |
| 24 | +import org.junit.Before; |
| 25 | +import org.junit.Test; |
| 26 | +import org.junit.runner.RunWith; |
| 27 | +import org.mockito.junit.MockitoJUnitRunner; |
| 28 | + |
| 29 | +import com.cloud.configuration.Resource; |
| 30 | +import com.cloud.configuration.Resource.ResourceType; |
| 31 | +import com.cloud.domain.Domain; |
| 32 | +import com.cloud.domain.DomainVO; |
| 33 | +import com.cloud.domain.dao.DomainDao; |
| 34 | +import com.cloud.user.ResourceLimitService; |
| 35 | + |
| 36 | +@RunWith(MockitoJUnitRunner.Silent.class) |
| 37 | +public class ApiDBUtilsTest { |
| 38 | + |
| 39 | + private static final long DOMAIN_ID = 2L; |
| 40 | + |
| 41 | + private DomainDao originalDomainDao; |
| 42 | + private ResourceLimitService originalResourceLimitMgr; |
| 43 | + private DomainVO domain; |
| 44 | + private ResourceLimitService resourceLimitMgr; |
| 45 | + |
| 46 | + @Before |
| 47 | + public void setUp() { |
| 48 | + originalDomainDao = ApiDBUtils.s_domainDao; |
| 49 | + originalResourceLimitMgr = ApiDBUtils.s_resourceLimitMgr; |
| 50 | + |
| 51 | + domain = mock(DomainVO.class); |
| 52 | + DomainDao domainDao = mock(DomainDao.class); |
| 53 | + when(domainDao.findById(DOMAIN_ID)).thenReturn(domain); |
| 54 | + resourceLimitMgr = mock(ResourceLimitService.class); |
| 55 | + ApiDBUtils.s_domainDao = domainDao; |
| 56 | + ApiDBUtils.s_resourceLimitMgr = resourceLimitMgr; |
| 57 | + } |
| 58 | + |
| 59 | + @After |
| 60 | + public void tearDown() { |
| 61 | + ApiDBUtils.s_domainDao = originalDomainDao; |
| 62 | + ApiDBUtils.s_resourceLimitMgr = originalResourceLimitMgr; |
| 63 | + } |
| 64 | + |
| 65 | + @Test |
| 66 | + public void testDomainWithoutLimitGetsTheEnforcedLimit() { |
| 67 | + // max.domain.buckets is enforced when the domain has no bucket limit of its own |
| 68 | + when(resourceLimitMgr.findCorrectResourceLimitForDomain(domain, ResourceType.bucket, null)).thenReturn(20L); |
| 69 | + when(resourceLimitMgr.findDefaultResourceLimitForDomain(ResourceType.bucket)).thenReturn((long) Resource.RESOURCE_UNLIMITED); |
| 70 | + |
| 71 | + Assert.assertEquals(20L, ApiDBUtils.findCorrectResourceLimitForDomain(null, ResourceType.bucket, DOMAIN_ID)); |
| 72 | + } |
| 73 | + |
| 74 | + @Test |
| 75 | + public void testDomainLimitIsUsedWhenSet() { |
| 76 | + Assert.assertEquals(7L, ApiDBUtils.findCorrectResourceLimitForDomain(7L, ResourceType.bucket, DOMAIN_ID)); |
| 77 | + } |
| 78 | + |
| 79 | + @Test |
| 80 | + public void testRootDomainIsUnlimited() { |
| 81 | + Assert.assertEquals(Resource.RESOURCE_UNLIMITED, |
| 82 | + ApiDBUtils.findCorrectResourceLimitForDomain(null, ResourceType.bucket, Domain.ROOT_DOMAIN)); |
| 83 | + } |
| 84 | +} |
0 commit comments