def testGetHostname_NoArgs(self): """Test we pass through the expected args when none are given.""" expected_request = modules_service_pb2.GetHostnameRequest() service_response = modules_service_pb2.GetHostnameResponse() service_response.hostname = 'abc' self.SetSuccessExpectations('GetHostname', expected_request, service_response) self.assertEqual('abc', modules.get_hostname())
def testGetHostname_InstanceZero(self): """Test we pass through the expected args when instance zero is given.""" expected_request = modules_service_pb2.GetHostnameRequest() expected_request.module = 'module1' expected_request.instance = '0' service_response = modules_service_pb2.GetHostnameResponse() service_response.hostname = 'abc' self.SetSuccessExpectations('GetHostname', expected_request, service_response) self.assertEqual('abc', modules.get_hostname(module='module1', instance=0))
def testGetHostname_NoModule(self): """Test we pass through the expected args when no module is specified.""" expected_request = modules_service_pb2.GetHostnameRequest() expected_request.version = 'v1' expected_request.instance = '3' service_response = modules_service_pb2.GetHostnameResponse() service_response.hostname = 'abc' self.SetSuccessExpectations('GetHostname', expected_request, service_response) self.assertEqual('abc', modules.get_hostname(version='v1', instance='3'))
def testGetHostname_UnKnownError(self): """Test we raise an error when we receive one from the API.""" expected_request = modules_service_pb2.GetHostnameRequest() expected_request.module = 'module1' expected_request.version = 'v1' self.SetExceptionExpectations( 'GetHostname', expected_request, 1099) self.assertRaisesRegex(modules.Error, 'ApplicationError: 1099', modules.get_hostname, 'module1', 'v1')
def testGetHostname_InvalidModuleError(self): """Test we raise an error when we receive one from the API.""" expected_request = modules_service_pb2.GetHostnameRequest() expected_request.module = 'module1' expected_request.version = 'v1' self.SetExceptionExpectations( 'GetHostname', expected_request, modules_service_pb2.ModulesServiceError.INVALID_MODULE) self.assertRaises(modules.InvalidModuleError, modules.get_hostname, 'module1', 'v1')
def testGetHostname_UnMappedError(self): """Test we raise an error when we receive one from the API.""" expected_request = modules_service_pb2.GetHostnameRequest() expected_request.module = 'module1' expected_request.version = 'v1' self.SetExceptionExpectations( 'GetHostname', expected_request, modules_service_pb2.ModulesServiceError.INVALID_VERSION) expected_message = 'ApplicationError: %s' % ( modules_service_pb2.ModulesServiceError.INVALID_VERSION) self.assertRaisesRegex(modules.Error, expected_message, modules.get_hostname, 'module1', 'v1')
def get_hostname(module=None, version=None, instance=None): """Returns a hostname to use to contact the module. Args: module: Name of module, if None, take module of the current instance. version: Name of version, if version is None then either use the version of the current instance if that version exists for the target module, otherwise use the default version of the target module. instance: Instance to construct a hostname for, if instance is None, a loadbalanced hostname for the module will be returned. If the target module is not a fixed module, then instance is not considered valid. Returns: A valid canonical hostname that can be used to communicate with the given module/version/instance. For example: `0.v1.module5.myapp.appspot.com` Raises: InvalidModuleError: if the given module version is invalid. InvalidInstancesError: if the given instance value is invalid. TypeError: if the given instance type is invalid. """ def _ResultHook(rpc): mapped_errors = [ modules_service_pb2.ModulesServiceError.INVALID_MODULE, modules_service_pb2.ModulesServiceError.INVALID_INSTANCES ] _CheckAsyncResult(rpc, mapped_errors, []) return rpc.response.hostname request = modules_service_pb2.GetHostnameRequest() if module: request.module = module if version: request.version = version if instance or instance == 0: if not isinstance(instance, (six.string_types, six.integer_types)): raise TypeError( "'instance' arg must be of type basestring, long or int.") request.instance = str(instance) response = modules_service_pb2.GetHostnameResponse() return _MakeAsyncCall('GetHostname', request, response, _ResultHook).get_result()
def testGetHostname_InvalidInstancesError(self): """Test we raise an error when we receive one from the API.""" self.SetExceptionExpectations( 'GetHostname', modules_service_pb2.GetHostnameRequest(), modules_service_pb2.ModulesServiceError.INVALID_INSTANCES) self.assertRaises(modules.InvalidInstancesError, modules.get_hostname)