~didrocks/unity/altf10

« back to all changes in this revision

Viewing changes to grackle/tests/test_client.py

  • Committer: Aaron Bentley
  • Date: 2012-01-10 14:41:33 UTC
  • Revision ID: aaron@canonical.com-20120110144133-p9dk4oktqnp3lnlw
Fix URLs etc.

Show diffs side-by-side

added added

removed removed

Lines of Context:
10
10
 
11
11
from testtools import ExpectedException
12
12
 
13
 
from grackle import client
 
13
from grackle.client import GrackleClient
14
14
 
15
15
 
16
16
class Forked:
17
17
 
18
 
    def __init__(self, func_or_method):
 
18
    def __init__(self, func_or_method, *args):
19
19
        self.func_or_method = func_or_method
20
20
        self.pid = None
 
21
        self.args = args
21
22
 
22
23
    def __enter__(self):
23
24
        pid = os.fork()
24
25
        if pid != 0:
25
26
            self.pid = pid
26
27
            return
27
 
        self.func_or_method()
 
28
        self.func_or_method(*self.args)
28
29
 
29
30
 
30
31
    def __exit__(self, exc_type, exc_val, traceback):
33
34
 
34
35
class FakeGrackleRequestHandler(BaseHTTPRequestHandler):
35
36
 
36
 
    def do_POST(self):
 
37
    def do_PUT(self):
37
38
        message = self.rfile.read(int(self.headers['content-length']))
38
39
        if message == 'This is a message':
39
40
            self.send_response(httplib.CREATED)
43
44
            self.send_error(httplib.BAD_REQUEST)
44
45
 
45
46
 
46
 
def run_service():
47
 
    service = HTTPServer(('', 8435), FakeGrackleRequestHandler)
 
47
def run_service(port):
 
48
    service = HTTPServer(('', port), FakeGrackleRequestHandler)
48
49
    service.serve_forever()
49
50
 
50
51
 
52
53
class TestPutMessage(TestCase):
53
54
 
54
55
    def test_put_message(self):
55
 
        with Forked(run_service):
56
 
            client.put_message('arch1', StringIO('This is a message'))
 
56
        client = GrackleClient('localhost', 8435)
 
57
        with Forked(run_service, client.port):
 
58
            client.put_message('arch1', 'asdf', StringIO('This is a message'))
57
59
            with ExpectedException(Exception, 'wtf'):
58
 
                client.put_message('arch1', StringIO('This is not a message'))
 
60
                client.put_message('arch1', 'asdf',
 
61
                    StringIO('This is not a message'))