|
20 | 20 | import static org.mockito.ArgumentMatchers.anyMap; |
21 | 21 | import static org.mockito.ArgumentMatchers.anyString; |
22 | 22 | import static org.mockito.ArgumentMatchers.nullable; |
| 23 | +import static org.mockito.Mockito.mock; |
23 | 24 | import static org.mockito.Mockito.times; |
24 | 25 |
|
25 | 26 | import java.io.File; |
26 | 27 | import java.io.IOException; |
27 | 28 | import java.nio.charset.Charset; |
28 | 29 | import java.nio.charset.StandardCharsets; |
29 | 30 | import java.util.ArrayList; |
| 31 | +import java.util.Arrays; |
| 32 | +import java.util.Collections; |
30 | 33 | import java.util.HashMap; |
31 | 34 | import java.util.List; |
32 | 35 | import java.util.Map; |
33 | 36 |
|
34 | 37 | import com.cloud.network.Network; |
35 | 38 | import com.cloud.vm.NicProfile; |
| 39 | +import com.google.gson.JsonArray; |
| 40 | +import com.google.gson.JsonParser; |
36 | 41 | import org.apache.commons.io.FileUtils; |
37 | 42 | import org.apache.commons.lang3.StringUtils; |
38 | 43 | import org.junit.Assert; |
39 | 44 | import org.junit.BeforeClass; |
40 | 45 | import org.junit.Test; |
| 46 | +import org.junit.rules.TemporaryFolder; |
41 | 47 | import org.junit.runner.RunWith; |
42 | 48 | import org.mockito.InOrder; |
43 | 49 | import org.mockito.MockedConstruction; |
@@ -515,4 +521,220 @@ public void getProgramToGenerateIsoTestMkIsoMac() throws Exception { |
515 | 521 | Mockito.verify(mkIsoProgramInMacOsFileMock, Mockito.times(1)).getCanonicalPath(); |
516 | 522 | } |
517 | 523 | } |
| 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 | + } |
518 | 740 | } |
0 commit comments