Skip to content

Commit eb798d3

Browse files
committed
Test for AgentShell.loadProperties
- stream closed after properties load (with commons io) - test added Signed-off-by: Laszlo Hornyak <laszlo.hornyak@gmail.com>
1 parent 8d67e15 commit eb798d3

3 files changed

Lines changed: 20 additions & 3 deletions

File tree

agent/pom.xml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,10 @@
3636
<artifactId>cloud-utils</artifactId>
3737
<version>${project.version}</version>
3838
</dependency>
39+
<dependency>
40+
<groupId>commons-io</groupId>
41+
<artifactId>commons-io</artifactId>
42+
</dependency>
3943
<dependency>
4044
<groupId>commons-daemon</groupId>
4145
<artifactId>commons-daemon</artifactId>

agent/src/com/cloud/agent/AgentShell.java

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
import java.io.FileInputStream;
2121
import java.io.FileNotFoundException;
2222
import java.io.IOException;
23+
import java.io.InputStream;
2324
import java.lang.reflect.Constructor;
2425
import java.lang.reflect.InvocationTargetException;
2526
import java.util.ArrayList;
@@ -36,6 +37,7 @@
3637
import org.apache.commons.daemon.Daemon;
3738
import org.apache.commons.daemon.DaemonContext;
3839
import org.apache.commons.daemon.DaemonInitException;
40+
import org.apache.commons.io.IOUtils;
3941
import org.apache.commons.lang.math.NumberUtils;
4042
import org.apache.log4j.Logger;
4143
import org.apache.log4j.xml.DOMConfigurator;
@@ -165,22 +167,26 @@ public void setPersistentProperty(String prefix, String name, String value) {
165167
_storage.persist(name, value);
166168
}
167169

168-
private void loadProperties() throws ConfigurationException {
170+
void loadProperties() throws ConfigurationException {
169171
final File file = PropertiesUtil.findConfigFile("agent.properties");
170172
if (file == null) {
171173
throw new ConfigurationException("Unable to find agent.properties.");
172174
}
173175

174176
s_logger.info("agent.properties found at " + file.getAbsolutePath());
175177

178+
InputStream propertiesStream = null;
176179
try {
177-
_properties.load(new FileInputStream(file));
180+
propertiesStream = new FileInputStream(file);
181+
_properties.load(propertiesStream);
178182
} catch (final FileNotFoundException ex) {
179183
throw new CloudRuntimeException("Cannot find the file: "
180184
+ file.getAbsolutePath(), ex);
181185
} catch (final IOException ex) {
182186
throw new CloudRuntimeException("IOException in reading "
183187
+ file.getAbsolutePath(), ex);
188+
} finally {
189+
IOUtils.closeQuietly(propertiesStream);
184190
}
185191
}
186192

@@ -304,7 +310,7 @@ public void init(String[] args) throws ConfigurationException {
304310
// For KVM agent, do it specially here
305311

306312
File file = new File("/etc/cloudstack/agent/log4j-cloud.xml");
307-
if(file == null || !file.exists()) {
313+
if(!file.exists()) {
308314
file = PropertiesUtil.findConfigFile("log4j-cloud.xml");
309315
}
310316
DOMConfigurator.configureAndWatch(file.getAbsolutePath());

agent/test/com/cloud/agent/AgentShellTest.java

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,4 +22,11 @@ public void parseCommand() throws ConfigurationException {
2222
Assert.assertEquals("pod1", shell.getPod());
2323
Assert.assertEquals("zone1", shell.getZone());
2424
}
25+
@Test
26+
public void loadProperties() throws ConfigurationException {
27+
AgentShell shell = new AgentShell();
28+
shell.loadProperties();
29+
Assert.assertNotNull(shell.getProperties());
30+
Assert.assertFalse(shell.getProperties().entrySet().isEmpty());
31+
}
2532
}

0 commit comments

Comments
 (0)