|
65 | 65 | import org.mockito.junit.MockitoJUnitRunner; |
66 | 66 | import org.springframework.test.util.ReflectionTestUtils; |
67 | 67 |
|
| 68 | +import com.cloud.agent.api.GetExternalConsoleAnswer; |
| 69 | +import com.cloud.agent.api.GetExternalConsoleCommand; |
68 | 70 | import com.cloud.agent.api.HostVmStateReportEntry; |
69 | 71 | import com.cloud.agent.api.PrepareExternalProvisioningAnswer; |
70 | 72 | import com.cloud.agent.api.PrepareExternalProvisioningCommand; |
@@ -734,4 +736,215 @@ public void parsePowerStateFromResponseReturnsPowerStateForPlainTextResponse() { |
734 | 736 | VirtualMachine.PowerState result = provisioner.parsePowerStateFromResponse(vm, response); |
735 | 737 | assertEquals(VirtualMachine.PowerState.PowerOn, result); |
736 | 738 | } |
| 739 | + |
| 740 | + @Test |
| 741 | + public void getVirtualMachineTOReturnsNullWhenVmIsNull() { |
| 742 | + VirtualMachineTO result = provisioner.getVirtualMachineTO(null); |
| 743 | + assertNull(result); |
| 744 | + } |
| 745 | + |
| 746 | + @Test |
| 747 | + public void getVirtualMachineTOReturnsValidTOWhenVmIsNotNull() { |
| 748 | + VirtualMachine vm = mock(VirtualMachine.class); |
| 749 | + VirtualMachineTO vmTO = mock(VirtualMachineTO.class); |
| 750 | + when(hypervisorGuruManager.getGuru(Hypervisor.HypervisorType.External)).thenReturn(hypervisorGuru); |
| 751 | + when(hypervisorGuru.implement(any(VirtualMachineProfile.class))).thenReturn(vmTO); |
| 752 | + VirtualMachineTO result = provisioner.getVirtualMachineTO(vm); |
| 753 | + assertNotNull(result); |
| 754 | + assertEquals(vmTO, result); |
| 755 | + Mockito.verify(hypervisorGuruManager).getGuru(Hypervisor.HypervisorType.External); |
| 756 | + Mockito.verify(hypervisorGuru).implement(any(VirtualMachineProfile.class)); |
| 757 | + } |
| 758 | + |
| 759 | + @Test |
| 760 | + public void getInstanceConsoleReturnsAnswerWhenConsoleDetailsAreValid() { |
| 761 | + GetExternalConsoleCommand cmd = mock(GetExternalConsoleCommand.class); |
| 762 | + VirtualMachineTO vmTO = mock(VirtualMachineTO.class); |
| 763 | + when(cmd.getVirtualMachine()).thenReturn(vmTO); |
| 764 | + when(vmTO.getUuid()).thenReturn("test-uuid"); |
| 765 | + |
| 766 | + Map<String, Object> accessDetails = new HashMap<>(); |
| 767 | + when(provisioner.loadAccessDetails(any(), eq(vmTO))).thenReturn(accessDetails); |
| 768 | + |
| 769 | + String validOutput = "{\"console\":{\"host\":\"127.0.0.1\",\"port\":5900,\"password\":\"pass\",\"protocol\":\"vnc\"}}"; |
| 770 | + doReturn(new Pair<>(true, validOutput)).when(provisioner) |
| 771 | + .getInstanceConsoleOnExternalSystem(anyString(), anyString(), anyString(), anyMap(), anyInt()); |
| 772 | + |
| 773 | + GetExternalConsoleAnswer result = provisioner.getInstanceConsole("host-guid", "test-extension", "test-extension.sh", cmd); |
| 774 | + |
| 775 | + assertNotNull(result); |
| 776 | + assertEquals("127.0.0.1", result.getHost()); |
| 777 | + assertEquals(5900, result.getPort()); |
| 778 | + assertEquals("pass", result.getPassword()); |
| 779 | + assertEquals("vnc", result.getProtocol()); |
| 780 | + } |
| 781 | + |
| 782 | + @Test |
| 783 | + public void getInstanceConsoleReturnsErrorWhenExtensionNotConfigured() { |
| 784 | + GetExternalConsoleCommand cmd = mock(GetExternalConsoleCommand.class); |
| 785 | + when(provisioner.getExtensionCheckedPath(anyString(), anyString())).thenReturn(null); |
| 786 | + |
| 787 | + GetExternalConsoleAnswer result = provisioner.getInstanceConsole("host-guid", |
| 788 | + "test-extension", "test-extension.sh", cmd); |
| 789 | + |
| 790 | + assertNotNull(result); |
| 791 | + assertEquals("Extension not configured", result.getDetails()); |
| 792 | + } |
| 793 | + |
| 794 | + @Test |
| 795 | + public void getInstanceConsoleReturnsErrorWhenExternalSystemFails() { |
| 796 | + GetExternalConsoleCommand cmd = mock(GetExternalConsoleCommand.class); |
| 797 | + VirtualMachineTO vmTO = mock(VirtualMachineTO.class); |
| 798 | + when(cmd.getVirtualMachine()).thenReturn(vmTO); |
| 799 | + when(vmTO.getUuid()).thenReturn("test-uuid"); |
| 800 | + |
| 801 | + doReturn(new Pair<>(false, "External system error")).when(provisioner) |
| 802 | + .getInstanceConsoleOnExternalSystem(anyString(), anyString(), anyString(), anyMap(), anyInt()); |
| 803 | + |
| 804 | + GetExternalConsoleAnswer result = provisioner.getInstanceConsole("host-guid", "test-extension", "test-extension.sh", cmd); |
| 805 | + |
| 806 | + assertNotNull(result); |
| 807 | + assertEquals("External system error", result.getDetails()); |
| 808 | + } |
| 809 | + |
| 810 | + @Test |
| 811 | + public void getInstanceConsoleReturnsErrorWhenConsoleObjectIsMissing() { |
| 812 | + GetExternalConsoleCommand cmd = mock(GetExternalConsoleCommand.class); |
| 813 | + VirtualMachineTO vmTO = mock(VirtualMachineTO.class); |
| 814 | + when(cmd.getVirtualMachine()).thenReturn(vmTO); |
| 815 | + when(vmTO.getUuid()).thenReturn("test-uuid"); |
| 816 | + |
| 817 | + String invalidOutput = "{\"invalid_key\":\"value\"}"; |
| 818 | + doReturn(new Pair<>(true, invalidOutput)).when(provisioner) |
| 819 | + .getInstanceConsoleOnExternalSystem(anyString(), anyString(), anyString(), anyMap(), anyInt()); |
| 820 | + |
| 821 | + GetExternalConsoleAnswer result = provisioner.getInstanceConsole("host-guid", "test-extension", "test-extension.sh", cmd); |
| 822 | + |
| 823 | + assertNotNull(result); |
| 824 | + assertEquals("Missing console object in output", result.getDetails()); |
| 825 | + } |
| 826 | + |
| 827 | + @Test |
| 828 | + public void getInstanceConsoleReturnsErrorWhenRequiredFieldsAreMissing() { |
| 829 | + GetExternalConsoleCommand cmd = mock(GetExternalConsoleCommand.class); |
| 830 | + VirtualMachineTO vmTO = mock(VirtualMachineTO.class); |
| 831 | + when(cmd.getVirtualMachine()).thenReturn(vmTO); |
| 832 | + when(vmTO.getUuid()).thenReturn("test-uuid"); |
| 833 | + |
| 834 | + String incompleteOutput = "{\"console\":{\"host\":\"127.0.0.1\"}}"; |
| 835 | + doReturn(new Pair<>(true, incompleteOutput)).when(provisioner) |
| 836 | + .getInstanceConsoleOnExternalSystem(anyString(), anyString(), anyString(), anyMap(), anyInt()); |
| 837 | + |
| 838 | + GetExternalConsoleAnswer result = provisioner.getInstanceConsole("host-guid", "test-extension", "test-extension.sh", cmd); |
| 839 | + |
| 840 | + assertNotNull(result); |
| 841 | + assertEquals("Missing required fields in output", result.getDetails()); |
| 842 | + } |
| 843 | + |
| 844 | + @Test |
| 845 | + public void getInstanceConsoleReturnsErrorWhenOutputParsingFails() { |
| 846 | + GetExternalConsoleCommand cmd = mock(GetExternalConsoleCommand.class); |
| 847 | + VirtualMachineTO vmTO = mock(VirtualMachineTO.class); |
| 848 | + when(cmd.getVirtualMachine()).thenReturn(vmTO); |
| 849 | + when(vmTO.getUuid()).thenReturn("test-uuid"); |
| 850 | + |
| 851 | + String malformedOutput = "{console:invalid}"; |
| 852 | + doReturn(new Pair<>(true, malformedOutput)).when(provisioner) |
| 853 | + .getInstanceConsoleOnExternalSystem(anyString(), anyString(), anyString(), anyMap(), anyInt()); |
| 854 | + |
| 855 | + GetExternalConsoleAnswer result = provisioner.getInstanceConsole("host-guid", "test-extension", "test-extension.sh", cmd); |
| 856 | + |
| 857 | + assertNotNull(result); |
| 858 | + assertEquals("Failed to parse output", result.getDetails()); |
| 859 | + } |
| 860 | + |
| 861 | + @Test |
| 862 | + public void getInstanceConsoleOnExternalSystemReturnsSuccessWhenCommandExecutesSuccessfully() { |
| 863 | + String extensionName = "test-extension"; |
| 864 | + String filename = "test-script.sh"; |
| 865 | + String vmUUID = "test-vm-uuid"; |
| 866 | + Map<String, Object> accessDetails = new HashMap<>(); |
| 867 | + int wait = 30; |
| 868 | + |
| 869 | + doReturn(new Pair<>(true, "Console details")).when(provisioner) |
| 870 | + .executeExternalCommand(eq(extensionName), eq("getconsole"), eq(accessDetails), eq(wait), anyString(), eq(filename)); |
| 871 | + |
| 872 | + Pair<Boolean, String> result = provisioner.getInstanceConsoleOnExternalSystem(extensionName, filename, vmUUID, accessDetails, wait); |
| 873 | + |
| 874 | + assertTrue(result.first()); |
| 875 | + assertEquals("Console details", result.second()); |
| 876 | + } |
| 877 | + |
| 878 | + @Test |
| 879 | + public void getInstanceConsoleOnExternalSystemReturnsFailureWhenCommandFails() { |
| 880 | + String extensionName = "test-extension"; |
| 881 | + String filename = "test-script.sh"; |
| 882 | + String vmUUID = "test-vm-uuid"; |
| 883 | + Map<String, Object> accessDetails = new HashMap<>(); |
| 884 | + int wait = 30; |
| 885 | + |
| 886 | + doReturn(new Pair<>(false, "Failed to get console")).when(provisioner) |
| 887 | + .executeExternalCommand(eq(extensionName), eq("getconsole"), eq(accessDetails), eq(wait), anyString(), eq(filename)); |
| 888 | + |
| 889 | + Pair<Boolean, String> result = provisioner.getInstanceConsoleOnExternalSystem(extensionName, filename, vmUUID, accessDetails, wait); |
| 890 | + |
| 891 | + assertFalse(result.first()); |
| 892 | + assertEquals("Failed to get console", result.second()); |
| 893 | + } |
| 894 | + |
| 895 | + @Test |
| 896 | + public void getInstanceConsoleOnExternalSystemHandlesNullResponseGracefully() { |
| 897 | + String extensionName = "test-extension"; |
| 898 | + String filename = "test-script.sh"; |
| 899 | + String vmUUID = "test-vm-uuid"; |
| 900 | + Map<String, Object> accessDetails = new HashMap<>(); |
| 901 | + int wait = 30; |
| 902 | + |
| 903 | + doReturn(null).when(provisioner) |
| 904 | + .executeExternalCommand(eq(extensionName), eq("getconsole"), eq(accessDetails), eq(wait), anyString(), eq(filename)); |
| 905 | + |
| 906 | + Pair<Boolean, String> result = provisioner.getInstanceConsoleOnExternalSystem(extensionName, filename, vmUUID, accessDetails, wait); |
| 907 | + |
| 908 | + assertNull(result); |
| 909 | + } |
| 910 | + |
| 911 | + @Test |
| 912 | + public void getSanitizedJsonStringForLogReturnsNullWhenInputIsNull() { |
| 913 | + String result = provisioner.getSanitizedJsonStringForLog(null); |
| 914 | + assertNull(result); |
| 915 | + } |
| 916 | + |
| 917 | + @Test |
| 918 | + public void getSanitizedJsonStringForLogReturnsEmptyWhenInputIsEmpty() { |
| 919 | + String result = provisioner.getSanitizedJsonStringForLog(""); |
| 920 | + assertEquals("", result); |
| 921 | + } |
| 922 | + |
| 923 | + @Test |
| 924 | + public void getSanitizedJsonStringForLogReturnsSameStringWhenNoPasswordField() { |
| 925 | + String json = "{\"key\":\"value\"}"; |
| 926 | + String result = provisioner.getSanitizedJsonStringForLog(json); |
| 927 | + assertEquals(json, result); |
| 928 | + } |
| 929 | + |
| 930 | + @Test |
| 931 | + public void getSanitizedJsonStringForLogMasksPasswordField() { |
| 932 | + String json = "{\"password\":\"secret\"}"; |
| 933 | + String result = provisioner.getSanitizedJsonStringForLog(json); |
| 934 | + assertEquals("{\"password\":\"****\"}", result); |
| 935 | + } |
| 936 | + |
| 937 | + @Test |
| 938 | + public void getSanitizedJsonStringForLogHandlesMultiplePasswordFields() { |
| 939 | + String json = "{\"password\":\"secret\",\"nested\":{\"password\":\"anotherSecret\"}}"; |
| 940 | + String result = provisioner.getSanitizedJsonStringForLog(json); |
| 941 | + assertEquals("{\"password\":\"****\",\"nested\":{\"password\":\"****\"}}", result); |
| 942 | + } |
| 943 | + |
| 944 | + @Test |
| 945 | + public void getSanitizedJsonStringForLogHandlesMalformedJsonGracefully() { |
| 946 | + String json = "{password:\"secret\""; |
| 947 | + String result = provisioner.getSanitizedJsonStringForLog(json); |
| 948 | + assertEquals("{password:\"secret\"", result); |
| 949 | + } |
737 | 950 | } |
0 commit comments