Skip to content

Commit 880b2ed

Browse files
authored
Iot fix tests (#1487)
* Adds function for clearing registry * Clean up registries in tests
1 parent 9772f04 commit 880b2ed

File tree

4 files changed

+180
-2
lines changed

4 files changed

+180
-2
lines changed

iot/api-client/manager/pom.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,7 @@
6262
<dependency>
6363
<groupId>com.google.apis</groupId>
6464
<artifactId>google-api-services-cloudiot</artifactId>
65-
<version>v1-rev20181120-1.27.0</version>
65+
<version>v1-rev77-1.25.0</version>
6666
</dependency>
6767
<dependency>
6868
<groupId>com.google.cloud</groupId>

iot/api-client/manager/src/main/java/com/example/cloud/iot/examples/DeviceRegistryExample.java

Lines changed: 121 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@
3535
import com.google.api.services.cloudiot.v1.model.GatewayConfig;
3636
import com.google.api.services.cloudiot.v1.model.GetIamPolicyRequest;
3737
import com.google.api.services.cloudiot.v1.model.ListDeviceStatesResponse;
38+
import com.google.api.services.cloudiot.v1.model.ListDevicesResponse;
3839
import com.google.api.services.cloudiot.v1.model.ModifyCloudToDeviceConfigRequest;
3940
import com.google.api.services.cloudiot.v1.model.PublicKeyCredential;
4041
import com.google.api.services.cloudiot.v1.model.SendCommandToDeviceRequest;
@@ -166,13 +167,128 @@ public static void deleteRegistry(String cloudRegion, String projectId, String r
166167

167168
final String registryPath =
168169
String.format(
169-
"projects/%s/locations/%s/registries/%s", projectId, cloudRegion, registryName);
170+
"projects/%s/locations/%s/registries/%s", projectId, cloudRegion, registryName);
170171

171172
System.out.println("Deleting: " + registryPath);
172173
service.projects().locations().registries().delete(registryPath).execute();
173174
}
174175
// [END iot_delete_registry]
175176

177+
/**
178+
* clearRegistry
179+
* <ul>
180+
* <li>Registries can't be deleted if they contain devices,</li>
181+
* <li>Gateways (a type of device) can't be deleted if they have bound devices</li>
182+
* <li>Devices can't be deleted if bound to gateways...</li>
183+
* </ul>
184+
* To completely remove a registry, you must unbind all devices from gateways,
185+
* then remove all devices in a registry before removing the registry.
186+
* As pseudocode:
187+
* <code>
188+
* ForAll gateways
189+
* ForAll devicesBoundToGateway
190+
* unbindDeviceFromGateway
191+
* ForAll devices
192+
* Delete device by ID
193+
* Delete registry
194+
* </code>
195+
*/
196+
// [START iot_clear_registry]
197+
public static void clearRegistry(String cloudRegion, String projectId, String registryName)
198+
throws GeneralSecurityException, IOException {
199+
GoogleCredential credential =
200+
GoogleCredential.getApplicationDefault().createScoped(CloudIotScopes.all());
201+
JsonFactory jsonFactory = JacksonFactory.getDefaultInstance();
202+
HttpRequestInitializer init = new RetryHttpInitializerWrapper(credential);
203+
final CloudIot service =
204+
new CloudIot.Builder(GoogleNetHttpTransport.newTrustedTransport(), jsonFactory, init)
205+
.setApplicationName(APP_NAME)
206+
.build();
207+
final String registryPath =
208+
String.format(
209+
"projects/%s/locations/%s/registries/%s", projectId, cloudRegion, registryName);
210+
211+
ListDevicesResponse listGatewaysRes =
212+
service
213+
.projects()
214+
.locations()
215+
.registries()
216+
.devices()
217+
.list(registryPath)
218+
.setGatewayListOptionsGatewayType("GATEWAY")
219+
.execute();
220+
List<Device> gateways = listGatewaysRes.getDevices();
221+
222+
// Unbind all devices from all gateways
223+
if (gateways != null) {
224+
System.out.println("Found " + gateways.size() + " devices");
225+
for (Device g : gateways) {
226+
String gatewayId = g.getId();
227+
System.out.println("Id: " + gatewayId);
228+
229+
ListDevicesResponse res =
230+
service
231+
.projects()
232+
.locations()
233+
.registries()
234+
.devices()
235+
.list(registryPath)
236+
.setGatewayListOptionsAssociationsGatewayId(gatewayId)
237+
.execute();
238+
List<Device> deviceNumIds = res.getDevices();
239+
240+
if (deviceNumIds != null) {
241+
System.out.println("Found " + deviceNumIds.size() + " devices");
242+
for (Device device : deviceNumIds) {
243+
String deviceId = device.getId();
244+
System.out.println(String.format("ID: %s", deviceId));
245+
246+
// Remove any bindings from the device
247+
UnbindDeviceFromGatewayRequest request = new UnbindDeviceFromGatewayRequest();
248+
request.setDeviceId(deviceId);
249+
request.setGatewayId(gatewayId);
250+
UnbindDeviceFromGatewayResponse response =
251+
service
252+
.projects()
253+
.locations()
254+
.registries()
255+
.unbindDeviceFromGateway(registryPath, request)
256+
.execute();
257+
}
258+
} else {
259+
System.out.println("Gateway has no bound devices.");
260+
}
261+
}
262+
}
263+
264+
// Remove all devices from the regsitry
265+
List<Device> devices =
266+
service
267+
.projects()
268+
.locations()
269+
.registries()
270+
.devices()
271+
.list(registryPath)
272+
.execute()
273+
.getDevices();
274+
275+
if (devices != null) {
276+
System.out.println("Found " + devices.size() + " devices");
277+
for (Device d : devices) {
278+
String deviceId = d.getId();
279+
String devicePath = String.format(
280+
"%s/devices/%s", registryPath, deviceId);
281+
service.projects().locations().registries().devices().delete(devicePath).execute();
282+
}
283+
}
284+
285+
// Delete the registry
286+
service.projects().locations().registries().delete(registryPath).execute();
287+
288+
}
289+
// [END iot_clear_registry]
290+
291+
176292
// [START iot_list_devices]
177293
/** Print all of the devices in this registry to standard out. */
178294
public static void listDevices(String projectId, String cloudRegion, String registryName)
@@ -1099,6 +1215,10 @@ public static void main(String[] args) throws Exception {
10991215
}
11001216

