def test_update(self): from gcloud._testing import _Monkey from gcloud.bigtable._generated import ( bigtable_cluster_data_pb2 as data_pb2) from gcloud.bigtable._generated import operations_pb2 from gcloud.bigtable._testing import _FakeStub from gcloud.bigtable import cluster as MUT project = 'PROJECT' zone = 'zone' cluster_id = 'cluster-id' serve_nodes = 81 display_name = 'display_name' timeout_seconds = 9 client = _Client(project, timeout_seconds=timeout_seconds) cluster = self._makeOne(zone, cluster_id, client, display_name=display_name, serve_nodes=serve_nodes) # Create request_pb cluster_name = ('projects/' + project + '/zones/' + zone + '/clusters/' + cluster_id) request_pb = data_pb2.Cluster( name=cluster_name, display_name=display_name, serve_nodes=serve_nodes, ) # Create response_pb current_op = operations_pb2.Operation() response_pb = data_pb2.Cluster(current_operation=current_op) # Patch the stub used by the API method. client._cluster_stub = stub = _FakeStub(response_pb) # Create expected_result. op_id = 5678 op_begin = object() expected_result = MUT.Operation('update', op_id, op_begin, cluster=cluster) # Create mocks process_operation_called = [] def mock_process_operation(operation_pb): process_operation_called.append(operation_pb) return op_id, op_begin # Perform the method and check the result. with _Monkey(MUT, _process_operation=mock_process_operation): result = cluster.update() self.assertEqual(result, expected_result) self.assertEqual(stub.method_calls, [( 'UpdateCluster', (request_pb, timeout_seconds), {}, )]) self.assertEqual(process_operation_called, [current_op])
def test_op_name_parsing_failure(self): from gcloud.bigtable._generated import ( bigtable_cluster_data_pb2 as data_pb2) from gcloud.bigtable._generated import operations_pb2 current_op = operations_pb2.Operation(name='invalid') cluster = data_pb2.Cluster(current_operation=current_op) with self.assertRaises(ValueError): self._callFUT(cluster)
def test_undelete(self): from gcloud._testing import _Monkey from gcloud.bigtable._generated import ( bigtable_cluster_service_messages_pb2 as messages_pb2) from gcloud.bigtable._generated import operations_pb2 from gcloud.bigtable._testing import _FakeStub from gcloud.bigtable import cluster as MUT project = 'PROJECT' zone = 'zone' cluster_id = 'cluster-id' timeout_seconds = 78 client = _Client(project, timeout_seconds=timeout_seconds) cluster = self._makeOne(zone, cluster_id, client) # Create request_pb cluster_name = ('projects/' + project + '/zones/' + zone + '/clusters/' + cluster_id) request_pb = messages_pb2.UndeleteClusterRequest(name=cluster_name) # Create response_pb response_pb = operations_pb2.Operation() # Patch the stub used by the API method. client._cluster_stub = stub = _FakeStub(response_pb) # Create expected_result. op_id = 5678 op_begin = object() expected_result = MUT.Operation('undelete', op_id, op_begin, cluster=cluster) # Create the mocks. process_operation_called = [] def mock_process_operation(operation_pb): process_operation_called.append(operation_pb) return op_id, op_begin # Perform the method and check the result. with _Monkey(MUT, _process_operation=mock_process_operation): result = cluster.undelete() self.assertEqual(result, expected_result) self.assertEqual(stub.method_calls, [( 'UndeleteCluster', (request_pb, timeout_seconds), {}, )]) self.assertEqual(process_operation_called, [response_pb])
def _finished_helper(self, done): import datetime from gcloud.bigtable._generated import operations_pb2 from gcloud.bigtable._testing import _FakeStub from gcloud.bigtable.cluster import Cluster project = 'PROJECT' zone = 'zone' cluster_id = 'cluster-id' op_type = 'fake-op' op_id = 789 begin = datetime.datetime(2015, 10, 22, 1, 1) timeout_seconds = 1 client = _Client(project, timeout_seconds=timeout_seconds) cluster = Cluster(zone, cluster_id, client) operation = self._makeOne(op_type, op_id, begin, cluster=cluster) # Create request_pb op_name = ('operations/projects/' + project + '/zones/' + zone + '/clusters/' + cluster_id + '/operations/%d' % (op_id,)) request_pb = operations_pb2.GetOperationRequest(name=op_name) # Create response_pb response_pb = operations_pb2.Operation(done=done) # Patch the stub used by the API method. client._operations_stub = stub = _FakeStub(response_pb) # Create expected_result. expected_result = done # Perform the method and check the result. result = operation.finished() self.assertEqual(result, expected_result) self.assertEqual(stub.method_calls, [( 'GetOperation', (request_pb, timeout_seconds), {}, )]) if done: self.assertTrue(operation._complete) else: self.assertFalse(operation._complete)
def test_it(self): from gcloud._testing import _Monkey from gcloud.bigtable._generated import ( bigtable_cluster_service_messages_pb2 as messages_pb2) from gcloud.bigtable._generated import operations_pb2 from gcloud.bigtable import cluster as MUT project = 'PROJECT' zone = 'zone' cluster_id = 'cluster-id' expected_operation_id = 234 operation_name = ('operations/projects/%s/zones/%s/clusters/%s/' 'operations/%d' % (project, zone, cluster_id, expected_operation_id)) current_op = operations_pb2.Operation(name=operation_name) # Create mocks. request_metadata = messages_pb2.CreateClusterMetadata() parse_pb_any_called = [] def mock_parse_pb_any_to_native(any_val, expected_type=None): parse_pb_any_called.append((any_val, expected_type)) return request_metadata expected_operation_begin = object() ts_to_dt_called = [] def mock_pb_timestamp_to_datetime(timestamp): ts_to_dt_called.append(timestamp) return expected_operation_begin # Exectute method with mocks in place. with _Monkey(MUT, _parse_pb_any_to_native=mock_parse_pb_any_to_native, _pb_timestamp_to_datetime=mock_pb_timestamp_to_datetime): operation_id, operation_begin = self._callFUT(current_op) # Check outputs. self.assertEqual(operation_id, expected_operation_id) self.assertTrue(operation_begin is expected_operation_begin) # Check mocks were used correctly. self.assertEqual(parse_pb_any_called, [(current_op.metadata, None)]) self.assertEqual(ts_to_dt_called, [request_metadata.request_time])
def test_create(self): from gcloud._testing import _Monkey from gcloud.bigtable._generated import ( bigtable_cluster_data_pb2 as data_pb2) from gcloud.bigtable._generated import operations_pb2 from gcloud.bigtable._testing import _FakeStub from gcloud.bigtable import cluster as MUT project = 'PROJECT' zone = 'zone' cluster_id = 'cluster-id' timeout_seconds = 578 client = _Client(project, timeout_seconds=timeout_seconds) cluster = self._makeOne(zone, cluster_id, client) # Create request_pb. Just a mock since we monkey patch # _prepare_create_request request_pb = object() # Create response_pb op_id = 5678 op_begin = object() op_name = ('operations/projects/%s/zones/%s/clusters/%s/' 'operations/%d' % (project, zone, cluster_id, op_id)) current_op = operations_pb2.Operation(name=op_name) response_pb = data_pb2.Cluster(current_operation=current_op) # Patch the stub used by the API method. client._cluster_stub = stub = _FakeStub(response_pb) # Create expected_result. expected_result = MUT.Operation('create', op_id, op_begin, cluster=cluster) # Create the mocks. prep_create_called = [] def mock_prep_create_req(cluster): prep_create_called.append(cluster) return request_pb process_operation_called = [] def mock_process_operation(operation_pb): process_operation_called.append(operation_pb) return op_id, op_begin # Perform the method and check the result. with _Monkey(MUT, _prepare_create_request=mock_prep_create_req, _process_operation=mock_process_operation): result = cluster.create() self.assertEqual(result, expected_result) self.assertEqual(stub.method_calls, [( 'CreateCluster', (request_pb, timeout_seconds), {}, )]) self.assertEqual(prep_create_called, [cluster]) self.assertEqual(process_operation_called, [current_op])