From 63d937ef80d6596b81fcf84e1a826ac320b77eca Mon Sep 17 00:00:00 2001 From: Ajit Thakor <49403056+athakor@users.noreply.github.com> Date: Fri, 14 Feb 2020 22:11:15 +0530 Subject: [PATCH] test: add system test for update redis instance Fixes #64 --- .../google/cloud/redis/it/ITSystemTest.java | 96 --------------- .../cloud/redis/v1beta1/it/ITSystemTest.java | 109 ++++++++++++++++++ 2 files changed, 109 insertions(+), 96 deletions(-) delete mode 100644 google-cloud-redis/src/test/java/com/google/cloud/redis/it/ITSystemTest.java create mode 100644 google-cloud-redis/src/test/java/com/google/cloud/redis/v1beta1/it/ITSystemTest.java diff --git a/google-cloud-redis/src/test/java/com/google/cloud/redis/it/ITSystemTest.java b/google-cloud-redis/src/test/java/com/google/cloud/redis/it/ITSystemTest.java deleted file mode 100644 index 2c4b2444..00000000 --- a/google-cloud-redis/src/test/java/com/google/cloud/redis/it/ITSystemTest.java +++ /dev/null @@ -1,96 +0,0 @@ -/* - * Copyright 2019 Google LLC - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -package com.google.cloud.redis.it; - -import static org.junit.Assert.assertEquals; - -import com.google.cloud.ServiceOptions; -import com.google.cloud.redis.v1beta1.CloudRedisClient; -import com.google.cloud.redis.v1beta1.Instance; -import com.google.cloud.redis.v1beta1.InstanceName; -import com.google.cloud.redis.v1beta1.LocationName; -import com.google.common.collect.Lists; -import java.util.List; -import java.util.UUID; -import java.util.logging.Logger; -import org.junit.AfterClass; -import org.junit.BeforeClass; -import org.junit.Test; - -public class ITSystemTest { - - private static CloudRedisClient client; - private static String projectId; - - private static final Logger log = Logger.getLogger(ITSystemTest.class.getName()); - private static final Instance.Tier TIER = Instance.Tier.BASIC; - private static final String INSTANCE_NAME_PREFIX = "test-instance"; - private static final String INSTANCE = - INSTANCE_NAME_PREFIX + "-" + UUID.randomUUID().toString().substring(0, 8); - private static final String LOCATION = "us-central1"; - private static final int MEMORY_SIZE_GB = 1; - private static final String AUTHORIZED_NETWORK = System.getProperty("redis.network", "default"); - - @BeforeClass - public static void beforeClass() throws Exception { - client = CloudRedisClient.create(); - projectId = ServiceOptions.getDefaultProjectId(); - - /** Creates a Redis instance based on the specified tier and memory size. */ - LocationName parent = LocationName.of(projectId, LOCATION); - String authorizedNetwork = "projects/" + projectId + "/global/networks/" + AUTHORIZED_NETWORK; - Instance instance = - Instance.newBuilder() - .setTier(TIER) - .setMemorySizeGb(MEMORY_SIZE_GB) - .setAuthorizedNetwork(authorizedNetwork) - .build(); - client.createInstanceAsync(parent, INSTANCE, instance).get(); - log.info("redis instance created successfully."); - } - - @AfterClass - public static void afterClass() { - - /** Deletes a specific Redis instance. Instance stops serving and data is deleted. */ - InstanceName name = InstanceName.of(projectId, LOCATION, INSTANCE); - client.deleteInstanceAsync(name); - log.info("redis instance deleted successfully."); - client.close(); - } - - @Test - public void testGetInstance() { - InstanceName name = InstanceName.of(projectId, LOCATION, INSTANCE); - Instance response = client.getInstance(name); - assertEquals(TIER, response.getTier()); - assertEquals(MEMORY_SIZE_GB, response.getMemorySizeGb()); - } - - @Test - public void testListInstance() { - LocationName parent = LocationName.of(projectId, LOCATION); - CloudRedisClient.ListInstancesPagedResponse pagedListResponse = client.listInstances(parent); - List resources = Lists.newArrayList(pagedListResponse.iterateAll()); - int instance = 0, count = 0; - while (instance < resources.size()) { - count++; - instance++; - } - assertEquals(count, resources.size()); - } -} diff --git a/google-cloud-redis/src/test/java/com/google/cloud/redis/v1beta1/it/ITSystemTest.java b/google-cloud-redis/src/test/java/com/google/cloud/redis/v1beta1/it/ITSystemTest.java new file mode 100644 index 00000000..ece5262f --- /dev/null +++ b/google-cloud-redis/src/test/java/com/google/cloud/redis/v1beta1/it/ITSystemTest.java @@ -0,0 +1,109 @@ +/* + * Copyright 2019 Google LLC + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package com.google.cloud.redis.v1beta1.it; + +import static org.junit.Assert.assertEquals; + +import com.google.cloud.ServiceOptions; +import com.google.cloud.redis.v1beta1.CloudRedisClient; +import com.google.cloud.redis.v1beta1.Instance; +import com.google.cloud.redis.v1beta1.InstanceName; +import com.google.cloud.redis.v1beta1.LocationName; +import com.google.cloud.redis.v1beta1.UpdateInstanceRequest; +import com.google.common.collect.Lists; +import com.google.protobuf.FieldMask; +import java.util.Arrays; +import java.util.List; +import java.util.UUID; +import java.util.concurrent.ExecutionException; +import java.util.logging.Logger; +import org.junit.AfterClass; +import org.junit.BeforeClass; +import org.junit.Test; + +public class ITSystemTest { + + private static CloudRedisClient client; + + private static final Logger LOG = Logger.getLogger(ITSystemTest.class.getName()); + private static final String PROJECT_ID = ServiceOptions.getDefaultProjectId(); + private static final String NETWORK = System.getProperty("redis.network", "default"); + private static final String INSTANCE = + "test-instance-" + UUID.randomUUID().toString().substring(0, 8); + private static final String LOCATION = "us-central1"; + private static final String AUTHORIZED_NETWORK = + "projects/" + PROJECT_ID + "/global/networks/" + NETWORK; + private static final Instance.Tier TIER = Instance.Tier.BASIC; + private static final LocationName PARENT = LocationName.of(PROJECT_ID, LOCATION); + private static final InstanceName INSTANCE_NAME = InstanceName.of(PROJECT_ID, LOCATION, INSTANCE); + + @BeforeClass + public static void setUp() throws Exception { + client = CloudRedisClient.create(); + /* Creates a Redis instance based on the specified tier and memory size. */ + Instance instance = + Instance.newBuilder() + .setTier(TIER) + .setMemorySizeGb(1) + .setAuthorizedNetwork(AUTHORIZED_NETWORK) + .build(); + client.createInstanceAsync(PARENT, INSTANCE, instance).get(); + LOG.info("redis instance created successfully."); + } + + @AfterClass + public static void tearDown() { + /* Deletes a specific Redis instance. Instance stops serving and data is deleted. */ + client.deleteInstanceAsync(INSTANCE_NAME); + LOG.info("redis instance deleted successfully."); + client.close(); + } + + @Test + public void testGetInstance() { + Instance response = client.getInstance(INSTANCE_NAME); + assertEquals(TIER, response.getTier()); + assertEquals(INSTANCE_NAME.toString(), response.getName()); + } + + @Test + public void testListInstances() { + List instances = Lists.newArrayList(client.listInstances(PARENT).iterateAll()); + for (Instance instance : instances) { + if (INSTANCE_NAME.toString().equals(instance.getName())) { + assertEquals(TIER, instance.getTier()); + assertEquals(INSTANCE_NAME.toString(), instance.getName()); + } + } + } + + @Test + public void testUpdateInstance() throws ExecutionException, InterruptedException { + int memorySizeGb = 4; + FieldMask updateMask = + FieldMask.newBuilder().addAllPaths(Arrays.asList("memory_size_gb")).build(); + Instance instance = + Instance.newBuilder() + .setName(INSTANCE_NAME.toString()) + .setMemorySizeGb(memorySizeGb) + .build(); + UpdateInstanceRequest updateInstanceRequest = + UpdateInstanceRequest.newBuilder().setInstance(instance).setUpdateMask(updateMask).build(); + Instance actualInstance = client.updateInstanceAsync(updateInstanceRequest).get(); + assertEquals(memorySizeGb, actualInstance.getMemorySizeGb()); + } +}