bundy_d2_controller_unittests.cc 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191
  1. // Copyright (C) 2014 Internet Systems Consortium, Inc. ("ISC")
  2. //
  3. // Permission to use, copy, modify, and/or distribute this software for any
  4. // purpose with or without fee is hereby granted, provided that the above
  5. // copyright notice and this permission notice appear in all copies.
  6. //
  7. // THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES WITH
  8. // REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
  9. // AND FITNESS. IN NO EVENT SHALL ISC BE LIABLE FOR ANY SPECIAL, DIRECT,
  10. // INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
  11. // LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE
  12. // OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
  13. // PERFORMANCE OF THIS SOFTWARE.
  14. #include <config/ccsession.h>
  15. #include <d_test_stubs.h>
  16. #include <d2/bundy_d2_controller.h>
  17. #include <d2/spec_config.h>
  18. #include <boost/pointer_cast.hpp>
  19. #include <boost/date_time/posix_time/posix_time.hpp>
  20. #include <gtest/gtest.h>
  21. #include <config.h>
  22. #include <sstream>
  23. using namespace boost::posix_time;
  24. namespace isc {
  25. namespace d2 {
  26. /// @brief Test fixture class for testing D2Controller class. This class
  27. /// derives from DControllerTest and wraps a D2Controller. Much of the
  28. /// underlying functionality is in the DControllerBase class which has an
  29. /// extensive set of unit tests that are independent of DHCP-DDNS.
  30. /// @TODO Currently These tests are relatively light and duplicate some of
  31. /// the testing done on the base class. These tests are sufficient to ensure
  32. /// that D2Controller properly derives from its base class and to test the
  33. /// logic that is unique to D2Controller. These tests will be augmented and
  34. /// or new tests added as additional functionality evolves.
  35. /// Unlike the stub testing, there is no use of SimFailure to induce error
  36. /// conditions as this is production code.
  37. class BundyD2ControllerTest : public DControllerTest {
  38. public:
  39. /// @brief Constructor
  40. /// Note the constructor passes in the static D2Controller instance
  41. /// method.
  42. BundyD2ControllerTest() : DControllerTest(D2Controller::instance) {
  43. }
  44. /// @brief Destructor
  45. ~BundyD2ControllerTest() {
  46. }
  47. };
  48. /// @brief Basic Controller instantiation testing.
  49. /// Verifies that the controller singleton gets created and that the
  50. /// basic derivation from the base class is intact.
  51. TEST_F(BundyD2ControllerTest, basicInstanceTesting) {
  52. // Verify the we can the singleton instance can be fetched and that
  53. // it is the correct type.
  54. DControllerBasePtr& controller = DControllerTest::getController();
  55. ASSERT_TRUE(controller);
  56. ASSERT_NO_THROW(boost::dynamic_pointer_cast<D2Controller>(controller));
  57. // Verify that controller's app name is correct.
  58. EXPECT_TRUE(checkAppName(D2Controller::d2_app_name_));
  59. // Verify that controller's bin name is correct.
  60. EXPECT_TRUE(checkBinName(D2Controller::d2_bin_name_));
  61. // Verify that controller's spec file name is correct.
  62. EXPECT_TRUE(checkSpecFileName(D2_SPECFILE_LOCATION));
  63. // Verify that controller's IOService exists.
  64. EXPECT_TRUE(checkIOService());
  65. // Verify that the Process does NOT exist.
  66. EXPECT_FALSE(checkProcess());
  67. }
  68. /// @brief Tests basic command line processing.
  69. /// Verifies that:
  70. /// 1. Standard command line options are supported.
  71. /// 2. Invalid options are detected.
  72. TEST_F(BundyD2ControllerTest, commandLineArgs) {
  73. char* argv[] = { const_cast<char*>("progName"),
  74. const_cast<char*>("-v") };
  75. int argc = 2;
  76. // Verify that verbose flag is false initially.
  77. EXPECT_TRUE(checkVerbose(false));
  78. // Verify that standard options can be parsed without error.
  79. EXPECT_NO_THROW(parseArgs(argc, argv));
  80. // Verify that verbose flag is now true.
  81. EXPECT_TRUE(checkVerbose(true));
  82. // Verify that an unknown option is detected.
  83. char* argv2[] = { const_cast<char*>("progName"),
  84. const_cast<char*>("-x") };
  85. argc = 2;
  86. EXPECT_THROW(parseArgs(argc, argv2), InvalidUsage);
  87. }
  88. /// @brief Tests application process creation and initialization.
  89. /// Verifies that the process can be successfully created and initialized.
  90. TEST_F(BundyD2ControllerTest, initProcessTesting) {
  91. ASSERT_NO_THROW(initProcess());
  92. EXPECT_TRUE(checkProcess());
  93. }
  94. /// @brief Configuration update event testing.
  95. /// This really tests just the ability of the handlers to invoke the necessary
  96. /// chain of methods and handle error conditions. Configuration parsing and
  97. /// retrieval should be tested as part of the d2 configuration management
  98. /// implementation. Note that this testing calls the configuration update event
  99. /// callback, configHandler, directly.
  100. /// This test verifies that:
  101. /// 1. Configuration will be rejected in integrated mode when there is no
  102. /// session established. (This is a very contrived situation).
  103. /// 2. In stand-alone mode a configuration update results in successful
  104. /// status return.
  105. /// 3. That an application process error in configuration updating is handled
  106. /// properly.
  107. TEST_F(BundyD2ControllerTest, configUpdateTests) {
  108. int rcode = -1;
  109. isc::data::ConstElementPtr answer;
  110. // Initialize the application process.
  111. ASSERT_NO_THROW(initProcess());
  112. EXPECT_TRUE(checkProcess());
  113. // Create a configuration set using a small, valid D2 configuration.
  114. isc::data::ElementPtr config_set =
  115. isc::data::Element::fromJSON(valid_d2_config);
  116. // Configuration should be rejected as there is no session. This is a
  117. // pretty contrived situation that shouldn't be possible other than the
  118. // handler being called directly (like this does).
  119. answer = D2Controller::configHandler(config_set);
  120. isc::config::parseAnswer(rcode, answer);
  121. EXPECT_EQ(1, rcode);
  122. }
  123. /// @brief Command execution tests.
  124. /// This really tests just the ability of the handler to invoke the necessary
  125. /// chain of methods and to handle error conditions. Note that this testing
  126. /// calls the command callback, commandHandler, directly.
  127. /// This test verifies that:
  128. /// 1. That an unrecognized command is detected and returns a status of
  129. /// d2::COMMAND_INVALID.
  130. /// 2. Shutdown command is recognized and returns a d2::COMMAND_SUCCESS status.
  131. TEST_F(BundyD2ControllerTest, executeCommandTests) {
  132. int rcode = -1;
  133. isc::data::ConstElementPtr answer;
  134. isc::data::ElementPtr arg_set;
  135. // Initialize the application process.
  136. ASSERT_NO_THROW(initProcess());
  137. EXPECT_TRUE(checkProcess());
  138. // Verify that an unknown command returns an COMMAND_INVALID response.
  139. std::string bogus_command("bogus");
  140. answer = D2Controller::commandHandler(bogus_command, arg_set);
  141. isc::config::parseAnswer(rcode, answer);
  142. EXPECT_EQ(COMMAND_INVALID, rcode);
  143. // Verify that shutdown command returns COMMAND_SUCCESS response.
  144. //answer = executeCommand(SHUT_DOWN_COMMAND, isc::data::ElementPtr());
  145. answer = D2Controller::commandHandler(SHUT_DOWN_COMMAND, arg_set);
  146. isc::config::parseAnswer(rcode, answer);
  147. EXPECT_EQ(COMMAND_SUCCESS, rcode);
  148. }
  149. /// @brief Tests launch with a session establishment failure.
  150. /// This test launches with a valid command line for integrated mode and no.
  151. /// Attempting to connect to BIND10 should fail, even if BIND10 is running
  152. /// UNLESS the test is run as root. Launch should throw SessionStartError.
  153. TEST_F(BundyD2ControllerTest, launchSessionFailure) {
  154. // Command line to run integrated
  155. char* argv[] = { (char*)"progName" };
  156. int argc = 1;
  157. // Launch the controller in integrated mode.
  158. EXPECT_THROW(launch(argc, argv), SessionStartError);
  159. }
  160. }; // end of isc::d2 namespace
  161. }; // end of isc namespace