Skip to content

Commit 24d4cfb

Browse files
committed
Unit Tests for multi scope config
1 parent 6e7fe6e commit 24d4cfb

File tree

6 files changed

+151
-4
lines changed

6 files changed

+151
-4
lines changed

framework/config/src/test/java/org/apache/cloudstack/framework/config/ConfigKeyTest.java

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,8 @@
1616
// under the License.
1717
package org.apache.cloudstack.framework.config;
1818

19+
import java.util.List;
20+
1921
import org.junit.Assert;
2022
import org.junit.Test;
2123

@@ -47,4 +49,31 @@ public void testIsSameKeyAsThrowingCloudRuntimeException() {
4749
ConfigKey key = new ConfigKey("hond", Boolean.class, "naam", "truus", "thrown name", false);
4850
Assert.assertFalse("zero and 0L should be considered the same address", key.isSameKeyAs(0L));
4951
}
52+
53+
@Test
54+
public void testDecode() {
55+
ConfigKey key = new ConfigKey("testcategoey", Boolean.class, "test", "true", "test descriptuin", false, List.of(Scope.Zone, Scope.StoragePool));
56+
int bitmask = key.getScopeBitmask();
57+
List<Scope> scopes = ConfigKey.Scope.decode(bitmask);
58+
Assert.assertEquals(bitmask, ConfigKey.Scope.getBitmask(scopes.toArray(new Scope[0])));
59+
for (Scope scope : scopes) {
60+
Assert.assertTrue(scope == Scope.Zone || scope == Scope.StoragePool);
61+
}
62+
}
63+
64+
@Test
65+
public void testDecodeAsCsv() {
66+
ConfigKey key = new ConfigKey("testcategoey", Boolean.class, "test", "true", "test descriptuin", false, List.of(Scope.Zone, Scope.StoragePool));
67+
int bitmask = key.getScopeBitmask();
68+
String scopes = ConfigKey.Scope.decodeAsCsv(bitmask);
69+
Assert.assertTrue("Zone, StoragePool".equals(scopes));
70+
}
71+
72+
@Test
73+
public void testGetDescendants() {
74+
List<Scope> descendants = ConfigKey.Scope.getAllDescendants(Scope.Zone.name());
75+
for (Scope descendant : descendants) {
76+
Assert.assertTrue(descendant == Scope.Cluster || descendant == Scope.StoragePool || descendant == Scope.ImageStore);
77+
}
78+
}
5079
}

framework/config/src/test/java/org/apache/cloudstack/framework/config/impl/ConfigDepotImplTest.java

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,10 +20,13 @@
2020

2121
import java.util.Collections;
2222
import java.util.HashSet;
23+
import java.util.List;
2324
import java.util.Set;
2425

2526
import org.apache.cloudstack.framework.config.ConfigKey;
27+
import org.apache.cloudstack.framework.config.Configurable;
2628
import org.apache.cloudstack.framework.config.dao.ConfigurationDao;
29+
import org.apache.cloudstack.framework.config.dao.ConfigurationSubGroupDao;
2730
import org.junit.Assert;
2831
import org.junit.Test;
2932
import org.junit.runner.RunWith;
@@ -38,6 +41,8 @@ public class ConfigDepotImplTest {
3841

3942
@Mock
4043
ConfigurationDao _configDao;
44+
@Mock
45+
ConfigurationSubGroupDao configSubGroupDao;
4146

4247
@InjectMocks
4348
private ConfigDepotImpl configDepotImpl = new ConfigDepotImpl();
@@ -107,4 +112,30 @@ public void testGetConfigStringValueAfterExpiry() {
107112
runTestGetConfigStringValueExpiry(((ConfigDepotImpl.CONFIG_CACHE_EXPIRE_SECONDS) + 5) * 1000,
108113
2);
109114
}
115+
116+
@Test
117+
public void testPopulateConfiguration() {
118+
ConfigKey StorageDisableThreshold = new ConfigKey<>(ConfigKey.CATEGORY_ALERT, Double.class, "pool.storage.capacity.disablethreshold", "0.85",
119+
"Percentage (as a value between 0 and 1) of storage utilization above which allocators will disable using the pool for low storage available.",
120+
true, List.of(ConfigKey.Scope.StoragePool, ConfigKey.Scope.Zone));
121+
Configurable configurable = new Configurable() {
122+
@Override
123+
public String getConfigComponentName() {
124+
return "test";
125+
}
126+
127+
@Override
128+
public ConfigKey<?>[] getConfigKeys() {
129+
return new ConfigKey<?>[] { StorageDisableThreshold };
130+
}
131+
};
132+
configDepotImpl.setConfigurables(List.of(configurable));
133+
configDepotImpl.populateConfigurations();
134+
135+
Assert.assertEquals("pool.storage.capacity.disablethreshold",
136+
configDepotImpl._scopeLevelConfigsMap.get(ConfigKey.Scope.Zone).iterator().next().key());
137+
Assert.assertEquals("pool.storage.capacity.disablethreshold",
138+
configDepotImpl._scopeLevelConfigsMap.get(ConfigKey.Scope.StoragePool).iterator().next().key());
139+
Assert.assertEquals(0, configDepotImpl._scopeLevelConfigsMap.get(ConfigKey.Scope.Cluster).size());
140+
}
110141
}

