d_cfg_mgr_unittests.cc 18.1 KB
Newer Older
1
// Copyright (C) 2013, 2015  Internet Systems Consortium, Inc. ("ISC")
2 3 4 5 6 7 8 9 10 11 12 13 14
//
// Permission to use, copy, modify, and/or distribute this software for any
// purpose with or without fee is hereby granted, provided that the above
// copyright notice and this permission notice appear in all copies.
//
// THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES WITH
// REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
// AND FITNESS.  IN NO EVENT SHALL ISC BE LIABLE FOR ANY SPECIAL, DIRECT,
// INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
// LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE
// OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
// PERFORMANCE OF THIS SOFTWARE.

15 16
#include <config.h>

17 18
#include <config/ccsession.h>
#include <config/module_spec.h>
19
#include <dhcpsrv/parsers/dhcp_parsers.h>
20 21 22
#include <d2/d_cfg_mgr.h>
#include <d_test_stubs.h>

23
#include <boost/foreach.hpp>
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
#include <boost/date_time/posix_time/posix_time.hpp>
#include <gtest/gtest.h>

#include <sstream>

using namespace std;
using namespace isc;
using namespace isc::config;
using namespace isc::d2;
using namespace boost::posix_time;

namespace {

/// @brief Test Class for verifying that configuration context cannot be null
/// during construction.
class DCtorTestCfgMgr : public DCfgMgrBase {
public:
    /// @brief Constructor - Note that is passes in an empty configuration
    /// pointer to the base class constructor.
    DCtorTestCfgMgr() : DCfgMgrBase(DCfgContextBasePtr()) {
    }

    /// @brief Destructor
    virtual ~DCtorTestCfgMgr() {
    }

50 51 52 53 54
    /// @brief Dummy implementation as this method is abstract.
    virtual DCfgContextBasePtr createNewContext() {
        return (DCfgContextBasePtr());
    }

55 56
    /// @brief Dummy implementation as this method is abstract.
    virtual isc::dhcp::ParserPtr
57 58
    createConfigParser(const std::string& /* element_id */,
                       const isc::data::Element::Position& /* pos */) {
59 60
        return (isc::dhcp::ParserPtr());
    }
61 62

    /// @brief Returns summary of configuration in the textual format.
63
    virtual std::string getConfigSummary(const uint32_t) {
64 65
        return ("");
    }
66 67 68
};

/// @brief Test fixture class for testing DCfgMgrBase class.
69 70 71 72 73
/// It maintains an member instance of DStubCfgMgr and derives from
/// ConfigParseTest fixture, thus providing methods for converting JSON
/// strings to configuration element sets, checking parse results, and
/// accessing the configuration context.
class DStubCfgMgrTest : public ConfigParseTest {
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
public:

    /// @brief Constructor
    DStubCfgMgrTest():cfg_mgr_(new DStubCfgMgr) {
    }

    /// @brief Destructor
    ~DStubCfgMgrTest() {
    }

    /// @brief Convenience method which returns a DStubContextPtr to the
    /// configuration context.
    ///
    /// @return returns a DStubContextPtr.
    DStubContextPtr getStubContext() {
        return (boost::dynamic_pointer_cast<DStubContext>
                (cfg_mgr_->getContext()));
    }

