|
| 1 | +# Copyright 2020 The StackStorm Authors. |
| 2 | +# Copyright 2019 Extreme Networks, Inc. |
| 3 | +# |
| 4 | +# Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | +# you may not use this file except in compliance with the License. |
| 6 | +# You may obtain a copy of the License at |
| 7 | +# |
| 8 | +# http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | +# |
| 10 | +# Unless required by applicable law or agreed to in writing, software |
| 11 | +# distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | +# See the License for the specific language governing permissions and |
| 14 | +# limitations under the License. |
| 15 | + |
| 16 | +import mock |
| 17 | +import unittest2 |
| 18 | + |
| 19 | +from st2common.stream import listener |
| 20 | + |
| 21 | + |
| 22 | +class MockBody(object): |
| 23 | + |
| 24 | + def __init__(self, id): |
| 25 | + self.id = id |
| 26 | + self.status = "succeeded" |
| 27 | + |
| 28 | + |
| 29 | +INCLUDE = "test" |
| 30 | +END_EVENT = "test_end_event" |
| 31 | +END_ID = "test_end_id" |
| 32 | +EVENTS = [(INCLUDE, MockBody("notend")), (END_EVENT, MockBody(END_ID))] |
| 33 | + |
| 34 | + |
| 35 | +class MockQueue(): |
| 36 | + |
| 37 | + def __init__(self): |
| 38 | + self.items = EVENTS |
| 39 | + |
| 40 | + def get(self, *args, **kwargs): |
| 41 | + if len(self.items) > 0: |
| 42 | + return self.items.pop(0) |
| 43 | + return None |
| 44 | + |
| 45 | + def put(self, event): |
| 46 | + self.items.append(event) |
| 47 | + |
| 48 | + |
| 49 | +class MockListener(listener.BaseListener): |
| 50 | + |
| 51 | + def __init__(self, *args, **kwargs): |
| 52 | + super(MockListener, self).__init__(*args, **kwargs) |
| 53 | + |
| 54 | + def get_consumers(self, consumer, channel): |
| 55 | + pass |
| 56 | + |
| 57 | + |
| 58 | +class TestStream(unittest2.TestCase): |
| 59 | + |
| 60 | + @mock.patch('st2common.stream.listener.BaseListener._get_action_ref_for_body') |
| 61 | + @mock.patch('eventlet.Queue') |
| 62 | + def test_generator(self, mock_queue, |
| 63 | + get_action_ref_for_body): |
| 64 | + get_action_ref_for_body.return_value = None |
| 65 | + mock_queue.return_value = MockQueue() |
| 66 | + mock_consumer = MockListener(connection=None) |
| 67 | + mock_consumer._stopped = False |
| 68 | + app_iter = mock_consumer.generator(events=INCLUDE, |
| 69 | + end_event=END_EVENT, |
| 70 | + end_statuses=["succeeded"], |
| 71 | + end_execution_id=END_ID) |
| 72 | + events = EVENTS.append('') |
| 73 | + for index, val in enumerate(app_iter): |
| 74 | + self.assertEquals(val, events[index]) |
0 commit comments