Skip to content

Commit 94417df

Browse files
committed
add tests, fix comment
Signed-off-by: Abhishek Kumar <[email protected]>
1 parent 41b3658 commit 94417df

File tree

4 files changed

+123
-5
lines changed

4 files changed

+123
-5
lines changed

client/bindir/cloud-setup-management.in

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -188,7 +188,6 @@ if __name__ == '__main__':
188188
print("CloudStack Management Server setup is Done!")
189189
print("Please ensure ports 8080, 8250, 8443, and 9090 are opened and not firewalled for the management server and not in use by other processes on this host.")
190190
except (CloudRuntimeException, CloudInternalException) as e:
191-
192191
print(e)
193192
print("Try to restore your system:")
194193
try:

plugins/hypervisors/external/src/main/java/org/apache/cloudstack/hypervisor/external/provisioner/ExternalPathPayloadProvisioner.java

Lines changed: 26 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020

2121
import java.io.BufferedReader;
2222
import java.io.File;
23+
import java.io.FileInputStream;
2324
import java.io.IOException;
2425
import java.io.InputStreamReader;
2526
import java.nio.file.Files;
@@ -50,7 +51,6 @@
5051
import org.apache.cloudstack.api.ApiConstants;
5152
import org.apache.cloudstack.extension.Extension;
5253
import org.apache.cloudstack.framework.extensions.manager.ExtensionsManager;
53-
import org.apache.cloudstack.utils.server.ServerPropertiesUtil;
5454
import org.apache.cloudstack.utils.security.DigestHelper;
5555
import org.apache.commons.collections.CollectionUtils;
5656
import org.apache.commons.collections.MapUtils;
@@ -80,6 +80,7 @@
8080
import com.cloud.serializer.GsonHelper;
8181
import com.cloud.utils.FileUtil;
8282
import com.cloud.utils.Pair;
83+
import com.cloud.utils.PropertiesUtil;
8384
import com.cloud.utils.StringUtils;
8485
import com.cloud.utils.component.ManagerBase;
8586
import com.cloud.utils.component.PluggableService;
@@ -226,6 +227,29 @@ protected String getSanitizedJsonStringForLog(String json) {
226227
return json.replaceAll("(\"password\"\\s*:\\s*\")([^\"]*)(\")", "$1****$3");
227228
}
228229

230+
private String getServerProperty(String name) {
231+
Properties props = propertiesRef.get();
232+
if (props == null) {
233+
File propsFile = PropertiesUtil.findConfigFile(PROPERTIES_FILE);
234+
if (propsFile == null) {
235+
logger.error("{} file not found", PROPERTIES_FILE);
236+
return null;
237+
}
238+
Properties tempProps = new Properties();
239+
try (FileInputStream is = new FileInputStream(propsFile)) {
240+
tempProps.load(is);
241+
} catch (IOException e) {
242+
logger.error("Error loading {}: {}", PROPERTIES_FILE, e.getMessage(), e);
243+
return null;
244+
}
245+
if (!propertiesRef.compareAndSet(null, tempProps)) {
246+
tempProps = propertiesRef.get();
247+
}
248+
props = tempProps;
249+
}
250+
return props.getProperty(name);
251+
}
252+
229253
@Override
230254
public boolean configure(String name, Map<String, Object> params) throws ConfigurationException {
231255
super.configure(name, params);
@@ -237,7 +261,7 @@ public boolean configure(String name, Map<String, Object> params) throws Configu
237261
}
238262

