summaryrefslogtreecommitdiff
path: root/cpp/src/tests/BrokerMgmtAgent.cpp
blob: bad7e768a6b8e399fab3e79a7d12281a47bc3cec (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
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
/*
 *
 * 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.
 *
 */

#include "unit_test.h"
#include "MessagingFixture.h"
#include "qpid/management/Buffer.h"
#include "qpid/management/ManagementAgent.h"
#include "qpid/messaging/Message.h"
#include "qpid/amqp_0_10/Codecs.h"
#include "qpid/log/Logger.h"
#include "qpid/log/Options.h"

#include "qmf/org/apache/qpid/broker/mgmt/test/TestObject.h"

#include <iomanip>


using           qpid::management::Mutex;
using           qpid::management::Manageable;
using           qpid::management::Buffer;
using namespace qpid::messaging;
using namespace qpid::types;



namespace qpid {
namespace tests {

namespace _qmf = qmf::org::apache::qpid::broker::mgmt::test;
namespace {

typedef boost::shared_ptr<_qmf::TestObject> TestObjectPtr;
typedef std::vector<TestObjectPtr> TestObjectVector;

// Instantiates a broker and its internal management agent.  Provides
// factories for constructing Receivers for object indication messages.
//
class AgentFixture
{
    MessagingFixture *mFix;

  public:
    AgentFixture( unsigned int pubInterval=10,
                  bool qmfV2=false,
                  qpid::broker::Broker::Options opts = qpid::broker::Broker::Options())
    {
        opts.enableMgmt=true;
        opts.qmf1Support=!qmfV2;
        opts.qmf2Support=qmfV2;
        opts.mgmtPubInterval=pubInterval;
        mFix = new MessagingFixture(opts, true);

        _qmf::TestObject::registerSelf(getBrokerAgent());
    };
    ~AgentFixture()
    {
        delete mFix;
    };
    ::qpid::management::ManagementAgent *getBrokerAgent() { return mFix->broker->getManagementAgent(); }
    Receiver createV1DataIndRcvr( const std::string package, const std::string klass )
    {
        return mFix->session.createReceiver(std::string("kqueue; {create: always, delete: always, "
                                                        "node: {type: queue, "
                                                        "x-bindings: [{exchange: qpid.management, "
                                                        "key: 'console.obj.1.0.")
                                            + package + std::string(".") + klass
                                            + std::string("'}]}}"));
    };
    Receiver createV2DataIndRcvr( const std::string package, const std::string klass )
    {
        std::string p(package);
        std::replace(p.begin(), p.end(), '.', '_');
        std::string k(klass);
        std::replace(k.begin(), k.end(), '.', '_');

        return mFix->session.createReceiver(std::string("kqueue; {create: always, delete: always, "
                                                        "node: {type: queue, "
                                                        "x-bindings: [{exchange: qmf.default.topic, "
                                                        "key: 'agent.ind.data.")
                                            + p + std::string(".") + k
                                            + std::string("'}]}}"));
    };
};


// A "management object" that supports the TestObject
//
class TestManageable : public qpid::management::Manageable
{
    management::ManagementObject::shared_ptr mgmtObj;
    const std::string key;
  public:
    TestManageable(management::ManagementAgent *agent, std::string _key)
        : key(_key)
    {
        _qmf::TestObject::shared_ptr tmp(new _qmf::TestObject(agent, this));

        // seed it with some default values...
        tmp->set_string1(key);
        tmp->set_bool1(true);
        qpid::types::Variant::Map vMap;
        vMap["one"] = qpid::types::Variant(1);
        vMap["two"] = qpid::types::Variant("two");
        vMap["three"] = qpid::types::Variant("whatever");
        tmp->set_map1(vMap);

        mgmtObj = tmp;
    };
    ~TestManageable() { mgmtObj.reset(); }
    management::ManagementObject::shared_ptr GetManagementObject() const { return mgmtObj; };
    static void validateTestObjectProperties(_qmf::TestObject& to)
    {
        // verify the default values are as expected.  We don't check 'string1',
        // as it is the object key, and is unique for each object (no default value).
        BOOST_CHECK(to.get_bool1() == true);
        BOOST_CHECK(to.get_map1().size() == 3);
        qpid::types::Variant::Map mappy = to.get_map1();
        BOOST_CHECK(1 == (unsigned int)mappy["one"]);
        BOOST_CHECK(mappy["two"].asString() == std::string("two"));
        BOOST_CHECK(mappy["three"].asString() == std::string("whatever"));
    };
};


// decode a V1 Content Indication message
//
void decodeV1ObjectUpdates(const Message& inMsg, TestObjectVector& objs, const size_t objLen)
{
    const size_t MAX_BUFFER_SIZE=65536;
    char tmp[MAX_BUFFER_SIZE];

    objs.clear();

    BOOST_CHECK(inMsg.getContent().size() <= MAX_BUFFER_SIZE);

    ::memcpy(tmp, inMsg.getContent().data(), inMsg.getContent().size());
    Buffer buf(tmp, inMsg.getContent().size());

    while (buf.available() > 8) {     // 8 == qmf v1 header size
        BOOST_CHECK_EQUAL(buf.getOctet(), 'A');
        BOOST_CHECK_EQUAL(buf.getOctet(), 'M');
        BOOST_CHECK_EQUAL(buf.getOctet(), '2');
        BOOST_CHECK_EQUAL(buf.getOctet(), 'c'); // opcode == content indication
        // @@todo: kag: how do we skip 'i' entries???
        buf.getLong();  // ignore sequence

        std::string str1;                   // decode content body as string
        buf.getRawData(str1, objLen);

        TestObjectPtr fake(new _qmf::TestObject(0,0));
        fake->readProperties( str1 );
        objs.push_back(fake);
    }
}


// decode a V2 Content Indication message
//
void decodeV2ObjectUpdates(const qpid::messaging::Message& inMsg, TestObjectVector& objs)
{
    objs.clear();

    BOOST_CHECK_EQUAL(inMsg.getContentType(), std::string("amqp/list"));

    const ::qpid::types::Variant::Map& m = inMsg.getProperties();
    Variant::Map::const_iterator iter = m.find(std::string("qmf.opcode"));
    BOOST_CHECK(iter != m.end());
    BOOST_CHECK_EQUAL(iter->second.asString(), std::string("_data_indication"));

    Variant::List vList;
    ::qpid::amqp_0_10::ListCodec::decode(inMsg.getContent(), vList);

    for (Variant::List::iterator lIter = vList.begin(); lIter != vList.end(); lIter++) {
        TestObjectPtr fake(new _qmf::TestObject(0,0));
        fake->readTimestamps(lIter->asMap());
        fake->mapDecodeValues((lIter->asMap())["_values"].asMap());
        objs.push_back(fake);
    }
}
}

QPID_AUTO_TEST_SUITE(BrokerMgmtAgent)

// verify that an object that is added to the broker's management database is
// published correctly.  Furthermore, verify that it is published once after
// it has been deleted.
//
QPID_AUTO_TEST_CASE(v1ObjPublish)
{
    AgentFixture* fix = new AgentFixture(3);
    management::ManagementAgent* agent;
    agent = fix->getBrokerAgent();

    // create a manageable test object
    TestManageable *tm = new TestManageable(agent, std::string("obj1"));
    uint32_t objLen = tm->GetManagementObject()->writePropertiesSize();

    Receiver r1 = fix->createV1DataIndRcvr("org.apache.qpid.broker.mgmt.test", "#");

    agent->addObject(tm->GetManagementObject(), 1);

    // wait for the object to be published
    Message m1;
    BOOST_CHECK(r1.fetch(m1, Duration::SECOND * 6));

    TestObjectVector objs;
    decodeV1ObjectUpdates(m1, objs, objLen);
    BOOST_CHECK(objs.size() > 0);

    for (TestObjectVector::iterator oIter = objs.begin(); oIter != objs.end(); oIter++) {

        TestManageable::validateTestObjectProperties(**oIter);

        qpid::types::Variant::Map mappy;
        (*oIter)->writeTimestamps(mappy);
        BOOST_CHECK(0 == mappy["_delete_ts"].asUint64());   // not deleted
    }

    // destroy the object

    tm->GetManagementObject()->resourceDestroy();

    // wait for the deleted object to be published

    bool isDeleted = false;
    while (!isDeleted && r1.fetch(m1, Duration::SECOND * 6)) {

        decodeV1ObjectUpdates(m1, objs, objLen);
        BOOST_CHECK(objs.size() > 0);

        for (TestObjectVector::iterator oIter = objs.begin(); oIter != objs.end(); oIter++) {

            TestManageable::validateTestObjectProperties(**oIter);

            qpid::types::Variant::Map mappy;
            (*oIter)->writeTimestamps(mappy);
            if (mappy["_delete_ts"].asUint64() != 0)
                isDeleted = true;
        }
    }

    BOOST_CHECK(isDeleted);

    r1.close();
    delete fix;
    delete tm;
}

// Repeat the previous test, but with V2-based object support
//
QPID_AUTO_TEST_CASE(v2ObjPublish)
{
    AgentFixture* fix = new AgentFixture(3, true);
    management::ManagementAgent* agent;
    agent = fix->getBrokerAgent();

    TestManageable *tm = new TestManageable(agent, std::string("obj2"));

    Receiver r1 = fix->createV2DataIndRcvr(tm->GetManagementObject()->getPackageName(), "#");

    agent->addObject(tm->GetManagementObject(), "testobj-1");

    // wait for the object to be published
    Message m1;
    BOOST_CHECK(r1.fetch(m1, Duration::SECOND * 6));

    TestObjectVector objs;
    decodeV2ObjectUpdates(m1, objs);
    BOOST_CHECK(objs.size() > 0);

    for (TestObjectVector::iterator oIter = objs.begin(); oIter != objs.end(); oIter++) {

        TestManageable::validateTestObjectProperties(**oIter);

        qpid::types::Variant::Map mappy;
        (*oIter)->writeTimestamps(mappy);
        BOOST_CHECK(0 == mappy["_delete_ts"].asUint64());
    }

    // destroy the object

    tm->GetManagementObject()->resourceDestroy();

    // wait for the deleted object to be published

    bool isDeleted = false;
    while (!isDeleted && r1.fetch(m1, Duration::SECOND * 6)) {

        decodeV2ObjectUpdates(m1, objs);
        BOOST_CHECK(objs.size() > 0);

        for (TestObjectVector::iterator oIter = objs.begin(); oIter != objs.end(); oIter++) {

            TestManageable::validateTestObjectProperties(**oIter);

            qpid::types::Variant::Map mappy;
            (*oIter)->writeTimestamps(mappy);
            if (mappy["_delete_ts"].asUint64() != 0)
                isDeleted = true;
        }
    }

    BOOST_CHECK(isDeleted);

    r1.close();
    delete fix;
    delete tm;
}

// See QPID-2997
QPID_AUTO_TEST_CASE(v2RapidRestoreObj)
{
    AgentFixture* fix = new AgentFixture(3, true);
    management::ManagementAgent* agent;
    agent = fix->getBrokerAgent();

    // two objects, same ObjID
    TestManageable *tm1 = new TestManageable(agent, std::string("obj2"));
    TestManageable *tm2 = new TestManageable(agent, std::string("obj2"));

    Receiver r1 = fix->createV2DataIndRcvr(tm1->GetManagementObject()->getPackageName(), "#");

    // add, then immediately delete and re-add a copy of the object
    agent->addObject(tm1->GetManagementObject(), "testobj-1");
    tm1->GetManagementObject()->resourceDestroy();
    agent->addObject(tm2->GetManagementObject(), "testobj-1");

    // expect: a delete notification, then an update notification
    TestObjectVector objs;
    bool isDeleted = false;
    bool isAdvertised = false;
    size_t count = 0;
    Message m1;
    while (r1.fetch(m1, Duration::SECOND * 6)) {

        decodeV2ObjectUpdates(m1, objs);
        BOOST_CHECK(objs.size() > 0);

        for (TestObjectVector::iterator oIter = objs.begin(); oIter != objs.end(); oIter++) {
            count++;
            TestManageable::validateTestObjectProperties(**oIter);

            qpid::types::Variant::Map mappy;
            (*oIter)->writeTimestamps(mappy);
            if (mappy["_delete_ts"].asUint64() != 0) {
                isDeleted = true;
                BOOST_CHECK(isAdvertised == false);  // delete must be first
            } else {
                isAdvertised = true;
                BOOST_CHECK(isDeleted == true);     // delete must be first
            }
        }
    }

    BOOST_CHECK(isDeleted);
    BOOST_CHECK(isAdvertised);
    BOOST_CHECK(count == 2);

    r1.close();
    delete fix;
    delete tm1;
    delete tm2;
}

QPID_AUTO_TEST_SUITE_END()
}
}