Skip to content

Commit 73d2970

Browse files
committed
Add unit tests
1 parent ea700b9 commit 73d2970

File tree

2 files changed

+223
-19
lines changed

2 files changed

+223
-19
lines changed

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

Lines changed: 1 addition & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,6 @@
3737

3838
import com.cloud.network.Network;
3939
import com.cloud.vm.NicProfile;
40-
import com.google.gson.JsonParser;
4140
import com.googlecode.ipv6.IPv6Network;
4241
import org.apache.commons.codec.binary.Base64;
4342
import org.apache.commons.collections.MapUtils;
@@ -227,23 +226,6 @@ static void writeVmMetadata(List<String[]> vmData, String tempDirName, File open
227226
writeFile(openStackFolder, "meta_data.json", metaData.toString());
228227
}
229228

230-
static JsonObject getExistingNetworkData(File openStackFolder) {
231-
if (openStackFolder.exists()) {
232-
File networkDataFile = getFile(openStackFolder.getAbsolutePath(), "network_data.json");
233-
if (networkDataFile.exists()) {
234-
try {
235-
String networkDataFileContent = FileUtils.readFileToString(networkDataFile, com.cloud.utils.StringUtils.getPreferredCharset());
236-
if (StringUtils.isNotBlank(networkDataFileContent)) {
237-
return new JsonParser().parse(networkDataFileContent).getAsJsonObject();
238-
}
239-
} catch (IOException e) {
240-
LOGGER.warn("Failed to read existing network data file: {}", networkDataFile.getAbsolutePath(), e);
241-
}
242-
}
243-
}
244-
return new JsonObject();
245-
}
246-
247229
/**
248230
* First we generate a JSON object using {@link #getNetworkDataJsonObjectForNic(NicProfile, List)}, then we write it to a file called "network_data.json".
249231
*/
@@ -394,7 +376,7 @@ static JsonArray getNetworksJsonArrayForNic(NicProfile nic) {
394376
ipv6Network.addProperty("ip_address", nic.getIPv6Address());
395377
ipv6Network.addProperty("link", String.format("eth%d", nic.getDeviceId()));
396378
ipv6Network.addProperty("netmask", IPv6Network.fromString(nic.getIPv6Cidr()).getNetmask().toString());
397-
ipv6Network.addProperty("network_id", nic.getNetworkId());
379+
ipv6Network.addProperty("network_id", nic.getUuid());
398380
ipv6Network.addProperty("type", "ipv6");
399381

400382
JsonArray ipv6RouteArray = new JsonArray();

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

Lines changed: 222 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,24 +20,30 @@
2020
import static org.mockito.ArgumentMatchers.anyMap;
2121
import static org.mockito.ArgumentMatchers.anyString;
2222
import static org.mockito.ArgumentMatchers.nullable;
23+
import static org.mockito.Mockito.mock;
2324
import static org.mockito.Mockito.times;
2425

2526
import java.io.File;
2627
import java.io.IOException;
2728
import java.nio.charset.Charset;
2829
import java.nio.charset.StandardCharsets;
2930
import java.util.ArrayList;
31+
import java.util.Arrays;
32+
import java.util.Collections;
3033
import java.util.HashMap;
3134
import java.util.List;
3235
import java.util.Map;
3336

3437
import com.cloud.network.Network;
3538
import com.cloud.vm.NicProfile;
39+
import com.google.gson.JsonArray;
40+
import com.google.gson.JsonParser;
3641
import org.apache.commons.io.FileUtils;
3742
import org.apache.commons.lang3.StringUtils;
3843
import org.junit.Assert;
3944
import org.junit.BeforeClass;
4045
import org.junit.Test;
46+
import org.junit.rules.TemporaryFolder;
4147
import org.junit.runner.RunWith;
4248
import org.mockito.InOrder;
4349
import org.mockito.MockedConstruction;
@@ -515,4 +521,220 @@ public void getProgramToGenerateIsoTestMkIsoMac() throws Exception {
515521
Mockito.verify(mkIsoProgramInMacOsFileMock, Mockito.times(1)).getCanonicalPath();
516522
}
517523
}
524+
525+
@Test
526+
public void testWriteNetworkData() throws Exception {
527+
// Setup
528+
NicProfile nicp = mock(NicProfile.class);
529+
Mockito.when(nicp.getId()).thenReturn(1L);
530+
531+
Mockito.when(nicp.getMacAddress()).thenReturn("00:00:00:00:00:00");
532+
Mockito.when(nicp.getMtu()).thenReturn(2000);
533+
534+
Mockito.when(nicp.getIPv4Address()).thenReturn("172.31.0.10");
535+
Mockito.when(nicp.getDeviceId()).thenReturn(1);
536+
Mockito.when(nicp.getIPv4Netmask()).thenReturn("255.255.255.0");
537+
Mockito.when(nicp.getUuid()).thenReturn("NETWORK UUID");
538+
Mockito.when(nicp.getIPv4Gateway()).thenReturn("172.31.0.1");
539+
540+
541+
Mockito.when(nicp.getIPv6Address()).thenReturn("2001:db8:0:1234:0:567:8:1");
542+
Mockito.when(nicp.getIPv6Cidr()).thenReturn("2001:db8:0:1234:0:567:8:1/64");
543+
Mockito.when(nicp.getIPv6Gateway()).thenReturn("2001:db8:0:1234:0:567:8::1");
544+
545+
Mockito.when(nicp.getIPv4Dns1()).thenReturn("8.8.8.8");
546+
Mockito.when(nicp.getIPv4Dns2()).thenReturn("1.1.1.1");
547+
Mockito.when(nicp.getIPv6Dns1()).thenReturn("2001:4860:4860::8888");
548+
Mockito.when(nicp.getIPv6Dns2()).thenReturn("2001:4860:4860::8844");
549+
550+
551+
List<Network.Service> services1 = Arrays.asList(Network.Service.Dhcp, Network.Service.Dns);
552+
553+
Map<Long, List<Network.Service>> supportedServices = new HashMap<>();
554+
supportedServices.put(1L, services1);
555+
556+
TemporaryFolder folder = new TemporaryFolder();
557+
folder.create();
558+
File openStackFolder = folder.newFolder("openStack");
559+
560+
// Expected JSON structure
561+
String expectedJson = "{" +
562+
" \"links\": [" +
563+
" {" +
564+
" \"ethernet_mac_address\": \"00:00:00:00:00:00\"," +
565+
" \"id\": \"eth1\"," +
566+
" \"mtu\": 2000," +
567+
" \"type\": \"phy\"" +
568+
" }" +
569+
" ]," +
570+
" \"networks\": [" +
571+
" {" +
572+
" \"id\": \"eth1\"," +
573+
" \"ip_address\": \"172.31.0.10\"," +
574+
" \"link\": \"eth1\"," +
575+
" \"netmask\": \"255.255.255.0\"," +
576+
" \"network_id\": \"NETWORK UUID\"," +
577+
" \"type\": \"ipv4\"," +
578+
" \"routes\": [" +
579+
" {" +
580+
" \"gateway\": \"172.31.0.1\"," +
581+
" \"netmask\": \"0.0.0.0\"," +
582+
" \"network\": \"0.0.0.0\"" +
583+
" }" +
584+
" ]" +
585+
" }," +
586+
" {" +
587+
" \"id\": \"eth1\"," +
588+
" \"ip_address\": \"2001:db8:0:1234:0:567:8:1\"," +
589+
" \"link\": \"eth1\"," +
590+
" \"netmask\": \"64\"," +
591+
" \"network_id\": \"NETWORK UUID\"," +
592+
" \"type\": \"ipv6\"," +
593+
" \"routes\": [" +
594+
" {" +
595+
" \"gateway\": \"2001:db8:0:1234:0:567:8::1\"," +
596+
" \"netmask\": \"0\"," +
597+
" \"network\": \"::\"" +
598+
" }" +
599+
" ]" +
600+
" }" +
601+
" ]," +
602+
" \"services\": [" +
603+
" {" +
604+
" \"address\": \"8.8.8.8\"," +
605+
" \"type\": \"dns\"" +
606+
" }," +
607+
" {" +
608+
" \"address\": \"1.1.1.1\"," +
609+
" \"type\": \"dns\"" +
610+
" }," +
611+
" {" +
612+
" \"address\": \"2001:4860:4860::8888\"," +
613+
" \"type\": \"dns\"" +
614+
" }," +
615+
" {" +
616+
" \"address\": \"2001:4860:4860::8844\"," +
617+
" \"type\": \"dns\"" +
618+
" }" +
619+
" ]" +
620+
"}";
621+
622+
// Action
623+
ConfigDriveBuilder.writeNetworkData(Arrays.asList(nicp), supportedServices, openStackFolder);
624+
625+
// Verify
626+
File networkDataFile = new File(openStackFolder, "network_data.json");
627+
String content = FileUtils.readFileToString(networkDataFile, StandardCharsets.UTF_8);
628+
JsonObject actualJson = new JsonParser().parse(content).getAsJsonObject();
629+
JsonObject expectedJsonObject = new JsonParser().parse(expectedJson).getAsJsonObject();
630+
631+
Assert.assertEquals(expectedJsonObject, actualJson);
632+
folder.delete();
633+
}
634+
635+
@Test
636+
public void testWriteNetworkDataEmptyJson() throws Exception {
637+
// Setup
638+
NicProfile nicp = mock(NicProfile.class);
639+
Mockito.when(nicp.getId()).thenReturn(1L);
640+
641+
List<Network.Service> services1 = Collections.emptyList();
642+
643+
Map<Long, List<Network.Service>> supportedServices = new HashMap<>();
644+
supportedServices.put(1L, services1);
645+
646+
TemporaryFolder folder = new TemporaryFolder();
647+
folder.create();
648+
File openStackFolder = folder.newFolder("openStack");
649+
650+
// Expected JSON structure
651+
String expectedJson = "{}";
652+
653+
// Action
654+
ConfigDriveBuilder.writeNetworkData(Arrays.asList(nicp), supportedServices, openStackFolder);
655+
656+
// Verify
657+
File networkDataFile = new File(openStackFolder, "network_data.json");
658+
String content = FileUtils.readFileToString(networkDataFile, StandardCharsets.UTF_8);
659+
JsonObject actualJson = new JsonParser().parse(content).getAsJsonObject();
660+
JsonObject expectedJsonObject = new JsonParser().parse(expectedJson).getAsJsonObject();
661+
662+
Assert.assertEquals(expectedJsonObject, actualJson);
663+
folder.delete();
664+
}
665+
666+
@Test
667+
public void testMergeJsonArraysAndUpdateObjectWithEmptyObjects() {
668+
JsonObject finalObject = new JsonObject();
669+
JsonObject newObj = new JsonObject();
670+
ConfigDriveBuilder.mergeJsonArraysAndUpdateObject(finalObject, newObj, "links", "id", "type");
671+
Assert.assertEquals("{}", finalObject.toString());
672+
}
673+
674+
@Test
675+
public void testMergeJsonArraysAndUpdateObjectWithNewMembersAdded() {
676+
JsonObject finalObject = new JsonObject();
677+
678+
JsonObject newObj = new JsonObject();
679+
JsonArray newMembers = new JsonArray();
680+
JsonObject newMember = new JsonObject();
681+
newMember.addProperty("id", "eth0");
682+
newMember.addProperty("type", "phy");
683+
newMembers.add(newMember);
684+
newObj.add("links", newMembers);
685+
686+
ConfigDriveBuilder.mergeJsonArraysAndUpdateObject(finalObject, newObj, "links", "id", "type");
687+
Assert.assertEquals(1, finalObject.getAsJsonArray("links").size());
688+
JsonObject expectedObj = new JsonParser().parse("{'links': [{'id': 'eth0', 'type': 'phy'}]}").getAsJsonObject();
689+
Assert.assertEquals(expectedObj, finalObject);
690+
}
691+
692+
@Test
693+
public void testMergeJsonArraysAndUpdateObjectWithDuplicateMembersIgnored() {
694+
JsonObject finalObject = new JsonObject();
695+
JsonArray existingMembers = new JsonArray();
696+
JsonObject existingMember = new JsonObject();
697+
existingMember.addProperty("id", "eth0");
698+
existingMember.addProperty("type", "phy");
699+
existingMembers.add(existingMember);
700+
finalObject.add("links", existingMembers);
701+
702+
JsonObject newObj = new JsonObject();
703+
newObj.add("links", existingMembers); // same as existingMembers for duplication
704+
705+
ConfigDriveBuilder.mergeJsonArraysAndUpdateObject(finalObject, newObj, "links", "id", "type");
706+
Assert.assertEquals(1, finalObject.getAsJsonArray("links").size());
707+
JsonObject expectedObj = new JsonParser().parse("{'links': [{'id': 'eth0', 'type': 'phy'}]}").getAsJsonObject();
708+
Assert.assertEquals(expectedObj, finalObject);
709+
}
710+
711+
@Test
712+
public void testMergeJsonArraysAndUpdateObjectWithDifferentMembers() {
713+
JsonObject finalObject = new JsonObject();
714+
715+
JsonArray newMembers = new JsonArray();
716+
JsonObject newMember = new JsonObject();
717+
newMember.addProperty("id", "eth0");
718+
newMember.addProperty("type", "phy");
719+
newMembers.add(newMember);
720+
finalObject.add("links", newMembers);
721+
722+
JsonObject newObj = new JsonObject();
723+
newMembers = new JsonArray();
724+
newMember = new JsonObject();
725+
newMember.addProperty("id", "eth1");
726+
newMember.addProperty("type", "phy");
727+
newMembers.add(newMember);
728+
newObj.add("links", newMembers);
729+
730+
ConfigDriveBuilder.mergeJsonArraysAndUpdateObject(finalObject, newObj, "links", "id", "type");
731+
Assert.assertEquals(2, finalObject.getAsJsonArray("links").size());
732+
JsonObject expectedObj = new JsonParser().parse("{'links': [{'id': 'eth0', 'type': 'phy'}, {'id': 'eth1', 'type': 'phy'}]}").getAsJsonObject();
733+
Assert.assertEquals(expectedObj, finalObject);
734+
}
735+
736+
@Test(expected = NullPointerException.class)
737+
public void testMergeJsonArraysAndUpdateObjectWithNullObjects() {
738+
ConfigDriveBuilder.mergeJsonArraysAndUpdateObject(null, null, "services", "id", "type");
739+
}
518740
}

0 commit comments

Comments
 (0)