    /// @brief Configuration manager instance.
    DStubCfgMgrPtr cfg_mgr_;
};

///@brief Tests basic construction/destruction of configuration manager.
/// Verifies that:
/// 1. Proper construction succeeds.
/// 2. Configuration context is initialized by construction.
/// 3. Destruction works properly.
/// 4. Construction with a null context is not allowed.
TEST(DCfgMgrBase, construction) {
104
    DCfgMgrBasePtr cfg_mgr;
105 106

    // Verify that configuration manager constructions without error.
107
    ASSERT_NO_THROW(cfg_mgr.reset(new DStubCfgMgr()));
108 109 110 111 112 113

    // Verify that the context can be retrieved and is not null.
    DCfgContextBasePtr context = cfg_mgr->getContext();
    EXPECT_TRUE(context);

    // Verify that the manager can be destructed without error.
114
    EXPECT_NO_THROW(cfg_mgr.reset());
115 116 117 118 119 120 121 122 123 124 125 126 127

    // Verify that an attempt to construct a manger with a null context fails.
    ASSERT_THROW(DCtorTestCfgMgr(), DCfgMgrBaseError);
}

///@brief Tests fundamental aspects of configuration parsing.
/// Verifies that:
/// 1. A correctly formed simple configuration parses without error.
/// 2. An error building the element is handled.
/// 3. An error committing the element is handled.
/// 4. An unknown element error is handled.
TEST_F(DStubCfgMgrTest, basicParseTest) {
    // Create a simple configuration.
128
    string config = "{ \"test-value\": [] } ";
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
    ASSERT_TRUE(fromJSON(config));

    // Verify that we can parse a simple configuration.
    answer_ = cfg_mgr_->parseConfig(config_set_);
    EXPECT_TRUE(checkAnswer(0));

    // Verify that an error building the element is caught and returns a
    // failed parse result.
    SimFailure::set(SimFailure::ftElementBuild);
    answer_ = cfg_mgr_->parseConfig(config_set_);
    EXPECT_TRUE(checkAnswer(1));

    // Verify that an error committing the element is caught and returns a
    // failed parse result.
    SimFailure::set(SimFailure::ftElementCommit);
    answer_ = cfg_mgr_->parseConfig(config_set_);
    EXPECT_TRUE(checkAnswer(1));

    // Verify that an unknown element error is caught and returns a failed
    // parse result.
    SimFailure::set(SimFailure::ftElementUnknown);
    answer_ = cfg_mgr_->parseConfig(config_set_);
    EXPECT_TRUE(checkAnswer(1));
}

///@brief Tests ordered and non-ordered element parsing
/// This test verifies that:
/// 1. Non-ordered parsing parses elements in the order they are presented
/// by the configuration set (as-they-come).
/// 2. A parse order list with too few elements is detected.
/// 3. Ordered parsing parses the elements in the order specified by the
/// configuration manager's parse order list.
161
/// 4. A parse order list with too many elements is detected.
162 163 164 165 166
TEST_F(DStubCfgMgrTest, parseOrderTest) {
    // Element ids used for test.
    std::string charlie("charlie");
    std::string bravo("bravo");
    std::string alpha("alpha");
167 168 169
    std::string string_test("string_test");
    std::string uint32_test("uint32_test");
    std::string bool_test("bool_test");
170 171 172 173 174 175 176

    // Create the test configuration with the elements in "random" order.

    // NOTE that element sets produced by  isc::data::Element::fromJSON(),
    // are in lexical order by element_id. This means that iterating over
    // such an element set, will present the elements in lexical order. Should
    // this change, this test will need to be modified accordingly.
177 178 179 180 181 182 183 184 185
    string config = "{"
                    " \"string_test\": \"hoopla\", "
                    " \"bravo\": [],  "
                    " \"uint32_test\": 55, "
                    " \"alpha\": {},  "
                    " \"charlie\": [], "
                    " \"bool_test\": true "
                    "} ";

186 187 188 189 190 191 192 193
    ASSERT_TRUE(fromJSON(config));

    // Verify that non-ordered parsing, results in an as-they-come parse order.
    // Create an expected parse order.
    // (NOTE that iterating over Element sets produced by fromJSON() will
    // present the elements in lexical order.  Should this change, the expected
    // order list below would need to be changed accordingly).
    ElementIdList order_expected;
194 195 196 197 198 199 200

    // scalar params should be first and lexically
    order_expected.push_back(bool_test);
    order_expected.push_back(string_test);
    order_expected.push_back(uint32_test);

    // objects second and lexically
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
    order_expected.push_back(alpha);
    order_expected.push_back(bravo);
    order_expected.push_back(charlie);

    // Verify that the manager has an EMPTY parse order list. (Empty list
    // instructs the manager to parse them as-they-come.)
    EXPECT_EQ(0, cfg_mgr_->getParseOrder().size());

    // Parse the configuration, verify it parses without error.
    answer_ = cfg_mgr_->parseConfig(config_set_);
    EXPECT_TRUE(checkAnswer(0));

    // Verify that the parsed order matches what we expected.
    EXPECT_TRUE(cfg_mgr_->parsed_order_ ==  order_expected);

    // Clear the manager's parse order "memory".
    cfg_mgr_->parsed_order_.clear();

    // Create a parse order list that has too few entries.  Verify that
    // when parsing the test config, it fails.
    cfg_mgr_->addToParseOrder(charlie);
    // Verify the parse order list is the size we expect.
    EXPECT_EQ(1, cfg_mgr_->getParseOrder().size());

    // Verify the configuration fails.
    answer_ = cfg_mgr_->parseConfig(config_set_);
    EXPECT_TRUE(checkAnswer(1));

    // Verify that the configuration parses correctly, when the parse order
    // is correct.  Add the needed entries to the parse order
    cfg_mgr_->addToParseOrder(bravo);
    cfg_mgr_->addToParseOrder(alpha);

    // Verify the parse order list is the size we expect.
    EXPECT_EQ(3, cfg_mgr_->getParseOrder().size());

    // Clear the manager's parse order "memory".
    cfg_mgr_->parsed_order_.clear();

    // Verify the configuration parses without error.
    answer_ = cfg_mgr_->parseConfig(config_set_);
    EXPECT_TRUE(checkAnswer(0));

244 245 246 247 248 249 250 251 252 253 254 255
    // Build expected order
    // primitives should be first and lexically
    order_expected.clear();
    order_expected.push_back(bool_test);
    order_expected.push_back(string_test);
    order_expected.push_back(uint32_test);

    // objects second and by the parse order
    order_expected.push_back(charlie);
    order_expected.push_back(bravo);
    order_expected.push_back(alpha);

256
    // Verify that the parsed order is the order we configured.
257
    EXPECT_TRUE(cfg_mgr_->parsed_order_ ==  order_expected);
258 259 260 261 262 263 264 265 266 267 268

    // Create a parse order list that has too many entries.  Verify that
    // when parsing the test config, it fails.
    cfg_mgr_->addToParseOrder("delta");

    // Verify the parse order list is the size we expect.
    EXPECT_EQ(4, cfg_mgr_->getParseOrder().size());

    // Verify the configuration fails.
    answer_ = cfg_mgr_->parseConfig(config_set_);
    EXPECT_TRUE(checkAnswer(1));
269 270 271 272 273 274 275 276
}

/// @brief Tests that element ids supported by the base class as well as those
/// added by the derived class function properly.
/// This test verifies that:
/// 1. Boolean parameters can be parsed and retrieved.
/// 2. Uint32 parameters can be parsed and retrieved.
/// 3. String parameters can be parsed and retrieved.
277 278 279
/// 4. Map elements can be parsed and retrieved.
/// 5. List elements can be parsed and retrieved.
/// 6. Parsing a second configuration, updates the existing context values
280 281 282 283 284 285
/// correctly.
TEST_F(DStubCfgMgrTest, simpleTypesTest) {
    // Create a configuration with all of the parameters.
    string config = "{ \"bool_test\": true , "
                    "  \"uint32_test\": 77 , "
                    "  \"string_test\": \"hmmm chewy\" , "
286 287
                    "  \"map_test\" : {} , "
                    "  \"list_test\": [] }";
288 289 290 291 292
    ASSERT_TRUE(fromJSON(config));

    // Verify that the configuration parses without error.
    answer_ = cfg_mgr_->parseConfig(config_set_);
    ASSERT_TRUE(checkAnswer(0));
293 294
    DStubContextPtr context = getStubContext();
    ASSERT_TRUE(context);
295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313

    // Verify that the boolean parameter was parsed correctly by retrieving
    // its value from the context.
    bool actual_bool = false;
    EXPECT_NO_THROW(context->getParam("bool_test", actual_bool));
    EXPECT_EQ(true, actual_bool);

    // Verify that the uint32 parameter was parsed correctly by retrieving
    // its value from the context.
    uint32_t actual_uint32 = 0;
    EXPECT_NO_THROW(context->getParam("uint32_test", actual_uint32));
    EXPECT_EQ(77, actual_uint32);

    // Verify that the string parameter was parsed correctly by retrieving
    // its value from the context.
    std::string actual_string = "";
    EXPECT_NO_THROW(context->getParam("string_test", actual_string));
    EXPECT_EQ("hmmm chewy", actual_string);

314 315 316 317 318 319
    isc::data::ConstElementPtr object;
    EXPECT_NO_THROW(context->getObjectParam("map_test", object));
    EXPECT_TRUE(object);

    EXPECT_NO_THROW(context->getObjectParam("list_test", object));
    EXPECT_TRUE(object);
320 321 322 323 324

    // Create a configuration which "updates" all of the parameter values.
    string config2 = "{ \"bool_test\": false , "
                    "  \"uint32_test\": 88 , "
                    "  \"string_test\": \"ewww yuk!\" , "
325 326
                    "  \"map_test2\" : {} , "
                    "  \"list_test2\": [] }";
327 328 329 330 331
    ASSERT_TRUE(fromJSON(config2));

    // Verify that the configuration parses without error.
    answer_ = cfg_mgr_->parseConfig(config_set_);
    EXPECT_TRUE(checkAnswer(0));
332 333
    context = getStubContext();
    ASSERT_TRUE(context);
334 335 336 337 338

    // Verify that the boolean parameter was updated correctly by retrieving
    // its value from the context.
    actual_bool = true;
    EXPECT_NO_THROW(context->getParam("bool_test", actual_bool));
339
    EXPECT_FALSE(actual_bool);
340 341 342 343 344 345 346 347 348 349 350 351 352

    // Verify that the uint32 parameter was updated correctly by retrieving
    // its value from the context.
    actual_uint32 = 0;
    EXPECT_NO_THROW(context->getParam("uint32_test", actual_uint32));
    EXPECT_EQ(88, actual_uint32);

    // Verify that the string parameter was updated correctly by retrieving
    // its value from the context.
    actual_string = "";
    EXPECT_NO_THROW(context->getParam("string_test", actual_string));
    EXPECT_EQ("ewww yuk!", actual_string);

353 354 355 356 357 358 359 360 361 362 363 364 365
    // Verify previous objects are not there.
    EXPECT_THROW(context->getObjectParam("map_test", object),
                                         isc::dhcp::DhcpConfigError);
    EXPECT_THROW(context->getObjectParam("list_test", object),
                                         isc::dhcp::DhcpConfigError);

    // Verify new map object is there.
    EXPECT_NO_THROW(context->getObjectParam("map_test2", object));
    EXPECT_TRUE(object);

    // Verify new list object is there.
    EXPECT_NO_THROW(context->getObjectParam("list_test2", object));
    EXPECT_TRUE(object);
366 367 368 369 370 371 372 373 374 375
}

/// @brief Tests that the configuration context is preserved after failure
/// during parsing causes a rollback.
/// 1. Verifies configuration context rollback.
TEST_F(DStubCfgMgrTest, rollBackTest) {
    // Create a configuration with all of the parameters.
    string config = "{ \"bool_test\": true , "
                    "  \"uint32_test\": 77 , "
                    "  \"string_test\": \"hmmm chewy\" , "
376 377
                    "  \"map_test\" : {} , "
                    "  \"list_test\": [] }";
378 379 380 381 382
    ASSERT_TRUE(fromJSON(config));

    // Verify that the configuration parses without error.
    answer_ = cfg_mgr_->parseConfig(config_set_);
    EXPECT_TRUE(checkAnswer(0));
383 384
    DStubContextPtr context = getStubContext();
    ASSERT_TRUE(context);
385 386 387 388 389 390 391 392 393 394 395 396 397 398

    // Verify that all of parameters have the expected values.
    bool actual_bool = false;
    EXPECT_NO_THROW(context->getParam("bool_test", actual_bool));
    EXPECT_EQ(true, actual_bool);

    uint32_t actual_uint32 = 0;
    EXPECT_NO_THROW(context->getParam("uint32_test", actual_uint32));
    EXPECT_EQ(77, actual_uint32);

    std::string actual_string = "";
    EXPECT_NO_THROW(context->getParam("string_test", actual_string));
    EXPECT_EQ("hmmm chewy", actual_string);

399 400 401 402 403 404
    isc::data::ConstElementPtr object;
    EXPECT_NO_THROW(context->getObjectParam("map_test", object));
    EXPECT_TRUE(object);

    EXPECT_NO_THROW(context->getObjectParam("list_test", object));
    EXPECT_TRUE(object);
405 406 407 408 409 410

    // Create a configuration which "updates" all of the parameter values
    // plus one unknown at the end.
    string config2 = "{ \"bool_test\": false , "
                    "  \"uint32_test\": 88 , "
                    "  \"string_test\": \"ewww yuk!\" , "
411 412
                    "  \"map_test2\" : {} , "
                    "  \"list_test2\": [] , "
413 414 415 416 417 418 419 420
                    "  \"zeta_unknown\": 33 } ";
    ASSERT_TRUE(fromJSON(config2));

    // Force a failure on the last element
    SimFailure::set(SimFailure::ftElementUnknown);
    answer_ = cfg_mgr_->parseConfig(config_set_);
    EXPECT_TRUE(checkAnswer(1));
    context = getStubContext();
421
    ASSERT_TRUE(context);
422 423 424 425 426 427 428 429 430 431 432 433 434 435

    // Verify that all of parameters have the original values.
    actual_bool = false;
    EXPECT_NO_THROW(context->getParam("bool_test", actual_bool));
    EXPECT_EQ(true, actual_bool);

    actual_uint32 = 0;
    EXPECT_NO_THROW(context->getParam("uint32_test", actual_uint32));
    EXPECT_EQ(77, actual_uint32);

    actual_string = "";
    EXPECT_NO_THROW(context->getParam("string_test", actual_string));
    EXPECT_EQ("hmmm chewy", actual_string);

436 437 438 439 440
    EXPECT_NO_THROW(context->getObjectParam("map_test", object));
    EXPECT_TRUE(object);

    EXPECT_NO_THROW(context->getObjectParam("list_test", object));
    EXPECT_TRUE(object);
441 442
}

443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488
// Tests that configuration element position is returned by getParam variants.
TEST_F(DStubCfgMgrTest, paramPosition) {
    // Create a configuration with one of each scalar types.  We end them
    // with line feeds so we can test position value.
    string config = "{ \"bool_test\": true , \n"
                    "  \"uint32_test\": 77 , \n"
                    "  \"string_test\": \"hmmm chewy\" }";
    ASSERT_TRUE(fromJSON(config));

    // Verify that the configuration parses without error.
    answer_ = cfg_mgr_->parseConfig(config_set_);
    ASSERT_TRUE(checkAnswer(0));
    DStubContextPtr context = getStubContext();
    ASSERT_TRUE(context);

    // Verify that the boolean parameter was parsed correctly by retrieving
    // its value from the context.
    bool actual_bool = false;
    isc::data::Element::Position pos;
    EXPECT_NO_THROW(pos = context->getParam("bool_test", actual_bool));
    EXPECT_EQ(true, actual_bool);
    EXPECT_EQ(1, pos.line_);

    // Verify that the uint32 parameter was parsed correctly by retrieving
    // its value from the context.
    uint32_t actual_uint32 = 0;
    EXPECT_NO_THROW(pos = context->getParam("uint32_test", actual_uint32));
    EXPECT_EQ(77, actual_uint32);
    EXPECT_EQ(2, pos.line_);

    // Verify that the string parameter was parsed correctly by retrieving
    // its value from the context.
    std::string actual_string = "";
    EXPECT_NO_THROW(pos = context->getParam("string_test", actual_string));
    EXPECT_EQ("hmmm chewy", actual_string);
    EXPECT_EQ(3, pos.line_);

    // Verify that an optional parameter that is not defined, returns the
    // zero position.
    pos = isc::data::Element::ZERO_POSITION();
    EXPECT_NO_THROW(pos = context->getParam("bogus_value",
                                            actual_string, true));
    EXPECT_EQ(pos.file_, isc::data::Element::ZERO_POSITION().file_);
}


489
} // end of anonymous namespace