Ejemplo n.º 1
0
    def setUp(self):
        super(AppEngineTestBase, self).setUp()
        empty_environ()

        # setup an app to be tested
        self.testapp = webtest.TestApp(self.getApp())
        self.testbed = testbed.Testbed()
        self.testbed.activate()

        # configure datastore policy to emulate instantaneously and globally
        # consistent HRD; we also patch dev_appserver in main.py to run under
        # the same policy
        policy = datastore_stub_util.PseudoRandomHRConsistencyPolicy(
            probability=1)

        # declare any relevant App Engine service stubs here
        self.testbed.init_user_stub()
        self.testbed.init_memcache_stub()
        self.testbed.init_datastore_v3_stub(consistency_policy=policy)
        self.testbed.init_taskqueue_stub(root_path=os.environ['SOURCE_DIR'])
        self.taskq = self.testbed.get_stub(testbed.TASKQUEUE_SERVICE_NAME)
        self.testbed.init_urlfetch_stub()
        self.testbed.init_files_stub()
        self.testbed.init_blobstore_stub()
        self.testbed.init_mail_stub()
        # TODO(emichael): Fix this when an official stub is created
        self.testbed._register_stub('search',
                                    simple_search_stub.SearchServiceStub())
        self.task_dispatcher = task_queue.TaskQueueHandlerDispatcher(
            self.testapp, self.taskq)
Ejemplo n.º 2
0
    def setUp(self):
        """Initializes the App Engine stubs."""
        # Evil os-environ patching which mirrors dev_appserver and production.
        # This patch turns os.environ into a thread-local object, which also happens
        # to support storing more than just strings. This patch must come first.
        self._old_os_environ = os.environ.copy()
        request_environment.current_request.Clear()
        request_environment.PatchOsEnviron()
        os.environ.update(self._old_os_environ)

        # Setup and activate the testbed.
        self.InitTestbed()

        # Register the search stub (until included in init_all_stubs).
        if (simple_search_stub
                and apiproxy_stub_map.apiproxy.GetStub('search') is None):
            self.search_stub = simple_search_stub.SearchServiceStub()
            apiproxy_stub_map.apiproxy.RegisterStub('search', self.search_stub)

        # Fake an always strongly-consistent HR datastore.
        policy = datastore_stub_util.PseudoRandomHRConsistencyPolicy(
            probability=1)
        self.testbed.init_datastore_v3_stub(consistency_policy=policy)
        self.datastore_stub = self.testbed.get_stub(
            testbed.DATASTORE_SERVICE_NAME)

        # Save the taskqueue_stub for use in RunDeferredTasks.
        self.testbed.init_taskqueue_stub(_all_queues_valid=True)
        self.taskqueue_stub = self.testbed.get_stub(
            testbed.TASKQUEUE_SERVICE_NAME)

        # Save other stubs for use in helper methods and tests.
        self.users_stub = self.testbed.get_stub(testbed.USER_SERVICE_NAME)
        self.channel_stub = self.testbed.get_stub(testbed.CHANNEL_SERVICE_NAME)

        # Each time setUp is called, treat it like a different request to a
        # different app instance.
        request_id_hash = ''.join(
            random.sample(string.letters + string.digits, 26))
        instance_id = ''.join(random.sample(string.letters + string.digits,
                                            26))
        # More like the production environment: "testbed-version.123123123", rather
        # than the default "testbed-version".
        current_version_id = 'testbed-version.%s' % random.randint(
            1, 1000000000000)
        self.testbed.setup_env(request_id_hash=request_id_hash,
                               instance_id=instance_id,
                               current_version_id=current_version_id,
                               overwrite=True)

        self.Logout()
        super(AppEngineTestCase, self).setUp()
Ejemplo n.º 3
0
  def init_search_stub(self, enable=True):
    """Enables the search stub.

    Args:
      enable: `True` if the fake service should be enabled, or `False` if the
          real service should be disabled.
    """
    if not enable:
      self._disable_stub(SEARCH_SERVICE_NAME)
      return
    if simple_search_stub is None:
      raise StubNotSupportedError('Could not initialize search API')
    stub = simple_search_stub.SearchServiceStub()
    self._register_stub(SEARCH_SERVICE_NAME, stub)
Ejemplo n.º 4
0
def execute_from_command_line():
    _patch_deferred()

    # TODO: get app_id from app.yaml
    storage_path = _get_storage_path(None, "greenday-project-v02-dev")
    search_stub = simple_search_stub.SearchServiceStub(
        index_file=os.path.join(storage_path, 'search_indexes'))
    apiproxy_stub_map.apiproxy.RegisterStub('search', search_stub)

    from greenday_core.utils import get_settings_name
    os.environ.setdefault('DJANGO_SETTINGS_MODULE', get_settings_name())

    import django.core.management
    try:
        django.core.management.execute_from_command_line()
    finally:
        search_stub.Write()
