~didrocks/unity/altf10

« back to all changes in this revision

Viewing changes to grackle/tests/test_client.py

  • Committer: Aaron Bentley
  • Date: 2012-01-16 20:31:23 UTC
  • Revision ID: aaron@canonical.com-20120116203123-bxg29ktwtq19al75
Cleanup

Show diffs side-by-side

added added

removed removed

Lines of Context:
37
37
 
38
38
 
39
39
class GrackleStore:
 
40
    """A memory-backed message store."""
40
41
 
41
42
    def __init__(self, messages):
 
43
        """Constructor."""
42
44
        self.messages = messages
43
45
 
44
46
    def get_messages(self, archive_id, query_string):
 
47
        """Return matching messages.
 
48
 
 
49
        :param archive_id: The archive to retrieve from.
 
50
        :param query_string: Contains 'parameters', which is a JSON-format
 
51
            string describing parameters.
 
52
        """
45
53
        query = parse_qs(query_string)
46
54
        parameters = simplejson.loads(query['parameters'][0])
47
55
        order = parameters.get('order')
112
120
 
113
121
 
114
122
class ForkedFake:
 
123
    """A Grackle service fake, as a ContextManager."""
115
124
 
116
125
    def __init__(self, port, messages=None, write_logs=False):
 
126
        """Constructor.
 
127
        :param port: The tcp port to use
 
128
        :param messages: A dict of lists of dicts representing messages.  The
 
129
            outer dict represents the archive, the list represents the list of
 
130
            messages for that archive.
 
131
        :param write_logs: If true, log messages will be written to stdout.
 
132
        """
117
133
        self.pid = None
118
134
        self.port = port
119
135
        if messages is None:
125
141
 
126
142
    @staticmethod
127
143
    def from_client(client, messages=None):
 
144
        """Instantiate a ForkedFake from the client.
 
145
 
 
146
        :param port: The client  to provide service for.
 
147
        :param messages: A dict of lists of dicts representing messages.  The
 
148
            outer dict represents the archive, the list represents the list of
 
149
            messages for that archive.
 
150
        """
128
151
        return ForkedFake(client.port, messages)
129
152
 
130
153
    def is_ready(self):
 
154
        """Tell the parent process that the server is ready for writes."""
131
155
        os.write(self.write_end, 'asdf')
132
156
 
133
157
    def __enter__(self):
 
158
        """Run the service.
 
159
 
 
160
        Fork and start a server in the child.  Return when the server is ready
 
161
        for use."""
134
162
        pid = os.fork()
135
163
        if pid == 0:
136
164
            self.start_server()
139
167
        return
140
168
 
141
169
    def start_server(self):
 
170
        """Start the HTTP server."""
142
171
        service = HTTPServer(('', self.port), FakeGrackleRequestHandler)
143
172
        service.store = GrackleStore(self.messages)
144
173
        for archive_id, messages in service.store.messages.iteritems():
160
189
 
161
190
 
162
191
class FakeGrackleRequestHandler(BaseHTTPRequestHandler):
 
192
    """A request handler that forwards to server.store."""
163
193
 
164
194
    def __init__(self, *args, **kwargs):
 
195
        """Constructor.  Sets up logging."""
165
196
        self.logger = logging.getLogger('http')
166
197
        BaseHTTPRequestHandler.__init__(self, *args, **kwargs)
167
198
 
168
199
    def do_POST(self):
 
200
        """Create a message on POST."""
169
201
        message = self.rfile.read(int(self.headers['content-length']))
170
202
        if message == 'This is a message':
171
203
            self.send_response(httplib.CREATED)
175
207
            self.send_error(httplib.BAD_REQUEST)
176
208
 
177
209
    def do_GET(self):
 
210
        """Retrieve a list of messages on GET."""
178
211
        scheme, netloc, path, params, query_string, fragments = (
179
212
            urlparse(self.path))
180
213
        parts = path.split('/')
191
224
                return
192
225
 
193
226
    def log_message(self, format, *args):
 
227
        """Override log_message to use standard Python logging."""
194
228
        message = "%s - - [%s] %s\n" % (
195
229
            self.address_string(), self.log_date_time_string(), format%args)
196
230
        self.logger.info(message)