1

Directory structure

/service-session
    - app.yaml
/service-dashboard
    - app.yaml
    /handlers
        - login.py
    /tests
        - login_test.py

service-dashboard uses webapp2 to respond to the user. service-session manages the session.

What the unittest has to do:

  1. Insert valid session into datastore
  2. Set session cookie
  3. Request login page to perform an auto-login based on session cookie

Unit Test

class TestHandlers(unittest.TestCase):
    def setUp(self):
        self.testbed = testbed.Testbed()
        self.testbed.activate()
        self.testbed.init_datastore_v3_stub()
        self.testbed.init_memcache_stub()
        self.testbed.init_modules_stub()
        self.testbed.init_urlfetch_stub()

        self.app = main.app

        self.customer = service_customer.Customer(password='test', salt='', email='[email protected]')
        self.customer.put()

        self.session = service_session.Session(customer=self.customer.key, session_id="test_session")
        self.session.put()


    def test_list(self):
        request = webapp2.Request.blank('/login', headers={'Cookie': 'session_id=%s' % 'test_session'})
        response = request.get_response(self.app)
        self.assertEqual(response.status_int, 200)

The issue

Dashboard makes a request to service-session, this fails because the session cannot be located. This is because the session has not been dispatched.

In search for answers I came across this post where they use a _LocalFakeDispatcher to bind the services to a certain location. However, this requires to dispatch the services with the devserver because it does not actually dispatch the services. Doing so causes the services to not use the datastore_v3_stub which is initialized in the unit test preventing me to insert a valid session into the datastore.

How does one work around this issue? Is it possible to dispatch the services with the datastore stub? What tactics do you use when testing a microservice architecture on app engine?

NoseGAE + WebTest

nosetests --logging-level=ERROR --gae-lib-root "C:\google_appengine" --with-gae --gae-application="C:\service-session\app.yaml,C:\service-dashboard\app.yaml"

Unittest remains about the same except the webtest.TestApp is used to perform requests to dashboard service.

When the dashboard service attempts to locate the session service the application terminates with an InvalidModuleError. Trace:

Traceback (most recent call last):
  File "C:\google_appengine\lib\webapp2-2.5.2\webapp2.py", line 1535, in __call__
    rv = self.handle_exception(request, response, e)
  File "C:\google_appengine\lib\webapp2-2.5.2\webapp2.py", line 1529, in __call__
    rv = self.router.dispatch(request, response)
  File "C:\google_appengine\lib\webapp2-2.5.2\webapp2.py", line 1278, in default_dispatcher
    return route.handler_adapter(request, response)
  File "C:\google_appengine\lib\webapp2-2.5.2\webapp2.py", line 1102, in __call__
    return handler.dispatch()
  File "C:\service-dashboard\core.py", line 40, in dispatch
    url = 'http://' + modules.get_hostname('service-session') + '/session.get'
  File "C:\google_appengine\google\appengine\api\modules\modules.py", line 458, in get_hostname
    _ResultHook).get_result()
  File "C:\google_appengine\google\appengine\api\apiproxy_stub_map.py", line 613, in get_result
    return self.__get_result_hook(self)
  File "C:\google_appengine\google\appengine\api\modules\modules.py", line 441, in _ResultHook
    _CheckAsyncResult(rpc, mapped_errors, [])
  File "C:\google_appengine\google\appengine\api\modules\modules.py", line 146, in _CheckAsyncResult
    raise mapped_error()
InvalidModuleError

2 Answers 2

0

I believe you can do what you need with these three tools:

  • NoseGAE -- "NoseGAE sets up the GAE development environment before your test run. This means that you can easily write functional tests for your application without having to actually start the dev server and test over http. "
  • WebTest -- "With this you can test your web applications without starting an HTTP server, and without poking into the web framework shortcutting pieces of your application that need to be tested. The tests WebTest runs are entirely equivalent to how a WSGI HTTP server would call an application. "
  • mock -- "It allows you to replace parts of your system under test with mock objects and make assertions about how they have been used."

I'm not sure if you need mock. I believe using NoseGAE and WebTest will allow your services to interact with each other in the testing environment.

Even if you don't need mock, however, it is often useful to isolate your testing more effectively. I use mock for mocking third-party services when testing my code, but you may also be able to use it to mock your session service when testing your dashboard service and vice versa.

2
  • Giving NoseGAE + WebTest a chance. It does execute the test but I get a InvalidModuleError when the application tries to locate the session service using modules.gethostname. I did supply both the session and dashboard app.yaml to nosetests using --gae-application. Mar 5, 2017 at 15:16
  • I added this information to the question to give you a better idea of what i'm talking about. Mar 5, 2017 at 15:26
0

I'm working also with an app deployed in App Engine that is micro services based and as I try to build this mServices to can run in any place (in spite of this is running of App Engine) my strategy is use as few as possible Google tools. I prefer do unit tests based in raw request (also between mServices) to check everything, but of course it depends of each domain and each specific architecture implemented, but this is only my opinion.

If you want talk with people with experience you can join to Slack group and visit also community forum for users of Google App Engine in Google Groups.

Anyway please tell us how have you solved your problem when you do this because there isn't too much info about in the net.

2
  • You use your own service locator instead of modules.get_hostname ? Mar 5, 2017 at 15:42
  • No, is too easy to search anocher option (but it will be a problem if i decide change to deploy some service in another place in the future, I Know) Mar 5, 2017 at 16:10

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.