daemon_unittest.cc 3.31 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
// Copyright (C) 2014 Internet Systems Consortium, Inc. ("ISC")
//
// 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.

#include <config.h>
16

17
#include <exceptions/exceptions.h>
18
#include <cc/data.h>
19
#include <dhcpsrv/daemon.h>
20
#include <dhcpsrv/logging.h>
21 22
#include <log/logger_unittest_support.h>

23 24 25 26
#include <gtest/gtest.h>

using namespace isc;
using namespace isc::dhcp;
27
using namespace isc::data;
28

Tomek Mrugalski's avatar
Tomek Mrugalski committed
29 30 31 32 33 34 35 36
std::string isc::dhcp::Daemon::getVersion(bool extended) {
    if (extended) {
        return (std::string("EXTENDED"));
    } else {
        return (std::string("BASIC"));
    }
}

37 38
namespace {

39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55
/// @brief Daemon Test test fixture class
class DaemonTest : public ::testing::Test {
public:
    /// @brief Constructor
    DaemonTest() {
    }

    /// @brief Destructor
    ///
    /// As some of the tests have the side-effect of altering the logging
    /// settings (when configureLogger is called), the logging is reset to
    /// the default after each test completes.
    ~DaemonTest() {
        isc::log::resetUnitTestRootLogger();
    }
};

Tomek Mrugalski's avatar
Tomek Mrugalski committed
56

Tomek Mrugalski's avatar
Tomek Mrugalski committed
57 58
// Very simple test. Checks whether Daemon can be instantiated and its
// default parameters are sane
59
TEST_F(DaemonTest, constructor) {
Tomek Mrugalski's avatar
Tomek Mrugalski committed
60
    EXPECT_NO_THROW(Daemon instance1);
Tomek Mrugalski's avatar
Tomek Mrugalski committed
61 62

    // Check that the verbose mode is not set by default.
Tomek Mrugalski's avatar
Tomek Mrugalski committed
63 64
    Daemon instance2;
    EXPECT_FALSE(instance2.getVerbose());
65 66
}

67 68 69
// Checks that configureLogger method is behaving properly.
// More dedicated tests are availablef for LogConfigParser class.
// See logger_unittest.cc
70
TEST_F(DaemonTest, parsingConsoleOutput) {
71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91

    // Storage - parsed configuration will be stored here
    ConfigurationPtr storage(new Configuration());

    const char* config_txt =
    "{ \"loggers\": ["
    "    {"
    "        \"name\": \"kea\","
    "        \"output_options\": ["
    "            {"
    "                \"output\": \"stdout\""
    "            }"
    "        ],"
    "        \"debuglevel\": 99,"
    "        \"severity\": \"DEBUG\""
    "    }"
    "]}";
    ConstElementPtr config = Element::fromJSON(config_txt);

    // Spawn a daemon and tell it to configure logger
    Daemon x;
Tomek Mrugalski's avatar
Tomek Mrugalski committed
92
    EXPECT_NO_THROW(x.configureLogger(config, storage, false));
93 94 95 96 97 98 99 100 101 102 103 104 105 106

    // The parsed configuration should be processed by the daemon and
    // stored in configuration storage.
    ASSERT_EQ(1, storage->logging_info_.size());

    EXPECT_EQ("kea", storage->logging_info_[0].name_);
    EXPECT_EQ(99, storage->logging_info_[0].debuglevel_);
    EXPECT_EQ(isc::log::DEBUG, storage->logging_info_[0].severity_);

    ASSERT_EQ(1, storage->logging_info_[0].destinations_.size());
    EXPECT_EQ("stdout" , storage->logging_info_[0].destinations_[0].output_);
}


107
// More tests will appear here as we develop Daemon class.
108 109

};