11011217
switch (options.command) {
1218+
case "clear-registry":
1219+
System.out.println("Clear registry");
1220+
clearRegistry(options.cloudRegion, options.projectId, options.registryName);
1221+
break;
11021222
case "create-iot-topic":
11031223
System.out.println("Create IoT Topic:");
11041224
createIotTopic(options.projectId, options.pubsubTopic);

iot/api-client/manager/src/main/java/com/example/cloud/iot/examples/DeviceRegistryExampleOptions.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,7 @@ public static DeviceRegistryExampleOptions fromFlags(String[] args) {
5252
.hasArg()
5353
.desc(
5454
"Command to run:"
55+
+ "\n\tclear-registry"
5556
+ "\n\tcreate-iot-topic" // TODO: Descriptions or too verbose?
5657
+ "\n\tcreate-rsa"
5758
+ "\n\tcreate-es"

iot/api-client/manager/src/test/java/com/example/cloud/iot/examples/ManagerIT.java

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,12 +16,22 @@
1616

1717
package com.example.cloud.iot.examples;
1818

19+
import com.google.api.client.googleapis.auth.oauth2.GoogleCredential;
20+
import com.google.api.client.googleapis.javanet.GoogleNetHttpTransport;
21+
import com.google.api.client.http.HttpRequestInitializer;
22+
import com.google.api.client.json.JsonFactory;
23+
import com.google.api.client.json.jackson2.JacksonFactory;
24+
import com.google.api.services.cloudiot.v1.CloudIot;
25+
import com.google.api.services.cloudiot.v1.CloudIotScopes;
26+
import com.google.api.services.cloudiot.v1.model.DeviceRegistry;
1927
import com.google.cloud.pubsub.v1.TopicAdminClient;
2028
import com.google.pubsub.v1.Topic;
2129

2230
import java.io.ByteArrayOutputStream;
2331
import java.io.PrintStream;
2432

33+
import java.util.List;
34+
2535
import org.eclipse.paho.client.mqttv3.MqttClient;
2636
import org.junit.After;
2737
import org.junit.Assert;
@@ -30,6 +40,7 @@
3040
import org.junit.runner.RunWith;
3141
import org.junit.runners.JUnit4;
3242

43+
3344
/** Tests for iot "Management" sample. */
3445
@RunWith(JUnit4.class)
3546
@SuppressWarnings("checkstyle:abbreviationaswordinname")
@@ -49,9 +60,15 @@ public class ManagerIT {
4960
private static final String ROLE = "roles/viewer";
5061

5162
private static Topic topic;
63+
private static boolean hasCleared = false;
5264

5365
@Before
5466
public void setUp() throws Exception {
67+
if (!hasCleared) {
68+
clearTestRegistries(); // Remove old / unused registries
69+
hasCleared = true;
70+
}
71+
5572
bout = new ByteArrayOutputStream();
5673
out = new PrintStream(bout);
5774
System.setOut(out);
@@ -62,6 +79,46 @@ public void tearDown() throws Exception {
6279
System.setOut(null);
6380
}
6481

82+
public void clearTestRegistries() throws Exception {
83+
GoogleCredential credential =
84+
GoogleCredential.getApplicationDefault().createScoped(CloudIotScopes.all());
85+
JsonFactory jsonFactory = JacksonFactory.getDefaultInstance();
86+
HttpRequestInitializer init = new RetryHttpInitializerWrapper(credential);
87+
final CloudIot service =
88+
new CloudIot.Builder(GoogleNetHttpTransport.newTrustedTransport(), jsonFactory, init)
89+
.setApplicationName("TEST")
90+
.build();
91+
92+
final String projectPath = "projects/" + PROJECT_ID + "/locations/" + CLOUD_REGION;
93+
94+
List<DeviceRegistry> registries =
95+
service
96+
.projects()
97+
.locations()
98+
.registries()
99+
.list(projectPath)
100+
.execute()
101+
.getDeviceRegistries();
102+
103+
if (registries != null) {
104+
for (DeviceRegistry r : registries) {
105+
String registryId = r.getId();
106+
if (registryId.startsWith("java-reg-")) {
107+
long currSecs = System.currentTimeMillis() / 1000L;
108+
long regSecs = Long.parseLong(registryId.substring(
109+
"java-reg-".length(), registryId.length()));
110+
long diffSecs = currSecs - regSecs;
111+
if (diffSecs > (60 * 60 * 24 * 7)) { // tests from last week or older
112+
System.out.println("Remove Id: " + r.getId());
113+
DeviceRegistryExample.clearRegistry(CLOUD_REGION, PROJECT_ID, registryId);
114+
}
115+
}
116+
}
117+
} else {
118+
System.out.println("Project has no registries.");
119+
}
120+
}
121+
65122
@Test
66123
public void testPatchRsa() throws Exception {
67124
final String deviceName = "patchme-device-rsa";

0 commit comments

Comments
 (0)