~didrocks/unity/altf10

4 by Aaron Bentley
Initial test.
1
from BaseHTTPServer import (
2
    HTTPServer,
3
    BaseHTTPRequestHandler,
4
    )
6 by Aaron Bentley
Use constants.
5
import httplib
4 by Aaron Bentley
Initial test.
6
import os
5 by Aaron Bentley
Actual fake service working.
7
from signal import SIGKILL
13 by Aaron Bentley
Retrieve messages.
8
import simplejson
4 by Aaron Bentley
Initial test.
9
from StringIO import StringIO
3 by Aaron Bentley
Add test framework.
10
from unittest import TestCase
13 by Aaron Bentley
Retrieve messages.
11
from urlparse import urlparse
3 by Aaron Bentley
Add test framework.
12
5 by Aaron Bentley
Actual fake service working.
13
from testtools import ExpectedException
14
8 by Aaron Bentley
Test message path.
15
from grackle.client import (
16
    GrackleClient,
17
    )
4 by Aaron Bentley
Initial test.
18
19
12 by Aaron Bentley
Use pipe to ensure we only use HTTP once it's running.
20
class ForkedFake:
5 by Aaron Bentley
Actual fake service working.
21
12 by Aaron Bentley
Use pipe to ensure we only use HTTP once it's running.
22
    def __init__(self, port, messages=None):
5 by Aaron Bentley
Actual fake service working.
23
        self.pid = None
12 by Aaron Bentley
Use pipe to ensure we only use HTTP once it's running.
24
        self.port = port
25
        self.messages = messages
26
        self.read_end, self.write_end = os.pipe()
27
28
    def is_ready(self):
29
        os.write(self.write_end, 'asdf')
5 by Aaron Bentley
Actual fake service working.
30
31
    def __enter__(self):
32
        pid = os.fork()
12 by Aaron Bentley
Use pipe to ensure we only use HTTP once it's running.
33
        if pid == 0:
34
            self.start_server()
35
        self.pid = pid
36
        os.read(self.read_end, 1)
37
        return
5 by Aaron Bentley
Actual fake service working.
38
12 by Aaron Bentley
Use pipe to ensure we only use HTTP once it's running.
39
    def start_server(self):
40
        service = HTTPServer(('', self.port), FakeGrackleRequestHandler)
41
        service.messages = self.messages
42
        self.is_ready()
43
        service.serve_forever()
5 by Aaron Bentley
Actual fake service working.
44
45
    def __exit__(self, exc_type, exc_val, traceback):
46
        os.kill(self.pid, SIGKILL)
47
48
49
class FakeGrackleRequestHandler(BaseHTTPRequestHandler):
50
11 by Aaron Bentley
Start working on GET.
51
    def do_POST(self):
5 by Aaron Bentley
Actual fake service working.
52
        message = self.rfile.read(int(self.headers['content-length']))
53
        if message == 'This is a message':
6 by Aaron Bentley
Use constants.
54
            self.send_response(httplib.CREATED)
5 by Aaron Bentley
Actual fake service working.
55
            self.end_headers()
56
            self.wfile.close()
57
        else:
6 by Aaron Bentley
Use constants.
58
            self.send_error(httplib.BAD_REQUEST)
5 by Aaron Bentley
Actual fake service working.
59
13 by Aaron Bentley
Retrieve messages.
60
    def do_GET(self):
61
        scheme, netloc, path, params, query, fragments = urlparse(self.path)
62
        archive = os.path.split(path)[1]
63
        self.send_response(httplib.OK)
64
        self.end_headers()
65
        self.wfile.write(simplejson.dumps(self.server.messages[archive]))
66
5 by Aaron Bentley
Actual fake service working.
67
11 by Aaron Bentley
Start working on GET.
68
def fake_grackle_service(client, messages=None):
69
    if messages is None:
70
        messages = {}
12 by Aaron Bentley
Use pipe to ensure we only use HTTP once it's running.
71
    return ForkedFake(client.port, messages)
11 by Aaron Bentley
Start working on GET.
72
4 by Aaron Bentley
Initial test.
73
3 by Aaron Bentley
Add test framework.
74
class TestPutMessage(TestCase):
75
76
    def test_put_message(self):
12 by Aaron Bentley
Use pipe to ensure we only use HTTP once it's running.
77
        client = GrackleClient('localhost', 8436)
11 by Aaron Bentley
Start working on GET.
78
        with fake_grackle_service(client):
7 by Aaron Bentley
Fix URLs etc.
79
            client.put_message('arch1', 'asdf', StringIO('This is a message'))
5 by Aaron Bentley
Actual fake service working.
80
            with ExpectedException(Exception, 'wtf'):
7 by Aaron Bentley
Fix URLs etc.
81
                client.put_message('arch1', 'asdf',
82
                    StringIO('This is not a message'))
11 by Aaron Bentley
Start working on GET.
83
84
85
class TestGetMessages(TestCase):
86
87
    def test_get_messages(self):
88
        client = GrackleClient('localhost', 8435)
89
        with fake_grackle_service(client,
90
            {'baz':
91
            [{'message-id': 'foo'},
92
             {'message-id': 'bar'}]}):
13 by Aaron Bentley
Retrieve messages.
93
            response = client.get_messages('baz', message_ids=['a'])
94
        self.assertEqual(['bar', 'foo'], sorted(m['message-id'] for m in
95
            response))