1
# Copyright (c) 2012 Canonical Ltd
3
# This program is free software: you can redistribute it and/or modify
4
# it under the terms of the GNU Affero General Public License as published by
5
# the Free Software Foundation, either version 3 of the License, or
6
# (at your option) any later version.
8
# This program is distributed in the hope that it will be useful,
9
# but WITHOUT ANY WARRANTY; without even the implied warranty of
10
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11
# GNU Affero General Public License for more details.
13
# You should have received a copy of the GNU Affero General Public
14
# License along with this program. If not, see
15
# <http://www.gnu.org/licenses/>.
20
from email.utils import formatdate
22
from tempfile import _RandomNameSequence
26
from dateutil.tz import (
30
from pycassa.util import convert_uuid_to_time
32
from grackle.model import (
40
TEMPLATE_MESSAGE = """\
41
From: sysadmin@example.com
42
To: developer@example.com
43
Subject: Everything is broken
47
Help, everything has just broken.
50
TEST_MESSAGE = TEMPLATE_MESSAGE.format(
51
date='Sat, 1 Jan 2000 11:02:34 +1100',
52
id='<aaaaaaaaaaaaa@example.com>')
55
class TestParseMessage(unittest.TestCase):
58
# _parse_message extracts interesting fields. It also parses the
59
# date and returns it separately.
60
date, msg = _parse_message(TEST_MESSAGE)
61
self.assertEqual('sysadmin@example.com', msg['from'])
62
self.assertEqual('developer@example.com', msg['to'])
63
self.assertEqual('Everything is broken', msg['subject'])
64
self.assertEqual('2000-01-01T11:02:34+11:00', msg['date'])
65
self.assertEqual('<aaaaaaaaaaaaa@example.com>', msg['message-id'])
68
2000, 1, 1, 11, 2, 34, tzinfo=tzoffset('', 39600)),
72
class TestAddMessage(unittest.TestCase):
74
def test_add_message(self):
75
c = CassandraConnection(
76
os.environ['GRACKLE_TEST_KEYSPACE'], ['localhost:9160'])
77
archive = next(_RandomNameSequence())
79
# Write the message out to Cassandra, and read it back in.
80
key = c.add_message(archive, TEST_MESSAGE)
81
cmsg = c.messages.get(key)
83
# The archive should contain a single message, a reference to
85
archive_messages = c.archive_messages.get(archive).items()
86
self.assertEqual(1, len(archive_messages))
87
self.assertEqual(key, archive_messages[0][1])
89
# The key in archive_message is a TimeUUID for the Date field in
90
# the message. There is no UTC equivalent of time.mktime, so we
91
# must subtract the offset.
92
utctime = time.mktime(datetime.datetime(
93
2000, 1, 1, 0, 2, 34, tzinfo=tzutc()).timetuple()) - time.timezone
96
convert_uuid_to_time(archive_messages[0][0]))
98
# The stored message contains the full original text of the
99
# message, as well as interesting fields parsed out.
100
self.assertEqual(TEST_MESSAGE, cmsg['content'])
101
parsed_message = _parse_message(TEST_MESSAGE)[1]
102
for key, value in parsed_message.iteritems():
103
self.assertEqual(value, cmsg[key])
106
class TestGetMessages(unittest.TestCase):
108
def assertMessages(self, expected_ids, messages):
110
'<message%d@example.com>' % id for id in expected_ids]
111
actual_msgids = [msg['message-id'] for msg in messages]
112
self.assertEqual(expected_msgids, actual_msgids)
114
def makeMessages(self, conn, archive, count):
118
TEMPLATE_MESSAGE.format(
119
date=formatdate(i * 100),
120
id='<message%d@example.com>' % i))
121
for i in range(count)]
123
def makeArchive(self):
124
conn = CassandraConnection(
125
os.environ['GRACKLE_TEST_KEYSPACE'], ['localhost:9160'])
126
archive = next(_RandomNameSequence())
129
def test_single_message(self):
130
conn, archive = self.makeArchive()
131
self.makeMessages(conn, archive, 1)
133
# We get a single message when we ask for it.
134
messages = conn.get_messages(archive, 'date', 1, '')[1]
135
self.assertMessages([0], messages)
138
# The content matches what we generated, and the result is
139
# formatted according to the default settings.
140
expected_content = TEMPLATE_MESSAGE.format(
141
date=formatdate(0), id='<message0@example.com>')
142
self.assertEqual(expected_content, messages[0]['content'])
143
pmsg = _parse_message(expected_content)[1]
144
pmsg['content'] = expected_content
147
['date', 'from', 'subject', 'message-id', 'content'],
151
def test_limit(self):
152
conn, archive = self.makeArchive()
153
self.makeMessages(conn, archive, 4)
155
[0, 1], conn.get_messages(archive, 'date', 2, '')[1])
157
def test_order(self):
158
conn, archive = self.makeArchive()
159
self.makeMessages(conn, archive, 4)
161
[3, 2], conn.get_messages(archive, '-date', 2, '')[1])
163
def test_batching_forward(self):
164
conn, archive = self.makeArchive()
165
self.makeMessages(conn, archive, 5)
166
prev, messages, next = conn.get_messages(archive, 'date', 2, '')
167
self.assertMessages([0, 1], messages)
168
prev, messages, next = conn.get_messages(archive, 'date', 2, next)
169
self.assertMessages([2, 3], messages)
170
prev, messages, next = conn.get_messages(archive, 'date', 2, next)
171
self.assertMessages([4], messages)
172
prev, messages, next = conn.get_messages(archive, 'date', 2, next)
173
self.assertIs(None, prev)
174
self.assertMessages([], messages)
175
self.assertIs(None, next)
177
def test_batching_backward(self):
178
conn, archive = self.makeArchive()
179
self.makeMessages(conn, archive, 5)
180
prev, messages, next = conn.get_messages(archive, 'date', 2, '')
181
self.assertMessages([0, 1], messages)
182
prev, messages, next = conn.get_messages(archive, 'date', 2, next)
183
self.assertMessages([2, 3], messages)
184
prev, messages, next = conn.get_messages(
185
archive, 'date', 2, prev, backward=True)
186
self.assertMessages([0, 1], messages)
187
prev, messages, next = conn.get_messages(
188
archive, 'date', 2, prev, backward=True)
189
self.assertIs(None, prev)
190
self.assertMessages([], messages)
191
self.assertIs(None, next)
193
def test_date_filter(self):
194
conn, archive = self.makeArchive()
195
self.makeMessages(conn, archive, 10)
196
start = datetime.datetime.utcfromtimestamp(250).replace(
198
finish = datetime.datetime.utcfromtimestamp(500).replace(
200
prev, messages, next = conn.get_messages(
201
archive, 'date', 2, '', start_date=start, finish_date=finish)
202
self.assertMessages([3, 4], messages)
203
prev, messages, next = conn.get_messages(
204
archive, 'date', 2, next, start_date=start, finish_date=finish)
205
self.assertMessages([5], messages)
208
class TestMessageFormatters(unittest.TestCase):
211
parsed = _parse_message(TEST_MESSAGE)[1]
212
parsed['content'] = TEST_MESSAGE
213
want, func = FORMATS['all'](['date', 'from', 'subject', 'message-id'])
215
['content', 'date', 'from', 'message-id', 'subject'],
217
formatted = func(parsed)
219
'date': '2000-01-01T11:02:34+11:00',
220
'from': 'sysadmin@example.com',
221
'message-id': '<aaaaaaaaaaaaa@example.com>',
222
'subject': 'Everything is broken',
223
'content': TEST_MESSAGE,
225
self.assertEqual(expected, formatted)