Skip to content

Commit 57f57d0

Browse files
Created test
1 parent 35b9bf4 commit 57f57d0

File tree

3 files changed

+128
-52
lines changed

3 files changed

+128
-52
lines changed

compute/cloud-client/src/main/java/compute/disks/consistencygroup/BulkCreateDisks.java

Lines changed: 0 additions & 52 deletions
This file was deleted.
Lines changed: 90 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,90 @@
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.consistencygroup;
18+
19+
// [START compute_consistency_group_clone]
20+
// If your disk has zonal location uncomment these lines
21+
//import com.google.cloud.compute.v1.DisksClient;
22+
//import com.google.cloud.compute.v1.BulkInsertDiskRequest;
23+
import com.google.cloud.compute.v1.BulkInsertDiskResource;
24+
import com.google.cloud.compute.v1.BulkInsertRegionDiskRequest;
25+
import com.google.cloud.compute.v1.Operation;
26+
import com.google.cloud.compute.v1.RegionDisksClient;
27+
import java.io.IOException;
28+
import java.util.concurrent.ExecutionException;
29+
import java.util.concurrent.TimeUnit;
30+
import java.util.concurrent.TimeoutException;
31+
32+
public class CloneDisksFromConsistencyGroup {
33+
34+
public static void main(String[] args)
35+
throws IOException, ExecutionException, InterruptedException, TimeoutException {
36+
// TODO(developer): Replace these variables before running the sample.
37+
// Project ID or project number of the Cloud project you want to use.
38+
String project = "YOUR_PROJECT_ID";
39+
// Name of the region or zone in which your disk is located.
40+
String disksLocation = "us-central1";
41+
// Name of the consistency group you want to clone disks from.
42+
String consistencyGroupName = "YOUR_CONSISTENCY_GROUP_NAME";
43+
// Name of the region in which your consistency group is located.
44+
String consistencyGroupLocation = "us-central1";
45+
46+
cloneDisksFromConsistencyGroup(
47+
project, disksLocation, consistencyGroupName, consistencyGroupLocation);
48+
}
49+
50+
// Clones disks from a consistency group.
51+
public static void cloneDisksFromConsistencyGroup(String project, String disksLocation,
52+
String consistencyGroupName, String consistencyGroupLocation)
53+
throws IOException, InterruptedException, ExecutionException, TimeoutException {
54+
String sourceConsistencyGroupPolicy = String.format(
55+
"projects/%s/regions/%s/resourcePolicies/%s", project, consistencyGroupLocation,
56+
consistencyGroupName);
57+
58+
//try (DisksClient disksClient = DisksClient.create()){
59+
// BulkInsertDiskRequest request = BulkInsertDiskRequest.newBuilder()
60+
// .setProject(project)
61+
// .setZone(disksLocation)
62+
// .setBulkInsertDiskResourceResource(
63+
// BulkInsertDiskResource.newBuilder()
64+
// .setSourceConsistencyGroupPolicy(sourceConsistencyGroupPolicy)
65+
// .build())
66+
// .build();
67+
//Operation response = disksClient.bulkInsertAsync(request).get(3, TimeUnit.MINUTES);
68+
69+
70+
try (RegionDisksClient regionDisksClient = RegionDisksClient.create()) {
71+
BulkInsertRegionDiskRequest request = BulkInsertRegionDiskRequest.newBuilder()
72+
.setProject(project)
73+
.setRegion(disksLocation)
74+
.setBulkInsertDiskResourceResource(
75+
BulkInsertDiskResource.newBuilder()
76+
.setSourceConsistencyGroupPolicy(sourceConsistencyGroupPolicy)
77+
.build())
78+
.build();
79+
80+
Operation response = regionDisksClient.bulkInsertAsync(request).get(3, TimeUnit.MINUTES);
81+
82+
if (response.hasError()) {
83+
System.out.println(String.format("Error cloning disks: %s", response.getError()));
84+
return;
85+
}
86+
System.out.printf("Disks cloned from consistency group: %s\n", consistencyGroupName);
87+
}
88+
}
89+
}
90+
// [END compute_consistency_group_clone]

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

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,19 +19,31 @@
1919
import static com.google.common.truth.Truth.assertThat;
2020
import static com.google.common.truth.Truth.assertWithMessage;
2121
import static org.junit.Assert.assertNotNull;
22+
import static org.mockito.Mockito.*;
2223