Ejemplo n.º 5
0
 def setUp(self):
     self.testbed = testbed.Testbed()
     self.testbed.activate()
     self.testbed.init_datastore_v3_stub()
     self.testbed.init_blobstore_stub()
     self.testbed.init_memcache_stub()
     self.testbed.init_taskqueue_stub()
     self.testbed.init_user_stub()
     # TODO: Search stub does not exist
     # self.testbed.init_search_stub()
     search_stub = simple_search_stub.SearchServiceStub()
     self.testbed._register_stub("search", search_stub)
     self.model_url = "/api/todos/"
     self.user_url = "/api/users/"
     self.user_id = "118124022271294486125"
     self.setCurrentUser("*****@*****.**", self.user_id, True)
     search._searchable = None
Ejemplo n.º 6
0
def setup_stubs(storage_path, options, configuration):
    datastore_path = options.datastore_path or os.path.join(storage_path, 'datastore.db')
    search_index_path = options.search_indexes_path or os.path.join(storage_path, 'search_indexes')
    blobstore_path = options.blobstore_path or os.path.join(storage_path, 'blobs')

    # Init the proxy map and stubs
    from google.appengine.api import apiproxy_stub_map
    apiproxy_stub_map.apiproxy = apiproxy_stub_map.APIProxyStubMap()


    # DB
    from google.appengine.datastore import datastore_sqlite_stub
    apiproxy_stub_map.apiproxy.RegisterStub('datastore_v3', datastore_sqlite_stub.DatastoreSqliteStub(configuration.app_id, datastore_path))

    # Search service
    from google.appengine.api.search import simple_search_stub
    apiproxy_stub_map.apiproxy.RegisterStub('search', simple_search_stub.SearchServiceStub(index_file=search_index_path))

    from google.appengine.api.blobstore import file_blob_storage
    blob_storage = file_blob_storage.FileBlobStorage(blobstore_path, configuration.app_id)

    from google.appengine.api.blobstore import blobstore_stub
    apiproxy_stub_map.apiproxy.RegisterStub('blobstore', blobstore_stub.BlobstoreServiceStub(blob_storage))


    from google.appengine.api.app_identity import app_identity_stub
    apiproxy_stub_map.apiproxy.RegisterStub('app_identity_service', app_identity_stub.AppIdentityServiceStub())

    # Capability
    from google.appengine.api.capabilities import capability_stub
    apiproxy_stub_map.apiproxy.RegisterStub('capability_service', capability_stub.CapabilityServiceStub())

    # Memcache
    from google.appengine.api.memcache import memcache_stub
    apiproxy_stub_map.apiproxy.RegisterStub('memcache', memcache_stub.MemcacheServiceStub())

    # Task queues
    from google.appengine.api.taskqueue import taskqueue_stub
    apiproxy_stub_map.apiproxy.RegisterStub('taskqueue', taskqueue_stub.TaskQueueServiceStub())

    # URLfetch service
    from google.appengine.api import urlfetch_stub
    apiproxy_stub_map.apiproxy.RegisterStub('urlfetch', urlfetch_stub.URLFetchServiceStub())
