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