Skip to content

Commit 05f051a

Browse files
committed
UT for Upgrade42010to42100 and ConfigDepotImpl
1 parent a0aee79 commit 05f051a

File tree

3 files changed

+123
-2
lines changed

3 files changed

+123
-2
lines changed
Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
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.upgrade.dao;
18+
19+
import static org.mockito.Mockito.when;
20+
21+
import java.sql.Connection;
22+
import java.sql.PreparedStatement;
23+
import java.sql.SQLException;
24+
25+
import org.junit.Test;
26+
import org.junit.runner.RunWith;
27+
import org.mockito.Mock;
28+
import org.mockito.MockedStatic;
29+
import org.mockito.Mockito;
30+
import org.mockito.Spy;
31+
import org.mockito.junit.MockitoJUnitRunner;
32+
33+
import com.cloud.utils.db.TransactionLegacy;
34+
35+
@RunWith(MockitoJUnitRunner.class)
36+
public class Upgrade42010to42100Test {
37+
@Spy
38+
Upgrade42010to42100 upgrade;
39+
40+
@Mock
41+
private Connection conn;
42+
43+
@Test
44+
public void testPerformDataMigration() throws SQLException {
45+
try (MockedStatic<DbUpgradeUtils> ignored = Mockito.mockStatic(DbUpgradeUtils.class)) {
46+
DbUpgradeUtils dbUpgradeUtils = Mockito.mock(DbUpgradeUtils.class);
47+
when(dbUpgradeUtils.getTableColumnType(conn, "configuration", "scope")).thenReturn("varchar(255)");
48+
49+
try (MockedStatic<TransactionLegacy> ignored2 = Mockito.mockStatic(TransactionLegacy.class)) {
50+
TransactionLegacy txn = Mockito.mock(TransactionLegacy.class);
51+
when(TransactionLegacy.currentTxn()).thenReturn(txn);
52+
PreparedStatement pstmt = Mockito.mock(PreparedStatement.class);
53+
String sql = "UPDATE configuration\n" +
54+
"SET new_scope =" +
55+
" CASE" +
56+
" WHEN scope = 'Global' THEN 1" +
57+
" WHEN scope = 'Zone' THEN 2" +
58+
" WHEN scope = 'Cluster' THEN 4" +
59+
" WHEN scope = 'StoragePool' THEN 8" +
60+
" WHEN scope = 'ManagementServer' THEN 16" +
61+
" WHEN scope = 'ImageStore' THEN 32" +
62+
" WHEN scope = 'Domain' THEN 64" +
63+
" WHEN scope = 'Account' THEN 128" +
64+
" ELSE 0" +
65+
" END WHERE scope IS NOT NULL;";
66+
when(txn.prepareAutoCloseStatement(sql)).thenReturn(pstmt);
67+
upgrade.performDataMigration(conn);
68+
69+
Mockito.verify(pstmt, Mockito.times(1)).executeUpdate();
70+
}
71+
}
72+
}
73+
}

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

Lines changed: 50 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525

2626
import org.apache.cloudstack.framework.config.ConfigKey;
2727
import org.apache.cloudstack.framework.config.Configurable;
28+
import org.apache.cloudstack.framework.config.ScopedConfigStorage;
2829
import org.apache.cloudstack.framework.config.dao.ConfigurationDao;
2930
import org.apache.cloudstack.framework.config.dao.ConfigurationSubGroupDao;
3031
import org.junit.Assert;
@@ -36,6 +37,8 @@
3637
import org.mockito.junit.MockitoJUnitRunner;
3738
import org.springframework.test.util.ReflectionTestUtils;
3839

