d_test_stubs.cc 9.64 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14
// Copyright (C) 2013  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.

15
#include <d2/d2_log.h>
16 17 18 19 20 21 22 23
#include <d2/spec_config.h>
#include <d2/tests/d_test_stubs.h>

using namespace asio;

namespace isc {
namespace d2 {

24
// Initialize the static failure flag.
25 26
SimFailure::FailureType SimFailure::failure_type_ = SimFailure::ftNoFailure;

27
// Define custom process command supported by DStubProcess.
28
const char*  DStubProcess::stub_proc_command_("cool_proc_cmd");
29

30
DStubProcess::DStubProcess(const char* name, IOServicePtr io_service)
31
    : DProcessBase(name, io_service, DCfgMgrBasePtr(new DStubCfgMgr())) {
32 33 34 35 36
};

void
DStubProcess::init() {
    if (SimFailure::shouldFailOn(SimFailure::ftProcessInit)) {
37 38
        // Simulates a failure to instantiate the process.
        isc_throw(DProcessBaseError, "DStubProcess simulated init() failure");
39 40 41
    }
};

42
void
43 44 45 46
DStubProcess::run() {
    // Until shut down or an fatal error occurs, wait for and
    // execute a single callback. This is a preliminary implementation
    // that is likely to evolve as development progresses.
47
    // To use run(), the "managing" layer must issue an io_service::stop
48 49 50 51 52 53 54
    // or the call to run will continue to block, and shutdown will not
    // occur.
    IOServicePtr& io_service = getIoService();
    while (!shouldShutdown()) {
        try {
            io_service->run_one();
        } catch (const std::exception& ex) {
55 56
            isc_throw (DProcessBaseError,
                std::string("Process run method failed:") + ex.what());
57 58 59 60
        }
    }
};

61
void
62
DStubProcess::shutdown() {
63 64 65 66
    if (SimFailure::shouldFailOn(SimFailure::ftProcessShutdown)) {
        // Simulates a failure during shutdown process.
        isc_throw(DProcessBaseError, "DStubProcess simulated shutdown failure");
    }
67 68

    DProcessBase::shutdown();
69
}
70

71
isc::data::ConstElementPtr
72
DStubProcess::configure(isc::data::ConstElementPtr /*config_set*/) {
73
    if (SimFailure::shouldFailOn(SimFailure::ftProcessConfigure)) {
74
        // Simulates a process configure failure.
75 76 77 78 79 80 81
        return (isc::config::createAnswer(1,
                "Simulated process configuration error."));
    }

    return (isc::config::createAnswer(0, "Configuration accepted."));
}

82 83
isc::data::ConstElementPtr
DStubProcess::command(const std::string& command,
84
                      isc::data::ConstElementPtr /* args */) {
85 86
    isc::data::ConstElementPtr answer;
    if (SimFailure::shouldFailOn(SimFailure::ftProcessCommand)) {
87
        // Simulates a process command execution failure.
88 89
        answer = isc::config::createAnswer(COMMAND_ERROR,
                                          "SimFailure::ftProcessCommand");
90
    } else if (command.compare(stub_proc_command_) == 0) {
91 92 93 94 95 96 97 98 99 100 101 102 103 104
        answer = isc::config::createAnswer(COMMAND_SUCCESS, "Command accepted");
    } else {
        answer = isc::config::createAnswer(COMMAND_INVALID,
                                           "Unrecognized command:" + command);
    }

    return (answer);
}

DStubProcess::~DStubProcess() {
};

//************************** DStubController *************************

105
// Define custom controller command supported by DStubController.
106
const char* DStubController::stub_ctl_command_("spiffy");
107 108 109

// Define custom command line option command supported by DStubController.
const char* DStubController::stub_option_x_ = "x";
110

111 112 113 114 115 116
/// @brief Defines the app name used to construct the controller
const char* DStubController::stub_app_name_ = "TestService";

/// @brief Defines the bin name used to construct the controller
const char* DStubController::stub_bin_name_ = "TestBin";

117 118
DControllerBasePtr&
DStubController::instance() {
119
    // If the singleton hasn't been created, do it now.
120
    if (!getController()) {
121 122
        DControllerBasePtr p(new DStubController());
        setController(p);
123 124 125 126 127 128
    }

    return (getController());
}

DStubController::DStubController()
129
    : DControllerBase(stub_app_name_, stub_bin_name_) {
130 131 132

    if (getenv("B10_FROM_BUILD")) {
        setSpecFileName(std::string(getenv("B10_FROM_BUILD")) +
133
            "/src/bin/d2/dhcp-ddns.spec");
134 135 136 137 138 139 140 141
    } else {
        setSpecFileName(D2_SPECFILE_LOCATION);
    }
}

bool
DStubController::customOption(int option, char* /* optarg */)
{
142
    // Check for the custom option supported by DStubController.
143
    if (static_cast<char>(option) == *stub_option_x_) {
144
        return (true);
145
    }
146

147 148 149 150 151
    return (false);
}

DProcessBase* DStubController::createProcess() {
    if (SimFailure::shouldFailOn(SimFailure::ftCreateProcessException)) {
152
        // Simulates a failure to instantiate the process due to exception.
153 154 155 156
        throw std::runtime_error("SimFailure::ftCreateProcess");
    }

    if (SimFailure::shouldFailOn(SimFailure::ftCreateProcessNull)) {
157
        // Simulates a failure to instantiate the process.
158 159 160 161
        return (NULL);
    }

    // This should be a successful instantiation.
162
    return (new DStubProcess(getAppName().c_str(), getIOService()));
163 164 165 166 167 168 169
}

isc::data::ConstElementPtr
DStubController::customControllerCommand(const std::string& command,
                                     isc::data::ConstElementPtr /* args */) {
    isc::data::ConstElementPtr answer;
    if (SimFailure::shouldFailOn(SimFailure::ftControllerCommand)) {
170
        // Simulates command failing to execute.
171 172
        answer = isc::config::createAnswer(COMMAND_ERROR,
                                          "SimFailure::ftControllerCommand");
173
    } else if (command.compare(stub_ctl_command_) == 0) {
174 175 176 177 178 179 180 181 182
        answer = isc::config::createAnswer(COMMAND_SUCCESS, "Command accepted");
    } else {
        answer = isc::config::createAnswer(COMMAND_INVALID,
                                           "Unrecognized command:" + command);
    }

    return (answer);
}

183
const std::string DStubController::getCustomOpts() const {
184 185
    // Return the "list" of custom options supported by DStubController.
    return (std::string(stub_option_x_));
186 187 188 189 190
}

DStubController::~DStubController() {
}

191
// Initialize controller wrapper's static instance getter member.
192 193
DControllerTest::InstanceGetter DControllerTest::instanceGetter_ = NULL;

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
//************************** TestParser *************************

TestParser::TestParser(const std::string& param_name):param_name_(param_name) {
}

TestParser::~TestParser(){
}

void
TestParser::build(isc::data::ConstElementPtr new_config) {
    if (SimFailure::shouldFailOn(SimFailure::ftElementBuild)) {
        // Simulates an error during element data parsing.
        isc_throw (DCfgMgrBaseError, "Simulated build exception");
    }

    value_ = new_config;
}

void
TestParser::commit() {
    if (SimFailure::shouldFailOn(SimFailure::ftElementCommit)) {
        // Simulates an error while committing the parsed element data.
        throw std::runtime_error("Simulated commit exception");
    }
}

//************************** DStubContext *************************

DStubContext::DStubContext(): extra_values_(new isc::dhcp::Uint32Storage()) {
}

DStubContext::~DStubContext() {
}

void
DStubContext::getExtraParam(const std::string& name, uint32_t& value) {
    value = extra_values_->getParam(name);
}

isc::dhcp::Uint32StoragePtr
DStubContext::getExtraStorage() {
    return (extra_values_);
}

DStubContext*
DStubContext::clone() {
    return (new DStubContext(*this));
}

DStubContext::DStubContext(const DStubContext& rhs): DCfgContextBase(rhs),
    extra_values_(new isc::dhcp::Uint32Storage(*(rhs.extra_values_))) {
}

//************************** DStubCfgMgr *************************

DStubCfgMgr::DStubCfgMgr()
    : DCfgMgrBase(DCfgContextBasePtr(new DStubContext())) {
}

DStubCfgMgr::~DStubCfgMgr() {
}

isc::dhcp::ParserPtr
DStubCfgMgr::createConfigParser(const std::string& element_id) {
    isc::dhcp::DhcpConfigParser* parser = NULL;
    DStubContextPtr context =
                    boost::dynamic_pointer_cast<DStubContext>(getContext());

    if (element_id == "bool_test") {
        parser = new isc::dhcp::BooleanParser(element_id,
                                              context->getBooleanStorage());
    } else if (element_id == "uint32_test") {
        parser = new isc::dhcp::Uint32Parser(element_id,
                                             context->getUint32Storage());
    } else if (element_id == "string_test") {
        parser = new isc::dhcp::StringParser(element_id,
                                             context->getStringStorage());
    } else if (element_id == "extra_test") {
        parser = new isc::dhcp::Uint32Parser(element_id,
                                             context->getExtraStorage());
    } else {
        // Fail only if SimFailure dictates we should.  This makes it easier
        // to test parse ordering, by permitting a wide range of element ids
        // to "succeed" without specifically supporting them.
        if (SimFailure::shouldFailOn(SimFailure::ftElementUnknown)) {
            isc_throw(DCfgMgrBaseError, "Configuration parameter not supported"
                      << element_id);
        }

        parsed_order_.push_back(element_id);
        parser = new TestParser(element_id);
    }

    return (isc::dhcp::ParserPtr(parser));
}


291
}; // namespace isc::d2
292
}; // namespace isc