|
| 1 | +/* |
| 2 | + * Copyright 2020 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 | + * https://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 com.google.cloud.notebooks.v1beta1.it; |
| 18 | + |
| 19 | +import static org.junit.Assert.assertEquals; |
| 20 | +import static org.junit.Assert.assertTrue; |
| 21 | + |
| 22 | +import com.google.api.FieldBehavior; |
| 23 | +import com.google.cloud.ServiceOptions; |
| 24 | +import com.google.cloud.notebooks.v1beta1.ContainerImage; |
| 25 | +import com.google.cloud.notebooks.v1beta1.CreateEnvironmentRequest; |
| 26 | +import com.google.cloud.notebooks.v1beta1.CreateInstanceRequest; |
| 27 | +import com.google.cloud.notebooks.v1beta1.DeleteEnvironmentRequest; |
| 28 | +import com.google.cloud.notebooks.v1beta1.DeleteInstanceRequest; |
| 29 | +import com.google.cloud.notebooks.v1beta1.Environment; |
| 30 | +import com.google.cloud.notebooks.v1beta1.GetEnvironmentRequest; |
| 31 | +import com.google.cloud.notebooks.v1beta1.GetInstanceRequest; |
| 32 | +import com.google.cloud.notebooks.v1beta1.Instance; |
| 33 | +import com.google.cloud.notebooks.v1beta1.ListEnvironmentsRequest; |
| 34 | +import com.google.cloud.notebooks.v1beta1.ListInstancesRequest; |
| 35 | +import com.google.cloud.notebooks.v1beta1.NotebookServiceClient; |
| 36 | +import com.google.cloud.notebooks.v1beta1.ResetInstanceRequest; |
| 37 | +import com.google.cloud.notebooks.v1beta1.SetInstanceAcceleratorRequest; |
| 38 | +import com.google.cloud.notebooks.v1beta1.SetInstanceLabelsRequest; |
| 39 | +import com.google.cloud.notebooks.v1beta1.SetInstanceMachineTypeRequest; |
| 40 | +import com.google.cloud.notebooks.v1beta1.StartInstanceRequest; |
| 41 | +import com.google.cloud.notebooks.v1beta1.StopInstanceRequest; |
| 42 | +import java.io.IOException; |
| 43 | +import java.util.UUID; |
| 44 | +import java.util.concurrent.ExecutionException; |
| 45 | +import org.junit.AfterClass; |
| 46 | +import org.junit.BeforeClass; |
| 47 | +import org.junit.Test; |
| 48 | + |
| 49 | +public class ITNotebookServiceClientTest { |
| 50 | + |
| 51 | + private static final String PROJECT_ID = ServiceOptions.getDefaultProjectId(); |
| 52 | + private static final String LOCATION = "us-central1-a"; |
| 53 | + private static final String PARENT = "projects/" + PROJECT_ID + "/locations/" + LOCATION; |
| 54 | + private static NotebookServiceClient client; |
| 55 | + private static final String ID = UUID.randomUUID().toString().substring(0, 8); |
| 56 | + private static final String NOTEBOOK_INSTANCE_ID = "test-notebook-instance-id-" + ID; |
| 57 | + private static final String ENVIRONMENT_ID = "test-environment-id-" + ID; |
| 58 | + private static final String INSTANCE_NAME = PARENT + "/instances/" + NOTEBOOK_INSTANCE_ID; |
| 59 | + private static final String ENVIRONMENT_NAME = PARENT + "/environments/" + ENVIRONMENT_ID; |
| 60 | + private static Instance expectedNotebookInstance; |
| 61 | + private static Environment expectedEnvironmentResponse; |
| 62 | + private static final String MACHINE_TYPE_A = "n1-standard-4"; |
| 63 | + private static final String MACHINE_TYPE_B = "n1-standard-2"; |
| 64 | + |
| 65 | + @BeforeClass |
| 66 | + public static void setUp() throws IOException, ExecutionException, InterruptedException { |
| 67 | + // Create Test Notebook Instance |
| 68 | + client = NotebookServiceClient.create(); |
| 69 | + ContainerImage containerImage = |
| 70 | + ContainerImage.newBuilder().setRepository(FieldBehavior.OPTIONAL.name()).build(); |
| 71 | + |
| 72 | + Environment environment = |
| 73 | + Environment.newBuilder() |
| 74 | + .setName(ENVIRONMENT_NAME) |
| 75 | + .setContainerImage(containerImage) |
| 76 | + .build(); |
| 77 | + |
| 78 | + CreateEnvironmentRequest environmentRequest = |
| 79 | + CreateEnvironmentRequest.newBuilder() |
| 80 | + .setParent(PARENT) |
| 81 | + .setEnvironmentId(ENVIRONMENT_ID) |
| 82 | + .setEnvironment(environment) |
| 83 | + .build(); |
| 84 | + |
| 85 | + expectedEnvironmentResponse = client.createEnvironmentAsync(environmentRequest).get(); |
| 86 | + |
| 87 | + Instance notebookInstance = |
| 88 | + Instance.newBuilder() |
| 89 | + .setContainerImage(containerImage) |
| 90 | + .setMachineType(MACHINE_TYPE_A) |
| 91 | + .build(); |
| 92 | + CreateInstanceRequest instanceRequest = |
| 93 | + CreateInstanceRequest.newBuilder() |
| 94 | + .setParent(PARENT) |
| 95 | + .setInstanceId(NOTEBOOK_INSTANCE_ID) |
| 96 | + .setInstance(notebookInstance) |
| 97 | + .build(); |
| 98 | + expectedNotebookInstance = client.createInstanceAsync(instanceRequest).get(); |
| 99 | + } |
| 100 | + |
| 101 | + @AfterClass |
| 102 | + public static void tearDown() throws ExecutionException, InterruptedException { |
| 103 | + // Delete Test Environment Instance |
| 104 | + DeleteEnvironmentRequest deleteEnvironmentRequest = |
| 105 | + DeleteEnvironmentRequest.newBuilder().setName(ENVIRONMENT_NAME).build(); |
| 106 | + client.deleteEnvironmentAsync(deleteEnvironmentRequest).get(); |
| 107 | + // Delete Test Notebook Instance |
| 108 | + DeleteInstanceRequest deleteInstanceRequest = |
| 109 | + DeleteInstanceRequest.newBuilder().setName(INSTANCE_NAME).build(); |
| 110 | + client.deleteInstanceAsync(deleteInstanceRequest).get(); |
| 111 | + client.close(); |
| 112 | + } |
| 113 | + |
| 114 | + private String stopInstance() throws ExecutionException, InterruptedException { |
| 115 | + StopInstanceRequest request = StopInstanceRequest.newBuilder().setName(INSTANCE_NAME).build(); |
| 116 | + Instance response = client.stopInstanceAsync(request).get(); |
| 117 | + return response.getState().name(); |
| 118 | + } |
| 119 | + |
| 120 | + private String startInstance() throws ExecutionException, InterruptedException { |
| 121 | + StartInstanceRequest request = StartInstanceRequest.newBuilder().setName(INSTANCE_NAME).build(); |
| 122 | + Instance response = client.startInstanceAsync(request).get(); |
| 123 | + return response.getState().name(); |
| 124 | + } |
| 125 | + |
| 126 | + @Test |
| 127 | + public void testGetInstance() { |
| 128 | + GetInstanceRequest request = GetInstanceRequest.newBuilder().setName(INSTANCE_NAME).build(); |
| 129 | + Instance actualNotebookInstance = client.getInstance(request); |
| 130 | + assertEquals( |
| 131 | + expectedNotebookInstance.getContainerImage(), actualNotebookInstance.getContainerImage()); |
| 132 | + assertEquals(expectedNotebookInstance.getName(), actualNotebookInstance.getName()); |
| 133 | + assertEquals(expectedNotebookInstance.getNetwork(), actualNotebookInstance.getNetwork()); |
| 134 | + assertEquals(expectedNotebookInstance.getSubnet(), actualNotebookInstance.getSubnet()); |
| 135 | + } |
| 136 | + |
| 137 | + @Test |
| 138 | + public void testListInstances() { |
| 139 | + ListInstancesRequest request = ListInstancesRequest.newBuilder().setParent(PARENT).build(); |
| 140 | + for (Instance element : client.listInstances(request).iterateAll()) { |
| 141 | + if (element.getName().equals(NOTEBOOK_INSTANCE_ID)) { |
| 142 | + assertEquals(expectedNotebookInstance.getContainerImage(), element.getContainerImage()); |
| 143 | + assertEquals(expectedNotebookInstance.getName(), element.getName()); |
| 144 | + assertEquals(expectedNotebookInstance.getMachineType(), element.getMachineType()); |
| 145 | + assertEquals(expectedNotebookInstance.getNetwork(), element.getNetwork()); |
| 146 | + assertEquals(expectedNotebookInstance.getSubnet(), element.getSubnet()); |
| 147 | + } |
| 148 | + } |
| 149 | + } |
| 150 | + |
| 151 | + @Test |
| 152 | + public void testGetEnvironment() { |
| 153 | + GetEnvironmentRequest environmentRequest = |
| 154 | + GetEnvironmentRequest.newBuilder().setName(ENVIRONMENT_NAME).build(); |
| 155 | + Environment expectedEnvironmentResponse = client.getEnvironment(environmentRequest); |
| 156 | + assertEquals(expectedEnvironmentResponse.getName(), expectedEnvironmentResponse.getName()); |
| 157 | + assertEquals( |
| 158 | + expectedEnvironmentResponse.getContainerImage(), |
| 159 | + expectedEnvironmentResponse.getContainerImage()); |
| 160 | + } |
| 161 | + |
| 162 | + @Test |
| 163 | + public void testListEnvironment() { |
| 164 | + ListEnvironmentsRequest request = |
| 165 | + ListEnvironmentsRequest.newBuilder().setParent(PARENT).build(); |
| 166 | + for (Environment element : client.listEnvironments(request).iterateAll()) { |
| 167 | + if (element.getName().equals(ENVIRONMENT_ID)) { |
| 168 | + assertEquals(expectedEnvironmentResponse.getName(), element.getName()); |
| 169 | + assertEquals(expectedEnvironmentResponse.getContainerImage(), element.getContainerImage()); |
| 170 | + } |
| 171 | + } |
| 172 | + } |
| 173 | + |
| 174 | + @Test |
| 175 | + public void testSetInstanceLabels() throws ExecutionException, InterruptedException { |
| 176 | + SetInstanceLabelsRequest setInstanceLabelsRequest = |
| 177 | + SetInstanceLabelsRequest.newBuilder().setName(INSTANCE_NAME).putLabels("a", "100").build(); |
| 178 | + Instance response = client.setInstanceLabelsAsync(setInstanceLabelsRequest).get(); |
| 179 | + assertEquals(response.getLabelsMap().get("a"), "100"); |
| 180 | + } |
| 181 | + |
| 182 | + @Test |
| 183 | + public void testSetInstanceMachineType() throws ExecutionException, InterruptedException { |
| 184 | + this.stopInstance(); |
| 185 | + SetInstanceMachineTypeRequest request = |
| 186 | + SetInstanceMachineTypeRequest.newBuilder() |
| 187 | + .setName(INSTANCE_NAME) |
| 188 | + .setMachineType(MACHINE_TYPE_B) |
| 189 | + .build(); |
| 190 | + Instance response = client.setInstanceMachineTypeAsync(request).get(); |
| 191 | + assertTrue(response.getMachineType().endsWith(MACHINE_TYPE_B)); |
| 192 | + this.startInstance(); |
| 193 | + } |
| 194 | + |
| 195 | + @Test |
| 196 | + public void testStartInstance() throws ExecutionException, InterruptedException { |
| 197 | + String state = this.stopInstance(); |
| 198 | + state = this.startInstance(); |
| 199 | + assertEquals(state, "PROVISIONING"); |
| 200 | + } |
| 201 | + |
| 202 | + @Test |
| 203 | + public void testStopInstance() throws ExecutionException, InterruptedException { |
| 204 | + String state = this.stopInstance(); |
| 205 | + assertEquals(state, "STOPPED"); |
| 206 | + state = this.startInstance(); |
| 207 | + } |
| 208 | + |
| 209 | + @Test |
| 210 | + public void testSetInstanceAccelarator() throws ExecutionException, InterruptedException { |
| 211 | + this.stopInstance(); |
| 212 | + SetInstanceAcceleratorRequest request = |
| 213 | + SetInstanceAcceleratorRequest.newBuilder() |
| 214 | + .setName(INSTANCE_NAME) |
| 215 | + .setType(Instance.AcceleratorType.NVIDIA_TESLA_P4) |
| 216 | + .setCoreCount(1L) |
| 217 | + .build(); |
| 218 | + Instance response = client.setInstanceAcceleratorAsync(request).get(); |
| 219 | + this.startInstance(); |
| 220 | + assertEquals( |
| 221 | + response.getAcceleratorConfig().getType().name(), |
| 222 | + Instance.AcceleratorType.NVIDIA_TESLA_P4.name()); |
| 223 | + } |
| 224 | + |
| 225 | + @Test |
| 226 | + public void testResetInstance() throws ExecutionException, InterruptedException { |
| 227 | + ResetInstanceRequest request = ResetInstanceRequest.newBuilder().setName(INSTANCE_NAME).build(); |
| 228 | + Instance response = client.resetInstanceAsync(request).get(); |
| 229 | + assertTrue(response.getMachineType().endsWith(MACHINE_TYPE_B)); |
| 230 | + } |
| 231 | +} |
0 commit comments