Illyoung Choi | a9d2c2c | 2019-07-12 13:29:42 -0700 | [diff] [blame] | 1 | # Copyright 2019-present Open Networking Foundation |
| 2 | # |
| 3 | # Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | # you may not use this file except in compliance with the License. |
| 5 | # You may obtain a copy of the License at |
| 6 | # |
| 7 | # http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | # |
| 9 | # Unless required by applicable law or agreed to in writing, software |
| 10 | # distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | # See the License for the specific language governing permissions and |
| 13 | # limitations under the License. |
| 14 | |
| 15 | from __future__ import absolute_import |
| 16 | import unittest |
| 17 | import time |
| 18 | from cord_workflow_controller_client.probe import Probe |
| 19 | from multistructlog import create_logger |
| 20 | from .dummy_server import start as server_start, stop as server_stop |
| 21 | |
| 22 | log = create_logger() |
| 23 | |
| 24 | |
| 25 | class TestProbe(unittest.TestCase): |
| 26 | """ |
| 27 | Try to connect to a local fake Controller Service as a Probe. |
| 28 | """ |
| 29 | |
| 30 | def setUp(self): |
| 31 | self.server = server_start(17080) |
| 32 | |
| 33 | def tearDown(self): |
| 34 | server_stop(self.server) |
| 35 | self.server = None |
| 36 | |
| 37 | def test_connect(self): |
| 38 | """ |
| 39 | This tests if Probe client can connect to a socket.io server properly. |
| 40 | """ |
| 41 | succeed = False |
| 42 | try: |
| 43 | probe = Probe(logger=log) |
| 44 | probe.connect('http://localhost:17080') |
| 45 | |
| 46 | time.sleep(1) |
| 47 | |
| 48 | probe.disconnect() |
| 49 | succeed = True |
| 50 | finally: |
| 51 | self.assertTrue(succeed, 'Finished with error') |
| 52 | |
| 53 | def test_emit_string(self): |
| 54 | """ |
| 55 | This tests if Probe client can emit an event. |
| 56 | """ |
| 57 | succeed = False |
| 58 | try: |
| 59 | probe = Probe(logger=log) |
| 60 | probe.connect('http://localhost:17080') |
| 61 | |
| 62 | probe.emit_event('xos.test.event', 'string message - hello') |
| 63 | time.sleep(1) |
| 64 | |
| 65 | probe.disconnect() |
| 66 | succeed = True |
| 67 | finally: |
| 68 | self.assertTrue(succeed, 'Finished with error') |
| 69 | |
| 70 | def test_emit_json(self): |
| 71 | """ |
| 72 | This tests if Probe client can emit an event with a dict (json) object. |
| 73 | """ |
| 74 | succeed = False |
| 75 | try: |
| 76 | probe = Probe(logger=log) |
| 77 | probe.connect('http://localhost:17080') |
| 78 | |
| 79 | probe.emit_event( |
| 80 | 'xos.test.event', |
| 81 | { |
| 82 | 'str_key': 'value', |
| 83 | 'int_key': 32335 |
| 84 | } |
| 85 | ) |
| 86 | time.sleep(1) |
| 87 | |
| 88 | probe.disconnect() |
| 89 | succeed = True |
| 90 | finally: |
| 91 | self.assertTrue(succeed, 'Finished with error') |
| 92 | |
| 93 | |
| 94 | if __name__ == "__main__": |
| 95 | unittest.main() |