Skip to content

Commit a42bc9f

Browse files
author
GabrielBrascher
committed
Add unit test for UsageManagerImpl.configureUsageManagerServicePid()
1 parent 7a5649e commit a42bc9f

2 files changed

Lines changed: 95 additions & 10 deletions

File tree

usage/src/main/java/com/cloud/usage/UsageManagerImpl.java

Lines changed: 11 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -183,7 +183,7 @@ public class UsageManagerImpl extends ManagerBase implements UsageManager, Runna
183183
private boolean usageSnapshotSelection = false;
184184

185185
/**
186-
* File "/var/run/cloudstack-usage.service.pid" that stores a string corresponding to the cloudstack-usage.service process id (pid).
186+
* File "/var/run/cloudstack-usage.service.pid" that stores a string corresponding to the cloudstack-usage.service process identified (PID).
187187
*/
188188
private static final String CLOUDSTACK_USAGE_SERVER_SERVICE_PID_FILE = "/var/run/cloudstack-usage.service.pid";
189189

@@ -283,20 +283,20 @@ public boolean configure(String name, Map<String, Object> params) throws Configu
283283
throw new ConfigurationException("Unhandled exception configuring UsageManager " + e.toString());
284284
}
285285

286-
configureUsageManagerServicePid();
286+
_pid = retrieveUsageManagerServicePid();
287287
return true;
288288
}
289289

290290
/**
291-
* Sets the '_pid' variable based on the cloudstack-usage.service process id (pid) according to the file /var/run/cloudstack-usage.service.pid. </br>
291+
* Retrieves the cloudstack-usage.service PID according to the file /var/run/cloudstack-usage.service.pid.</br>
292292
* It throws a CloudRuntimeException in the following cases:
293293
* <ul>
294-
* <li>Cannot find the pid file</li>
295-
* <li>Cannot read the pid file</li>
296-
* <li>NumberUtils.toInt returns 0 (zero) if the conversion fails (pid is null or "")</li>
294+
* <li>Cannot find the PID file</li>
295+
* <li>Cannot read the PID file</li>
296+
* <li>NumberUtils.toInt returns 0 (zero) if the conversion fails (PID is null or "")</li>
297297
* </ul>
298298
*/
299-
protected void configureUsageManagerServicePid() {
299+
protected int retrieveUsageManagerServicePid() {
300300
File usageServicePid = new File(CLOUDSTACK_USAGE_SERVER_SERVICE_PID_FILE);
301301
if (!usageServicePid.exists()) {
302302
throw new CloudRuntimeException(String.format("Cannot find file [%s].", CLOUDSTACK_USAGE_SERVER_SERVICE_PID_FILE));
@@ -306,12 +306,13 @@ protected void configureUsageManagerServicePid() {
306306
}
307307
try {
308308
String pidAsString = FileUtils.readFileToString(usageServicePid, Charset.defaultCharset());
309-
String pidAsStringWithoutLineSeparator = pidAsString.replace(System.getProperty("line.separator"), "");
310-
_pid = NumberUtils.toInt(pidAsStringWithoutLineSeparator);
311-
if (_pid == 0) {
309+
String pidAsStringWithoutLineSeparator = pidAsString.trim();
310+
int pid = NumberUtils.toInt(pidAsStringWithoutLineSeparator);
311+
if (pid == 0) {
312312
throw new CloudRuntimeException(
313313
String.format("Failed to retrieve a valid cloudstack-usage.service pid [file:%s, pid at file:%s]", CLOUDSTACK_USAGE_SERVER_SERVICE_PID_FILE, pidAsString));
314314
}
315+
return pid;
315316
} catch (IOException e) {
316317
throw new CloudRuntimeException(e);
317318
}
Lines changed: 84 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,84 @@
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+
// 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.usage;
18+
19+
import java.io.File;
20+
import java.io.IOException;
21+
import java.nio.charset.Charset;
22+
23+
import org.apache.commons.io.FileUtils;
24+
import org.apache.commons.lang3.math.NumberUtils;
25+
import org.junit.Assert;
26+
import org.junit.Test;
27+
import org.junit.runner.RunWith;
28+
import org.mockito.InOrder;
29+
import org.mockito.Mockito;
30+
import org.powermock.api.mockito.PowerMockito;
31+
import org.powermock.core.classloader.annotations.PrepareForTest;
32+
import org.powermock.modules.junit4.PowerMockRunner;
33+
34+
import com.cloud.utils.exception.CloudRuntimeException;
35+
36+
@RunWith(PowerMockRunner.class)
37+
@PrepareForTest({FileUtils.class, UsageManagerImpl.class, NumberUtils.class})
38+
public class UsageManagerImplTest {
39+
40+
private static final String CLOUDSTACK_USAGE_SERVER_SERVICE_PID_FILE = "/var/run/cloudstack-usage.service.pid";
41+
private UsageManagerImpl usageManagerImpl = new UsageManagerImpl();
42+
43+
@Test
44+
public void retrieveUsageManagerServicePidTestCompleteExecutionFlow() throws Exception {
45+
prepareAndVerifyTestRetrieveUsageManagerServicePidTest(true, true, 1, 1234, "1234" + System.getProperty("line.separator"));
46+
}
47+
48+
@Test(expected=CloudRuntimeException.class)
49+
public void retrieveUsageManagerServicePidTestFileDoesNotExist() throws Exception {
50+
prepareAndVerifyTestRetrieveUsageManagerServicePidTest(false, true, 0, 1234, "1234" + System.getProperty("line.separator"));
51+
}
52+
53+
@Test(expected = CloudRuntimeException.class)
54+
public void retrieveUsageManagerServicePidTestCannotReadFile() throws Exception {
55+
prepareAndVerifyTestRetrieveUsageManagerServicePidTest(true, false, 1, 1234, "1234" + System.getProperty("line.separator"));
56+
}
57+
58+
@Test(expected = CloudRuntimeException.class)
59+
public void retrieveUsageManagerServicePidTestPidEqualsToZero() throws Exception {
60+
prepareAndVerifyTestRetrieveUsageManagerServicePidTest(true, true, 1, 0, "abc");
61+
}
62+
63+
private void prepareAndVerifyTestRetrieveUsageManagerServicePidTest(boolean fileExists, boolean canReadFile, int timesCanReadIsExecuted, int expectedPid,
64+
String readedStringFromFile)
65+
throws Exception, IOException {
66+
File usageServicePid = Mockito.mock(File.class);
67+
PowerMockito.whenNew(File.class).withArguments(CLOUDSTACK_USAGE_SERVER_SERVICE_PID_FILE).thenReturn(usageServicePid);
68+
69+
Mockito.when(usageServicePid.exists()).thenReturn(fileExists);
70+
Mockito.when(usageServicePid.canRead()).thenReturn(canReadFile);
71+
72+
PowerMockito.mockStatic(FileUtils.class);
73+
Mockito.when(FileUtils.readFileToString(usageServicePid, Charset.defaultCharset())).thenReturn(readedStringFromFile);
74+
75+
int result = usageManagerImpl.retrieveUsageManagerServicePid();
76+
77+
InOrder inOrder = Mockito.inOrder(usageServicePid);
78+
inOrder.verify(usageServicePid).exists();
79+
inOrder.verify(usageServicePid, Mockito.times(timesCanReadIsExecuted)).canRead();
80+
81+
Assert.assertEquals(expectedPid, result);
82+
}
83+
84+
}

0 commit comments

Comments
 (0)