plugins/metrics/src/main/java/org/apache/cloudstack/metrics/MetricsServiceImpl.java

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,6 @@
6161
import org.apache.cloudstack.api.response.ZoneResponse;
6262
import org.apache.cloudstack.cluster.ClusterDrsAlgorithm;
6363
import org.apache.cloudstack.context.CallContext;
64-
import org.apache.cloudstack.framework.config.ConfigKey;
6564
import org.apache.cloudstack.management.ManagementServerHost.State;
6665
import org.apache.cloudstack.response.ClusterMetricsResponse;
6766
import org.apache.cloudstack.response.DbMetricsResponse;

server/src/main/java/com/cloud/server/ManagementServerImpl.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -892,7 +892,7 @@ public class ManagementServerImpl extends ManagerBase implements ManagementServe
892892
@Inject
893893
protected UserVmDao _userVmDao;
894894
@Inject
895-
private ConfigurationDao _configDao;
895+
protected ConfigurationDao _configDao;
896896
@Inject
897897
private ConfigurationGroupDao _configGroupDao;
898898
@Inject
@@ -904,7 +904,7 @@ public class ManagementServerImpl extends ManagerBase implements ManagementServe
904904
@Inject
905905
private DiskOfferingDao _diskOfferingDao;
906906
@Inject
907-
private DomainDao _domainDao;
907+
protected DomainDao _domainDao;
908908
@Inject
909909
private AccountDao _accountDao;
910910
@Inject
@@ -960,7 +960,7 @@ public class ManagementServerImpl extends ManagerBase implements ManagementServe
960960
@Inject
961961
private HostTagsDao _hostTagsDao;
962962
@Inject
963-
private ConfigDepot _configDepot;
963+
protected ConfigDepot _configDepot;
964964
@Inject
965965
private UserVmManager _userVmMgr;
966966
@Inject

