datasrc_configurator_unittest.cc 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286
  1. // Copyright (C) 2012 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 <auth/datasrc_configurator.h>
  15. #include <config/tests/fake_session.h>
  16. #include <config/ccsession.h>
  17. #include <gtest/gtest.h>
  18. #include <memory>
  19. #include <boost/shared_ptr.hpp>
  20. using namespace isc;
  21. using namespace isc::cc;
  22. using namespace isc::config;
  23. using namespace isc::data;
  24. using namespace isc::dns;
  25. using namespace std;
  26. using namespace boost;
  27. namespace {
  28. class DatasrcConfiguratorTest;
  29. class FakeList {
  30. public:
  31. FakeList() :
  32. configuration_(new ListElement)
  33. {}
  34. void configure(const ConstElementPtr& configuration, bool allow_cache) {
  35. EXPECT_TRUE(allow_cache);
  36. conf_ = configuration->get(0)->get("type")->stringValue();
  37. configuration_ = configuration;
  38. }
  39. const string& getConf() const {
  40. return (conf_);
  41. }
  42. ConstElementPtr getConfiguration() const {
  43. return (configuration_);
  44. }
  45. private:
  46. string conf_;
  47. ConstElementPtr configuration_;
  48. };
  49. typedef shared_ptr<FakeList> ListPtr;
  50. // We use the test fixture as both parameters, this makes it possible
  51. // to easily fake all needed methods and look that they were called.
  52. typedef DataSourceConfiguratorGeneric<DatasrcConfiguratorTest,
  53. FakeList> Configurator;
  54. class DatasrcConfiguratorTest : public ::testing::Test {
  55. public:
  56. // These pretend to be the server
  57. ListPtr getClientList(const RRClass& rrclass) {
  58. log_ += "get " + rrclass.toText() + "\n";
  59. return (lists_[rrclass]);
  60. }
  61. void setClientList(const RRClass& rrclass, const ListPtr& list) {
  62. log_ += "set " + rrclass.toText() + " " +
  63. (list ? list->getConf() : "") + "\n";
  64. lists_[rrclass] = list;
  65. }
  66. vector<RRClass> getClientListClasses() const {
  67. vector<RRClass> result;
  68. for (map<RRClass, ListPtr>::const_iterator it(lists_.begin());
  69. it != lists_.end(); ++it) {
  70. result.push_back(it->first);
  71. }
  72. return (result);
  73. }
  74. protected:
  75. DatasrcConfiguratorTest() :
  76. session(ElementPtr(new ListElement), ElementPtr(new ListElement),
  77. ElementPtr(new ListElement)),
  78. specfile(string(TEST_OWN_DATA_DIR) + "/spec.spec")
  79. {
  80. initSession();
  81. }
  82. void initSession() {
  83. session.getMessages()->add(createAnswer());
  84. mccs.reset(new ModuleCCSession(specfile, session, NULL, NULL, false,
  85. false));
  86. }
  87. void TearDown() {
  88. // Make sure no matter what we did, it is cleaned up.
  89. Configurator::deinit();
  90. }
  91. void init(const ElementPtr& config = ElementPtr()) {
  92. session.getMessages()->
  93. add(createAnswer(0,
  94. moduleSpecFromFile(string(PLUGIN_DATA_PATH) +
  95. "/datasrc.spec").
  96. getFullSpec()));
  97. if (config) {
  98. session.getMessages()->add(createAnswer(0, config));
  99. } else {
  100. session.getMessages()->
  101. add(createAnswer(0, ElementPtr(new MapElement)));
  102. }
  103. Configurator::init(mccs.get(), this);
  104. }
  105. void SetUp() {
  106. init();
  107. }
  108. void doInInit() {
  109. const ElementPtr
  110. config(Element::fromJSON("{\"IN\": [{\"type\": \"xxx\"}]}"));
  111. session.addMessage(createCommand("config_update", config), "data_sources",
  112. "*");
  113. mccs->checkCommand();
  114. // Check it called the correct things (check that there's no IN yet and
  115. // set a new one.
  116. EXPECT_EQ("get IN\nset IN xxx\n", log_);
  117. }
  118. FakeSession session;
  119. auto_ptr<ModuleCCSession> mccs;
  120. const string specfile;
  121. map<RRClass, ListPtr> lists_;
  122. string log_;
  123. };
  124. // Check the initialization (and deinitialization)
  125. TEST_F(DatasrcConfiguratorTest, initialization) {
  126. // It can't be initialized again
  127. EXPECT_THROW(init(), InvalidOperation);
  128. EXPECT_TRUE(session.haveSubscription("data_sources", "*"));
  129. // Deinitialize to make the tests reasonable
  130. Configurator::deinit();
  131. EXPECT_FALSE(session.haveSubscription("data_sources", "*"));
  132. // We can't reconfigure now (not even manually)
  133. EXPECT_THROW(Configurator::reconfigure(ElementPtr(new MapElement())),
  134. InvalidOperation);
  135. // If one of them is NULL, it does not work
  136. EXPECT_THROW(Configurator::init(NULL, this), InvalidParameter);
  137. EXPECT_FALSE(session.haveSubscription("data_sources", "*"));
  138. EXPECT_THROW(Configurator::init(mccs.get(), NULL), InvalidParameter);
  139. EXPECT_FALSE(session.haveSubscription("data_sources", "*"));
  140. // But we can initialize it again now
  141. EXPECT_NO_THROW(init());
  142. EXPECT_TRUE(session.haveSubscription("data_sources", "*"));
  143. }
  144. // Push there a configuration with a single list.
  145. TEST_F(DatasrcConfiguratorTest, createList) {
  146. doInInit();
  147. }
  148. TEST_F(DatasrcConfiguratorTest, modifyList) {
  149. // First, initialize the list
  150. doInInit();
  151. // And now change the configuration of the list
  152. const ElementPtr
  153. config(Element::fromJSON("{\"IN\": [{\"type\": \"yyy\"}]}"));
  154. session.addMessage(createCommand("config_update", config), "data_sources",
  155. "*");
  156. log_ = "";
  157. mccs->checkCommand();
  158. // This one does not set
  159. EXPECT_EQ("get IN\n", log_);
  160. // But this should contain the yyy configuration
  161. EXPECT_EQ("yyy", lists_[RRClass::IN()]->getConf());
  162. }
  163. // Check we can have multiple lists at once
  164. TEST_F(DatasrcConfiguratorTest, multiple) {
  165. const ElementPtr
  166. config(Element::fromJSON("{\"IN\": [{\"type\": \"yyy\"}], "
  167. "\"CH\": [{\"type\": \"xxx\"}]}"));
  168. session.addMessage(createCommand("config_update", config), "data_sources",
  169. "*");
  170. mccs->checkCommand();
  171. // This one does not set
  172. EXPECT_EQ("get CH\nset CH xxx\nget IN\nset IN yyy\n", log_);
  173. // We should have both there
  174. EXPECT_EQ("yyy", lists_[RRClass::IN()]->getConf());
  175. EXPECT_EQ("xxx", lists_[RRClass::CH()]->getConf());
  176. EXPECT_EQ(2, lists_.size());
  177. }
  178. // Check we can add another one later and the old one does not get
  179. // overwritten.
  180. //
  181. // It's almost like above, but we initialize first with single-list
  182. // config.
  183. TEST_F(DatasrcConfiguratorTest, updateAdd) {
  184. doInInit();
  185. const ElementPtr
  186. config(Element::fromJSON("{\"IN\": [{\"type\": \"yyy\"}], "
  187. "\"CH\": [{\"type\": \"xxx\"}]}"));
  188. session.addMessage(createCommand("config_update", config), "data_sources",
  189. "*");
  190. log_ = "";
  191. mccs->checkCommand();
  192. // This one does not set
  193. EXPECT_EQ("get CH\nset CH xxx\nget IN\n", log_);
  194. // But this should contain the yyy configuration
  195. EXPECT_EQ("xxx", lists_[RRClass::CH()]->getConf());
  196. EXPECT_EQ("yyy", lists_[RRClass::IN()]->getConf());
  197. EXPECT_EQ(2, lists_.size());
  198. }
  199. // We delete a class list in this test.
  200. TEST_F(DatasrcConfiguratorTest, updateDelete) {
  201. doInInit();
  202. const ElementPtr
  203. config(Element::fromJSON("{}"));
  204. session.addMessage(createCommand("config_update", config), "data_sources",
  205. "*");
  206. log_ = "";
  207. mccs->checkCommand();
  208. EXPECT_EQ("get IN\nset IN \n", log_);
  209. EXPECT_FALSE(lists_[RRClass::IN()]);
  210. }
  211. // Check that we can rollback an addition if something else fails
  212. TEST_F(DatasrcConfiguratorTest, rollbackAddition) {
  213. doInInit();
  214. // The configuration is wrong. However, the CH one will get done first.
  215. const ElementPtr
  216. config(Element::fromJSON("{\"IN\": [{\"type\": 13}], "
  217. "\"CH\": [{\"type\": \"xxx\"}]}"));
  218. session.addMessage(createCommand("config_update", config), "data_sources",
  219. "*");
  220. log_ = "";
  221. // It does not throw, as it is handled in the ModuleCCSession.
  222. // Throwing from the reconfigure is checked in other tests.
  223. EXPECT_NO_THROW(mccs->checkCommand());
  224. // Anyway, the result should not contain CH now and the original IN should
  225. // be there.
  226. EXPECT_EQ("xxx", lists_[RRClass::IN()]->getConf());
  227. EXPECT_FALSE(lists_[RRClass::CH()]);
  228. }
  229. // Check that we can rollback a deletion if something else fails
  230. TEST_F(DatasrcConfiguratorTest, rollbackDeletion) {
  231. doInInit();
  232. // Put the CH there
  233. const ElementPtr
  234. config1(Element::fromJSON("{\"IN\": [{\"type\": \"yyy\"}], "
  235. "\"CH\": [{\"type\": \"xxx\"}]}"));
  236. Configurator::reconfigure(config1);
  237. const ElementPtr
  238. config2(Element::fromJSON("{\"IN\": [{\"type\": 13}]}"));
  239. // This would delete CH. However, the IN one fails.
  240. // As the deletions happen after the additions/settings
  241. // and there's no known way to cause an exception during the
  242. // deletions, it is not a true rollback, but the result should
  243. // be the same.
  244. EXPECT_THROW(Configurator::reconfigure(config2), TypeError);
  245. EXPECT_EQ("yyy", lists_[RRClass::IN()]->getConf());
  246. EXPECT_EQ("xxx", lists_[RRClass::CH()]->getConf());
  247. }
  248. // Check that we can roll back configuration change if something
  249. // fails later on.
  250. TEST_F(DatasrcConfiguratorTest, rollbackConfiguration) {
  251. doInInit();
  252. // Put the CH there
  253. const ElementPtr
  254. config1(Element::fromJSON("{\"IN\": [{\"type\": \"yyy\"}], "
  255. "\"CH\": [{\"type\": \"xxx\"}]}"));
  256. Configurator::reconfigure(config1);
  257. // Now, the CH happens first. But nevertheless, it should be
  258. // restored to the previoeus version.
  259. const ElementPtr
  260. config2(Element::fromJSON("{\"IN\": [{\"type\": 13}], "
  261. "\"CH\": [{\"type\": \"yyy\"}]}"));
  262. EXPECT_THROW(Configurator::reconfigure(config2), TypeError);
  263. EXPECT_EQ("yyy", lists_[RRClass::IN()]->getConf());
  264. EXPECT_EQ("xxx", lists_[RRClass::CH()]->getConf());
  265. }
  266. }