d2_process_unittests.cc 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167
  1. // Copyright (C) 2013 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 <d2/d2_process.h>
  16. #include <d_test_stubs.h>
  17. #include <boost/date_time/posix_time/posix_time.hpp>
  18. #include <gtest/gtest.h>
  19. #include <config.h>
  20. #include <sstream>
  21. using namespace std;
  22. using namespace isc;
  23. using namespace isc::config;
  24. using namespace isc::d2;
  25. using namespace boost::posix_time;
  26. namespace {
  27. /// @brief D2Process test fixture class
  28. class D2ProcessTest : public ::testing::Test {
  29. public:
  30. /// @brief Static instance accessible via test callbacks.
  31. static DProcessBasePtr process_;
  32. /// @brief Constructor
  33. D2ProcessTest() {
  34. io_service_.reset(new isc::asiolink::IOService());
  35. process_.reset(new D2Process("TestProcess", io_service_));
  36. }
  37. /// @brief Destructor
  38. ~D2ProcessTest() {
  39. io_service_.reset();
  40. process_.reset();
  41. }
  42. /// @brief Callback that will invoke shutdown method.
  43. static void genShutdownCallback() {
  44. process_->shutdown();
  45. }
  46. /// @brief Callback that throws an exception.
  47. static void genFatalErrorCallback() {
  48. isc_throw (DProcessBaseError, "simulated fatal error");
  49. }
  50. /// @brief IOService for event processing. Fills in for IOService
  51. /// supplied by management layer.
  52. IOServicePtr io_service_;
  53. };
  54. // Define the static process instance
  55. DProcessBasePtr D2ProcessTest::process_;
  56. /// @brief Verifies D2Process constructor behavior.
  57. /// 1. Verifies that constructor fails with an invalid IOService
  58. /// 2. Verifies that constructor succeeds with a valid IOService
  59. TEST(D2Process, construction) {
  60. // Verify that the constructor will fail if given an empty
  61. // io service.
  62. IOServicePtr lcl_io_service;
  63. EXPECT_THROW (D2Process("TestProcess", lcl_io_service), DProcessBaseError);
  64. // Verify that the constructor succeeds with a valid io_service
  65. lcl_io_service.reset(new isc::asiolink::IOService());
  66. ASSERT_NO_THROW (D2Process("TestProcess", lcl_io_service));
  67. }
  68. /// @brief Verifies basic configure method behavior.
  69. /// This test is simplistic and will need to be augmented as configuration
  70. /// ability is implemented.
  71. TEST_F(D2ProcessTest, configure) {
  72. int rcode = -1;
  73. // Use a small, valid D2 configuration to verify successful parsing.
  74. isc::data::ElementPtr json = isc::data::Element::fromJSON(valid_d2_config);
  75. isc::data::ConstElementPtr answer = process_->configure(json);
  76. isc::config::parseAnswer(rcode, answer);
  77. EXPECT_EQ(0, rcode);
  78. // Use an invalid configuration to verify parsing error return.
  79. string config = "{ \"bogus\": 1000 } ";
  80. json = isc::data::Element::fromJSON(config);
  81. answer = process_->configure(json);
  82. isc::config::parseAnswer(rcode, answer);
  83. EXPECT_EQ(1, rcode);
  84. }
  85. /// @brief Verifies basic command method behavior.
  86. /// @TODO IF the D2Process is extended to support extra commands this testing
  87. /// will need to augmented accordingly.
  88. TEST_F(D2ProcessTest, command) {
  89. // Verify that the process will process unsupported command and
  90. // return a failure response.
  91. int rcode = -1;
  92. string args = "{ \"arg1\": 77 } ";
  93. isc::data::ElementPtr json = isc::data::Element::fromJSON(args);
  94. isc::data::ConstElementPtr answer =
  95. process_->command("bogus_command", json);
  96. parseAnswer(rcode, answer);
  97. EXPECT_EQ(COMMAND_INVALID, rcode);
  98. }
  99. /// @brief Verifies that an "external" call to shutdown causes the run method
  100. /// to exit gracefully.
  101. TEST_F(D2ProcessTest, normalShutdown) {
  102. // Use an asiolink IntervalTimer and callback to generate the
  103. // shutdown invocation. (Note IntervalTimer setup is in milliseconds).
  104. isc::asiolink::IntervalTimer timer(*io_service_);
  105. timer.setup(genShutdownCallback, 2 * 1000);
  106. // Record start time, and invoke run().
  107. ptime start = microsec_clock::universal_time();
  108. EXPECT_NO_THROW(process_->run());
  109. // Record stop time.
  110. ptime stop = microsec_clock::universal_time();
  111. // Verify that duration of the run invocation is the same as the
  112. // timer duration. This demonstrates that the shutdown was driven
  113. // by an io_service event and callback.
  114. time_duration elapsed = stop - start;
  115. EXPECT_TRUE(elapsed.total_milliseconds() >= 1900 &&
  116. elapsed.total_milliseconds() <= 2100);
  117. }
  118. /// @brief Verifies that an "uncaught" exception thrown during event loop
  119. /// execution is treated as a fatal error.
  120. TEST_F(D2ProcessTest, fatalErrorShutdown) {
  121. // Use an asiolink IntervalTimer and callback to generate the
  122. // the exception. (Note IntervalTimer setup is in milliseconds).
  123. isc::asiolink::IntervalTimer timer(*io_service_);
  124. timer.setup(genFatalErrorCallback, 2 * 1000);
  125. // Record start time, and invoke run().
  126. ptime start = microsec_clock::universal_time();
  127. EXPECT_THROW(process_->run(), DProcessBaseError);
  128. // Record stop time.
  129. ptime stop = microsec_clock::universal_time();
  130. // Verify that duration of the run invocation is the same as the
  131. // timer duration. This demonstrates that the anomaly occurred
  132. // during io callback processing.
  133. time_duration elapsed = stop - start;
  134. EXPECT_TRUE(elapsed.total_milliseconds() >= 1900 &&
  135. elapsed.total_milliseconds() <= 2100);
  136. }
  137. } // end of anonymous namespace