ctrl_agent_controller_unittests.cc 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  1. // Copyright (C) 2016 Internet Systems Consortium, Inc. ("ISC")
  2. //
  3. // This Source Code Form is subject to the terms of the Mozilla Public
  4. // License, v. 2.0. If a copy of the MPL was not distributed with this
  5. // file, You can obtain one at http://mozilla.org/MPL/2.0/.
  6. #include <config.h>
  7. #include <agent/ctrl_agent_controller.h>
  8. #include <agent/ctrl_agent_process.h>
  9. #include <process/testutils/d_test_stubs.h>
  10. using namespace isc::agent;
  11. using namespace isc::process;
  12. namespace {
  13. /// @brief Valid Control Agent Config used in tests.
  14. /// @todo Use actual config once configuration parsing is implemented.
  15. const char* valid_agent_config = "{ }";
  16. /// @brief test fixture class for testing CtrlAgentController class. This
  17. /// class derives from DControllerTest and wraps CtrlAgentController. Much
  18. /// of the underlying functionality is in the DControllerBase class which
  19. /// has extensive set of unit tests that are independent from the Control
  20. /// Agent.
  21. class CtrlAgentControllerTest : public DControllerTest {
  22. public:
  23. /// @brief Constructor.
  24. CtrlAgentControllerTest()
  25. : DControllerTest(CtrlAgentController::instance) {
  26. }
  27. };
  28. // Basic Controller instantiation testing.
  29. // Verifies that the controller singleton gets created and that the
  30. // basic derivation from the base class is intact.
  31. TEST_F(CtrlAgentControllerTest, basicInstanceTesting) {
  32. // Verify the we can the singleton instance can be fetched and that
  33. // it is the correct type.
  34. DControllerBasePtr& controller = DControllerTest::getController();
  35. ASSERT_TRUE(controller);
  36. ASSERT_NO_THROW(boost::dynamic_pointer_cast<CtrlAgentController>(controller));
  37. // Verify that controller's app name is correct.
  38. EXPECT_TRUE(checkAppName(CtrlAgentController::agent_app_name_));
  39. // Verify that controller's bin name is correct.
  40. EXPECT_TRUE(checkBinName(CtrlAgentController::agent_bin_name_));
  41. // Verify that controller's IOService exists.
  42. EXPECT_TRUE(checkIOService());
  43. // Verify that the Process does NOT exist.
  44. EXPECT_FALSE(checkProcess());
  45. }
  46. // Tests basic command line processing.
  47. // Verifies that:
  48. // 1. Standard command line options are supported.
  49. // 2. Invalid options are detected.
  50. TEST_F(CtrlAgentControllerTest, commandLineArgs) {
  51. char* argv[] = { const_cast<char*>("progName"),
  52. const_cast<char*>("-c"),
  53. const_cast<char*>(DControllerTest::CFG_TEST_FILE),
  54. const_cast<char*>("-d") };
  55. int argc = 4;
  56. // Verify that verbose flag is false initially.
  57. EXPECT_TRUE(checkVerbose(false));
  58. // Verify that standard options can be parsed without error.
  59. EXPECT_NO_THROW(parseArgs(argc, argv));
  60. // Verify that verbose flag is true.
  61. EXPECT_TRUE(checkVerbose(true));
  62. // Verify configuration file name is correct.
  63. EXPECT_TRUE(checkConfigFileName(DControllerTest::CFG_TEST_FILE));
  64. // Verify that an unknown option is detected.
  65. char* argv2[] = { const_cast<char*>("progName"),
  66. const_cast<char*>("-x") };
  67. argc = 2;
  68. EXPECT_THROW(parseArgs(argc, argv2), InvalidUsage);
  69. }
  70. // Tests application process creation and initialization.
  71. // Verifies that the process can be successfully created and initialized.
  72. TEST_F(CtrlAgentControllerTest, initProcessTesting) {
  73. ASSERT_NO_THROW(initProcess());
  74. EXPECT_TRUE(checkProcess());
  75. }
  76. // Tests launch and normal shutdown (stand alone mode).
  77. // This creates an interval timer to generate a normal shutdown and then
  78. // launches with a valid, stand-alone command line and no simulated errors.
  79. TEST_F(CtrlAgentControllerTest, launchNormalShutdown) {
  80. // Write valid_agent_config and then run launch() for 1000 ms.
  81. time_duration elapsed_time;
  82. runWithConfig(valid_agent_config, 1000, elapsed_time);
  83. // Give a generous margin to accomodate slower test environs.
  84. EXPECT_TRUE(elapsed_time.total_milliseconds() >= 800 &&
  85. elapsed_time.total_milliseconds() <= 1300);
  86. }
  87. // Tests that the SIGINT triggers a normal shutdown.
  88. TEST_F(CtrlAgentControllerTest, sigintShutdown) {
  89. // Setup to raise SIGHUP in 1 ms.
  90. TimedSignal sighup(*getIOService(), SIGINT, 1);
  91. // Write valid_agent_config and then run launch() for a maximum
  92. // of 1000 ms.
  93. time_duration elapsed_time;
  94. runWithConfig(valid_agent_config, 1000, elapsed_time);
  95. // Signaled shutdown should make our elapsed time much smaller than
  96. // the maximum run time. Give generous margin to accomodate slow
  97. // test environs.
  98. EXPECT_TRUE(elapsed_time.total_milliseconds() < 300);
  99. }
  100. // Tests that the SIGTERM triggers a normal shutdown.
  101. TEST_F(CtrlAgentControllerTest, sigtermShutdown) {
  102. // Setup to raise SIGHUP in 1 ms.
  103. TimedSignal sighup(*getIOService(), SIGTERM, 1);
  104. // Write valid_agent_config and then run launch() for a maximum of 1 s.
  105. time_duration elapsed_time;
  106. runWithConfig(valid_agent_config, 1000, elapsed_time);
  107. // Signaled shutdown should make our elapsed time much smaller than
  108. // the maximum run time. Give generous margin to accomodate slow
  109. // test environs.
  110. EXPECT_TRUE(elapsed_time.total_milliseconds() < 300);
  111. }
  112. }