In Python, How to Test If I'm in Google App Engine Sdk

In Python, how can I test if I'm in Google App Engine SDK?

See: https://cloud.google.com/appengine/docs/python/how-requests-are-handled#Python_The_environment

The following environment variables are part of the CGI standard, with special behavior in App Engine:
SERVER_SOFTWARE:

In the development web server, this value is "Development/X.Y" where "X.Y" is the version of the runtime.

When running on App Engine, this value is "Google App Engine/X.Y.Z".

Under GAE, can a Python function detect if it is running locally or in production?

Have a read of this documentation on handling requests https://cloud.google.com/appengine/docs/python/requests#Python_The_environment specifically the section on the environment.

You will see a number of potentially relevant environment variables. The one you want is SERVER_SOFTWARE . It will contain "Development" in the value.

Also this has been answered before on SO. In Python, how can I test if I'm in Google App Engine SDK?

Google App Engine set up a user when using testbed

To simulate a sign in of an admin user, you can call anywhere in your test function:

self.testbed.setup_env(
USER_EMAIL = 'test@example.com',
USER_ID = '123',
USER_IS_ADMIN = '1',
overwrite = True)

Note that you need to use the overwrite=True parameter!

test function with Google App Engine `files` api

It seems like testbed.init_blobstore_stub() is outdated, because dev_appserver inits blobstore stubs differently. Here is my implementation of init_blobstore_stub that allows you to write to and read from blobstore in your tests.

from google.appengine.ext import testbed
from google.appengine.api.blobstore import blobstore_stub, file_blob_storage
from google.appengine.api.files import file_service_stub

class TestbedWithFiles(testbed.Testbed):

def init_blobstore_stub(self):
blob_storage = file_blob_storage.FileBlobStorage('/tmp/testbed.blobstore',
testbed.DEFAULT_APP_ID)
blob_stub = blobstore_stub.BlobstoreServiceStub(blob_storage)
file_stub = file_service_stub.FileServiceStub(blob_storage)
self._register_stub('blobstore', blob_stub)
self._register_stub('file', file_stub)

# Your code...
def foo():
from google.appengine.api import files
blob_filename = files.blobstore.create(mime_type='text/plain')
with files.open(blob_filename, 'a') as googfile:
googfile.write("Test data")

files.finalize(blob_filename)

tb = TestbedWithFiles()
tb.activate()
tb.init_blobstore_stub()

foo()

Checking status of Task Queue in Google App Engine

The new REST/JSON task queue API will let you do this.

http://code.google.com/appengine/docs/python/taskqueue/rest.html

This does not scale well to thousands of tasks...

I do like the pipeline API suggestion though!

Google Cloud Django if os.getenv('SERVER_SOFTWARE', '').startswith('Google App Engine'): not working

I'll present a couple other solutions.

(1) GAE provided env vars

GAE automatically sets environment variables that you probably don't set when running locally. I use this:

version = os.environ.get('GAE_VERSION', 'local')

You'll get the actual version in production and 'local' on your machine.

(2) Check the request URL

This is Flask but there must be something similar in Django:

request.url_root == 'http://localhost:8080/'

google app engine comments and check dev environment in html views

As for the comment piece, you can use similar syntax to what you have above:

{# some comment here #}
{{ variable }}
{% for some item in another_variable %}
<p>{{ item }}</p>
{% endfor %}

Regarding the hostname part, I'm not aware of any template built-in that would handle that. I would suggest making this check in your server-side code and passing the result to the template. Here is a question/answer that should accomplish what you need. Pilfering code from that answer, you could define a variable in you code such as (using Python, as I'm not sure which runtime you are using):

dev_server = os.environ['SERVER_SOFTWARE'].startswith('Development')

You could then reference this in your template (assuming you pass the variable into the template as dev_server) as follows:

{% if dev_server %}
<p>comment visible only in localhost!</p>
{% endif %}

What approach(es) have you used for lightweight Python unit-tests on App Engine?

You don't need to write your own stubs - the SDK includes them, since they're what it uses to emulate the production APIs. Not all of them are suitable for use in unit-tests, but most are. Check out this code for an example of the setup/teardown code you need to make use of the built in stubs.



Related Topics



Leave a reply



Submit