Source

Dad / dad / tests / unit / web / test_dad_api.py

Full commit
import httplib2
import py.test
import simplejson

from pprint import pprint


class TestDadConfigAPI(object):

    @property
    def base(self):
        return 'http://%s:%s' % (py.test.dev_server.host,
                                 py.test.dev_server.port)

    def add_service(self):
        h = httplib2.Http()
        kw = dict(
            method='POST',
            headers={'Content-Type': 'application/json'},
            body=simplejson.dumps({'listdir': {'command': 'ls -la'}})
            )
        resp, content = h.request(self.base + '/services/', **kw)
        return resp, content
        
    def test_add_service(self):
        resp, content = self.add_service()
        assert resp['status'] == '201'
        result = simplejson.loads(content)
        assert result['Location'] == '/services/listdir'

    def test_get_service(self):
        h = httplib2.Http()
        resp, content = h.request(self.base + '/services/simpleweb')
        assert resp['status'] == '200'
        assert content
        result = simplejson.loads(content)
        actions = ['start', 'stop', 'start']
        for a in actions:
            assert result[a] == '/services/simpleweb/%s' % (a)

    def test_start_service(self):
        resp, content = self.add_service()
        result = simplejson.loads(content)

        h = httplib2.Http()
        resp, content = h.request(self.base + '/services/listdir/start',
                                  method='POST',
                                  headers={'Content-Length': '0'})

        assert resp['status'] == '200'
        result = simplejson.loads(content)
        assert result
        assert result['started'] == 'listdir'
        assert result['num_instances'] == 1

    def test_stop_service(self):
        '''Stop our long running process started when the test dad
        server started'''
        resp, content = self.add_service()
        result = simplejson.loads(content)

        h = httplib2.Http()
        resp, content = h.request(self.base + '/services/simpleweb/stop',
                                  method='POST',
                                  headers={'Content-Length': '0'})
        assert resp['status'] == '200'
        result = simplejson.loads(content)
        assert result
        for port, output in result.iteritems():
            assert output['returncode'] == -15

    def test_restart_service(self):
        # start it if it isn't running
        h = httplib2.Http()
        resp, content = h.request(self.base + '/services/simpleweb/start',
                                  method='POST',
                                  headers={'Content-Length': '0'})
        result = simplejson.loads(content)
        assert result['started'] == 'simpleweb'
        pids = result['pids']

        resp, content = h.request(self.base + '/services/simpleweb/restart',
                                  method='POST',
                                  headers={'Content-Length': '0'})

        pprint(resp)
        print content

        assert resp['status'] == '200'
        result = simplejson.loads(content)
        assert result['started'] == 'simpleweb'        
        assert result['pids'] != pids