Ejemplo n.º 7
0
  def setUp(self, urlfetch_mock=URLFetchServiceMock()):
    unittest.TestCase.setUp(self)
    self.mox = mox.Mox()

    self.appid = "testapp"
    self.version_id = "1.23456789"
    os.environ["APPLICATION_ID"] = self.appid
    os.environ["CURRENT_VERSION_ID"] = self.version_id
    os.environ["HTTP_HOST"] = "localhost"

    self.memcache = memcache_stub.MemcacheServiceStub()
    self.taskqueue = taskqueue_stub.TaskQueueServiceStub()
    self.taskqueue.queue_yaml_parser = (
        lambda x: queueinfo.LoadSingleQueue(
            "queue:\n"
            "- name: default\n"
            "  rate: 10/s\n"
            "- name: crazy-queue\n"
            "  rate: 2000/d\n"
            "  bucket_size: 10\n"))
    self.datastore = datastore_file_stub.DatastoreFileStub(
        self.appid, "/dev/null", "/dev/null")

    self.blob_storage_directory = tempfile.mkdtemp()
    blob_storage = file_blob_storage.FileBlobStorage(
        self.blob_storage_directory, self.appid)
    self.blobstore_stub = blobstore_stub.BlobstoreServiceStub(blob_storage)
    self.file_service = self.createFileServiceStub(blob_storage)
    self._urlfetch_mock = urlfetch_mock
    self.search_service = simple_search_stub.SearchServiceStub()
    self.images_service = images_stub.ImagesServiceStub() 
    apiproxy_stub_map.apiproxy = apiproxy_stub_map.APIProxyStubMap()
    apiproxy_stub_map.apiproxy.RegisterStub("taskqueue", self.taskqueue)
    apiproxy_stub_map.apiproxy.RegisterStub("memcache", self.memcache)
    apiproxy_stub_map.apiproxy.RegisterStub("datastore_v3", self.datastore)
    apiproxy_stub_map.apiproxy.RegisterStub("blobstore", self.blobstore_stub)
    apiproxy_stub_map.apiproxy.RegisterStub("file", self.file_service)
    apiproxy_stub_map.apiproxy.RegisterStub("urlfetch", self._urlfetch_mock)
    apiproxy_stub_map.apiproxy.RegisterStub("search", self.search_service)
    apiproxy_stub_map.apiproxy.RegisterStub("images", self.images_service)
