summaryrefslogtreecommitdiff
path: root/python/tests_0-10/management.py
blob: 7e189678036176a2b1889f057f169470f370532f (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
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
#
# Licensed to the Apache Software Foundation (ASF) under one
# or more contributor license agreements.  See the NOTICE file
# distributed with this work for additional information
# regarding copyright ownership.  The ASF licenses this file
# to you 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 qpid.datatypes import Message, RangedSet
from qpid.testlib import TestBase010
from qpid.management import managementChannel, managementClient
from qpid import qmfconsole

class ManagementTest (TestBase010):
    """
    Tests for the management hooks
    """

    def test_broker_connectivity_oldAPI (self):
        """
        Call the "echo" method on the broker to verify it is alive and talking.
        """
        session = self.session
 
        mc  = managementClient (session.spec)
        mch = mc.addChannel (session)

        mc.syncWaitForStable (mch)
        brokers = mc.syncGetObjects (mch, "broker")
        self.assertEqual (len (brokers), 1)
        broker = brokers[0]
        args = {}
        body = "Echo Message Body"
        args["body"] = body

        for seq in range (1, 5):
            args["sequence"] = seq
            res = mc.syncCallMethod (mch, broker.id, broker.classKey, "echo", args)
            self.assertEqual (res.status,     0)
            self.assertEqual (res.statusText, "OK")
            self.assertEqual (res.sequence,   seq)
            self.assertEqual (res.body,       body)
        mc.removeChannel (mch)

    def test_broker_connectivity (self):
        """
        Call the "echo" method on the broker to verify it is alive and talking.
        """
        session = self.session
        self.startQmf()
 
        brokers = self.qmf.getObjects(name="broker")
        self.assertEqual (len(brokers), 1)
        broker = brokers[0]

        body = "Echo Message Body"
        for seq in range (1, 10):
            res = broker.echo(seq, body)
            self.assertEqual (res.status,   0)
            self.assertEqual (res.text,     "OK")
            self.assertEqual (res.sequence, seq)
            self.assertEqual (res.body,     body)

    def test_system_object (self):
        session = self.session
 
        mc  = managementClient (session.spec)
        mch = mc.addChannel (session)

        mc.syncWaitForStable (mch)
        systems = mc.syncGetObjects (mch, "system")
        self.assertEqual (len (systems), 1)
        mc.removeChannel (mch)

    def test_self_session_id (self):
        session = self.session
 
        mc  = managementClient (session.spec)
        mch = mc.addChannel (session)

        info = mc.syncWaitForStable (mch)
        brokerSessions = mc.syncGetObjects (mch, "session")
        found = False
        for bs in brokerSessions:
            if bs.name == info.sessionId:
                found = True
        self.assertEqual (found, True)
        mc.removeChannel (mch)

    def test_standard_exchanges (self):
        session = self.session
 
        mc  = managementClient (session.spec)
        mch = mc.addChannel (session)

        mc.syncWaitForStable (mch)
        exchanges = mc.syncGetObjects (mch, "exchange")
        exchange = self.findExchange (exchanges, "")
        self.assertEqual (exchange.type, "direct")
        exchange = self.findExchange (exchanges, "amq.direct")
        self.assertEqual (exchange.type, "direct")
        exchange = self.findExchange (exchanges, "amq.topic")
        self.assertEqual (exchange.type, "topic")
        exchange = self.findExchange (exchanges, "amq.fanout")
        self.assertEqual (exchange.type, "fanout")
        exchange = self.findExchange (exchanges, "amq.match")
        self.assertEqual (exchange.type, "headers")
        exchange = self.findExchange (exchanges, "qpid.management")
        self.assertEqual (exchange.type, "topic")
        mc.removeChannel (mch)

    def findExchange (self, exchanges, name):
        for exchange in exchanges:
            if exchange.name == name:
                return exchange
        return None

    def test_move_queued_messages(self):
        """
        Test ability to move messages from the head of one queue to another.
        Need to test moveing all and N messages.
        """
        self.startQmf()
        session = self.session
        "Set up source queue"
        session.queue_declare(queue="src-queue", exclusive=True, auto_delete=True)
        session.exchange_bind(queue="src-queue", exchange="amq.direct", binding_key="routing_key")
        move_part = "Move Messages "
        twenty = range(1,21)
        props = session.delivery_properties(routing_key="routing_key")
        for count in twenty:
            body = move_part + 'count'
            src_msg = Message(props, body)
            session.message_transfer(destination="amq.direct", message=src_msg)

        "Set up destination queue"
        session.queue_declare(queue="dest-queue", exclusive=True, auto_delete=True)
        session.exchange_bind(queue="dest-queue", exchange="amq.direct")

        "Get a management agent session interface"
        #qmf_sess = qmfconsole.Session()
        #broker = qmf_sess.addBroker("%s:%d" % (testrunner.host, testrunner.port))
        queues = self.qmf.getObjects(name="queue")

        "Move 10 messages from src-queue to dest-queue"
        result = self.qmf.getObjects(name="broker")[0].queueMoveMessages("src-queue", "dest-queue", 10)
        self.assertEqual (result.status, 0) 

        queues = self.qmf.getObjects(name="queue")
        for q in queues:
            if q.name == "src-queue":
               sq = q 
            elif q.name == "dest-queue":
               dq = q 

        self.assertEqual (sq.msgDepth,10)
        self.assertEqual (dq.msgDepth,10)

        "Move all remaining messages to destination"
        result = self.qmf.getObjects(name="broker")[0].queueMoveMessages("src-queue", "dest-queue", 0)
        self.assertEqual (result.status,0)

        queues = self.qmf.getObjects(name="queue")
        for q in queues:
            if q.name == "src-queue":
               sq = q 
            elif q.name == "dest-queue":
               dq = q 
        self.assertEqual (sq.msgDepth,0)
        self.assertEqual (dq.msgDepth,20)

        "Use a bad source queue name"
        result = self.qmf.getObjects(name="broker")[0].queueMoveMessages("bad-src-queue", "dest-queue", 0)
        self.assertEqual (result.status,4)

        "Use a bad destination queue name"
        result = self.qmf.getObjects(name="broker")[0].queueMoveMessages("src-queue", "bad-dest-queue", 0)
        self.assertEqual (result.status,4)

        " Use a large qty (40) to move from dest-queue back to "
        " src-queue- should move all "
        result = self.qmf.getObjects(name="broker")[0].queueMoveMessages("dest-queue", "src-queue", 40)
        self.assertEqual (result.status,0)

        queues = self.qmf.getObjects(name="queue")
        for q in queues:
            if q.name == "src-queue":
               sq = q 
            elif q.name == "dest-queue":
               dq = q 
        self.assertEqual (sq.msgDepth,20)
        self.assertEqual (dq.msgDepth,0)