Skip to content

Commit 3f9c498

Browse files
committed
Add unit tests
1 parent 19af2a7 commit 3f9c498

File tree

1 file changed

+90
-0
lines changed

1 file changed

+90
-0
lines changed

engine/userdata/src/test/java/org/apache/cloudstack/userdata/UserDataManagerImplTest.java

Lines changed: 90 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,19 +17,37 @@
1717
package org.apache.cloudstack.userdata;
1818

1919
import 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;
2127
import java.nio.charset.StandardCharsets;
28+
import java.util.Base64;
2229

2330
import org.apache.cloudstack.api.BaseCmd;
2431
import org.junit.Test;
2532
import org.junit.runner.RunWith;
2633
import org.mockito.InjectMocks;
34+
import org.mockito.Mock;
35+
import org.mockito.Mockito;
2736
import org.mockito.Spy;
2837
import 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)
3146
public 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\necho '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

Comments
 (0)