summaryrefslogtreecommitdiff
path: root/cpp/src/tests/policies.py
blob: ec0191f91e76ed1668df8ad243fb8be573defe08 (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
205
206
207
208
209
#
# 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.tests.messaging.implementation import *
from qpid.tests.messaging import VersionTest

class Mgmt:
    """
    Simple QMF management utility (qpidtoollibs uses
    qpid.messaging.Message rather than swigged version)
    """
    def __init__(self, conn):
        self.conn = conn
        self.sess = self.conn.session()
        self.reply_to = "qmf.default.topic/direct.%s;{node:{type:topic}, link:{x-declare:{auto-delete:True,exclusive:True}}}" % \
            str(uuid4())
        self.reply_rx = self.sess.receiver(self.reply_to)
        self.reply_rx.capacity = 10
        self.tx = self.sess.sender("qmf.default.direct/broker")
        self.next_correlator = 1

    def list(self, class_name):
        props = {'method'             : 'request',
                 'qmf.opcode'         : '_query_request',
                 'x-amqp-0-10.app-id' : 'qmf2'}
        correlator = str(self.next_correlator)
        self.next_correlator += 1

        content = {'_what'      : 'OBJECT',
                 '_schema_id' : {'_class_name' : class_name.lower()}}

        message = Message(content, reply_to=self.reply_to, correlation_id=correlator,
                          properties=props, subject="broker")
        self.tx.send(message)


        response = self.reply_rx.fetch(10)
        if response.properties['qmf.opcode'] != '_query_response':
            raise Exception("bad response")
        items = []
        done = False
        while not done:
            for item in response.content:
                items.append(item['_values'])
            if 'partial' in response.properties:
                response = self.reply_rx.fetch(10)
            else:
                done = True
            self.sess.acknowledge()
        return items

    def do_qmf_method(self, method, arguments, addr="org.apache.qpid.broker:broker:amqp-broker", timeout=10):
        props = {'method'             : 'request',
                 'qmf.opcode'         : '_method_request',
                 'x-amqp-0-10.app-id' : 'qmf2'}
        correlator = str(self.next_correlator)
        self.next_correlator += 1

        content = {'_object_id'   : {'_object_name' : addr},
                   '_method_name' : method,
                   '_arguments'   : arguments}

        message = Message(content, reply_to=self.reply_to, correlation_id=correlator,
                          properties=props, subject="broker")
        self.tx.send(message)
        response = self.reply_rx.fetch(timeout)
        self.sess.acknowledge()
        if response.properties['qmf.opcode'] == '_exception':
          raise Exception("Exception from Agent: %r" % response.content['_values'])
        if response.properties['qmf.opcode'] != '_method_response':
          raise Exception("bad response: %r" % response.properties)
        return response.content['_arguments']

    def create(self, _type, name, properties={}):
        return self.do_qmf_method('create', {'type': _type, 'name': name, 'properties': properties})

    def delete(self, _type, name):
        return self.do_qmf_method('delete', {'type': _type, 'name': name})


class PoliciesTests (VersionTest):
    """
    Tests for node policies with qpidd
    """

    def do_simple_queue_test(self, pattern, name, properties={}, autodeleted=True):
        mgmt = self.create_connection("amqp0-10", True)
        agent = Mgmt(mgmt)
        agent.create('QueuePolicy', pattern, properties)
        try:
            snd = self.ssn.sender(name)
            msgs = [Message(content=s, subject = s) for s in ['a','b','c','d']]
            for m in msgs: snd.send(m)
            snd.close()

            for expected in msgs:
                rcv = self.ssn.receiver(name)
                msg = rcv.fetch(0)
                assert msg.content == expected.content, (msg.content, expected.content)
                self.ssn.acknowledge()
                rcv.close() #close after each message to ensure queue isn't deleted with messages in it
            self.ssn.close()
            self.conn.close()

            matched = [q for q in agent.list("Queue") if q['name'] == name]
            if autodeleted:
                # ensure that queue is no longer there (as empty and unused)
                assert len(matched) == 0, (matched)
            else:
                # ensure that queue is still there though empty and unused
                assert len(matched) == 1, (matched)
        finally:
            agent.delete('QueuePolicy', pattern)
            mgmt.close()

    def test_queue(self):
        self.do_simple_queue_test("queue-*", "queue-1")

    def test_queue_not_autodeleted(self):
        self.do_simple_queue_test("permanent-queue-*", "permanent-queue-1", {'auto-delete':False}, False)

    def test_queue_manual_delete(self):
        self.do_simple_queue_test("permanent-queue-*", "permanent-queue-1", {'qpid.lifetime-policy':'manual'}, False)

    def test_queue_delete_if_unused_and_empty(self):
        self.do_simple_queue_test("queue-*", "queue-1", {'qpid.lifetime-policy':'delete-if-unused-and-empty'}, True)

    def do_simple_topic_test(self, pattern, name, properties={}, autodeleted=True):
        mgmt = self.create_connection("amqp0-10", True)
        agent = Mgmt(mgmt)
        agent.create('TopicPolicy', pattern, properties)
        try:
            snd = self.ssn.sender(name)
            rcv1 = self.ssn.receiver(name)
            rcv2 = self.ssn.receiver(name)

            msgs = [Message(content=s, subject = s) for s in ['a','b','c','d']]
            for m in msgs: snd.send(m)

            for rcv in [rcv1, rcv2]:
                for expected in msgs:
                    msg = rcv.fetch(0)
                    assert msg.content == expected.content, (msg.content, expected.content)
            self.ssn.acknowledge()
            rcv1.close()
            rcv2.close()
            snd.close()

            matched = [e for e in agent.list("Exchange") if e['name'] == name]
            if autodeleted:
                # ensure that exchange is no longer there (as it is now unused)
                assert len(matched) == 0, (matched)
            else:
                # ensure that exchange has not been autodeleted in spite of being unused
                assert len(matched) == 1, (matched)
        finally:
            agent.delete('TopicPolicy', pattern)
            mgmt.close()

    def test_topic(self):
        self.do_simple_topic_test('fanout-*', 'fanout-1', {'exchange-type':'fanout'})

    def test_topic_not_autodelete(self):
        self.do_simple_topic_test('permanent-fanout-*', 'permanent-fanout-1', {'exchange-type':'fanout', 'auto-delete':False}, False)

    def test_topic_manual_delete(self):
        self.do_simple_topic_test('permanent-fanout-*', 'permanent-fanout-1', {'exchange-type':'fanout', 'qpid.lifetime-policy':'manual'}, False)

    def test_topic_delete_if_unused(self):
        self.do_simple_topic_test('fanout-*', 'fanout-1', {'exchange-type':'fanout', 'qpid.lifetime-policy':'delete-if-unused'}, True)

    def test_mgmt(self):
        mgmt = self.create_connection("amqp0-10", True)
        agent = Mgmt(mgmt)
        agent.create('QueuePolicy', 'queue-*')
        agent.create('QueuePolicy', 'alt.queue.*')
        agent.create('TopicPolicy', 'topic-*')
        try:
            queues = [q['name'] for q in agent.list("QueuePolicy")]
            topics = [t['name'] for t in agent.list("TopicPolicy")]
            assert 'queue-*' in queues, (queues)
            assert 'alt.queue.*' in queues, (queues)

            try:
                agent.delete('TopicPolicy', 'queue-*')
                assert False, ('Deletion of policy using wrong type should fail')
            except: None

        finally:
            agent.delete('QueuePolicy', 'queue-*')
            agent.delete('QueuePolicy', 'alt.queue.*')
            agent.delete('TopicPolicy', 'topic-*')
            mgmt.close()