Skip to content

Commit 957939b

Browse files
RedfishClient tests & remove unused test class & checkstyle changes
1 parent 0aa8051 commit 957939b

File tree

7 files changed

+72
-86
lines changed

7 files changed

+72
-86
lines changed

engine/schema/src/main/java/org/apache/cloudstack/outofbandmanagement/dao/OutOfBandManagementDao.java

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -17,16 +17,16 @@
1717

1818
package org.apache.cloudstack.outofbandmanagement.dao;
1919

20-
import java.util.List;
20+
import com.cloud.utils.db.GenericDao;
21+
import com.cloud.utils.fsm.StateDao;
2122

2223
import org.apache.cloudstack.outofbandmanagement.OutOfBandManagement;
2324
import org.apache.cloudstack.outofbandmanagement.OutOfBandManagementVO;
2425

25-
import com.cloud.utils.db.GenericDao;
26-
import com.cloud.utils.fsm.StateDao;
26+
import java.util.List;
2727

28-
public interface OutOfBandManagementDao
29-
extends GenericDao<OutOfBandManagementVO, Long>, StateDao<OutOfBandManagement.PowerState, OutOfBandManagement.PowerState.Event, OutOfBandManagement> {
28+
public interface OutOfBandManagementDao extends GenericDao<OutOfBandManagementVO, Long>,
29+
StateDao<OutOfBandManagement.PowerState, OutOfBandManagement.PowerState.Event, OutOfBandManagement> {
3030
OutOfBandManagement findByHost(long hostId);
3131
OutOfBandManagementVO findByHostAddress(String address);
3232
List<OutOfBandManagementVO> findAllByManagementServer(long serverId);

plugins/outofbandmanagement-drivers/redfish/src/main/java/org/apache/cloudstack/outofbandmanagement/driver/redfish/RedfishOutOfBandManagementDriver.java

Lines changed: 5 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -42,16 +42,13 @@ public class RedfishOutOfBandManagementDriver extends AdapterBase implements Out
4242

4343
@Inject
4444
private OutOfBandManagementDao outOfBandManagementDao;
45-
46-
// private ExecutorService redfishExecutor = Executors.newFixedThreadPool(OutOfBandManagementService.SyncThreadPoolSize.value(), new NamedThreadFactory("RedfishDriver"));
4745
private RedfishWrapper redfishWrapper = new RedfishWrapper();
4846

49-
//TODO for now they are set to ConfigKey.Scope.Global, need to review it
5047
public static final ConfigKey<Boolean> IGNORE_SSL_CERTIFICATE = new ConfigKey<Boolean>("Advanced", Boolean.class, "redfish.ignore.ssl", "false",
51-
"Default value is true, ensuring that the client requests validate the certificate when using SSL. If set to true the redfish client will ignore SSL certificate validation when sending requests to a Redfish server.",
48+
"Default value is false, ensuring that the client requests validate the certificate when using SSL. If set to true the redfish client will ignore SSL certificate validation when sending requests to a Redfish server.",
5249
true, ConfigKey.Scope.Global);
5350

54-
public static final ConfigKey<Boolean> USE_HTTPS = new ConfigKey<Boolean>("Advanced", Boolean.class, "redfish.use.https", "false",
51+
public static final ConfigKey<Boolean> USE_HTTPS = new ConfigKey<Boolean>("Advanced", Boolean.class, "redfish.use.https", "true",
5552
"Use HTTPS/SSL for all connections.", true, ConfigKey.Scope.Global);
5653

5754
private static final String HTTP_STATUS_OK = String.valueOf(HttpStatus.SC_OK);
@@ -66,8 +63,6 @@ public OutOfBandManagementDriverResponse execute(OutOfBandManagementDriverComman
6663
} else {
6764
throw new CloudRuntimeException(String.format("Operation [%s] not supported by the Redfish out-of-band management driver", cmd.getClass().getSimpleName()));
6865
}
69-
70-
// TODO verify auth failure validation
7166
return response;
7267
}
7368

@@ -76,32 +71,29 @@ public OutOfBandManagementDriverResponse execute(OutOfBandManagementDriverComman
7671
*/
7772
private OutOfBandManagementDriverResponse execute(final OutOfBandManagementDriverPowerCommand cmd) {
7873
ImmutableMap<OutOfBandManagement.Option, String> outOfBandOptions = cmd.getOptions();
79-
8074
String username = outOfBandOptions.get(OutOfBandManagement.Option.USERNAME);
8175
String password = outOfBandOptions.get(OutOfBandManagement.Option.PASSWORD);
76+
String hostAddress = outOfBandOptions.get(OutOfBandManagement.Option.ADDRESS);
8277
RedfishClient redfishClient = new RedfishClient(username, password, USE_HTTPS.value(), IGNORE_SSL_CERTIFICATE.value());
8378

84-
String hostAddress = outOfBandOptions.get(OutOfBandManagement.Option.ADDRESS);
8579
RedfishClient.RedfishPowerState powerState = null;
86-
8780
if (cmd.getPowerOperation() == OutOfBandManagement.PowerOperation.STATUS) {
8881
powerState = redfishClient.getSystemPowerState(hostAddress);
8982
} else {
9083
RedfishResetCmd redfishCmd = redfishWrapper.parsePowerCommand(cmd.getPowerOperation());
9184
redfishClient.executeComputerSystemReset(hostAddress, redfishCmd);
9285
}
93-
OutOfBandManagementDriverResponse response = new OutOfBandManagementDriverResponse(HTTP_STATUS_OK, null, true);
9486

87+
OutOfBandManagementDriverResponse response = new OutOfBandManagementDriverResponse(HTTP_STATUS_OK, null, true);
9588
if (powerState != null) {
9689
OutOfBandManagement.PowerState oobPowerState = redfishWrapper.parseRedfishPowerStateToOutOfBand(powerState);
9790
response.setPowerState(oobPowerState);
9891
}
99-
10092
return response;
10193
}
10294

10395
/**
104-
* Change password command (OutOfBandManagementDriverChangePasswordCommand)
96+
* Executes the password change command (OutOfBandManagementDriverChangePasswordCommand)
10597
*/
10698
private OutOfBandManagementDriverResponse execute(final OutOfBandManagementDriverChangePasswordCommand cmd) {
10799
cmd.getNewPassword();

plugins/outofbandmanagement-drivers/redfish/src/test/java/org/apache/cloudstack/outofbandmanagement/driver/redfish/RedfishOutOfBandManagementDriverTest.java

Lines changed: 0 additions & 30 deletions
This file was deleted.

plugins/pom.xml

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
<?xml version="1.0" encoding="UTF-8"?>
21
<!--
32
Licensed to the Apache Software Foundation (ASF) under one
43
or more contributor license agreements. See the NOTICE file
@@ -16,7 +15,8 @@
1615
KIND, either express or implied. See the License for the
1716
specific language governing permissions and limitations
1817
under the License.
19-
--><project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
18+
-->
19+
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
2020
<modelVersion>4.0.0</modelVersion>
2121
<artifactId>cloudstack-plugins</artifactId>
2222
<name>Apache CloudStack Plugin POM</name>
@@ -129,7 +129,7 @@
129129
<module>user-authenticators/plain-text</module>
130130
<module>user-authenticators/saml2</module>
131131
<module>user-authenticators/sha256salted</module>
132-
</modules>
132+
</modules>
133133
<dependencies>
134134
<dependency>
135135
<groupId>org.apache.cloudstack</groupId>

utils/pom.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@
3232
<groupId>org.apache.cloudstack</groupId>
3333
<artifactId>cloud-framework-managed-context</artifactId>
3434
<version>${project.version}</version>
35-
</dependency>
35+
</dependency>
3636
<dependency>
3737
<groupId>org.apache.cloudstack</groupId>
3838
<artifactId>cloud-framework-ca</artifactId>

utils/src/main/java/org/apache/cloudstack/utils/redfish/RedfishClient.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -271,7 +271,7 @@ public String getSystemId(String hostAddress) {
271271
/**
272272
* Processes the response of request GET System ID as a JSON object.
273273
*/
274-
private String processGetSystemIdResponse(CloseableHttpResponse response) {
274+
protected String processGetSystemIdResponse(CloseableHttpResponse response) {
275275
InputStream in;
276276
String jsonString;
277277
try {
@@ -315,7 +315,7 @@ public RedfishPowerState getSystemPowerState(String hostAddress) {
315315
/**
316316
* Process 'ComputerSystem' GET request response and return as a RedfishPowerState
317317
*/
318-
private RedfishPowerState processGetSystemRequestResponse(CloseableHttpResponse response) {
318+
protected RedfishPowerState processGetSystemRequestResponse(CloseableHttpResponse response) {
319319
InputStream in;
320320
String jsonString = null;
321321
try {

utils/src/test/java/org/apache/cloudstack/utils/redfish/RedfishClientTest.java

Lines changed: 56 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -33,108 +33,132 @@ public class RedfishClientTest {
3333
private static final String USERNAME = "user";
3434
private static final String PASSWORD = "password";
3535
private static final String oobAddress = "oob.host.address";
36-
private static final String systemID = "SystemID.1";
36+
private static final String systemId = "SystemID.1";
3737
private final static String COMPUTER_SYSTEM_RESET_URL_PATH = "/Actions/ComputerSystem.Reset";
3838

39-
// RedfishClient redfishClientIgnoreSSL = new RedfishClient(USERNAME, PASSWORD, true, false);
40-
RedfishClient redfishClientUseSSL = new RedfishClient(USERNAME, PASSWORD, true, true);
4139
RedfishClient redfishClientspy = Mockito.spy(new RedfishClient(USERNAME, PASSWORD, true, true));
4240

4341
@Test(expected = RedfishException.class)
4442
public void validateAddressAndPrepareForUrlTestExpect() {
45-
redfishClientUseSSL.validateAddressAndPrepareForUrl("1:1:2:3:1");
46-
redfishClientUseSSL.validateAddressAndPrepareForUrl("1");
47-
redfishClientUseSSL.validateAddressAndPrepareForUrl("hostname");
48-
redfishClientUseSSL.validateAddressAndPrepareForUrl(oobAddress);
43+
redfishClientspy.validateAddressAndPrepareForUrl("1:1:2:3:1");
44+
redfishClientspy.validateAddressAndPrepareForUrl("1");
45+
redfishClientspy.validateAddressAndPrepareForUrl("hostname");
46+
redfishClientspy.validateAddressAndPrepareForUrl(oobAddress);
4947
}
5048

5149
@Test
5250
public void validateAddressAndPrepareForUrlTestDomainName() {
53-
String result = redfishClientUseSSL.validateAddressAndPrepareForUrl(oobAddress);
51+
String result = redfishClientspy.validateAddressAndPrepareForUrl(oobAddress);
5452
Assert.assertEquals(oobAddress, result);
5553
}
5654

5755
@Test
5856
public void validateAddressAndPrepareForUrlTestIpv4() {
5957
String ipv4 = "192.168.0.123";
60-
String result = redfishClientUseSSL.validateAddressAndPrepareForUrl(ipv4);
58+
String result = redfishClientspy.validateAddressAndPrepareForUrl(ipv4);
6159
Assert.assertEquals(ipv4, result);
6260
}
6361

6462
@Test
6563
public void validateAddressAndPrepareForUrlTestIpv6() {
6664
String ipv6 = "100::ffff:ffff:ffff:ffff";
6765
String expected = "["+ipv6+"]";
68-
String result = redfishClientUseSSL.validateAddressAndPrepareForUrl(ipv6);
66+
String result = redfishClientspy.validateAddressAndPrepareForUrl(ipv6);
6967
Assert.assertEquals(expected, result);
7068
}
7169

7270
@Test
7371
public void buildRequestUrlTestHttpsGetSystemId() {
7472
RedfishClient redfishclient = new RedfishClient(USERNAME, PASSWORD, true, false);
75-
String result = redfishclient.buildRequestUrl(oobAddress, RedfishClient.RedfishCmdType.GetSystemId, systemID);
76-
String expected = String.format("https://%s/redfish/v1/Systems/",oobAddress,systemID);
73+
String result = redfishclient.buildRequestUrl(oobAddress, RedfishClient.RedfishCmdType.GetSystemId, systemId);
74+
String expected = String.format("https://%s/redfish/v1/Systems/",oobAddress, systemId);
7775
Assert.assertEquals(expected, result);
7876
}
7977

8078
@Test
8179
public void buildRequestUrlTestGetSystemId() {
8280
RedfishClient redfishclient = new RedfishClient(USERNAME, PASSWORD, false, false);
83-
String result = redfishclient.buildRequestUrl(oobAddress, RedfishClient.RedfishCmdType.GetSystemId, systemID);
84-
String expected = String.format("http://%s/redfish/v1/Systems/",oobAddress,systemID);
81+
String result = redfishclient.buildRequestUrl(oobAddress, RedfishClient.RedfishCmdType.GetSystemId, systemId);
82+
String expected = String.format("http://%s/redfish/v1/Systems/",oobAddress, systemId);
8583
Assert.assertEquals(expected, result);
8684
}
8785

8886
@Test
8987
public void buildRequestUrlTestHttpsComputerSystemReset() {
9088
RedfishClient redfishclient = new RedfishClient(USERNAME, PASSWORD, true, false);
91-
String result = redfishclient.buildRequestUrl(oobAddress, RedfishClient.RedfishCmdType.ComputerSystemReset, systemID);
92-
String expected = String.format("https://%s/redfish/v1/Systems/%s%s",oobAddress,systemID,COMPUTER_SYSTEM_RESET_URL_PATH);
89+
String result = redfishclient.buildRequestUrl(oobAddress, RedfishClient.RedfishCmdType.ComputerSystemReset, systemId);
90+
String expected = String.format("https://%s/redfish/v1/Systems/%s%s",oobAddress, systemId,COMPUTER_SYSTEM_RESET_URL_PATH);
9391
Assert.assertEquals(expected, result);
9492
}
9593

9694
@Test
9795
public void buildRequestUrlTestComputerSystemReset() {
9896
RedfishClient redfishclient = new RedfishClient(USERNAME, PASSWORD, false, false);
99-
String result = redfishclient.buildRequestUrl(oobAddress, RedfishClient.RedfishCmdType.ComputerSystemReset, systemID);
100-
String expected = String.format("http://%s/redfish/v1/Systems/%s%s",oobAddress,systemID,COMPUTER_SYSTEM_RESET_URL_PATH);
97+
String result = redfishclient.buildRequestUrl(oobAddress, RedfishClient.RedfishCmdType.ComputerSystemReset, systemId);
98+
String expected = String.format("http://%s/redfish/v1/Systems/%s%s",oobAddress, systemId,COMPUTER_SYSTEM_RESET_URL_PATH);
10199
Assert.assertEquals(expected, result);
102100
}
103101

104102
@Test
105103
public void buildRequestUrlTestHttpsGetPowerState() {
106104
RedfishClient redfishclient = new RedfishClient(USERNAME, PASSWORD, true, false);
107-
String result = redfishclient.buildRequestUrl(oobAddress, RedfishClient.RedfishCmdType.GetPowerState, systemID);
108-
String expected = String.format("https://%s/redfish/v1/Systems/%s",oobAddress,systemID);
105+
String result = redfishclient.buildRequestUrl(oobAddress, RedfishClient.RedfishCmdType.GetPowerState, systemId);
106+
String expected = String.format("https://%s/redfish/v1/Systems/%s",oobAddress, systemId);
109107
Assert.assertEquals(expected, result);
110108
}
111109

112110
@Test
113111
public void buildRequestUrlTestGetPowerState() {
114112
RedfishClient redfishclient = new RedfishClient(USERNAME, PASSWORD, false, false);
115-
String result = redfishclient.buildRequestUrl(oobAddress, RedfishClient.RedfishCmdType.GetPowerState, systemID);
116-
String expected = String.format("http://%s/redfish/v1/Systems/%s",oobAddress,systemID);
113+
String result = redfishclient.buildRequestUrl(oobAddress, RedfishClient.RedfishCmdType.GetPowerState, systemId);
114+
String expected = String.format("http://%s/redfish/v1/Systems/%s",oobAddress, systemId);
117115
Assert.assertEquals(expected, result);
118116
}
119117

120-
// @Test
118+
@Test
121119
public void getSystemPowerStateTest() {
122-
// Preparing the test
123-
Mockito.doReturn(systemID).when(redfishClientspy).getSystemId(Mockito.anyString());
124-
// Mockito.when(redfishClientspy.getSystemId(Mockito.anyString())).thenReturn(systemID);
120+
Mockito.doReturn(systemId).when(redfishClientspy).getSystemId(Mockito.anyString());
121+
mockResponse(HttpStatus.SC_OK);
122+
RedfishClient.RedfishPowerState expectedState = RedfishClient.RedfishPowerState.On;
123+
Mockito.doReturn(expectedState).when(redfishClientspy).processGetSystemRequestResponse(Mockito.any(CloseableHttpResponse.class));
125124

126-
StatusLine statusLine = Mockito.mock(StatusLine.class);
127-
Mockito.doReturn(HttpStatus.SC_OK).when(statusLine).getStatusCode();
125+
RedfishClient.RedfishPowerState result = redfishClientspy.getSystemPowerState(oobAddress);
128126

127+
Assert.assertEquals(expectedState, result);
128+
}
129+
130+
@Test(expected = RedfishException.class)
131+
public void getSystemPowerStateTestHttpStatusNotOk() {
132+
Mockito.doReturn(systemId).when(redfishClientspy).getSystemId(Mockito.anyString());
133+
mockResponse(HttpStatus.SC_BAD_REQUEST);
134+
redfishClientspy.getSystemPowerState(oobAddress);
135+
}
136+
137+
private CloseableHttpResponse mockResponse(int httpStatusCode) {
138+
StatusLine statusLine = Mockito.mock(StatusLine.class);
139+
Mockito.doReturn(httpStatusCode).when(statusLine).getStatusCode();
129140
CloseableHttpResponse response = Mockito.mock(CloseableHttpResponse.class);
130141
Mockito.doReturn(statusLine).when(response).getStatusLine();
131-
132142
Mockito.doReturn(response).when(redfishClientspy).executeGetRequest(Mockito.anyString());
143+
return response;
144+
}
133145

134-
// Runnint the tested method
135-
redfishClientspy.getSystemPowerState(oobAddress);
146+
@Test
147+
public void getSystemIdTest() {
148+
CloseableHttpResponse mockedResponse = mockResponse(HttpStatus.SC_OK);
149+
Mockito.doReturn(mockedResponse).when(redfishClientspy).executeGetRequest(Mockito.anyString());
150+
Mockito.doReturn(systemId).when(redfishClientspy).processGetSystemIdResponse(Mockito.any(CloseableHttpResponse.class));
136151

137-
// validating the test
152+
String result = redfishClientspy.getSystemId(oobAddress);
153+
154+
Assert.assertEquals(systemId, result);
155+
}
156+
157+
@Test(expected = RedfishException.class)
158+
public void getSystemIdTestHttpStatusNotOk() {
159+
CloseableHttpResponse mockedResponse = mockResponse(HttpStatus.SC_UNAUTHORIZED);
160+
Mockito.doReturn(mockedResponse).when(redfishClientspy).executeGetRequest(Mockito.anyString());
161+
redfishClientspy.getSystemId(oobAddress);
138162
}
139163

140164
}

0 commit comments

Comments
 (0)