239263
private void initializeExtensionDirectories() {
240-
String deploymentMode = ServerPropertiesUtil.getProperty(EXTENSIONS_DEPLOYMENT_MODE_NAME);
264+
String deploymentMode = getServerProperty(EXTENSIONS_DEPLOYMENT_MODE_NAME);
241265
if ("developer".equals(deploymentMode)) {
242266
extensionsDirectory = EXTENSIONS_DIRECTORY_DEV;
243267
extensionsDataDirectory = EXTENSIONS_DATA_DIRECTORY_DEV;

utils/src/main/java/org/apache/cloudstack/utils/server/ServerPropertiesUtil.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,8 +30,8 @@
3030

3131
public class ServerPropertiesUtil {
3232
private static final Logger logger = LoggerFactory.getLogger(ServerPropertiesUtil.class);
33-
private static final String PROPERTIES_FILE = "server.properties";
34-
private static final AtomicReference<Properties> propertiesRef = new AtomicReference<>();
33+
protected static final String PROPERTIES_FILE = "server.properties";
34+
protected static final AtomicReference<Properties> propertiesRef = new AtomicReference<>();
3535

3636
public static String getProperty(String name) {
3737
Properties props = propertiesRef.get();
Lines changed: 95 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,95 @@
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+
18+
package org.apache.cloudstack.utils.server;
19+
20+
import static org.junit.Assert.assertEquals;
21+
import static org.junit.Assert.assertNull;
22+
import static org.mockito.Mockito.mock;
23+
import static org.mockito.Mockito.mockStatic;
24+
import static org.mockito.Mockito.when;
25+
26+
import java.io.File;
27+
import java.io.IOException;
28+
import java.nio.file.Files;
29+
import java.util.Properties;
30+
31+
import org.junit.After;
32+
import org.junit.Test;
33+
import org.junit.runner.RunWith;
34+
import org.mockito.MockedStatic;
35+
import org.mockito.junit.MockitoJUnitRunner;
36+
37+
import com.cloud.utils.PropertiesUtil;
38+
39+
@RunWith(MockitoJUnitRunner.class)
40+
public class ServerPropertiesUtilTest {
41+
42+
@After
43+
public void clearCache() {
44+
ServerPropertiesUtil.propertiesRef.set(null);
45+
}
46+
47+
@Test
48+
public void returnsPropertyValueWhenPropertiesAreLoaded() {
49+
Properties mockProperties = mock(Properties.class);
50+
when(mockProperties.getProperty("key")).thenReturn("value");
51+
ServerPropertiesUtil.propertiesRef.set(mockProperties);
52+
String result = ServerPropertiesUtil.getProperty("key");
53+
assertEquals("value", result);
54+
}
55+
56+
@Test
57+
public void returnsNullWhenPropertyDoesNotExist() {
58+
Properties mockProperties = mock(Properties.class);
59+
ServerPropertiesUtil.propertiesRef.set(mockProperties);
60+
assertNull(ServerPropertiesUtil.getProperty("nonexistentKey"));
61+
}
62+
63+
@Test
64+
public void loadsPropertiesFromFileWhenNotCached() throws Exception {
65+
File tempFile = Files.createTempFile("server", ".properties").toFile();
66+
tempFile.deleteOnExit();
67+
Files.writeString(tempFile.toPath(), "key=value\n");
68+
try (MockedStatic<PropertiesUtil> mocked = mockStatic(PropertiesUtil.class)) {
69+
mocked.when(() -> PropertiesUtil.findConfigFile(ServerPropertiesUtil.PROPERTIES_FILE))
70+
.thenReturn(tempFile);
71+
assertEquals("value", ServerPropertiesUtil.getProperty("key"));
72+
}
73+
}
74+
75+
@Test
76+
public void returnsNullWhenPropertiesFileNotFound() {
77+
try (MockedStatic<PropertiesUtil> mocked = mockStatic(PropertiesUtil.class)) {
78+
mocked.when(() -> PropertiesUtil.findConfigFile(ServerPropertiesUtil.PROPERTIES_FILE))
79+
.thenReturn(null);
80+
assertNull(ServerPropertiesUtil.getProperty("key"));
81+
}
82+
}
83+
84+
@Test
85+
public void returnsNullWhenIOExceptionOccurs() throws IOException {
86+
File tempFile = Files.createTempFile("bad", ".properties").toFile();
87+
tempFile.deleteOnExit();
88+
Files.writeString(tempFile.toPath(), "\u0000\u0000\u0000");
89+
try (MockedStatic<PropertiesUtil> mocked = mockStatic(PropertiesUtil.class)) {
90+
mocked.when(() -> PropertiesUtil.findConfigFile(ServerPropertiesUtil.PROPERTIES_FILE))
91+
.thenReturn(tempFile);
92+
assertNull(ServerPropertiesUtil.getProperty("key"));
93+
}
94+
}
95+
}

0 commit comments

Comments
 (0)