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
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
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
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
|
// Copyright (C) 2013-2017 Internet Systems Consortium, Inc. ("ISC")
//
// This Source Code Form is subject to the terms of the Mozilla Public
// License, v. 2.0. If a copy of the MPL was not distributed with this
// file, You can obtain one at http://mozilla.org/MPL/2.0/.
#include <config.h>
#include <cc/command_interpreter.h>
#include <process/spec_config.h>
#include <process/testutils/d_test_stubs.h>
#include <boost/date_time/posix_time/posix_time.hpp>
#include <gtest/gtest.h>
#include <sstream>
using namespace boost::posix_time;
namespace isc {
namespace process {
/// @brief Test fixture class for testing DControllerBase class. This class
/// derives from DControllerTest and wraps a DStubController. DStubController
/// has been constructed to exercise DControllerBase.
class DStubControllerTest : public DControllerTest {
public:
/// @brief Constructor.
/// Note the constructor passes in the static DStubController instance
/// method.
DStubControllerTest() : DControllerTest (DStubController::instance) {
controller_ = boost::dynamic_pointer_cast<DStubController>
(DControllerTest::
getController());
}
DStubControllerPtr controller_;
};
/// @brief Basic Controller instantiation testing.
/// Verifies that the controller singleton gets created and that the
/// basic derivation from the base class is intact.
TEST_F(DStubControllerTest, basicInstanceTesting) {
// Verify that the singleton exists and it is the correct type.
DControllerBasePtr& controller = DControllerTest::getController();
ASSERT_TRUE(controller);
ASSERT_NO_THROW(boost::dynamic_pointer_cast<DStubController>(controller));
// Verify that controller's app name is correct.
EXPECT_TRUE(checkAppName(DStubController::stub_app_name_));
// Verify that controller's bin name is correct.
EXPECT_TRUE(checkBinName(DStubController::stub_bin_name_));
// Verify that controller's spec file name is correct.
EXPECT_TRUE(checkSpecFileName(D2_SPECFILE_LOCATION));
// Verify that controller's IOService exists.
EXPECT_TRUE(checkIOService());
// Verify that the Process does NOT exist.
EXPECT_FALSE(checkProcess());
}
/// @brief Tests basic command line processing.
/// Verifies that:
/// 1. Standard command line options are supported.
/// 2. Custom command line options are supported.
/// 3. Invalid options are detected.
/// 4. Extraneous command line information is detected.
TEST_F(DStubControllerTest, commandLineArgs) {
// Verify that verbose flag is false initially.
EXPECT_TRUE(checkVerbose(false));
// Verify that standard options can be parsed without error.
char* argv[] = { const_cast<char*>("progName"),
const_cast<char*>("-c"),
const_cast<char*>("cfgName"),
const_cast<char*>("-d") };
int argc = 4;
EXPECT_NO_THROW(parseArgs(argc, argv));
// Verify that verbose is true.
EXPECT_TRUE(checkVerbose(true));
// Verify configuration file name is correct
EXPECT_TRUE(checkConfigFileName("cfgName"));
// Verify that the custom command line option is parsed without error.
char xopt[3] = "- ";
xopt[1] = *DStubController::stub_option_x_;
char* argv1[] = { const_cast<char*>("progName"), xopt};
argc = 2;
EXPECT_NO_THROW (parseArgs(argc, argv1));
// Verify that an unknown option is detected.
char* argv2[] = { const_cast<char*>("progName"),
const_cast<char*>("-bs") };
argc = 2;
EXPECT_THROW (parseArgs(argc, argv2), InvalidUsage);
// Verify that extraneous information is detected.
char* argv3[] = { const_cast<char*>("progName"),
const_cast<char*>("extra"),
const_cast<char*>("information") };
argc = 3;
EXPECT_THROW (parseArgs(argc, argv3), InvalidUsage);
}
/// @brief Tests application process creation and initialization.
/// Verifies that:
/// 1. An error during process creation is handled.
/// 2. A NULL returned by process creation is handled.
/// 3. An error during process initialization is handled.
/// 4. Process can be successfully created and initialized.
TEST_F(DStubControllerTest, initProcessTesting) {
// Verify that a failure during process creation is caught.
SimFailure::set(SimFailure::ftCreateProcessException);
EXPECT_THROW(initProcess(), DControllerBaseError);
EXPECT_FALSE(checkProcess());
// Verify that a NULL returned by process creation is handled.
SimFailure::set(SimFailure::ftCreateProcessNull);
EXPECT_THROW(initProcess(), DControllerBaseError);
EXPECT_FALSE(checkProcess());
// Re-create controller, verify that we are starting clean
resetController();
EXPECT_FALSE(checkProcess());
// Verify that an error during process initialization is handled.
SimFailure::set(SimFailure::ftProcessInit);
EXPECT_THROW(initProcess(), DProcessBaseError);
// Re-create controller, verify that we are starting clean
resetController();
EXPECT_FALSE(checkProcess());
// Verify that the application process can created and initialized.
ASSERT_NO_THROW(initProcess());
EXPECT_TRUE(checkProcess());
}
/// @brief Tests launch handling of invalid command line.
/// This test launches with an invalid command line which should throw
/// an InvalidUsage.
TEST_F(DStubControllerTest, launchInvalidUsage) {
// Command line to run integrated
char* argv[] = { const_cast<char*>("progName"),
const_cast<char*>("-z") };
int argc = 2;
// Launch the controller in integrated mode.
EXPECT_THROW(launch(argc, argv), InvalidUsage);
}
/// @brief Tests launch handling of failure in application process
/// initialization. This test launches with a valid command line but with
/// SimFailure set to fail during process creation. Launch should throw
/// ProcessInitError.
TEST_F(DStubControllerTest, launchProcessInitError) {
// Command line to run integrated
char* argv[] = { const_cast<char*>("progName"),
const_cast<char*>("-c"),
const_cast<char*>(DControllerTest::CFG_TEST_FILE),
const_cast<char*>("-d") };
int argc = 4;
// Launch the controller in stand alone mode.
SimFailure::set(SimFailure::ftCreateProcessException);
EXPECT_THROW(launch(argc, argv), ProcessInitError);
}
/// @brief Tests launch and normal shutdown (stand alone mode).
/// This creates an interval timer to generate a normal shutdown and then
/// launches with a valid, command line, with a valid configuration file
/// and no simulated errors.
TEST_F(DStubControllerTest, launchNormalShutdown) {
// Write the valid, empty, config and then run launch() for 1000 ms
time_duration elapsed_time;
ASSERT_NO_THROW(runWithConfig("{}", 2000, elapsed_time));
// Verify that duration of the run invocation is the same as the
// timer duration. This demonstrates that the shutdown was driven
// by an io_service event and callback.
EXPECT_TRUE(elapsed_time.total_milliseconds() >= 1900 &&
elapsed_time.total_milliseconds() <= 2300);
}
/// @brief Tests launch with an non-existing configuration file.
TEST_F(DStubControllerTest, nonExistingConfigFile) {
// command line to run standalone
char* argv[] = { const_cast<char*>("progName"),
const_cast<char*>("-c"),
const_cast<char*>("bogus-file"),
const_cast<char*>("-d") };
int argc = 4;
// Record start time, and invoke launch().
EXPECT_THROW(launch(argc, argv), ProcessInitError);
}
/// @brief Tests launch with configuration file argument but no file name
TEST_F(DStubControllerTest, missingConfigFileName) {
// command line to run standalone
char* argv[] = { const_cast<char*>("progName"),
const_cast<char*>("-c"),
const_cast<char*>("-d") };
int argc = 3;
// Record start time, and invoke launch().
EXPECT_THROW(launch(argc, argv), ProcessInitError);
}
/// @brief Tests launch with no configuration file argument
TEST_F(DStubControllerTest, missingConfigFileArgument) {
// command line to run standalone
char* argv[] = { const_cast<char*>("progName"),
const_cast<char*>("-d") };
int argc = 2;
// Record start time, and invoke launch().
EXPECT_THROW(launch(argc, argv), LaunchError);
}
/// @brief Tests launch with an operational error during application execution.
/// This test creates an interval timer to generate a runtime exception during
/// the process event loop. It launches wih a valid, stand-alone command line
/// and no simulated errors. Launch should throw ProcessRunError.
TEST_F(DStubControllerTest, launchRuntimeError) {
// Use an asiolink IntervalTimer and callback to generate the
// shutdown invocation. (Note IntervalTimer setup is in milliseconds).
isc::asiolink::IntervalTimer timer(*getIOService());
timer.setup(genFatalErrorCallback, 2000);
// Write the valid, empty, config and then run launch() for 5000 ms
time_duration elapsed_time;
EXPECT_THROW(runWithConfig("{}", 5000, elapsed_time), ProcessRunError);
// Verify that duration of the run invocation is the same as the
// timer duration. This demonstrates that the shutdown was driven
// by an io_service event and callback.
EXPECT_TRUE(elapsed_time.total_milliseconds() >= 1900 &&
elapsed_time.total_milliseconds() <= 2300);
}
/// @brief Configuration update event testing.
/// This really tests just the ability of the handlers to invoke the necessary
/// chain of methods and handle error conditions. Configuration parsing and
/// retrieval should be tested as part of the d2 configuration management
/// implementation.
/// This test verifies that:
/// 1. That a valid configuration update results in successful status return.
/// 2. That an application process error in configuration updating is handled
/// properly.
TEST_F(DStubControllerTest, configUpdateTests) {
int rcode = -1;
isc::data::ConstElementPtr answer;
// Initialize the application process.
ASSERT_NO_THROW(initProcess());
EXPECT_TRUE(checkProcess());
// Create a configuration set. Content is arbitrary, just needs to be
// valid JSON.
std::string config = "{ \"test-value\": 1000 } ";
isc::data::ElementPtr config_set = isc::data::Element::fromJSON(config);
// Verify that a valid config gets a successful update result.
answer = updateConfig(config_set);
isc::config::parseAnswer(rcode, answer);
EXPECT_EQ(0, rcode);
// Verify that a valid config gets a successful check result.
answer = checkConfig(config_set);
isc::config::parseAnswer(rcode, answer);
EXPECT_EQ(0, rcode);
// Verify that an error in process configure method is handled.
SimFailure::set(SimFailure::ftProcessConfigure);
answer = updateConfig(config_set);
isc::config::parseAnswer(rcode, answer);
EXPECT_EQ(1, rcode);
// Verify that an error is handled too when the config is checked for.
SimFailure::set(SimFailure::ftProcessConfigure);
answer = checkConfig(config_set);
isc::config::parseAnswer(rcode, answer);
EXPECT_EQ(1, rcode);
}
/// @brief Command execution tests.
/// This really tests just the ability of the handler to invoke the necessary
/// chain of methods and to handle error conditions.
/// This test verifies that:
/// 1. That an unrecognized command is detected and returns a status of
/// process::COMMAND_INVALID.
/// 2. Shutdown command is recognized and returns a process::COMMAND_SUCCESS
/// status.
/// 3. A valid, custom controller command is recognized a
/// process::COMMAND_SUCCESS
/// status.
/// 4. A valid, custom process command is recognized a
/// process::COMMAND_SUCCESS status.
/// 5. That a valid controller command that fails returns a
/// process::COMMAND_ERROR.
/// 6. That a valid process command that fails returns a process::COMMAND_ERROR.
TEST_F(DStubControllerTest, executeCommandTests) {
int rcode = -1;
isc::data::ConstElementPtr answer;
isc::data::ElementPtr arg_set;
// Initialize the application process.
ASSERT_NO_THROW(initProcess());
EXPECT_TRUE(checkProcess());
// Verify that an unknown command returns an process::COMMAND_INVALID
// response.
std::string bogus_command("bogus");
answer = executeCommand(bogus_command, arg_set);
isc::config::parseAnswer(rcode, answer);
EXPECT_EQ(COMMAND_INVALID, rcode);
// Verify that shutdown command returns process::COMMAND_SUCCESS response.
answer = executeCommand(SHUT_DOWN_COMMAND, arg_set);
isc::config::parseAnswer(rcode, answer);
EXPECT_EQ(COMMAND_SUCCESS, rcode);
// Verify that a valid custom controller command returns
// process::COMMAND_SUCCESS response.
answer = executeCommand(DStubController::stub_ctl_command_, arg_set);
isc::config::parseAnswer(rcode, answer);
EXPECT_EQ(COMMAND_SUCCESS, rcode);
// Verify that a valid custom process command returns
// process::COMMAND_SUCCESS response.
answer = executeCommand(DStubProcess::stub_proc_command_, arg_set);
isc::config::parseAnswer(rcode, answer);
EXPECT_EQ(COMMAND_SUCCESS, rcode);
// Verify that a valid custom controller command that fails returns
// a process::COMMAND_ERROR.
SimFailure::set(SimFailure::ftControllerCommand);
answer = executeCommand(DStubController::stub_ctl_command_, arg_set);
isc::config::parseAnswer(rcode, answer);
EXPECT_EQ(COMMAND_ERROR, rcode);
// Verify that a valid custom process command that fails returns
// a process::COMMAND_ERROR.
SimFailure::set(SimFailure::ftProcessCommand);
answer = executeCommand(DStubProcess::stub_proc_command_, arg_set);
isc::config::parseAnswer(rcode, answer);
EXPECT_EQ(COMMAND_ERROR, rcode);
}
// Tests that registered signals are caught and handled.
TEST_F(DStubControllerTest, ioSignals) {
// Tell test controller just to record the signals, don't call the
// base class signal handler.
controller_->recordSignalOnly(true);
// Setup to raise SIGHUP in 10 ms.
TimedSignal sighup(*getIOService(), SIGHUP, 10);
TimedSignal sigint(*getIOService(), SIGINT, 100);
TimedSignal sigterm(*getIOService(), SIGTERM, 200);
// Write the valid, empty, config and then run launch() for 500 ms
time_duration elapsed_time;
runWithConfig("{}", 500, elapsed_time);
// Verify that we caught the signals as expected.
std::vector<int>& signals = controller_->getProcessedSignals();
ASSERT_EQ(3, signals.size());
EXPECT_EQ(SIGHUP, signals[0]);
EXPECT_EQ(SIGINT, signals[1]);
EXPECT_EQ(SIGTERM, signals[2]);
}
// Tests that the original configuration is retained after a SIGHUP triggered
// reconfiguration fails due to invalid config content.
TEST_F(DStubControllerTest, invalidConfigReload) {
// Schedule to rewrite the configuration file after launch. This way the
// file is updated after we have done the initial configuration. The
// new content is invalid JSON which will cause the config parse to fail.
scheduleTimedWrite("{ \"string_test\": BOGUS JSON }", 100);
// Setup to raise SIGHUP in 200 ms.
TimedSignal sighup(*getIOService(), SIGHUP, 200);
// Write the config and then run launch() for 500 ms
// After startup, which will load the initial configuration this enters
// the process's runIO() loop. We will first rewrite the config file.
// Next we process the SIGHUP signal which should cause us to reconfigure.
time_duration elapsed_time;
runWithConfig("{ \"string_test\": \"first value\" }", 500, elapsed_time);
// Context is still available post launch. Check to see that our
// configuration value is still the original value.
std::string actual_value = "";
ASSERT_NO_THROW(getContext()->getParam("string_test", actual_value));
EXPECT_EQ("first value", actual_value);
// Verify that we saw the signal.
std::vector<int>& signals = controller_->getProcessedSignals();
ASSERT_EQ(1, signals.size());
EXPECT_EQ(SIGHUP, signals[0]);
}
// Tests that the original configuration is retained after a SIGHUP triggered
// reconfiguration fails due to invalid config content.
TEST_F(DStubControllerTest, alternateParsing) {
controller_->useAlternateParser(true);
// Setup to raise SIGHUP in 200 ms.
TimedSignal sighup(*getIOService(), SIGHUP, 200);
// Write the config and then run launch() for 500 ms
// After startup, which will load the initial configuration this enters
// the process's runIO() loop. We will first rewrite the config file.
// Next we process the SIGHUP signal which should cause us to reconfigure.
time_duration elapsed_time;
runWithConfig("{ \"string_test\": \"first value\" }", 500, elapsed_time);
// Context is still available post launch. Check to see that our
// configuration value is still the original value.
std::string actual_value = "";
ASSERT_NO_THROW(getContext()->getParam("string_test", actual_value));
EXPECT_EQ("alt value", actual_value);
// Verify that we saw the signal.
std::vector<int>& signals = controller_->getProcessedSignals();
ASSERT_EQ(1, signals.size());
EXPECT_EQ(SIGHUP, signals[0]);
}
// Tests that the original configuration is replaced after a SIGHUP triggered
// reconfiguration succeeds.
TEST_F(DStubControllerTest, validConfigReload) {
// Schedule to rewrite the configuration file after launch. This way the
// file is updated after we have done the initial configuration.
scheduleTimedWrite("{ \"string_test\": \"second value\" }", 100);
// Setup to raise SIGHUP in 200 ms and another at 400 ms.
TimedSignal sighup(*getIOService(), SIGHUP, 200);
TimedSignal sighup2(*getIOService(), SIGHUP, 400);
// Write the config and then run launch() for 800 ms
time_duration elapsed_time;
runWithConfig("{ \"string_test\": \"first value\" }", 800, elapsed_time);
// Context is still available post launch.
// Check to see that our configuration value is what we expect.
std::string actual_value = "";
ASSERT_NO_THROW(getContext()->getParam("string_test", actual_value));
EXPECT_EQ("second value", actual_value);
// Verify that we saw two occurrences of the signal.
std::vector<int>& signals = controller_->getProcessedSignals();
ASSERT_EQ(2, signals.size());
EXPECT_EQ(SIGHUP, signals[0]);
EXPECT_EQ(SIGHUP, signals[1]);
}
// Tests that the SIGINT triggers a normal shutdown.
TEST_F(DStubControllerTest, sigintShutdown) {
// Setup to raise SIGHUP in 1 ms.
TimedSignal sighup(*getIOService(), SIGINT, 1);
// Write the config and then run launch() for 1000 ms
time_duration elapsed_time;
runWithConfig("{ \"string_test\": \"first value\" }", 1000, elapsed_time);
// Verify that we saw the signal.
std::vector<int>& signals = controller_->getProcessedSignals();
ASSERT_EQ(1, signals.size());
EXPECT_EQ(SIGINT, signals[0]);
// Duration should be significantly less than our max run time.
EXPECT_TRUE(elapsed_time.total_milliseconds() < 300);
}
// Tests that the SIGTERM triggers a normal shutdown.
TEST_F(DStubControllerTest, sigtermShutdown) {
// Setup to raise SIGHUP in 1 ms.
TimedSignal sighup(*getIOService(), SIGTERM, 1);
// Write the config and then run launch() for 1000 ms
time_duration elapsed_time;
runWithConfig("{ \"string_test\": \"first value\" }", 1000, elapsed_time);
// Verify that we saw the signal.
std::vector<int>& signals = controller_->getProcessedSignals();
ASSERT_EQ(1, signals.size());
EXPECT_EQ(SIGTERM, signals[0]);
// Duration should be significantly less than our max run time.
EXPECT_TRUE(elapsed_time.total_milliseconds() < 300);
}
}; // end of isc::process namespace
}; // end of isc namespace
|