Ejemplo n.º 8
0
def setup_stubs(
    request_data,
    app_id,
    application_root,
    trusted,
    appidentity_email_address,
    appidentity_private_key_path,
    blobstore_path,
    datastore_consistency,
    datastore_path,
    datastore_require_indexes,
    datastore_auto_id_policy,
    images_host_prefix,
    logs_path,
    mail_smtp_host,
    mail_smtp_port,
    mail_smtp_user,
    mail_smtp_password,
    mail_enable_sendmail,
    mail_show_mail_body,
    mail_allow_tls,
    search_index_path,
    taskqueue_auto_run_tasks,
    taskqueue_default_http_server,
    user_login_url,
    user_logout_url,
    default_gcs_bucket_name,
    appidentity_oauth_url=None,
    support_datastore_emulator=False):
  """Configures the APIs hosted by this server.

  Args:
    request_data: An apiproxy_stub.RequestInformation instance used by the
        stubs to lookup information about the request associated with an API
        call.
    app_id: The str application id e.g. "guestbook".
    application_root: The path to the directory containing the user's
        application e.g. "/home/joe/myapp".
    trusted: A bool indicating if privileged APIs should be made available.
    appidentity_email_address: Email address associated with a service account
        that has a downloadable key. May be None for no local application
        identity.
    appidentity_private_key_path: Path to private key file associated with
        service account (.pem format). Must be set if appidentity_email_address
        is set.
    blobstore_path: The path to the file that should be used for blobstore
        storage.
    datastore_consistency: The datastore_stub_util.BaseConsistencyPolicy to
        use as the datastore consistency policy.
    datastore_path: The path to the file that should be used for datastore
        storage.
    datastore_require_indexes: A bool indicating if the same production
        datastore indexes requirements should be enforced i.e. if True then
        a google.appengine.ext.db.NeedIndexError will be be raised if a query
        is executed without the required indexes.
    datastore_auto_id_policy: The type of sequence from which the datastore
        stub assigns auto IDs, either datastore_stub_util.SEQUENTIAL or
        datastore_stub_util.SCATTERED.
    images_host_prefix: The URL prefix (protocol://host:port) to prepend to
        image urls on calls to images.GetUrlBase.
    logs_path: Path to the file to store the logs data in.
    mail_smtp_host: The SMTP hostname that should be used when sending e-mails.
        If None then the mail_enable_sendmail argument is considered.
    mail_smtp_port: The SMTP port number that should be used when sending
        e-mails. If this value is None then mail_smtp_host must also be None.
    mail_smtp_user: The username to use when authenticating with the
        SMTP server. This value may be None if mail_smtp_host is also None or if
        the SMTP server does not require authentication.
    mail_smtp_password: The password to use when authenticating with the
        SMTP server. This value may be None if mail_smtp_host or mail_smtp_user
        is also None.
    mail_enable_sendmail: A bool indicating if sendmail should be used when
        sending e-mails. This argument is ignored if mail_smtp_host is not None.
    mail_show_mail_body: A bool indicating whether the body of sent e-mails
        should be written to the logs.
    mail_allow_tls: A bool indicating whether TLS should be allowed when
        communicating with an SMTP server. This argument is ignored if
        mail_smtp_host is None.
    search_index_path: The path to the file that should be used for search index
        storage.
    taskqueue_auto_run_tasks: A bool indicating whether taskqueue tasks should
        be run automatically or it the must be manually triggered.
    taskqueue_default_http_server: A str containing the address of the http
        server that should be used to execute tasks.
    user_login_url: A str containing the url that should be used for user login.
    user_logout_url: A str containing the url that should be used for user
        logout.
    default_gcs_bucket_name: A str, overriding the default bucket behavior.
    appidentity_oauth_url: A str containing the url to the oauth2 server to use
        to authenticate the private key. If set to None, then the standard
        google oauth2 server is used.
    support_datastore_emulator: A bool indicating if datastore_emulator is
        supported.
  """
  identity_stub = app_identity_stub.AppIdentityServiceStub.Create(
      email_address=appidentity_email_address,
      private_key_path=appidentity_private_key_path,
      oauth_url=appidentity_oauth_url)
  if default_gcs_bucket_name is not None:
    identity_stub.SetDefaultGcsBucketName(default_gcs_bucket_name)
  apiproxy_stub_map.apiproxy.RegisterStub('app_identity_service', identity_stub)

  blob_storage = file_blob_storage.FileBlobStorage(blobstore_path, app_id)
  apiproxy_stub_map.apiproxy.RegisterStub(
      'blobstore',
      blobstore_stub.BlobstoreServiceStub(blob_storage,
                                          request_data=request_data))

  apiproxy_stub_map.apiproxy.RegisterStub(
      'capability_service',
      capability_stub.CapabilityServiceStub())

  apiproxy_stub_map.apiproxy.RegisterStub(
      'channel',
      channel_service_stub.ChannelServiceStub(request_data=request_data))

  if support_datastore_emulator:
    datastore_emulator_host = os.environ.get('DATASTORE_EMULATOR_HOST', '')
    error_msg = 'DATASTORE_EMULATOR_HOST is not found in environment variables'
    assert datastore_emulator_host, error_msg
    apiproxy_stub_map.apiproxy.ReplaceStub(
        'datastore_v3',
        datastore_grpc_stub.DatastoreGrpcStub(datastore_emulator_host)
    )
  else:
    apiproxy_stub_map.apiproxy.ReplaceStub(
        'datastore_v3',
        datastore_sqlite_stub.DatastoreSqliteStub(
            app_id,
            datastore_path,
            datastore_require_indexes,
            trusted,
            root_path=application_root,
            auto_id_policy=datastore_auto_id_policy,
            consistency_policy=datastore_consistency))

  apiproxy_stub_map.apiproxy.RegisterStub(
      'datastore_v4',
      datastore_v4_stub.DatastoreV4Stub(app_id))

  apiproxy_stub_map.apiproxy.RegisterStub(
      'file',
      file_service_stub.FileServiceStub(blob_storage))

  try:
    from google.appengine.api.images import images_stub
  except ImportError:




    # We register a stub which throws a NotImplementedError for most RPCs.
    from google.appengine.api.images import images_not_implemented_stub
    apiproxy_stub_map.apiproxy.RegisterStub(
        'images',
        images_not_implemented_stub.ImagesNotImplementedServiceStub(
            host_prefix=images_host_prefix))
  else:
    apiproxy_stub_map.apiproxy.RegisterStub(
        'images',
        images_stub.ImagesServiceStub(host_prefix=images_host_prefix))

  apiproxy_stub_map.apiproxy.RegisterStub(
      'logservice',
      logservice_stub.LogServiceStub(logs_path=logs_path))

  apiproxy_stub_map.apiproxy.RegisterStub(
      'mail',
      mail_stub.MailServiceStub(mail_smtp_host,
                                mail_smtp_port,
                                mail_smtp_user,
                                mail_smtp_password,
                                enable_sendmail=mail_enable_sendmail,
                                show_mail_body=mail_show_mail_body,
                                allow_tls=mail_allow_tls))

  apiproxy_stub_map.apiproxy.RegisterStub(
      'memcache',
      memcache_stub.MemcacheServiceStub())

  apiproxy_stub_map.apiproxy.RegisterStub(
      'modules',
      modules_stub.ModulesServiceStub(request_data))

  apiproxy_stub_map.apiproxy.RegisterStub(
      'remote_socket',
      _remote_socket_stub.RemoteSocketServiceStub())

  apiproxy_stub_map.apiproxy.RegisterStub(
      'search',
      simple_search_stub.SearchServiceStub(index_file=search_index_path))

  apiproxy_stub_map.apiproxy.RegisterStub(
      'system',
      system_stub.SystemServiceStub(request_data=request_data))

  apiproxy_stub_map.apiproxy.RegisterStub(
      'taskqueue',
      taskqueue_stub.TaskQueueServiceStub(
          root_path=application_root,
          auto_task_running=taskqueue_auto_run_tasks,
          default_http_server=taskqueue_default_http_server,
          request_data=request_data))
  apiproxy_stub_map.apiproxy.GetStub('taskqueue').StartBackgroundExecution()

  apiproxy_stub_map.apiproxy.RegisterStub(
      'urlfetch',
      urlfetch_stub.URLFetchServiceStub())

  apiproxy_stub_map.apiproxy.RegisterStub(
      'user',
      user_service_stub.UserServiceStub(login_url=user_login_url,
                                        logout_url=user_logout_url,
                                        request_data=request_data))

  apiproxy_stub_map.apiproxy.RegisterStub(
      'xmpp',
      xmpp_service_stub.XmppServiceStub())
