Skip to content

Commit de29722

Browse files
Created test for Hyperdisk with mocked client
1 parent a10ac97 commit de29722

File tree

6 files changed

+216
-146
lines changed

6 files changed

+216
-146
lines changed

compute/cloud-client/src/main/java/compute/disks/storagepool/CreateDiskInStoragePool.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@ public static void main(String[] args)
5858
public static Disk createDiskInStoragePool(String projectId, String zone, String diskName,
5959
String storagePoolName, String diskType,
6060
long diskSizeGb, long iops, long throughput)
61-
throws IOException, ExecutionException, InterruptedException, TimeoutException {
61+
throws IOException, ExecutionException, InterruptedException {
6262
// Initialize client that will be used to send requests. This client only needs to be created
6363
// once, and can be reused for multiple requests.
6464
try (DisksClient client = DisksClient.create()) {
@@ -80,7 +80,7 @@ public static Disk createDiskInStoragePool(String projectId, String zone, String
8080
.build();
8181

8282
// Wait for the insert disk operation to complete.
83-
Operation operation = client.insertAsync(request).get(1, TimeUnit.MINUTES);
83+
Operation operation = client.insertAsync(request).get();
8484

8585
if (operation.hasError()) {
8686
System.out.println("Disk creation failed!");

compute/cloud-client/src/main/java/compute/disks/storagepool/CreateHyperdiskStoragePool.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@ public static void main(String[] args)
6161
public static StoragePool createHyperdiskStoragePool(String projectId, String zone,
6262
String storagePoolName, String storagePoolType, String capacityProvisioningType,
6363
long capacity, long iops, long throughput, String performanceProvisioningType)
64-
throws IOException, ExecutionException, InterruptedException, TimeoutException {
64+
throws IOException, ExecutionException, InterruptedException {
6565
// Initialize client that will be used to send requests. This client only needs to be created
6666
// once, and can be reused for multiple requests.
6767
try (StoragePoolsClient client = StoragePoolsClient.create()) {
@@ -84,7 +84,7 @@ public static StoragePool createHyperdiskStoragePool(String projectId, String zo
8484
.build();
8585

8686
// Wait for the insert disk operation to complete.
87-
Operation operation = client.insertAsync(request).get(1, TimeUnit.MINUTES);
87+
Operation operation = client.insertAsync(request).get();
8888

8989
if (operation.hasError()) {
9090
System.out.println("StoragePool creation failed!");

compute/cloud-client/src/test/java/compute/disks/ConsistencyGroupIT.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,14 +35,15 @@
3535
import compute.disks.consistencygroup.CreateDiskConsistencyGroup;
3636
import compute.disks.consistencygroup.DeleteDiskConsistencyGroup;
3737
import compute.disks.consistencygroup.RemoveDiskFromConsistencyGroup;
38+
import java.util.concurrent.TimeUnit;
3839
import org.junit.jupiter.api.Test;
3940
import org.junit.jupiter.api.Timeout;
4041
import org.junit.runner.RunWith;
4142
import org.junit.runners.JUnit4;
4243
import org.mockito.MockedStatic;
4344

4445
@RunWith(JUnit4.class)
45-
@Timeout(value = 60)
46+
@Timeout(value = 2, unit = TimeUnit.MINUTES)
4647
public class ConsistencyGroupIT {
4748
private static final String PROJECT_ID = "project-id";
4849
private static final String REGION = "asia-east1";
Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
1+
/*
2+
* Copyright 2024 Google LLC
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package compute.disks;
18+
19+
import static com.google.common.truth.Truth.assertWithMessage;
20+
21+
import com.google.cloud.compute.v1.Disk;
22+
import java.io.IOException;
23+
import java.util.UUID;
24+
import java.util.concurrent.ExecutionException;
25+
import java.util.concurrent.TimeUnit;
26+
import java.util.concurrent.TimeoutException;
27+
import org.junit.Assert;
28+
import org.junit.jupiter.api.AfterAll;
29+
import org.junit.jupiter.api.BeforeAll;
30+
import org.junit.jupiter.api.Test;
31+
import org.junit.jupiter.api.Timeout;
32+
import org.junit.runner.RunWith;
33+
import org.junit.runners.JUnit4;
34+
35+
@RunWith(JUnit4.class)
36+
@Timeout(value = 3, unit = TimeUnit.MINUTES)
37+
public class CreateHyperdiskIT {
38+
private static final String PROJECT_ID = System.getenv("GOOGLE_CLOUD_PROJECT");
39+
private static final String ZONE = "us-west1-a";
40+
private static final String HYPERDISK_NAME = "test-hyperdisk-enc-" + UUID.randomUUID();
41+
42+
// Check if the required environment variables are set.
43+
public static void requireEnvVar(String envVarName) {
44+
assertWithMessage(String.format("Missing environment variable '%s' ", envVarName))
45+
.that(System.getenv(envVarName)).isNotEmpty();
46+
}
47+
48+
@BeforeAll
49+
public static void setUp()
50+
throws IOException, ExecutionException, InterruptedException, TimeoutException {
51+
requireEnvVar("GOOGLE_APPLICATION_CREDENTIALS");
52+
requireEnvVar("GOOGLE_CLOUD_PROJECT");
53+
}
54+
55+
@AfterAll
56+
public static void cleanup()
57+
throws IOException, InterruptedException, ExecutionException, TimeoutException {
58+
// Delete disk created for testing.
59+
DeleteDisk.deleteDisk(PROJECT_ID, ZONE, HYPERDISK_NAME);
60+
}
61+
62+
@Test
63+
public void testCreateHyperdisk()
64+
throws IOException, ExecutionException, InterruptedException, TimeoutException {
65+
String diskType = String.format("zones/%s/diskTypes/hyperdisk-balanced", ZONE);
66+
67+
Disk hyperdisk = CreateHyperdisk
68+
.createHyperdisk(PROJECT_ID, ZONE, HYPERDISK_NAME, diskType,
69+
10, 3000, 140);
70+
71+
Assert.assertNotNull(hyperdisk);
72+
Assert.assertEquals(HYPERDISK_NAME, hyperdisk.getName());
73+
Assert.assertEquals(3000, hyperdisk.getProvisionedIops());
74+
Assert.assertEquals(140, hyperdisk.getProvisionedThroughput());
75+
Assert.assertEquals(10, hyperdisk.getSizeGb());
76+
Assert.assertTrue(hyperdisk.getType().contains("hyperdisk-balanced"));
77+
Assert.assertTrue(hyperdisk.getZone().contains(ZONE));
78+
}
79+
}
Lines changed: 131 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,131 @@
1+
/*
2+
* Copyright 2024 Google LLC
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package compute.disks;
18+
19+
import static org.junit.Assert.assertEquals;
20+
import static org.mockito.ArgumentMatchers.any;
21+
import static org.mockito.Mockito.mock;
22+
import static org.mockito.Mockito.mockStatic;
23+
import static org.mockito.Mockito.times;
24+
import static org.mockito.Mockito.verify;
25+
import static org.mockito.Mockito.when;
26+
27+
import com.google.api.gax.longrunning.OperationFuture;
28+
import com.google.cloud.compute.v1.Disk;
29+
import com.google.cloud.compute.v1.DisksClient;
30+
import com.google.cloud.compute.v1.InsertDiskRequest;
31+
import com.google.cloud.compute.v1.InsertStoragePoolRequest;
32+
import com.google.cloud.compute.v1.Operation;
33+
import com.google.cloud.compute.v1.StoragePool;
34+
import com.google.cloud.compute.v1.StoragePoolsClient;
35+
import compute.disks.storagepool.CreateDiskInStoragePool;
36+
import compute.disks.storagepool.CreateHyperdiskStoragePool;
37+
import java.util.concurrent.TimeUnit;
38+
import org.junit.jupiter.api.Test;
39+
import org.junit.jupiter.api.Timeout;
40+
import org.junit.runner.RunWith;
41+
import org.junit.runners.JUnit4;
42+
import org.mockito.MockedStatic;
43+
import org.mockito.Mockito;
44+
45+
@RunWith(JUnit4.class)
46+
@Timeout(value = 5, unit = TimeUnit.MINUTES)
47+
public class HyperdiskIT {
48+
private static final String PROJECT_ID = "project-id";
49+
private static final String ZONE = "asia-east1-a";
50+
private static final String HYPERDISK_IN_POOL_NAME = "hyperdisk";
51+
private static final String STORAGE_POOL_NAME = "storage-pool";
52+
private static final String PERFORMANCE_PROVISIONING_TYPE = "advanced";
53+
private static final String CAPACITY_PROVISIONING_TYPE = "advanced";
54+
55+
@Test
56+
public void testCreateHyperdiskStoragePool() throws Exception {
57+
String poolType = String.format(
58+
"projects/%s/zones/%s/storagePoolTypes/%s", PROJECT_ID, ZONE, "hyperdisk-balanced");
59+
StoragePool storagePool = StoragePool.newBuilder()
60+
.setZone(ZONE)
61+
.setName(STORAGE_POOL_NAME)
62+
.setStoragePoolType(poolType)
63+
.setCapacityProvisioningType(CAPACITY_PROVISIONING_TYPE)
64+
.setPoolProvisionedCapacityGb(10240)
65+
.setPoolProvisionedIops(10000)
66+
.setPoolProvisionedThroughput(1024)
67+
.setPerformanceProvisioningType(PERFORMANCE_PROVISIONING_TYPE)
68+
.build();
69+
try (MockedStatic<StoragePoolsClient> mockedStoragePoolsClient =
70+
mockStatic(StoragePoolsClient.class)) {
71+
StoragePoolsClient mockClient = mock(StoragePoolsClient.class);
72+
OperationFuture<Operation, Operation> mockFuture =
73+
mock(OperationFuture.class, Mockito.RETURNS_DEEP_STUBS);
74+
Operation operation = mock(Operation.class, Mockito.RETURNS_DEEP_STUBS);
75+
76+
mockedStoragePoolsClient.when(StoragePoolsClient::create).thenReturn(mockClient);
77+
when(mockClient.insertAsync(any(InsertStoragePoolRequest.class)))
78+
.thenReturn(mockFuture);
79+
when(mockFuture.get()).thenReturn(operation);
80+
when(operation.getStatus()).thenReturn(Operation.Status.DONE);
81+
when(mockClient.get(PROJECT_ID, ZONE, STORAGE_POOL_NAME)).thenReturn(storagePool);
82+
83+
84+
StoragePool expectedStoragePool = CreateHyperdiskStoragePool
85+
.createHyperdiskStoragePool(PROJECT_ID, ZONE, STORAGE_POOL_NAME, poolType,
86+
CAPACITY_PROVISIONING_TYPE, 10240, 10000, 1024,
87+
PERFORMANCE_PROVISIONING_TYPE);
88+
89+
verify(mockClient, times(1)).insertAsync(any(InsertStoragePoolRequest.class));
90+
verify(mockFuture, times(1)).get();
91+
assertEquals(storagePool, expectedStoragePool);
92+
}
93+
}
94+
95+
@Test
96+
public void testCreateDiskInStoragePool() throws Exception {
97+
String diskType = String.format("zones/%s/diskTypes/hyperdisk-balanced", ZONE);
98+
Disk expectedHyperdisk = Disk.newBuilder()
99+
.setZone(ZONE)
100+
.setName(HYPERDISK_IN_POOL_NAME)
101+
.setType(diskType)
102+
.setSizeGb(10L)
103+
.setProvisionedIops(3000L)
104+
.setProvisionedThroughput(140L)
105+
.build();
106+
String storagePoolLink = String.format("https://www.googleapis.com/compute/v1/projects/%s/zones/%s/storagePools/%s",
107+
PROJECT_ID, ZONE, STORAGE_POOL_NAME);
108+
109+
try (MockedStatic<DisksClient> mockedDisksClient = mockStatic(DisksClient.class)) {
110+
DisksClient mockClient = mock(DisksClient.class);
111+
OperationFuture<Operation, Operation> mockFuture =
112+
mock(OperationFuture.class, Mockito.RETURNS_DEEP_STUBS);
113+
Operation operation = mock(Operation.class, Mockito.RETURNS_DEEP_STUBS);
114+
115+
mockedDisksClient.when(DisksClient::create).thenReturn(mockClient);
116+
when(mockClient.insertAsync(any(InsertDiskRequest.class))).thenReturn(mockFuture);
117+
when(mockFuture.get()).thenReturn(operation);
118+
when(operation.getStatus()).thenReturn(Operation.Status.DONE);
119+
when(mockClient.get(PROJECT_ID, ZONE, HYPERDISK_IN_POOL_NAME)).thenReturn(expectedHyperdisk);
120+
121+
122+
Disk returnedDisk = CreateDiskInStoragePool
123+
.createDiskInStoragePool(PROJECT_ID, ZONE, HYPERDISK_IN_POOL_NAME, storagePoolLink,
124+
diskType, 10, 3000, 140);
125+
126+
verify(mockClient, times(1)).insertAsync(any(InsertDiskRequest.class));
127+
verify(mockFuture, times(1)).get();
128+
assertEquals(expectedHyperdisk, returnedDisk);
129+
}
130+
}
131+
}

0 commit comments

Comments
 (0)