Skip to content

Commit 9b83337

Browse files
rafaelweingartnerDaanHoogland
authored andcommitted
Create unit test cases for 'ConfigDriveBuilder' class (#2674)
* Create unit test cases for 'ConfigDriveBuilder' class * add method 'getProgramToGenerateIso' as suggested by rohit and Daan * fix encoding for base64 to StandardCharsets.US_ASCII * fix MockServerTest.testIsMockServerCanUpgradeConnectionToSsl() This is another method that is causing Jenkins to fail for almost a month
1 parent d46fa6e commit 9b83337

7 files changed

Lines changed: 738 additions & 164 deletions

File tree

engine/storage/configdrive/src/org/apache/cloudstack/storage/configdrive/ConfigDrive.java

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -26,11 +26,12 @@ public class ConfigDrive {
2626
public static final String openStackConfigDriveName = "/openstack/latest/";
2727

2828
/**
29-
* This is the path to iso file relative to mount point
30-
* @return config drive iso file path
29+
* Creates the path to ISO file relative to mount point.
30+
* The config driver path will have the following formated: {@link #CONFIGDRIVEDIR} + / + instanceName + / + {@link #CONFIGDRIVEFILENAME}
31+
*
32+
* @return config drive ISO file path
3133
*/
32-
public static String createConfigDrivePath(final String instanceName) {
34+
public static String createConfigDrivePath(String instanceName) {
3335
return ConfigDrive.CONFIGDRIVEDIR + "/" + instanceName + "/" + ConfigDrive.CONFIGDRIVEFILENAME;
3436
}
35-
36-
}
37+
}

engine/storage/configdrive/src/org/apache/cloudstack/storage/configdrive/ConfigDriveBuilder.java

Lines changed: 207 additions & 115 deletions
Large diffs are not rendered by default.

engine/storage/configdrive/test/org/apache/cloudstack/storage/configdrive/ConfigDriveBuilderTest.java

Lines changed: 462 additions & 30 deletions
Large diffs are not rendered by default.
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
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.storage.configdrive;
19+
20+
import java.io.IOException;
21+
22+
import org.junit.Assert;
23+
import org.junit.Test;
24+
25+
public class ConfigDriveTest {
26+
27+
@Test
28+
public void testConfigDriveIsoPath() throws IOException {
29+
Assert.assertEquals(ConfigDrive.createConfigDrivePath("i-x-y"), "configdrive/i-x-y/configdrive.iso");
30+
}
31+
32+
}

server/test/com/cloud/network/element/ConfigDriveNetworkElementTest.java

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@
3131
import static org.mockito.Mockito.when;
3232

3333
import java.lang.reflect.Field;
34+
import java.lang.reflect.Method;
3435
import java.util.Arrays;
3536
import java.util.List;
3637
import java.util.Map;
@@ -41,12 +42,20 @@
4142
import org.apache.cloudstack.engine.subsystem.api.storage.EndPointSelector;
4243
import org.apache.cloudstack.framework.config.dao.ConfigurationDao;
4344
import org.apache.cloudstack.storage.configdrive.ConfigDrive;
45+
import org.apache.cloudstack.storage.configdrive.ConfigDriveBuilder;
46+
import org.junit.Before;
4447
import org.junit.Test;
48+
import org.junit.runner.RunWith;
4549
import org.mockito.ArgumentCaptor;
4650
import org.mockito.InjectMocks;
4751
import org.mockito.Mock;
52+
import org.mockito.Mockito;
4853
import org.mockito.MockitoAnnotations;
4954
import org.mockito.Spy;
55+
import org.powermock.api.mockito.PowerMockito;
56+
import org.powermock.core.classloader.annotations.PrepareForTest;
57+
import org.powermock.modules.junit4.PowerMockRunner;
58+
import org.reflections.ReflectionUtils;
5059

5160
import com.cloud.agent.AgentManager;
5261
import com.cloud.agent.api.Answer;
@@ -90,6 +99,7 @@
9099
import com.cloud.vm.dao.VMInstanceDao;
91100
import com.google.common.collect.Maps;
92101

102+
@RunWith(PowerMockRunner.class)
93103
public class ConfigDriveNetworkElementTest {
94104

95105
public static final String CLOUD_ID = "xx";
@@ -140,7 +150,7 @@ public class ConfigDriveNetworkElementTest {
140150
@InjectMocks private final ConfigDriveNetworkElement _configDrivesNetworkElement = new ConfigDriveNetworkElement();
141151
@InjectMocks @Spy private NetworkModelImpl _networkModel = new NetworkModelImpl();
142152

143-
@org.junit.Before
153+
@Before
144154
public void setUp() throws NoSuchFieldException, IllegalAccessException {
145155
MockitoAnnotations.initMocks(this);
146156

@@ -243,7 +253,14 @@ public void testGetCapabilities () {
243253
}
244254

245255
@Test
256+
@SuppressWarnings("unchecked")
257+
@PrepareForTest({ConfigDriveBuilder.class})
246258
public void testAddPasswordAndUserData() throws Exception {
259+
PowerMockito.mockStatic(ConfigDriveBuilder.class);
260+
261+
Method method = ReflectionUtils.getMethods(ConfigDriveBuilder.class, ReflectionUtils.withName("buildConfigDrive")).iterator().next();
262+
PowerMockito.when(ConfigDriveBuilder.class, method).withArguments(Mockito.anyListOf(String[].class), Mockito.anyString(), Mockito.anyString()).thenReturn("content");
263+
247264
final Answer answer = mock(Answer.class);
248265
final UserVmDetailVO userVmDetailVO = mock(UserVmDetailVO.class);
249266
when(agentManager.easySend(anyLong(), any(HandleConfigDriveIsoCommand.class))).thenReturn(answer);

services/console-proxy-rdp/rdpconsole/src/test/java/rdpclient/MockServerTest.java

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -29,10 +29,9 @@
2929
import javax.net.ssl.SSLSocket;
3030
import javax.net.ssl.SSLSocketFactory;
3131

32-
import junit.framework.TestCase;
33-
3432
import org.junit.Test;
3533

34+
import junit.framework.TestCase;
3635
import streamer.debug.MockServer;
3736
import streamer.debug.MockServer.Packet;
3837

@@ -93,7 +92,6 @@ public void testIsMockServerCanRespond() throws Exception {
9392

9493
@Test
9594
public void testIsMockServerCanUpgradeConnectionToSsl() throws Exception {
96-
9795
final byte[] mockClientData1 = new byte[] {0x01, 0x02, 0x03};
9896
final byte[] mockServerData1 = new byte[] {0x03, 0x02, 0x01};
9997

@@ -161,8 +159,7 @@ public void testIsMockServerCanUpgradeConnectionToSsl() throws Exception {
161159

162160
final SSLSocketFactory sslSocketFactory = (SSLSocketFactory)SSLSocketFactory.getDefault();
163161
SSLSocket sslSocket = (SSLSocket)sslSocketFactory.createSocket(socket, address.getHostName(), address.getPort(), true);
164-
//sslSocket.setEnabledCipherSuites(sslSocket.getSupportedCipherSuites());
165-
sslSocket.setEnabledCipherSuites(new String[] { "SSL_DH_anon_WITH_3DES_EDE_CBC_SHA" });
162+
sslSocket.setEnabledCipherSuites(sslSocket.getSupportedCipherSuites());
166163
sslSocket.startHandshake();
167164

168165
InputStream is = sslSocket.getInputStream();

services/secondary-storage/server/src/org/apache/cloudstack/storage/resource/NfsSecondaryStorageResource.java

Lines changed: 11 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -584,16 +584,19 @@ public static String getTemplateOnSecStorageFilePath(String secStorageMountPoint
584584

585585
StringBuffer sb = new StringBuffer();
586586
sb.append(secStorageMountPoint);
587-
if (!secStorageMountPoint.endsWith("/"))
587+
if (!secStorageMountPoint.endsWith("/")) {
588588
sb.append("/");
589+
}
589590

590591
sb.append(templateRelativeFolderPath);
591-
if (!secStorageMountPoint.endsWith("/"))
592+
if (!secStorageMountPoint.endsWith("/")) {
592593
sb.append("/");
594+
}
593595

594596
sb.append(templateName);
595-
if (!fileExtension.startsWith("."))
597+
if (!fileExtension.startsWith(".")) {
596598
sb.append(".");
599+
}
597600
sb.append(fileExtension);
598601

599602
return sb.toString();
@@ -904,7 +907,7 @@ protected Answer copySnapshotToTemplateFromNfsToNfs(CopyCommand cmd, SnapshotObj
904907
try {
905908
_storage.create(destFile.getAbsolutePath(), _tmpltpp);
906909
try ( // generate template.properties file
907-
FileWriter writer = new FileWriter(metaFile); BufferedWriter bufferWriter = new BufferedWriter(writer);) {
910+
FileWriter writer = new FileWriter(metaFile); BufferedWriter bufferWriter = new BufferedWriter(writer);) {
908911
// KVM didn't change template unique name, just used the template name passed from orchestration layer, so no need
909912
// to send template name back.
910913
bufferWriter.write("uniquename=" + destData.getName());
@@ -1450,7 +1453,7 @@ String swiftDownload(SwiftTO swift, String container, String rfilename, String l
14501453
Script command = new Script("/bin/bash", s_logger);
14511454
command.add("-c");
14521455
command.add("/usr/bin/python /usr/local/cloud/systemvm/scripts/storage/secondary/swift -A " + swift.getUrl() + " -U " + swift.getAccount() + ":" + swift.getUserName()
1453-
+ " -K " + swift.getKey() + " download " + container + " " + rfilename + " -o " + lFullPath);
1456+
+ " -K " + swift.getKey() + " download " + container + " " + rfilename + " -o " + lFullPath);
14541457
OutputInterpreter.AllLinesParser parser = new OutputInterpreter.AllLinesParser();
14551458
String result = command.execute(parser);
14561459
if (result != null) {
@@ -1554,7 +1557,7 @@ String[] swiftList(SwiftTO swift, String container, String rFilename) {
15541557
Script command = new Script("/bin/bash", s_logger);
15551558
command.add("-c");
15561559
command.add("/usr/bin/python /usr/local/cloud/systemvm/scripts/storage/secondary/swift -A " + swift.getUrl() + " -U " + swift.getAccount() + ":" + swift.getUserName()
1557-
+ " -K " + swift.getKey() + " list " + container + " " + rFilename);
1560+
+ " -K " + swift.getKey() + " list " + container + " " + rFilename);
15581561
OutputInterpreter.AllLinesParser parser = new OutputInterpreter.AllLinesParser();
15591562
String result = command.execute(parser);
15601563
if (result == null && parser.getLines() != null) {
@@ -1576,7 +1579,7 @@ String swiftDelete(SwiftTO swift, String container, String object) {
15761579
Script command = new Script("/bin/bash", s_logger);
15771580
command.add("-c");
15781581
command.add("/usr/bin/python /usr/local/cloud/systemvm/scripts/storage/secondary/swift -A " + swift.getUrl() + " -U " + swift.getAccount() + ":" + swift.getUserName()
1579-
+ " -K " + swift.getKey() + " delete " + container + " " + object);
1582+
+ " -K " + swift.getKey() + " delete " + container + " " + object);
15801583
OutputInterpreter.AllLinesParser parser = new OutputInterpreter.AllLinesParser();
15811584
String result = command.execute(parser);
15821585
if (result != null) {
@@ -3316,7 +3319,7 @@ public String postUpload(String uuid, String filename, long processTimeout) {
33163319
for (Processor processor : processors.values()) {
33173320
FormatInfo info = null;
33183321
try {
3319-
info = processor.process(resourcePath, null, templateName, processTimeout * 1000);
3322+
info = processor.process(resourcePath, null, templateName, processTimeout * 1000);
33203323
} catch (InternalErrorException e) {
33213324
s_logger.error("Template process exception ", e);
33223325
return e.toString();

0 commit comments

Comments
 (0)