1717package org .apache .cloudstack .userdata ;
1818
1919import static org .junit .Assert .assertEquals ;
20+ import static org .junit .Assert .assertFalse ;
21+ import static org .junit .Assert .assertNotEquals ;
22+ import static org .junit .Assert .assertNotNull ;
23+ import static org .junit .Assert .assertNull ;
24+ import static org .mockito .Mockito .when ;
2025
26+ import java .io .IOException ;
2127import java .nio .charset .StandardCharsets ;
28+ import java .util .Base64 ;
2229
2330import org .apache .cloudstack .api .BaseCmd ;
2431import org .junit .Test ;
2532import org .junit .runner .RunWith ;
2633import org .mockito .InjectMocks ;
34+ import org .mockito .Mock ;
35+ import org .mockito .Mockito ;
2736import org .mockito .Spy ;
2837import org .mockito .junit .MockitoJUnitRunner ;
2938
39+ import com .cloud .domain .Domain ;
40+ import com .cloud .user .User ;
41+ import com .cloud .user .UserDataVO ;
42+ import com .cloud .user .dao .UserDataDao ;
43+ import com .cloud .utils .exception .CloudRuntimeException ;
44+
3045@ RunWith (MockitoJUnitRunner .class )
3146public class UserDataManagerImplTest {
3247
48+ @ Mock
49+ private UserDataDao userDataDao ;
50+
3351 @ Spy
3452 @ InjectMocks
3553 private UserDataManagerImpl userDataManager ;
@@ -56,4 +74,76 @@ public void testValidateUrlEncodedBase64() {
5674 assertEquals ("validate return the value with padding" , encodedUserdata , userDataManager .validateUserData (urlEncodedUserdata , BaseCmd .HTTPMethod .GET ));
5775 }
5876
77+ @ Test
78+ public void testValidateAndGetUserDataForSystemVMWithBlankUuid () throws IOException {
79+ // Test with blank UUID should return null
80+ assertNull ("null UUID should return null" , userDataManager .validateAndGetUserDataForSystemVM (null ));
81+ assertNull ("blank UUID should return null" , userDataManager .validateAndGetUserDataForSystemVM ("" ));
82+ assertNull ("blank UUID should return null" , userDataManager .validateAndGetUserDataForSystemVM (" " ));
83+ }
84+
85+ @ Test
86+ public void testValidateAndGetUserDataForSystemVMNotFound () throws IOException {
87+ // Test when userDataVo is not found
88+ String testUuid = "test-uuid-123" ;
89+ when (userDataDao .findByUuid (testUuid )).thenReturn (null );
90+
91+ assertNull ("userdata not found should return null" , userDataManager .validateAndGetUserDataForSystemVM (testUuid ));
92+ }
93+
94+ @ Test (expected = CloudRuntimeException .class )
95+ public void testValidateAndGetUserDataForSystemVMInvalidDomain () throws IOException {
96+ // Test with userDataVo that doesn't belong to ROOT domain
97+ String testUuid = "test-uuid-123" ;
98+ UserDataVO userDataVo = Mockito .mock (UserDataVO .class );
99+ when (userDataVo .getDomainId ()).thenReturn (2L ); // Not ROOT domain
100+
101+ when (userDataDao .findByUuid (testUuid )).thenReturn (userDataVo );
102+ userDataManager .validateAndGetUserDataForSystemVM (testUuid );
103+ }
104+
105+ @ Test (expected = CloudRuntimeException .class )
106+ public void testValidateAndGetUserDataForSystemVMInvalidAccount () throws IOException {
107+ // Test with userDataVo that doesn't belong to ADMIN account
108+ String testUuid = "test-uuid-123" ;
109+ UserDataVO userDataVo = Mockito .mock (UserDataVO .class );
110+ when (userDataVo .getDomainId ()).thenReturn (Domain .ROOT_DOMAIN );
111+ when (userDataVo .getAccountId ()).thenReturn (3L );
112+ userDataVo .setUserData ("dGVzdCBkYXRh" ); // "test data" in base64
113+
114+ when (userDataDao .findByUuid (testUuid )).thenReturn (userDataVo );
115+ userDataManager .validateAndGetUserDataForSystemVM (testUuid );
116+ }
117+
118+ @ Test
119+ public void testValidateAndGetUserDataForSystemVMValidSystemVMUserData () throws IOException {
120+ // Test with valid system VM userdata (ROOT domain + ADMIN account)
121+ String testUuid = "test-uuid-123" ;
122+ String originalText = "#!/bin/bash\n echo 'Hello World'" ;
123+ String base64EncodedUserData = Base64 .getEncoder ().encodeToString (originalText .getBytes ());
124+
125+ UserDataVO userDataVo = Mockito .mock (UserDataVO .class );
126+ when (userDataVo .getDomainId ()).thenReturn (Domain .ROOT_DOMAIN );
127+ when (userDataVo .getAccountId ()).thenReturn (User .UID_ADMIN );
128+ when (userDataVo .getUserData ()).thenReturn (base64EncodedUserData );
129+
130+ when (userDataDao .findByUuid (testUuid )).thenReturn (userDataVo );
131+
132+ String result = userDataManager .validateAndGetUserDataForSystemVM (testUuid );
133+
134+ // Verify result is not null and is base64 encoded
135+ assertNotNull ("result should not be null" , result );
136+ assertFalse ("result should be base64 encoded" , result .isEmpty ());
137+
138+ // Verify the result is valid base64
139+ try {
140+ Base64 .getDecoder ().decode (result );
141+ } catch (IllegalArgumentException e ) {
142+ throw new AssertionError ("Result should be valid base64" , e );
143+ }
144+
145+ // The result should be different from input since it's compressed
146+ assertNotEquals ("compressed result should be different from original" , result , base64EncodedUserData );
147+ }
148+
59149}
0 commit comments