24+
import com.google.api.gax.longrunning.OperationFuture;
25+
import com.google.cloud.compute.v1.BulkInsertRegionDiskRequest;
26+
import com.google.cloud.compute.v1.Operation;
27+
import com.google.cloud.compute.v1.RegionDisksClient;
28+
import compute.disks.consistencygroup.CloneDisksFromConsistencyGroup;
2329
import compute.disks.consistencygroup.CreateDiskConsistencyGroup;
2430
import compute.disks.consistencygroup.DeleteDiskConsistencyGroup;
31+
32+
import java.io.ByteArrayOutputStream;
2533
import java.io.IOException;
34+
import java.io.PrintStream;
2635
import java.util.UUID;
2736
import java.util.concurrent.ExecutionException;
2837
import java.util.concurrent.TimeUnit;
38+
import java.util.concurrent.TimeoutException;
39+
2940
import org.junit.jupiter.api.AfterAll;
3041
import org.junit.jupiter.api.BeforeAll;
3142
import org.junit.jupiter.api.Test;
3243
import org.junit.jupiter.api.Timeout;
3344
import org.junit.runner.RunWith;
3445
import org.junit.runners.JUnit4;
46+
import org.mockito.MockedStatic;
3547

3648
@RunWith(JUnit4.class)
3749
@Timeout(value = 3, unit = TimeUnit.MINUTES)
@@ -41,6 +53,7 @@ public class ConsistencyGroupIT {
4153
private static final String CONSISTENCY_GROUP_NAME =
4254
"test-consistency-group-" + UUID.randomUUID();
4355

56+
4457
// Check if the required environment variables are set.
4558
public static void requireEnvVar(String envVarName) {
4659
assertWithMessage(String.format("Missing environment variable '%s' ", envVarName))
@@ -70,4 +83,29 @@ public void testCreateDiskConsistencyGroupResourcePolicy()
7083
assertNotNull(consistencyGroupLink);
7184
assertThat(consistencyGroupLink.contains(CONSISTENCY_GROUP_NAME));
7285
}
86+
87+
@Test
88+
public void testCloneDisksFromConsistencyGroup()
89+
throws IOException, InterruptedException, ExecutionException, TimeoutException {
90+
ByteArrayOutputStream bout = new ByteArrayOutputStream();
91+
System.setOut(new PrintStream(bout));
92+
try (MockedStatic<RegionDisksClient> mockedRegionDisksClient = mockStatic(RegionDisksClient.class)) {
93+
Operation mockOperation = mock(Operation.class);
94+
RegionDisksClient mockClient = mock(RegionDisksClient.class);
95+
OperationFuture mockFuture = mock(OperationFuture.class);
96+
97+
mockedRegionDisksClient.when(RegionDisksClient::create).thenReturn(mockClient);
98+
when(mockClient.bulkInsertAsync(any(BulkInsertRegionDiskRequest.class)))
99+
.thenReturn(mockFuture);
100+
when(mockFuture.get()).thenReturn(mockOperation);
101+
102+
CloneDisksFromConsistencyGroup.cloneDisksFromConsistencyGroup(PROJECT_ID, REGION,
103+
CONSISTENCY_GROUP_NAME, REGION);
104+
105+
106+
assertThat(bout).isEqualTo(String.format("Disks cloned from consistency group: %s", CONSISTENCY_GROUP_NAME));
107+
verify(mockClient, times(1))
108+
.bulkInsertAsync(any(BulkInsertRegionDiskRequest.class));
109+
}
110+
}
73111
}

0 commit comments

Comments
 (0)