server/src/test/java/com/cloud/configuration/ConfigurationManagerImplTest.java

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,21 +46,27 @@
4646
import com.cloud.storage.dao.VolumeDao;
4747
import com.cloud.user.Account;
4848
import com.cloud.user.User;
49+
import com.cloud.utils.Pair;
4950
import com.cloud.utils.db.EntityManager;
5051
import com.cloud.utils.db.SearchCriteria;
5152
import com.cloud.utils.net.NetUtils;
5253
import com.cloud.vm.dao.VMInstanceDao;
5354
import org.apache.cloudstack.annotation.dao.AnnotationDao;
55+
import org.apache.cloudstack.api.command.admin.config.ResetCfgCmd;
5456
import org.apache.cloudstack.api.command.admin.network.CreateNetworkOfferingCmd;
5557
import org.apache.cloudstack.api.command.admin.offering.UpdateDiskOfferingCmd;
5658
import org.apache.cloudstack.api.command.admin.zone.DeleteZoneCmd;
59+
import org.apache.cloudstack.config.Configuration;
5760
import org.apache.cloudstack.engine.subsystem.api.storage.ZoneScope;
5861
import org.apache.cloudstack.framework.config.ConfigDepot;
5962
import org.apache.cloudstack.framework.config.ConfigKey;
6063
import org.apache.cloudstack.framework.config.dao.ConfigurationDao;
6164
import org.apache.cloudstack.framework.config.impl.ConfigurationVO;
6265
import org.apache.cloudstack.storage.datastore.db.ImageStoreDao;
6366
import org.apache.cloudstack.resourcedetail.DiskOfferingDetailVO;
67+
import org.apache.cloudstack.storage.datastore.db.PrimaryDataStoreDao;
68+
import org.apache.cloudstack.storage.datastore.db.StoragePoolDetailsDao;
69+
import org.apache.cloudstack.storage.datastore.db.StoragePoolVO;
6470
import org.apache.cloudstack.vm.UnmanagedVMsManager;
6571
import org.junit.Assert;
6672
import org.junit.Before;
@@ -159,6 +165,10 @@ public class ConfigurationManagerImplTest {
159165
NetworkService networkService;
160166
@Mock
161167
NetworkModel networkModel;
168+
@Mock
169+
PrimaryDataStoreDao storagePoolDao;
170+
@Mock
171+
StoragePoolDetailsDao storagePoolDetailsDao;
162172

163173
DeleteZoneCmd deleteZoneCmd;
164174
CreateNetworkOfferingCmd createNetworkOfferingCmd;
@@ -852,4 +862,26 @@ public void shouldValidateConfigRangeTestValueIsNotNullAndConfigHasRangeReturnTr
852862
boolean result = configurationManagerImplSpy.shouldValidateConfigRange(Config.ConsoleProxySessionMax.name(), "test", Config.ConsoleProxyUrlDomain);
853863
Assert.assertTrue(result);
854864
}
865+
866+
@Test
867+
public void testResetConfigurations() {
868+
Long poolId = 1L;
869+
ResetCfgCmd cmd = Mockito.mock(ResetCfgCmd.class);
870+
Mockito.when(cmd.getCfgName()).thenReturn("pool.storage.capacity.disablethreshold");
871+
Mockito.when(cmd.getStoragepoolId()).thenReturn(poolId);
872+
Mockito.when(cmd.getZoneId()).thenReturn(null);
873+
Mockito.when(cmd.getClusterId()).thenReturn(null);
874+
Mockito.when(cmd.getAccountId()).thenReturn(null);
875+
Mockito.when(cmd.getDomainId()).thenReturn(null);
876+
Mockito.when(cmd.getImageStoreId()).thenReturn(null);
877+
878+
ConfigurationVO cfg = new ConfigurationVO("Advanced", "DEFAULT", "test", "pool.storage.capacity.disablethreshold", null, "description");
879+
cfg.setScope(10);
880+
cfg.setDefaultValue(".85");
881+
Mockito.when(configDao.findByName("pool.storage.capacity.disablethreshold")).thenReturn(cfg);
882+
Mockito.when(storagePoolDao.findById(poolId)).thenReturn(Mockito.mock(StoragePoolVO.class));
883+
884+
Pair<Configuration, String> result = configurationManagerImplSpy.resetConfiguration(cmd);
885+
Assert.assertEquals(".85", result.second());
886+
}
855887
}

server/src/test/java/com/cloud/server/ManagementServerImplTest.java

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
package com.cloud.server;
1818

