summaryrefslogtreecommitdiffstats
path: root/tests/unit/network_services/nfvi/test_collectd.py
blob: 5bd7196df2f5a0279d8878145552e26ed7a5e1cf (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
# Copyright (c) 2016-2017 Intel Corporation
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#      http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

from __future__ import absolute_import
import unittest
import multiprocessing
import mock

from yardstick.network_services.nfvi.collectd import AmqpConsumer


class TestAmqpConsumer(unittest.TestCase):
    def setUp(self):
        self.queue = multiprocessing.Queue()
        self.url = 'amqp://admin:admin@1.1.1.1:5672/%2F'
        self.amqp_consumer = AmqpConsumer(self.url, self.queue)

    def test___init__(self):
        self.assertEqual(self.url, self.amqp_consumer._url)

    def test_connect(self):
        self.assertRaises(RuntimeError, self.amqp_consumer.connect)

    def test_on_connection_open(self):
        self.amqp_consumer._connection = mock.Mock(autospec=AmqpConsumer)
        self.amqp_consumer._connection.add_on_close_callback = \
            mock.Mock(return_value=0)
        self.amqp_consumer._connection.channel = mock.Mock(return_value=0)
        self.assertEqual(None, self.amqp_consumer.on_connection_open(10))

    def test_on_connection_closed(self):
        self.amqp_consumer._connection = mock.Mock(autospec=AmqpConsumer)
        self.amqp_consumer._connection.ioloop = mock.Mock()
        self.amqp_consumer._connection.ioloop.stop = mock.Mock(return_value=0)
        self.amqp_consumer._connection.add_timeout = mock.Mock(return_value=0)
        self.amqp_consumer._closing = True
        self.assertEqual(None,
                         self.amqp_consumer.on_connection_closed("", 404,
                                                                 "Not Found"))
        self.amqp_consumer._closing = False
        self.assertEqual(None,
                         self.amqp_consumer.on_connection_closed("", 404,
                                                                 "Not Found"))

    def test_reconnect(self):
        self.amqp_consumer._connection = mock.Mock(autospec=AmqpConsumer)
        self.amqp_consumer._connection.ioloop = mock.Mock()
        self.amqp_consumer._connection.ioloop.stop = mock.Mock(return_value=0)
        self.amqp_consumer.connect = mock.Mock(return_value=0)
        self.amqp_consumer._closing = True
        self.assertEqual(None, self.amqp_consumer.reconnect())

    def test_on_channel_open(self):
        self.amqp_consumer._connection = mock.Mock(autospec=AmqpConsumer)
        self.amqp_consumer._connection.add_on_close_callback = \
            mock.Mock(return_value=0)
        self.amqp_consumer._channel = mock.Mock()
        self.amqp_consumer.add_on_channel_close_callback = mock.Mock()
        self.amqp_consumer._channel.exchange_declare = \
            mock.Mock(return_value=0)
        self.assertEqual(None,
                         self.amqp_consumer.on_channel_open(
                             self.amqp_consumer._channel))

    def test_add_on_channel_close_callback(self):
        self.amqp_consumer._connection = mock.Mock(autospec=AmqpConsumer)
        self.amqp_consumer._connection.add_on_close_callback = \
            mock.Mock(return_value=0)
        self.amqp_consumer._channel = mock.Mock()
        self.amqp_consumer._channel.add_on_close_callback = mock.Mock()
        self.assertEqual(None,
                         self.amqp_consumer.add_on_channel_close_callback())

    def test_on_channel_closed(self):
        self.amqp_consumer._connection = mock.Mock(autospec=AmqpConsumer)
        self.amqp_consumer._connection.close = mock.Mock(return_value=0)
        _channel = mock.Mock()
        self.assertEqual(None,
                         self.amqp_consumer.on_channel_closed(_channel,
                                                              "", ""))

    def test_ion_exchange_declareok(self):
        self.amqp_consumer.setup_queue = mock.Mock(return_value=0)
        self.assertEqual(None, self.amqp_consumer.on_exchange_declareok(10))

    def test_setup_queue(self):
        self.amqp_consumer._channel = mock.Mock()
        self.amqp_consumer._channel.add_on_close_callback = mock.Mock()
        self.assertEqual(None, self.amqp_consumer.setup_queue("collectd"))

    def test_on_queue_declareok(self):
        self.amqp_consumer._channel = mock.Mock()
        self.amqp_consumer._channel.queue_bind = mock.Mock()
        self.assertEqual(None, self.amqp_consumer.on_queue_declareok(10))

    def test__on_bindok(self):
        self.amqp_consumer._channel = mock.Mock()
        self.amqp_consumer._channel.basic_consume = mock.Mock()
        self.amqp_consumer.add_on_cancel_callback = mock.Mock()
        self.assertEqual(None, self.amqp_consumer._on_bindok(10))

    def test_add_on_cancel_callback(self):
        self.amqp_consumer._channel = mock.Mock()
        self.amqp_consumer._channel.add_on_cancel_callback = mock.Mock()
        self.assertEqual(None, self.amqp_consumer.add_on_cancel_callback())

    def test_on_consumer_cancelled(self):
        self.amqp_consumer._channel = mock.Mock()
        self.amqp_consumer._channel.close = mock.Mock()
        self.assertEqual(None, self.amqp_consumer.on_consumer_cancelled(10))

    def test_on_message(self):
        body = "msg {} cpu/cpu-0/ipc 101010:10"
        properties = ""
        basic_deliver = mock.Mock()
        basic_deliver.delivery_tag = mock.Mock(return_value=0)
        self.amqp_consumer.ack_message = mock.Mock()
        self.assertEqual(None,
                         self.amqp_consumer.on_message(10, basic_deliver,
                                                       properties, body))

    def test_ack_message(self):
        self.amqp_consumer._channel = mock.Mock()
        self.amqp_consumer._channel.basic_ack = mock.Mock()
        self.assertEqual(None, self.amqp_consumer.ack_message(10))

    def test_on_cancelok(self):
        self.amqp_consumer._channel = mock.Mock()
        self.amqp_consumer._channel.close = mock.Mock()
        self.assertEqual(None, self.amqp_consumer.on_cancelok(10))

    def test_run(self):
        self.amqp_consumer._connection = mock.Mock(autospec=AmqpConsumer)
        self.amqp_consumer.connect = mock.Mock()
        self.amqp_consumer._connection.ioloop.start = mock.Mock()
        self.assertEqual(None, self.amqp_consumer.run())

    def test_stop(self):
        self.amqp_consumer._connection = mock.Mock(autospec=AmqpConsumer)
        self.amqp_consumer.connect = mock.Mock()
        self.amqp_consumer._connection.ioloop.start = mock.Mock()
        self.amqp_consumer._channel = mock.Mock()
        self.amqp_consumer._channel.basic_cancel = mock.Mock()
        self.assertEqual(None, self.amqp_consumer.stop())

    def test_close_connection(self):
        self.amqp_consumer._connection = mock.Mock(autospec=AmqpConsumer)
        self.amqp_consumer._connection.close = mock.Mock()
        self.assertEqual(None, self.amqp_consumer.close_connection())