Ejemplo n.º 9
0
def _SetupStubs(
    app_id,
    application_root,
    trusted,
    blobstore_path,
    use_sqlite,
    auto_id_policy,
    high_replication,
    datastore_path,
    datastore_require_indexes,
    images_host_prefix,
    logs_path,
    mail_smtp_host,
    mail_smtp_port,
    mail_smtp_user,
    mail_smtp_password,
    mail_enable_sendmail,
    mail_show_mail_body,
    matcher_prospective_search_path,
    taskqueue_auto_run_tasks,
    taskqueue_task_retry_seconds,
    taskqueue_default_http_server,
    user_login_url,
    user_logout_url):
  """Configures the APIs hosted by this server.

  Args:
    app_id: The str application id e.g. "guestbook".
    application_root: The path to the directory containing the user's
        application e.g. "/home/bquinlan/myapp".
    trusted: A bool indicating if privileged APIs should be made available.
    blobstore_path: The path to the file that should be used for blobstore
        storage.
    use_sqlite: A bool indicating whether DatastoreSqliteStub or
        DatastoreFileStub should be used.
    auto_id_policy: One of datastore_stub_util.SEQUENTIAL or .SCATTERED,
        indicating whether the Datastore stub should assign IDs sequentially
        or scattered.
    high_replication: A bool indicating whether to use the high replication
        consistency model.
    datastore_path: The path to the file that should be used for datastore
        storage.
    datastore_require_indexes: A bool indicating if the same production
        datastore indexes requirements should be enforced i.e. if True then
        a google.appengine.ext.db.NeedIndexError will be be raised if a query
        is executed without the required indexes.
    images_host_prefix: The URL prefix (protocol://host:port) to preprend to
        image urls on calls to images.GetUrlBase.
    logs_path: Path to the file to store the logs data in.
    mail_smtp_host: The SMTP hostname that should be used when sending e-mails.
        If None then the mail_enable_sendmail argument is considered.
    mail_smtp_port: The SMTP port number that should be used when sending
        e-mails. If this value is None then mail_smtp_host must also be None.
    mail_smtp_user: The username to use when authenticating with the
        SMTP server. This value may be None if mail_smtp_host is also None or if
        the SMTP server does not require authentication.
    mail_smtp_password: The password to use when authenticating with the
        SMTP server. This value may be None if mail_smtp_host or mail_smtp_user
        is also None.
    mail_enable_sendmail: A bool indicating if sendmail should be used when
        sending e-mails. This argument is ignored if mail_smtp_host is not None.
    mail_show_mail_body: A bool indicating whether the body of sent e-mails
        should be written to the logs.
    matcher_prospective_search_path: The path to the file that should be used to
        save prospective search subscriptions.
    taskqueue_auto_run_tasks: A bool indicating whether taskqueue tasks should
        be run automatically or it the must be manually triggered.
    taskqueue_task_retry_seconds: An int representing the number of seconds to
        wait before a retrying a failed taskqueue task.
    taskqueue_default_http_server: A str containing the address of the http
        server that should be used to execute tasks.
    user_login_url: A str containing the url that should be used for user login.
    user_logout_url: A str containing the url that should be used for user
        logout.
  """





  os.environ['APPLICATION_ID'] = app_id



  apiproxy_stub_map.apiproxy.RegisterStub(
      'app_identity_service',
      app_identity_stub.AppIdentityServiceStub())

  blob_storage = file_blob_storage.FileBlobStorage(blobstore_path, app_id)
  apiproxy_stub_map.apiproxy.RegisterStub(
      'blobstore',
      blobstore_stub.BlobstoreServiceStub(blob_storage))

  apiproxy_stub_map.apiproxy.RegisterStub(
      'capability_service',
      capability_stub.CapabilityServiceStub())








  apiproxy_stub_map.apiproxy.RegisterStub(
      'channel',
      channel_service_stub.ChannelServiceStub())

  if use_sqlite:
    datastore = datastore_sqlite_stub.DatastoreSqliteStub(
        app_id,
        datastore_path,
        datastore_require_indexes,
        trusted,
        root_path=application_root,
        auto_id_policy=auto_id_policy)
  else:
    datastore = datastore_file_stub.DatastoreFileStub(
        app_id,
        datastore_path,
        datastore_require_indexes,
        trusted,
        root_path=application_root,
        auto_id_policy=auto_id_policy)

  if high_replication:
    datastore.SetConsistencyPolicy(
        datastore_stub_util.TimeBasedHRConsistencyPolicy())

  apiproxy_stub_map.apiproxy.RegisterStub(
      'datastore_v3', datastore)

  apiproxy_stub_map.apiproxy.RegisterStub(
      'file',
      file_service_stub.FileServiceStub(blob_storage))

  try:
    from google.appengine.api.images import images_stub
  except ImportError:


    logging.warning('Could not initialize images API; you are likely missing '
                    'the Python "PIL" module.')

    from google.appengine.api.images import images_not_implemented_stub
    apiproxy_stub_map.apiproxy.RegisterStub(
        'images',
        images_not_implemented_stub.ImagesNotImplementedServiceStub())
  else:
    apiproxy_stub_map.apiproxy.RegisterStub(
        'images',
        images_stub.ImagesServiceStub(host_prefix=images_host_prefix))

  apiproxy_stub_map.apiproxy.RegisterStub(
      'logservice',
      logservice_stub.LogServiceStub(persist=True))

  apiproxy_stub_map.apiproxy.RegisterStub(
      'mail',
      mail_stub.MailServiceStub(mail_smtp_host,
                                mail_smtp_port,
                                mail_smtp_user,
                                mail_smtp_password,
                                enable_sendmail=mail_enable_sendmail,
                                show_mail_body=mail_show_mail_body))

  apiproxy_stub_map.apiproxy.RegisterStub(
      'memcache',
      memcache_stub.MemcacheServiceStub())

  apiproxy_stub_map.apiproxy.RegisterStub(
      'search',
      simple_search_stub.SearchServiceStub())

  apiproxy_stub_map.apiproxy.RegisterStub('system',
                                          system_stub.SystemServiceStub())

  apiproxy_stub_map.apiproxy.RegisterStub(
      'taskqueue',
      taskqueue_stub.TaskQueueServiceStub(
          root_path=application_root,
          auto_task_running=taskqueue_auto_run_tasks,
          task_retry_seconds=taskqueue_task_retry_seconds,
          default_http_server=taskqueue_default_http_server))
  apiproxy_stub_map.apiproxy.GetStub('taskqueue').StartBackgroundExecution()

  apiproxy_stub_map.apiproxy.RegisterStub(
      'urlfetch',
      urlfetch_stub.URLFetchServiceStub())

  apiproxy_stub_map.apiproxy.RegisterStub(
      'user',
      user_service_stub.UserServiceStub(login_url=user_login_url,
                                        logout_url=user_logout_url))

  apiproxy_stub_map.apiproxy.RegisterStub(
      'xmpp',
      xmpp_service_stub.XmppServiceStub())

  apiproxy_stub_map.apiproxy.RegisterStub(
      'matcher',
      prospective_search_stub.ProspectiveSearchStub(
          matcher_prospective_search_path,
          apiproxy_stub_map.apiproxy.GetStub('taskqueue')))