1919
import com.cloud.dc.Vlan.VlanType;
20+
import com.cloud.domain.dao.DomainDao;
2021
import com.cloud.exception.InvalidParameterValueException;
2122
import com.cloud.host.DetailVO;
2223
import com.cloud.host.Host;
@@ -48,15 +49,20 @@
4849
import org.apache.cloudstack.annotation.dao.AnnotationDao;
4950
import org.apache.cloudstack.api.ApiConstants;
5051
import org.apache.cloudstack.api.BaseCmd;
52+
import org.apache.cloudstack.api.command.admin.config.ListCfgsByCmd;
5153
import org.apache.cloudstack.api.command.user.address.ListPublicIpAddressesCmd;
5254
import org.apache.cloudstack.api.command.user.ssh.RegisterSSHKeyPairCmd;
5355
import org.apache.cloudstack.api.command.user.userdata.DeleteUserDataCmd;
5456
import org.apache.cloudstack.api.command.user.userdata.ListUserDataCmd;
5557
import org.apache.cloudstack.api.command.user.userdata.RegisterUserDataCmd;
58+
import org.apache.cloudstack.config.Configuration;
5659
import org.apache.cloudstack.context.CallContext;
5760
import org.apache.cloudstack.engine.subsystem.api.storage.PrimaryDataStore;
5861
import org.apache.cloudstack.engine.subsystem.api.storage.PrimaryDataStoreDriver;
62+
import org.apache.cloudstack.framework.config.ConfigDepot;
5963
import org.apache.cloudstack.framework.config.ConfigKey;
64+
import org.apache.cloudstack.framework.config.dao.ConfigurationDao;
65+
import org.apache.cloudstack.framework.config.impl.ConfigurationVO;
6066
import org.apache.cloudstack.userdata.UserDataManager;
6167
import org.junit.After;
6268
import org.junit.Assert;
@@ -131,6 +137,16 @@ public class ManagementServerImplTest {
131137

132138
@Mock
133139
HostDetailsDao hostDetailsDao;
140+
141+
@Mock
142+
ConfigurationDao configDao;
143+
144+
@Mock
145+
ConfigDepot configDepot;
146+
147+
@Mock
148+
DomainDao domainDao;
149+
134150
private AutoCloseable closeable;
135151

136152
@Before
@@ -145,6 +161,9 @@ public void setup() throws IllegalAccessException, NoSuchFieldException {
145161
spy._UserVmDetailsDao = userVmDetailsDao;
146162
spy._detailsDao = hostDetailsDao;
147163
spy.userDataManager = userDataManager;
164+
spy._configDao = configDao;
165+
spy._configDepot = configDepot;
166+
spy._domainDao = domainDao;
148167
}
149168

150169
@After
@@ -684,4 +703,41 @@ public void testZoneWideVolumeRequiresStorageMotionDriverDependent() {
684703
Mockito.when(driver.zoneWideVolumesAvailableWithoutClusterMotion()).thenReturn(false);
685704
Assert.assertTrue(spy.zoneWideVolumeRequiresStorageMotion(dataStore, host1, host2));
686705
}
706+
707+
@Test(expected = InvalidParameterValueException.class)
708+
public void testSearchForConfigurationsMultipleIds() {
709+
ListCfgsByCmd cmd = Mockito.mock(ListCfgsByCmd.class);
710+
Mockito.when(cmd.getConfigName()).thenReturn("pool.storage.capacity.disablethreshold");
711+
Mockito.when(cmd.getZoneId()).thenReturn(1L);
712+
Mockito.when(cmd.getStoragepoolId()).thenReturn(2L);
713+
spy.searchForConfigurations(cmd);
714+
}
715+
716+
@Test
717+
public void testSearchForConfigurations() {
718+
Long poolId = 1L;
719+
ListCfgsByCmd cmd = Mockito.mock(ListCfgsByCmd.class);
720+
Mockito.when(cmd.getConfigName()).thenReturn("pool.storage.capacity.disablethreshold");
721+
Mockito.when(cmd.getStoragepoolId()).thenReturn(poolId);
722+
Mockito.when(cmd.getZoneId()).thenReturn(null);
723+
Mockito.when(cmd.getClusterId()).thenReturn(null);
724+
Mockito.when(cmd.getAccountId()).thenReturn(null);
725+
Mockito.when(cmd.getDomainId()).thenReturn(null);
726+
Mockito.when(cmd.getImageStoreId()).thenReturn(null);
727+
728+
SearchCriteria<ConfigurationVO> sc = Mockito.mock(SearchCriteria.class);
729+
Mockito.when(configDao.createSearchCriteria()).thenReturn(sc);
730+
ConfigurationVO cfg = new ConfigurationVO("Advanced", "DEFAULT", "test", "pool.storage.capacity.disablethreshold", null, "description");
731+
Mockito.when(configDao.searchAndCount(any(), any())).thenReturn(new Pair<>(List.of(cfg), 1));
732+
Mockito.when(configDao.findByName("pool.storage.capacity.disablethreshold")).thenReturn(cfg);
733+
734+
ConfigKey storageDisableThreshold = new ConfigKey<>(ConfigKey.CATEGORY_ALERT, Double.class, "pool.storage.capacity.disablethreshold", "0.85",
735+
"Percentage (as a value between 0 and 1) of storage utilization above which allocators will disable using the pool for low storage available.",
736+
true, List.of(ConfigKey.Scope.StoragePool, ConfigKey.Scope.Zone));
737+
when(configDepot.get("pool.storage.capacity.disablethreshold")).thenReturn(storageDisableThreshold);
738+
739+
Pair<List<? extends Configuration>, Integer> result = spy.searchForConfigurations(cmd);
740+
741+
Assert.assertEquals("0.85", result.first().get(0).getValue());
742+
}
687743
}

0 commit comments

Comments
 (0)