40+
import com.cloud.utils.Pair;
41+
3942
@RunWith(MockitoJUnitRunner.class)
4043
public class ConfigDepotImplTest {
4144

@@ -114,7 +117,7 @@ public void testGetConfigStringValueAfterExpiry() {
114117
}
115118

116119
@Test
117-
public void testPopulateConfiguration() {
120+
public void testPopulateConfigurationNewVO() {
118121
ConfigKey StorageDisableThreshold = new ConfigKey<>(ConfigKey.CATEGORY_ALERT, Double.class, "pool.storage.capacity.disablethreshold", "0.85",
119122
"Percentage (as a value between 0 and 1) of storage utilization above which allocators will disable using the pool for low storage available.",
120123
true, List.of(ConfigKey.Scope.StoragePool, ConfigKey.Scope.Zone));
@@ -138,4 +141,50 @@ public ConfigKey<?>[] getConfigKeys() {
138141
configDepotImpl._scopeLevelConfigsMap.get(ConfigKey.Scope.StoragePool).iterator().next().key());
139142
Assert.assertEquals(0, configDepotImpl._scopeLevelConfigsMap.get(ConfigKey.Scope.Cluster).size());
140143
}
144+
145+
@Test
146+
public void testPopulateConfiguration() {
147+
ConfigKey StorageDisableThreshold = new ConfigKey<>(ConfigKey.CATEGORY_ALERT, Double.class, "pool.storage.capacity.disablethreshold", "0.85",
148+
"Percentage (as a value between 0 and 1) of storage utilization above which allocators will disable using the pool for low storage available.",
149+
true, List.of(ConfigKey.Scope.StoragePool, ConfigKey.Scope.Zone));
150+
Configurable configurable = new Configurable() {
151+
@Override
152+
public String getConfigComponentName() {
153+
return "test";
154+
}
155+
156+
@Override
157+
public ConfigKey<?>[] getConfigKeys() {
158+
return new ConfigKey<?>[]{StorageDisableThreshold};
159+
}
160+
};
161+
configDepotImpl.setConfigurables(List.of(configurable));
162+
163+
ConfigurationVO configurationVO = new ConfigurationVO(StorageDisableThreshold.category(), "DEFAULT", "component",
164+
StorageDisableThreshold.key(), StorageDisableThreshold.defaultValue(), StorageDisableThreshold.description(),
165+
StorageDisableThreshold.displayText(), StorageDisableThreshold.parent(), 1L, 10L);
166+
Mockito.when(_configDao.findById("pool.storage.capacity.disablethreshold")).thenReturn(configurationVO);
167+
configDepotImpl.populateConfigurations();
168+
169+
Mockito.verify(_configDao, Mockito.times(1)).persist(configurationVO);
170+
}
171+
172+
@Test
173+
public void getParentScopeWithValidScope() {
174+
ConfigKey.Scope scope = ConfigKey.Scope.Cluster;
175+
ScopedConfigStorage scopedConfigStorage = Mockito.mock(ScopedConfigStorage.class);
176+
Long id = 1L;
177+
ConfigKey.Scope parentScope = ConfigKey.Scope.Zone;
178+
Long parentId = 2L;
179+
180+
Mockito.when(scopedConfigStorage.getScope()).thenReturn(scope);
181+
Mockito.when(scopedConfigStorage.getParentScope(id)).thenReturn(new Pair<>(parentScope, parentId));
182+
183+
configDepotImpl.setScopedStorages(Collections.singletonList(scopedConfigStorage));
184+
Pair<ConfigKey.Scope, Long> result = configDepotImpl.getParentScope(scope, id);
185+
186+
Assert.assertNotNull(result);
187+
Assert.assertEquals(parentScope, result.first());
188+
Assert.assertEquals(parentId, result.second());
189+
}
141190
}

server/src/test/java/com/cloud/storage/StorageManagerImplTest.java

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -780,7 +780,6 @@ private Long testCheckPoolforSpaceForResizeSetup(StoragePoolVO pool, Long alloca
780780

781781
Mockito.when(pool.getId()).thenReturn(poolId);
782782
Mockito.when(pool.getCapacityBytes()).thenReturn(capacityBytes);
783-
Mockito.when(pool.getDataCenterId()).thenReturn(zoneId);
784783
Mockito.when(storagePoolDao.findById(poolId)).thenReturn(pool);
785784
Mockito.when(pool.getPoolType()).thenReturn(Storage.StoragePoolType.NetworkFilesystem);
786785

0 commit comments

Comments
 (0)