Ejemplo n.º 10
0
def setup_stubs(request_data, app_id, application_root, trusted,
                blobstore_path, datastore_consistency, datastore_path,
                datastore_require_indexes, datastore_auto_id_policy,
                images_host_prefix, logs_path, mail_smtp_host, mail_smtp_port,
                mail_smtp_user, mail_smtp_password, mail_enable_sendmail,
                mail_show_mail_body, matcher_prospective_search_path,
                search_index_path, taskqueue_auto_run_tasks,
                taskqueue_default_http_server, user_login_url,
                user_logout_url):
    """Configures the APIs hosted by this server.

  Args:
    request_data: An apiproxy_stub.RequestInformation instance used by the
        stubs to lookup information about the request associated with an API
        call.
    app_id: The str application id e.g. "guestbook".
    application_root: The path to the directory containing the user's
        application e.g. "/home/joe/myapp".
    trusted: A bool indicating if privileged APIs should be made available.
    blobstore_path: The path to the file that should be used for blobstore
        storage.
    datastore_consistency: The datastore_stub_util.BaseConsistencyPolicy to
        use as the datastore consistency policy.
    datastore_path: The path to the file that should be used for datastore
        storage.
    datastore_require_indexes: A bool indicating if the same production
        datastore indexes requirements should be enforced i.e. if True then
        a google.appengine.ext.db.NeedIndexError will be be raised if a query
        is executed without the required indexes.
    datastore_auto_id_policy: The type of sequence from which the datastore
        stub assigns auto IDs, either datastore_stub_util.SEQUENTIAL or
        datastore_stub_util.SCATTERED.
    images_host_prefix: The URL prefix (protocol://host:port) to prepend to
        image urls on calls to images.GetUrlBase.
    logs_path: Path to the file to store the logs data in.
    mail_smtp_host: The SMTP hostname that should be used when sending e-mails.
        If None then the mail_enable_sendmail argument is considered.
    mail_smtp_port: The SMTP port number that should be used when sending
        e-mails. If this value is None then mail_smtp_host must also be None.
    mail_smtp_user: The username to use when authenticating with the
        SMTP server. This value may be None if mail_smtp_host is also None or if
        the SMTP server does not require authentication.
    mail_smtp_password: The password to use when authenticating with the
        SMTP server. This value may be None if mail_smtp_host or mail_smtp_user
        is also None.
    mail_enable_sendmail: A bool indicating if sendmail should be used when
        sending e-mails. This argument is ignored if mail_smtp_host is not None.
    mail_show_mail_body: A bool indicating whether the body of sent e-mails
        should be written to the logs.
    matcher_prospective_search_path: The path to the file that should be used to
        save prospective search subscriptions.
    search_index_path: The path to the file that should be used for search index
        storage.
    taskqueue_auto_run_tasks: A bool indicating whether taskqueue tasks should
        be run automatically or it the must be manually triggered.
    taskqueue_default_http_server: A str containing the address of the http
        server that should be used to execute tasks.
    user_login_url: A str containing the url that should be used for user login.
    user_logout_url: A str containing the url that should be used for user
        logout.
  """

    apiproxy_stub_map.apiproxy.RegisterStub(
        'app_identity_service', app_identity_stub.AppIdentityServiceStub())

    blob_storage = file_blob_storage.FileBlobStorage(blobstore_path, app_id)
    apiproxy_stub_map.apiproxy.RegisterStub(
        'blobstore',
        blobstore_stub.BlobstoreServiceStub(blob_storage,
                                            request_data=request_data))

    apiproxy_stub_map.apiproxy.RegisterStub(
        'capability_service', capability_stub.CapabilityServiceStub())

    apiproxy_stub_map.apiproxy.RegisterStub(
        'channel',
        channel_service_stub.ChannelServiceStub(request_data=request_data))

    datastore_stub = datastore_sqlite_stub.DatastoreSqliteStub(
        app_id,
        datastore_path,
        datastore_require_indexes,
        trusted,
        root_path=application_root,
        auto_id_policy=datastore_auto_id_policy)

    datastore_stub.SetConsistencyPolicy(datastore_consistency)

    apiproxy_stub_map.apiproxy.ReplaceStub('datastore_v3', datastore_stub)

    apiproxy_stub_map.apiproxy.RegisterStub(
        'file', file_service_stub.FileServiceStub(blob_storage))

    try:
        from google.appengine.api.images import images_stub
    except ImportError:

        logging.warning(
            'Could not initialize images API; you are likely missing '
            'the Python "PIL" module.')
        # We register a stub which throws a NotImplementedError for most RPCs.
        from google.appengine.api.images import images_not_implemented_stub
        apiproxy_stub_map.apiproxy.RegisterStub(
            'images',
            images_not_implemented_stub.ImagesNotImplementedServiceStub(
                host_prefix=images_host_prefix))
    else:
        apiproxy_stub_map.apiproxy.RegisterStub(
            'images',
            images_stub.ImagesServiceStub(host_prefix=images_host_prefix))

    apiproxy_stub_map.apiproxy.RegisterStub(
        'logservice', logservice_stub.LogServiceStub(logs_path=logs_path))

    apiproxy_stub_map.apiproxy.RegisterStub(
        'mail',
        mail_stub.MailServiceStub(mail_smtp_host,
                                  mail_smtp_port,
                                  mail_smtp_user,
                                  mail_smtp_password,
                                  enable_sendmail=mail_enable_sendmail,
                                  show_mail_body=mail_show_mail_body))

    apiproxy_stub_map.apiproxy.RegisterStub(
        'memcache', memcache_stub.MemcacheServiceStub())

    apiproxy_stub_map.apiproxy.RegisterStub(
        'search',
        simple_search_stub.SearchServiceStub(index_file=search_index_path))

    apiproxy_stub_map.apiproxy.RegisterStub(
        'servers', servers_stub.ServersServiceStub(request_data))

    apiproxy_stub_map.apiproxy.RegisterStub(
        'system', system_stub.SystemServiceStub(request_data=request_data))

    apiproxy_stub_map.apiproxy.RegisterStub(
        'taskqueue',
        taskqueue_stub.TaskQueueServiceStub(
            root_path=application_root,
            auto_task_running=taskqueue_auto_run_tasks,
            default_http_server=taskqueue_default_http_server,
            request_data=request_data))
    apiproxy_stub_map.apiproxy.GetStub('taskqueue').StartBackgroundExecution()

    urlmatchers_to_fetch_functions = []
    urlmatchers_to_fetch_functions.extend(
        gcs_dispatcher.URLMATCHERS_TO_FETCH_FUNCTIONS)
    apiproxy_stub_map.apiproxy.RegisterStub(
        'urlfetch',
        urlfetch_stub.URLFetchServiceStub(
            urlmatchers_to_fetch_functions=urlmatchers_to_fetch_functions))

    apiproxy_stub_map.apiproxy.RegisterStub(
        'user',
        user_service_stub.UserServiceStub(login_url=user_login_url,
                                          logout_url=user_logout_url,
                                          request_data=request_data))

    apiproxy_stub_map.apiproxy.RegisterStub(
        'xmpp', xmpp_service_stub.XmppServiceStub())

    apiproxy_stub_map.apiproxy.RegisterStub(
        'matcher',
        prospective_search_stub.ProspectiveSearchStub(
            matcher_prospective_search_path,
            apiproxy_stub_map.apiproxy.GetStub('taskqueue')))

    apiproxy_stub_map.apiproxy.RegisterStub(
        'remote_socket', _remote_socket_stub.RemoteSocketServiceStub())
