1
|
|
|
import mock |
2
|
|
|
import yaml |
3
|
|
|
|
4
|
|
|
from st2tests.base import BaseSensorTestCase |
5
|
|
|
|
6
|
|
|
from sqs_sensor import AWSSQSSensor |
7
|
|
|
|
8
|
|
|
|
9
|
|
|
class MockQueue(object): |
10
|
|
|
def __init__(self, msgs=[]): |
11
|
|
|
self.dummy_messages = [MockMessage(x) for x in msgs] |
12
|
|
|
|
13
|
|
|
def receive_messages(self, **kwargs): |
14
|
|
|
return self.dummy_messages |
15
|
|
|
|
16
|
|
|
|
17
|
|
|
class MockMessage(object): |
18
|
|
|
def __init__(self, body=None): |
19
|
|
|
self.body = body |
20
|
|
|
|
21
|
|
|
def delete(self): |
22
|
|
|
return mock.MagicMock(return_value=None) |
23
|
|
|
|
24
|
|
|
|
25
|
|
|
class SQSSensorTestCase(BaseSensorTestCase): |
26
|
|
|
sensor_cls = AWSSQSSensor |
27
|
|
|
|
28
|
|
|
def setUp(self): |
29
|
|
|
super(SQSSensorTestCase, self).setUp() |
30
|
|
|
|
31
|
|
|
self.full_config = self.load_yaml('full.yaml') |
32
|
|
|
|
33
|
|
|
def load_yaml(self, filename): |
34
|
|
|
return yaml.safe_load(self.get_fixture_content(filename)) |
35
|
|
|
|
36
|
|
|
def test_poll_without_message(self): |
37
|
|
|
sensor = self.get_sensor_instance(config=self.full_config) |
38
|
|
|
|
39
|
|
|
sensor.setup() |
40
|
|
|
|
41
|
|
|
# replace ServiceResource object to mock |
42
|
|
|
sensor.sqs_res = mock.MagicMock() |
43
|
|
|
sensor.sqs_res.get_queue_by_name = mock.MagicMock(return_value=MockQueue()) |
44
|
|
|
|
45
|
|
|
sensor.poll() |
46
|
|
|
|
47
|
|
|
self.assertEqual(self.get_dispatched_triggers(), []) |
48
|
|
|
|
49
|
|
|
def test_poll_with_message(self): |
50
|
|
|
sensor = self.get_sensor_instance(config=self.full_config) |
51
|
|
|
|
52
|
|
|
sensor.setup() |
53
|
|
|
|
54
|
|
|
# replace ServiceResource object to mock |
55
|
|
|
sensor.sqs_res = mock.MagicMock() |
56
|
|
|
sensor.sqs_res.get_queue_by_name = mock.MagicMock(return_value=MockQueue(['foo'])) |
57
|
|
|
|
58
|
|
|
sensor.poll() |
59
|
|
|
|
60
|
|
|
self.assertTriggerDispatched(trigger='aws.sqs_new_message') |
61
|
|
|
self.assertNotEqual(self.get_dispatched_triggers(), []) |
62
|
|
|
|