Skip to content

Commit b49c8ce

Browse files
committed
Add some unit tests for gpu related changes
1 parent 60c091d commit b49c8ce

32 files changed

+1310
-116
lines changed

api/src/main/java/com/cloud/capacity/Capacity.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ public interface Capacity extends InternalIdentity, Identity {
3333
public static final short CAPACITY_TYPE_DIRECT_ATTACHED_PUBLIC_IP = 8;
3434
public static final short CAPACITY_TYPE_LOCAL_STORAGE = 9;
3535
public static final short CAPACITY_TYPE_VIRTUAL_NETWORK_IPV6_SUBNET = 10;
36-
public static final short CAPACITY_TYPE_GPU = 11;
36+
public static final short CAPACITY_TYPE_GPU = 19;
3737

3838
public static final short CAPACITY_TYPE_CPU_CORE = 90;
3939

Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
package org.apache.cloudstack.api.command.admin.gpu;
2+
3+
import com.cloud.user.Account;
4+
import org.junit.Test;
5+
import org.springframework.test.util.ReflectionTestUtils;
6+
7+
import static org.junit.Assert.assertEquals;
8+
import static org.junit.Assert.assertNull;
9+
10+
public class CreateGpuCardCmdTest {
11+
12+
@Test
13+
public void getDeviceId() {
14+
CreateGpuCardCmd cmd = new CreateGpuCardCmd();
15+
assertNull(cmd.getDeviceId());
16+
String deviceId = "0000:00:1f.6";
17+
ReflectionTestUtils.setField(cmd, "deviceId", deviceId);
18+
assertEquals(deviceId, cmd.getDeviceId());
19+
}
20+
21+
@Test
22+
public void getDeviceName() {
23+
CreateGpuCardCmd cmd = new CreateGpuCardCmd();
24+
assertNull(cmd.getDeviceName());
25+
String deviceName = "NVIDIA GeForce GTX 1080";
26+
ReflectionTestUtils.setField(cmd, "deviceName", deviceName);
27+
assertEquals(deviceName, cmd.getDeviceName());
28+
}
29+
30+
@Test
31+
public void getName() {
32+
CreateGpuCardCmd cmd = new CreateGpuCardCmd();
33+
assertNull(cmd.getName());
34+
String name = "Test GPU Card";
35+
ReflectionTestUtils.setField(cmd, "name", name);
36+
assertEquals(name, cmd.getName());
37+
}
38+
39+
@Test
40+
public void getVendorName() {
41+
CreateGpuCardCmd cmd = new CreateGpuCardCmd();
42+
assertNull(cmd.getVendorName());
43+
String vendorName = "NVIDIA";
44+
ReflectionTestUtils.setField(cmd, "vendorName", vendorName);
45+
assertEquals(vendorName, cmd.getVendorName());
46+
}
47+
48+
@Test
49+
public void getVendorId() {
50+
CreateGpuCardCmd cmd = new CreateGpuCardCmd();
51+
assertNull(cmd.getVendorId());
52+
String vendorId = "10de"; // NVIDIA vendor ID
53+
ReflectionTestUtils.setField(cmd, "vendorId", vendorId);
54+
assertEquals(vendorId, cmd.getVendorId());
55+
}
56+
57+
@Test
58+
public void getVideoRam() {
59+
CreateGpuCardCmd cmd = new CreateGpuCardCmd();
60+
assertNull(cmd.getVideoRam());
61+
Long videoRam = 8192L; // 8 GB
62+
ReflectionTestUtils.setField(cmd, "videoRam", videoRam);
63+
assertEquals(videoRam, cmd.getVideoRam());
64+
}
65+
66+
@Test
67+
public void getEntityOwnerId() {
68+
CreateGpuCardCmd cmd = new CreateGpuCardCmd();
69+
assertEquals(Account.ACCOUNT_ID_SYSTEM, cmd.getEntityOwnerId());
70+
}
71+
}
Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
1+
package org.apache.cloudstack.api.command.admin.gpu;
2+
3+
import com.cloud.user.Account;
4+
import org.apache.cloudstack.gpu.GpuDevice;
5+
import org.junit.Test;
6+
import org.springframework.test.util.ReflectionTestUtils;
7+
8+
import static org.junit.Assert.assertEquals;
9+
import static org.junit.Assert.assertNull;
10+
11+
public class CreateGpuDeviceCmdTest {
12+
13+
@Test
14+
public void getHostId() {
15+
CreateGpuDeviceCmd cmd = new CreateGpuDeviceCmd();
16+
assertNull(cmd.getHostId());
17+
Long hostId = 1L;
18+
ReflectionTestUtils.setField(cmd, "hostId", hostId);
19+
assertEquals(hostId, cmd.getHostId());
20+
}
21+
22+
@Test
23+
public void getBusAddress() {
24+
CreateGpuDeviceCmd cmd = new CreateGpuDeviceCmd();
25+
assertNull(cmd.getBusAddress());
26+
String busAddress = "0000:00:1f.6";
27+
ReflectionTestUtils.setField(cmd, "busAddress", busAddress);
28+
assertEquals(busAddress, cmd.getBusAddress());
29+
}
30+
31+
@Test
32+
public void getGpuCardId() {
33+
CreateGpuDeviceCmd cmd = new CreateGpuDeviceCmd();
34+
assertNull(cmd.getGpuCardId());
35+
Long gpuCardId = 1L;
36+
ReflectionTestUtils.setField(cmd, "gpuCardId", gpuCardId);
37+
assertEquals(gpuCardId, cmd.getGpuCardId());
38+
}
39+
40+
@Test
41+
public void getVgpuProfileId() {
42+
CreateGpuDeviceCmd cmd = new CreateGpuDeviceCmd();
43+
assertNull(cmd.getVgpuProfileId());
44+
Long vgpuProfileId = 1L;
45+
ReflectionTestUtils.setField(cmd, "vgpuProfileId", vgpuProfileId);
46+
assertEquals(vgpuProfileId, cmd.getVgpuProfileId());
47+
}
48+
49+
@Test
50+
public void getType() {
51+
CreateGpuDeviceCmd cmd = new CreateGpuDeviceCmd();
52+
assertEquals(GpuDevice.DeviceType.PCI, cmd.getType());
53+
String type = "MDEV";
54+
ReflectionTestUtils.setField(cmd, "type", type);
55+
assertEquals(GpuDevice.DeviceType.MDEV, cmd.getType());
56+
}
57+
58+
@Test
59+
public void getParentGpuDeviceId() {
60+
CreateGpuDeviceCmd cmd = new CreateGpuDeviceCmd();
61+
assertNull(cmd.getParentGpuDeviceId());
62+
Long parentGpuDeviceId = 1L;
63+
ReflectionTestUtils.setField(cmd, "parentGpuDeviceId", parentGpuDeviceId);
64+
assertEquals(parentGpuDeviceId, cmd.getParentGpuDeviceId());
65+
}
66+
67+
@Test
68+
public void getNumaNode() {
69+
CreateGpuDeviceCmd cmd = new CreateGpuDeviceCmd();
70+
assertEquals("-1", cmd.getNumaNode());
71+
String numaNode = "0";
72+
ReflectionTestUtils.setField(cmd, "numaNode", numaNode);
73+
assertEquals(numaNode, cmd.getNumaNode());
74+
}
75+
76+
@Test
77+
public void getEntityOwnerId() {
78+
CreateGpuDeviceCmd cmd = new CreateGpuDeviceCmd();
79+
assertEquals(Account.ACCOUNT_ID_SYSTEM, cmd.getEntityOwnerId());
80+
}
81+
}
Lines changed: 89 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,89 @@
1+
package org.apache.cloudstack.api.command.admin.gpu;
2+
3+
import com.cloud.user.Account;
4+
import org.junit.Test;
5+
import org.springframework.test.util.ReflectionTestUtils;
6+
7+
import static org.junit.Assert.assertEquals;
8+
import static org.junit.Assert.assertNull;
9+
10+
public class CreateVgpuProfileCmdTest {
11+
12+
@Test
13+
public void getName() {
14+
CreateVgpuProfileCmd cmd = new CreateVgpuProfileCmd();
15+
assertNull(cmd.getName());
16+
String name = "Test VGPU Profile";
17+
ReflectionTestUtils.setField(cmd, "name", name);
18+
assertEquals(name, cmd.getName());
19+
}
20+
21+
@Test
22+
public void getDescription() {
23+
CreateVgpuProfileCmd cmd = new CreateVgpuProfileCmd();
24+
assertNull(cmd.getDescription());
25+
String description = "Test VGPU Profile Description";
26+
ReflectionTestUtils.setField(cmd, "description", description);
27+
assertEquals(description, cmd.getDescription());
28+
}
29+
30+
@Test
31+
public void getCardId() {
32+
CreateVgpuProfileCmd cmd = new CreateVgpuProfileCmd();
33+
assertNull(cmd.getCardId());
34+
Long cardId = 1L;
35+
ReflectionTestUtils.setField(cmd, "cardId", cardId);
36+
assertEquals(cardId, cmd.getCardId());
37+
}
38+
39+
@Test
40+
public void getMaxVgpuPerPgpu() {
41+
CreateVgpuProfileCmd cmd = new CreateVgpuProfileCmd();
42+
assertNull(cmd.getMaxVgpuPerPgpu());
43+
Long maxVgpuPerPgpu = 8L;
44+
ReflectionTestUtils.setField(cmd, "maxVgpuPerPgpu", maxVgpuPerPgpu);
45+
assertEquals(maxVgpuPerPgpu, cmd.getMaxVgpuPerPgpu());
46+
}
47+
48+
@Test
49+
public void getVideoRam() {
50+
CreateVgpuProfileCmd cmd = new CreateVgpuProfileCmd();
51+
assertNull(cmd.getVideoRam());
52+
Long videoRam = 8192L; // 8 GB
53+
ReflectionTestUtils.setField(cmd, "videoRam", videoRam);
54+
assertEquals(videoRam, cmd.getVideoRam());
55+
}
56+
57+
@Test
58+
public void getMaxHeads() {
59+
CreateVgpuProfileCmd cmd = new CreateVgpuProfileCmd();
60+
assertNull(cmd.getMaxHeads());
61+
Long maxHeads = 2L;
62+
ReflectionTestUtils.setField(cmd, "maxHeads", maxHeads);
63+
assertEquals(maxHeads, cmd.getMaxHeads());
64+
}
65+
66+
@Test
67+
public void getMaxResolutionX() {
68+
CreateVgpuProfileCmd cmd = new CreateVgpuProfileCmd();
69+
assertNull(cmd.getMaxResolutionX());
70+
Long maxResolutionX = 1920L; // 1920 pixels
71+
ReflectionTestUtils.setField(cmd, "maxResolutionX", maxResolutionX);
72+
assertEquals(maxResolutionX, cmd.getMaxResolutionX());
73+
}
74+
75+
@Test
76+
public void getMaxResolutionY() {
77+
CreateVgpuProfileCmd cmd = new CreateVgpuProfileCmd();
78+
assertNull(cmd.getMaxResolutionY());
79+
Long maxResolutionY = 1080L; // 1080 pixels
80+
ReflectionTestUtils.setField(cmd, "maxResolutionY", maxResolutionY);
81+
assertEquals(maxResolutionY, cmd.getMaxResolutionY());
82+
}
83+
84+
@Test
85+
public void getEntityOwnerId() {
86+
CreateVgpuProfileCmd cmd = new CreateVgpuProfileCmd();
87+
assertEquals(Account.ACCOUNT_ID_SYSTEM, cmd.getEntityOwnerId());
88+
}
89+
}
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
package org.apache.cloudstack.api.command.admin.gpu;
2+
3+
import com.cloud.user.Account;
4+
import org.junit.Test;
5+
import org.springframework.test.util.ReflectionTestUtils;
6+
7+
import static org.junit.Assert.assertEquals;
8+
import static org.junit.Assert.assertNull;
9+
10+
public class DeleteGpuCardCmdTest {
11+
12+
@Test
13+
public void getId() {
14+
DeleteGpuCardCmd cmd = new DeleteGpuCardCmd();
15+
assertNull(cmd.getId());
16+
Long id = 1L;
17+
ReflectionTestUtils.setField(cmd, "id", id);
18+
assertEquals(id, cmd.getId());
19+
}
20+
21+
@Test
22+
public void getEntityOwnerId() {
23+
CreateVgpuProfileCmd cmd = new CreateVgpuProfileCmd();
24+
assertEquals(Account.ACCOUNT_ID_SYSTEM, cmd.getEntityOwnerId());
25+
}
26+
}
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
package org.apache.cloudstack.api.command.admin.gpu;
2+
3+
import com.cloud.user.Account;
4+
import org.junit.Test;
5+
import org.springframework.test.util.ReflectionTestUtils;
6+
7+
import java.util.List;
8+
9+
import static org.junit.Assert.assertEquals;
10+
import static org.junit.Assert.assertNull;
11+
12+
public class DeleteGpuDeviceCmdTest {
13+
14+
@Test
15+
public void getIds() {
16+
DeleteGpuDeviceCmd cmd = new DeleteGpuDeviceCmd();
17+
assertNull(cmd.getIds());
18+
List<Long> ids = List.of(1L, 2L, 3L);
19+
ReflectionTestUtils.setField(cmd, "ids", ids);
20+
assertEquals(ids, cmd.getIds());
21+
}
22+
23+
@Test
24+
public void getEntityOwnerId() {
25+
DeleteGpuDeviceCmd cmd = new DeleteGpuDeviceCmd();
26+
assertEquals(Account.ACCOUNT_ID_SYSTEM, cmd.getEntityOwnerId());
27+
}
28+
}
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
package org.apache.cloudstack.api.command.admin.gpu;
2+
3+
import com.cloud.user.Account;
4+
import org.junit.Test;
5+
import org.springframework.test.util.ReflectionTestUtils;
6+
7+
import static org.junit.Assert.assertEquals;
8+
import static org.junit.Assert.assertNull;
9+
10+
public class DeleteVgpuProfileCmdTest {
11+
12+
@Test
13+
public void getId() {
14+
DeleteVgpuProfileCmd cmd = new DeleteVgpuProfileCmd();
15+
assertNull(cmd.getId());
16+
Long id = 1L;
17+
ReflectionTestUtils.setField(cmd, "id", id);
18+
assertEquals(id, cmd.getId());
19+
}
20+
21+
@Test
22+
public void getEntityOwnerId() {
23+
DeleteVgpuProfileCmd cmd = new DeleteVgpuProfileCmd();
24+
assertEquals(Account.ACCOUNT_ID_SYSTEM, cmd.getEntityOwnerId());
25+
}
26+
}
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
package org.apache.cloudstack.api.command.admin.gpu;
2+
3+
import org.junit.Test;
4+
import org.springframework.test.util.ReflectionTestUtils;
5+
6+
import static org.junit.Assert.assertEquals;
7+
import static org.junit.Assert.assertNull;
8+
9+
public class DiscoverGpuDevicesCmdTest {
10+
11+
@Test
12+
public void getId() {
13+
DiscoverGpuDevicesCmd cmd = new DiscoverGpuDevicesCmd();
14+
assertNull(cmd.getId());
15+
Long id = 1L;
16+
ReflectionTestUtils.setField(cmd, "id", id);
17+
assertEquals(id, cmd.getId());
18+
}
19+
}

0 commit comments

Comments
 (0)