Ejemplo n.º 11
0
    def setUp(self,
              project_dir,
              datastore=True,
              memcache=True,
              app_identity=True,
              blobstore=True,
              files=True,
              images=True,
              taskqueue=True,
              urlfetch=True,
              mail=True,
              user=True,
              capability=True,
              channel=True,
              logservice=True,
              xmpp=True,
              search=True):  # pylint: disable=arguments-differ,too-many-arguments,too-many-locals,too-many-branches
        """
        https://cloud.google.com/appengine/docs/python/refdocs/google.appengine.ext.testbed
        """
        from google.appengine.ext import testbed

        self.testbed = testbed.Testbed()
        self.testbed.activate()

        self.policy = None
        self.blobstore_stub = None
        self.taskqueue_stub = None

        if datastore:
            # Create a consistency policy that will simulate the High
            # Replication consistency model. It's easier to test with
            # probability 1.
            from google.appengine.datastore import datastore_stub_util

            self.policy = datastore_stub_util.PseudoRandomHRConsistencyPolicy(
                probability=1)
            self.testbed.init_datastore_v3_stub(require_indexes=True,
                                                root_path=project_dir)

        if memcache:
            self.testbed.init_memcache_stub()

        if app_identity:
            self.testbed.init_app_identity_stub()

        if blobstore:
            self.testbed.init_blobstore_stub()
            self.blobstore_stub = self.testbed.get_stub(
                testbed.BLOBSTORE_SERVICE_NAME)

        if files:
            self.testbed.init_files_stub()

        if images:
            self.testbed.init_images_stub()

        if taskqueue:
            self.testbed.init_taskqueue_stub(root_path=project_dir)
            self.taskqueue_stub = self.testbed.get_stub(
                testbed.TASKQUEUE_SERVICE_NAME)

        if urlfetch:
            self.testbed.init_urlfetch_stub()

        if mail:
            self.testbed.init_mail_stub()

        if user:
            self.testbed.init_user_stub()

        if capability:
            self.testbed.init_capability_stub()

        if channel:
            self.testbed.init_channel_stub()

        if logservice:
            self.testbed.init_logservice_stub()

        if xmpp:
            self.testbed.init_xmpp_stub()

        if search:
            # search stub is not available via testbed, so doing this by myself.
            from google.appengine.api import apiproxy_stub_map
            from google.appengine.api.search import simple_search_stub

            apiproxy_stub_map.apiproxy.RegisterStub(
                'search', simple_search_stub.SearchServiceStub())