def test_update(self): from google.cloud.bigtable._generated import (instance_pb2 as data_v2_pb2) from unit_tests._testing import _FakeStub client = _Client(self.PROJECT) instance = self._make_one(self.INSTANCE_ID, client, self.LOCATION_ID, display_name=self.DISPLAY_NAME) # Create request_pb request_pb = data_v2_pb2.Instance( name=self.INSTANCE_NAME, display_name=self.DISPLAY_NAME, ) # Create response_pb response_pb = data_v2_pb2.Instance() # Patch the stub used by the API method. client._instance_stub = stub = _FakeStub(response_pb) # Create expected_result. expected_result = None # Perform the method and check the result. result = instance.update() self.assertEqual(result, expected_result) self.assertEqual(stub.method_calls, [( 'UpdateInstance', (request_pb, ), {}, )])
def test_from_pb_bad_instance_name(self): from google.cloud.bigtable._generated import (instance_pb2 as data_v2_pb2) instance_name = 'INCORRECT_FORMAT' instance_pb = data_v2_pb2.Instance(name=instance_name) klass = self._get_target_class() with self.assertRaises(ValueError): klass.from_pb(instance_pb, None)
def test__update_from_pb_no_display_name(self): from google.cloud.bigtable._generated import (instance_pb2 as data_v2_pb2) instance_pb = data_v2_pb2.Instance() instance = self._make_one(None, None, None, None) self.assertIsNone(instance.display_name) with self.assertRaises(ValueError): instance._update_from_pb(instance_pb) self.assertIsNone(instance.display_name)
def test__update_from_pb_success(self): from google.cloud.bigtable._generated import (instance_pb2 as data_v2_pb2) display_name = 'display_name' instance_pb = data_v2_pb2.Instance(display_name=display_name, ) instance = self._make_one(None, None, None, None) self.assertIsNone(instance.display_name) instance._update_from_pb(instance_pb) self.assertEqual(instance.display_name, display_name)
def test_from_pb_project_mistmatch(self): from google.cloud.bigtable._generated import (instance_pb2 as data_v2_pb2) ALT_PROJECT = 'ALT_PROJECT' client = _Client(project=ALT_PROJECT) self.assertNotEqual(self.PROJECT, ALT_PROJECT) instance_pb = data_v2_pb2.Instance(name=self.INSTANCE_NAME) klass = self._get_target_class() with self.assertRaises(ValueError): klass.from_pb(instance_pb, client)
def update(self): """Update this instance. .. note:: Updates the ``display_name``. To change that value before updating, reset its values via .. code:: python instance.display_name = 'New display name' before calling :meth:`update`. """ request_pb = data_v2_pb2.Instance( name=self.name, display_name=self.display_name, ) # Ignore the expected `data_v2_pb2.Instance`. self._client._instance_stub.UpdateInstance(request_pb)
def test_from_pb_success(self): from google.cloud.bigtable.instance import ( _EXISTING_INSTANCE_LOCATION_ID) from google.cloud.bigtable._generated import (instance_pb2 as data_v2_pb2) client = _Client(project=self.PROJECT) instance_pb = data_v2_pb2.Instance( name=self.INSTANCE_NAME, display_name=self.INSTANCE_ID, ) klass = self._get_target_class() instance = klass.from_pb(instance_pb, client) self.assertIsInstance(instance, klass) self.assertEqual(instance._client, client) self.assertEqual(instance.instance_id, self.INSTANCE_ID) self.assertEqual(instance._cluster_location_id, _EXISTING_INSTANCE_LOCATION_ID)
def _prepare_create_request(instance): """Creates a protobuf request for a CreateInstance request. :type instance: :class:`Instance` :param instance: The instance to be created. :rtype: :class:`.messages_v2_pb2.CreateInstanceRequest` :returns: The CreateInstance request object containing the instance info. """ parent_name = ('projects/' + instance._client.project) message = messages_v2_pb2.CreateInstanceRequest( parent=parent_name, instance_id=instance.instance_id, instance=data_v2_pb2.Instance(display_name=instance.display_name, ), ) cluster = message.clusters[instance.instance_id] cluster.name = instance.name + '/clusters/' + instance.instance_id cluster.location = (parent_name + '/locations/' + instance._cluster_location_id) cluster.serve_nodes = instance._cluster_serve_nodes return message
def test_reload(self): from google.cloud.bigtable._generated import ( instance_pb2 as data_v2_pb2) from google.cloud.bigtable._generated import ( bigtable_instance_admin_pb2 as messages_v2_pb) from unit_tests._testing import _FakeStub client = _Client(self.PROJECT) instance = self._make_one(self.INSTANCE_ID, client, self.LOCATION_ID) # Create request_pb request_pb = messages_v2_pb.GetInstanceRequest( name=self.INSTANCE_NAME) # Create response_pb DISPLAY_NAME = u'hey-hi-hello' response_pb = data_v2_pb2.Instance( display_name=DISPLAY_NAME, ) # Patch the stub used by the API method. client._instance_stub = stub = _FakeStub(response_pb) # Create expected_result. expected_result = None # reload() has no return value. # Check Instance optional config values before. self.assertEqual(instance.display_name, self.INSTANCE_ID) # Perform the method and check the result. result = instance.reload() self.assertEqual(result, expected_result) self.assertEqual(stub.method_calls, [( 'GetInstance', (request_pb,), {}, )]) # Check Instance optional config values before. self.assertEqual(instance.display_name, DISPLAY_NAME)
def test_list_instances(self): from google.cloud.bigtable._generated import (instance_pb2 as data_v2_pb2) from google.cloud.bigtable._generated import ( bigtable_instance_admin_pb2 as messages_v2_pb2) from unit_tests._testing import _FakeStub LOCATION = 'projects/' + self.PROJECT + '/locations/locname' FAILED_LOCATION = 'FAILED' INSTANCE_ID1 = 'instance-id1' INSTANCE_ID2 = 'instance-id2' INSTANCE_NAME1 = ('projects/' + self.PROJECT + '/instances/' + INSTANCE_ID1) INSTANCE_NAME2 = ('projects/' + self.PROJECT + '/instances/' + INSTANCE_ID2) credentials = _Credentials() client = self._make_oneWithMocks( project=self.PROJECT, credentials=credentials, admin=True, ) # Create request_pb request_pb = messages_v2_pb2.ListInstancesRequest(parent='projects/' + self.PROJECT, ) # Create response_pb response_pb = messages_v2_pb2.ListInstancesResponse( failed_locations=[ FAILED_LOCATION, ], instances=[ data_v2_pb2.Instance( name=INSTANCE_NAME1, display_name=INSTANCE_NAME1, ), data_v2_pb2.Instance( name=INSTANCE_NAME2, display_name=INSTANCE_NAME2, ), ], ) # Patch the stub used by the API method. client._instance_stub_internal = stub = _FakeStub(response_pb) # Create expected_result. failed_locations = [FAILED_LOCATION] instances = [ client.instance(INSTANCE_ID1, LOCATION), client.instance(INSTANCE_ID2, LOCATION), ] expected_result = (instances, failed_locations) # Perform the method and check the result. result = client.list_instances() self.assertEqual(result, expected_result) self.assertEqual(stub.method_calls, [( 'ListInstances', (request_pb, ), {}, )])