[Pulp-list] Testing Web Service Controllers

Jay Dobies jason.dobies at redhat.com
Wed Nov 16 20:28:05 UTC 2011


Currently we have pretty much zero tests for our web service 
controllers. The biggest problem is that it's a non-trivial setup to be 
able to do it. But as we go back now and implement the REST APIs 
correctly, we have a much stronger need to test that response codes are 
coming back correctly and the serialized return objects are what is 
expected (empty lists for collection GET, 404 for resource GET, etc.)

I added a new test case subclass (PulpWebserviceTest) to testutil that 
will set up web.py to be run without the need for an app server. That 
way all the ugly setup is hidden and automagically done so we can get 
right to the actual tests. Here are a few samples:


class RepoCollectionTest(testutil.PulpWebserviceTest):

     def test_get(self):
         """
         Tests retrieving a list of repositories.
         """

         # Setup
         repo_manager = manager_factory.repo_manager()
         repo_manager.create_repo('dummy-1')
         repo_manager.create_repo('dummy-2')

         # Test
         status, body = self.get('/v2/repositories/')

         # Verify
         self.assertEqual(200, status)
         self.assertEqual(2, len(body))

     def test_get_no_repos(self):
         """
         Tests that an empty list is returned when no repos are present.
         """

         # Test
         status, body = self.get('/v2/repositories/')

         # Verify
         self.assertEqual(200, status)
         self.assertEqual(0, len(body))

     def test_get_missing_repo(self):
         """
         Tests that an empty list is returned when no repos are present.
         """

         # Test
         status, body = self.get('/v2/repositories/foo/')

         # Verify
         self.assertEqual(404, status)


A few notes:
* Make sure you remember to call PulpWebserviceTest.setUp if you 
override the method in your test, that's where all the magic happens.
* PulpWebserviceTest has methods get, put, post, delete that are 
shortcuts to the HTTP method of the same name.
* Pass in just the API portion of the URI (i.e. /repositories/, not 
/pulp/api/repositories/) to the http method calls.
* The test case setup creates a super user and automatically adds in 
those auth credentials to the calls. Eventually we should make it 
possible to test permissions on calls through this setup, but frankly I 
have just too much else to do to go there right now.

I'm still not at a point where I can merge my v2 branch into master yet, 
but I wanted to get this out there for anyone who is working with 
refining our web services this sprint (I'm pretty sure we added a story 
for redoing the distribution services, for instance). I cherry picked it 
over to master and the existing tests still passed for me. Nothing in 
master is using it yet, but ping me if hudson starts to complain and it 
looks related.

So far it looks like it works for my v2 tests, but let me know if you 
run into any issues (or ping me if you fix anything so I can cherry pick 
it into my branch.



-- 
Jay Dobies
Freenode: jdob @ #pulp
http://pulpproject.org | http://blog.pulpproject.org




More information about the Pulp-list mailing list