diff_tests.py 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680
  1. # Copyright (C) 2011 Internet Systems Consortium.
  2. #
  3. # Permission to use, copy, modify, and 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 INTERNET SYSTEMS CONSORTIUM
  8. # DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL
  9. # IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL
  10. # INTERNET SYSTEMS CONSORTIUM BE LIABLE FOR ANY SPECIAL, DIRECT,
  11. # INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING
  12. # FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT,
  13. # NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION
  14. # WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
  15. import isc.log
  16. import unittest
  17. import isc.datasrc
  18. from isc.dns import Name, RRset, RRClass, RRType, RRTTL, Rdata
  19. from isc.xfrin.diff import Diff, NoSuchZone
  20. class TestError(Exception):
  21. """
  22. Just to have something to be raised during the tests.
  23. Not used outside.
  24. """
  25. pass
  26. class DiffTest(unittest.TestCase):
  27. """
  28. Tests for the isc.xfrin.diff.Diff class.
  29. It also plays role of a data source and an updater, so it can manipulate
  30. some test variables while being called.
  31. """
  32. def setUp(self):
  33. """
  34. This sets internal variables so we can see nothing was called yet.
  35. It also creates some variables used in multiple tests.
  36. """
  37. # Track what was called already
  38. self.__updater_requested = False
  39. self.__compact_called = False
  40. self.__data_operations = []
  41. self.__apply_called = False
  42. self.__commit_called = False
  43. self.__broken_called = False
  44. self.__warn_called = False
  45. self.__should_replace = False
  46. self.__find_called = False
  47. self.__find_name = None
  48. self.__find_type = None
  49. self.__find_options = None
  50. self.__find_all_called = False
  51. self.__find_all_name = None
  52. self.__find_all_options = None
  53. # Some common values
  54. self.__rrclass = RRClass.IN()
  55. self.__type = RRType.A()
  56. self.__ttl = RRTTL(3600)
  57. # And RRsets
  58. # Create two valid rrsets
  59. self.__rrset1 = RRset(Name('a.example.org.'), self.__rrclass,
  60. self.__type, self.__ttl)
  61. self.__rdata = Rdata(self.__type, self.__rrclass, '192.0.2.1')
  62. self.__rrset1.add_rdata(self.__rdata)
  63. self.__rrset2 = RRset(Name('b.example.org.'), self.__rrclass,
  64. self.__type, self.__ttl)
  65. self.__rrset2.add_rdata(self.__rdata)
  66. # And two invalid
  67. self.__rrset_empty = RRset(Name('empty.example.org.'), self.__rrclass,
  68. self.__type, self.__ttl)
  69. self.__rrset_multi = RRset(Name('multi.example.org.'), self.__rrclass,
  70. self.__type, self.__ttl)
  71. self.__rrset_multi.add_rdata(self.__rdata)
  72. self.__rrset_multi.add_rdata(Rdata(self.__type, self.__rrclass,
  73. '192.0.2.2'))
  74. def __mock_compact(self):
  75. """
  76. This can be put into the diff to hook into its compact method and see
  77. if it gets called.
  78. """
  79. self.__compact_called = True
  80. def __mock_apply(self):
  81. """
  82. This can be put into the diff to hook into its apply method and see
  83. it gets called.
  84. """
  85. self.__apply_called = True
  86. def __broken_operation(self, *args):
  87. """
  88. This can be used whenever an operation should fail. It raises TestError.
  89. It should take whatever amount of parameters needed, so it can be put
  90. quite anywhere.
  91. """
  92. self.__broken_called = True
  93. raise TestError("Test error")
  94. def warn(self, *args):
  95. """
  96. This is for checking the warn function was called, we replace the logger
  97. in the tested module.
  98. """
  99. self.__warn_called = True
  100. # Also log the message so we can check the log format (manually)
  101. self.orig_logger.warn(*args)
  102. def commit(self):
  103. """
  104. This is part of pretending to be a zone updater. This notes the commit
  105. was called.
  106. """
  107. self.__commit_called = True
  108. def add_rrset(self, rrset):
  109. """
  110. This one is part of pretending to be a zone updater. It writes down
  111. addition of an rrset was requested.
  112. """
  113. self.__data_operations.append(('add', rrset))
  114. def delete_rrset(self, rrset):
  115. """
  116. This one is part of pretending to be a zone updater. It writes down
  117. removal of an rrset was requested.
  118. """
  119. self.__data_operations.append(('delete', rrset))
  120. def get_class(self):
  121. """
  122. This one is part of pretending to be a zone updater. It returns
  123. the IN class.
  124. """
  125. return self.__rrclass
  126. def get_updater(self, zone_name, replace, journaling=False):
  127. """
  128. This one pretends this is the data source client and serves
  129. getting an updater.
  130. If zone_name is 'none.example.org.', it returns None, otherwise
  131. it returns self.
  132. """
  133. # The diff should not delete the old data.
  134. self.assertEqual(self.__should_replace, replace)
  135. self.__updater_requested = True
  136. if zone_name == Name('none.example.org.'):
  137. # Pretend this zone doesn't exist
  138. return None
  139. # If journaling is enabled, record the fact; for a special zone
  140. # pretend that we don't support journaling.
  141. if journaling:
  142. if zone_name == Name('nodiff.example.org'):
  143. raise isc.datasrc.NotImplemented('journaling not supported')
  144. self.__journaling_enabled = True
  145. else:
  146. self.__journaling_enabled = False
  147. return self
  148. def find(self, name, rrtype, options=None):
  149. self.__find_called = True
  150. self.__find_name = name
  151. self.__find_type = rrtype
  152. self.__find_options = options
  153. # Doesn't really matter what is returned, as long
  154. # as the test can check that it's passed along
  155. return "find_return"
  156. def find_all(self, name, options=None):
  157. self.__find_all_called = True
  158. self.__find_all_name = name
  159. self.__find_all_options = options
  160. # Doesn't really matter what is returned, as long
  161. # as the test can check that it's passed along
  162. return "find_all_return"
  163. def test_create(self):
  164. """
  165. This test the case when the diff is successfuly created. It just
  166. tries it does not throw and gets the updater.
  167. """
  168. diff = Diff(self, Name('example.org.'))
  169. self.assertTrue(self.__updater_requested)
  170. self.assertEqual([], diff.get_buffer())
  171. # By default journaling is disabled
  172. self.assertFalse(self.__journaling_enabled)
  173. def test_create_nonexist(self):
  174. """
  175. Try to create a diff on a zone that doesn't exist. This should
  176. raise a correct exception.
  177. """
  178. self.assertRaises(NoSuchZone, Diff, self, Name('none.example.org.'))
  179. self.assertTrue(self.__updater_requested)
  180. def test_create_withjournal(self):
  181. Diff(self, Name('example.org'), False, True)
  182. self.assertTrue(self.__journaling_enabled)
  183. def test_create_nojournal(self):
  184. Diff(self, Name('nodiff.example.org'), False, True)
  185. self.assertFalse(self.__journaling_enabled)
  186. def __data_common(self, diff, method, operation):
  187. """
  188. Common part of test for test_add and test_delte.
  189. """
  190. # Try putting there the bad data first
  191. self.assertRaises(ValueError, method, self.__rrset_empty)
  192. self.assertRaises(ValueError, method, self.__rrset_multi)
  193. # They were not added
  194. self.assertEqual([], diff.get_buffer())
  195. # Put some proper data into the diff
  196. method(self.__rrset1)
  197. method(self.__rrset2)
  198. dlist = [(operation, self.__rrset1), (operation, self.__rrset2)]
  199. self.assertEqual(dlist, diff.get_buffer())
  200. # Check the data are not destroyed by raising an exception because of
  201. # bad data
  202. self.assertRaises(ValueError, method, self.__rrset_empty)
  203. self.assertEqual(dlist, diff.get_buffer())
  204. def test_add(self):
  205. """
  206. Try to add few items into the diff and see they are stored in there.
  207. Also try passing an rrset that has differnt amount of RRs than 1.
  208. """
  209. diff = Diff(self, Name('example.org.'))
  210. self.__data_common(diff, diff.add_data, 'add')
  211. def test_delete(self):
  212. """
  213. Try scheduling removal of few items into the diff and see they are
  214. stored in there.
  215. Also try passing an rrset that has different amount of RRs than 1.
  216. """
  217. diff = Diff(self, Name('example.org.'))
  218. self.__data_common(diff, diff.delete_data, 'delete')
  219. def test_apply(self):
  220. """
  221. Schedule few additions and check the apply works by passing the
  222. data into the updater.
  223. """
  224. # Prepare the diff
  225. diff = Diff(self, Name('example.org.'))
  226. diff.add_data(self.__rrset1)
  227. diff.delete_data(self.__rrset2)
  228. dlist = [('add', self.__rrset1), ('delete', self.__rrset2)]
  229. self.assertEqual(dlist, diff.get_buffer())
  230. # Do the apply, hook the compact method
  231. diff.compact = self.__mock_compact
  232. diff.apply()
  233. # It should call the compact
  234. self.assertTrue(self.__compact_called)
  235. # And pass the data. Our local history of what happened is the same
  236. # format, so we can check the same way
  237. self.assertEqual(dlist, self.__data_operations)
  238. # And the buffer in diff should become empty, as everything
  239. # got inside.
  240. self.assertEqual([], diff.get_buffer())
  241. def test_commit(self):
  242. """
  243. If we call a commit, it should first apply whatever changes are
  244. left (we hook into that instead of checking the effect) and then
  245. the commit on the updater should have been called.
  246. Then we check it raises value error for whatever operation we try.
  247. """
  248. diff = Diff(self, Name('example.org.'))
  249. diff.add_data(self.__rrset1)
  250. orig_apply = diff.apply
  251. diff.apply = self.__mock_apply
  252. diff.commit()
  253. self.assertTrue(self.__apply_called)
  254. self.assertTrue(self.__commit_called)
  255. # The data should be handled by apply which we replaced.
  256. self.assertEqual([], self.__data_operations)
  257. # Now check all range of other methods raise ValueError
  258. self.assertRaises(ValueError, diff.commit)
  259. self.assertRaises(ValueError, diff.add_data, self.__rrset2)
  260. self.assertRaises(ValueError, diff.delete_data, self.__rrset1)
  261. diff.apply = orig_apply
  262. self.assertRaises(ValueError, diff.apply)
  263. # This one does not state it should raise, so check it doesn't
  264. # But it is NOP in this situation anyway
  265. diff.compact()
  266. def test_autoapply(self):
  267. """
  268. Test the apply is called all by itself after 100 tasks are added.
  269. """
  270. diff = Diff(self, Name('example.org.'))
  271. # A method to check the apply is called _after_ the 100th element
  272. # is added. We don't use it anywhere else, so we define it locally
  273. # as lambda function
  274. def check():
  275. self.assertEqual(100, len(diff.get_buffer()))
  276. self.__mock_apply()
  277. orig_apply = diff.apply
  278. diff.apply = check
  279. # If we put 99, nothing happens yet
  280. for i in range(0, 99):
  281. diff.add_data(self.__rrset1)
  282. expected = [('add', self.__rrset1)] * 99
  283. self.assertEqual(expected, diff.get_buffer())
  284. self.assertFalse(self.__apply_called)
  285. # Now we push the 100th and it should call the apply method
  286. # This will _not_ flush the data yet, as we replaced the method.
  287. # It, however, would in the real life.
  288. diff.add_data(self.__rrset1)
  289. # Now the apply method (which is replaced by our check) should
  290. # have been called. If it wasn't, this is false. If it was, but
  291. # still with 99 elements, the check would complain
  292. self.assertTrue(self.__apply_called)
  293. # Reset the buffer by calling the original apply.
  294. orig_apply()
  295. self.assertEqual([], diff.get_buffer())
  296. # Similar with delete
  297. self.__apply_called = False
  298. for i in range(0, 99):
  299. diff.delete_data(self.__rrset2)
  300. expected = [('delete', self.__rrset2)] * 99
  301. self.assertEqual(expected, diff.get_buffer())
  302. self.assertFalse(self.__apply_called)
  303. diff.delete_data(self.__rrset2)
  304. self.assertTrue(self.__apply_called)
  305. def test_compact(self):
  306. """
  307. Test the compaction works as expected, eg. it compacts only consecutive
  308. changes of the same operation and on the same domain/type.
  309. The test case checks that it does merge them, but also puts some
  310. different operations "in the middle", changes the type and name and
  311. places the same kind of change further away of each other to see they
  312. are not merged in that case.
  313. """
  314. diff = Diff(self, Name('example.org.'))
  315. # Check we can do a compact on empty data, it shouldn't break
  316. diff.compact()
  317. self.assertEqual([], diff.get_buffer())
  318. # This data is the way it should look like after the compact
  319. # ('operation', 'domain.prefix', 'type', ['rdata', 'rdata'])
  320. # The notes say why the each of consecutive can't be merged
  321. data = [
  322. ('add', 'a', 'A', ['192.0.2.1', '192.0.2.2']),
  323. # Different type.
  324. ('add', 'a', 'AAAA', ['2001:db8::1', '2001:db8::2']),
  325. # Different operation
  326. ('delete', 'a', 'AAAA', ['2001:db8::3']),
  327. # Different domain
  328. ('delete', 'b', 'AAAA', ['2001:db8::4']),
  329. # This does not get merged with the first, even if logically
  330. # possible. We just don't do this.
  331. ('add', 'a', 'A', ['192.0.2.3'])
  332. ]
  333. # Now, fill the data into the diff, in a "flat" way, one by one
  334. for (op, nprefix, rrtype, rdata) in data:
  335. name = Name(nprefix + '.example.org.')
  336. rrtype_obj = RRType(rrtype)
  337. for rdatum in rdata:
  338. rrset = RRset(name, self.__rrclass, rrtype_obj, self.__ttl)
  339. rrset.add_rdata(Rdata(rrtype_obj, self.__rrclass, rdatum))
  340. if op == 'add':
  341. diff.add_data(rrset)
  342. else:
  343. diff.delete_data(rrset)
  344. # Compact it
  345. diff.compact()
  346. # Now check they got compacted. They should be in the same order as
  347. # pushed inside. So it should be the same as data modulo being in
  348. # the rrsets and isc.dns objects.
  349. def check():
  350. buf = diff.get_buffer()
  351. self.assertEqual(len(data), len(buf))
  352. for (expected, received) in zip(data, buf):
  353. (eop, ename, etype, edata) = expected
  354. (rop, rrrset) = received
  355. self.assertEqual(eop, rop)
  356. ename_obj = Name(ename + '.example.org.')
  357. self.assertEqual(ename_obj, rrrset.get_name())
  358. # We check on names to make sure they are printed nicely
  359. self.assertEqual(etype, str(rrrset.get_type()))
  360. rdata = rrrset.get_rdata()
  361. self.assertEqual(len(edata), len(rdata))
  362. # It should also preserve the order
  363. for (edatum, rdatum) in zip(edata, rdata):
  364. self.assertEqual(edatum, str(rdatum))
  365. check()
  366. # Try another compact does nothing, but survives
  367. diff.compact()
  368. check()
  369. def test_wrong_class(self):
  370. """
  371. Test a wrong class of rrset is rejected.
  372. """
  373. diff = Diff(self, Name('example.org.'))
  374. rrset = RRset(Name('a.example.org.'), RRClass.CH(), RRType.NS(),
  375. self.__ttl)
  376. rrset.add_rdata(Rdata(RRType.NS(), RRClass.CH(), 'ns.example.org.'))
  377. self.assertRaises(ValueError, diff.add_data, rrset)
  378. self.assertRaises(ValueError, diff.delete_data, rrset)
  379. def __do_raise_test(self):
  380. """
  381. Do a raise test. Expects that one of the operations is exchanged for
  382. broken version.
  383. """
  384. diff = Diff(self, Name('example.org.'))
  385. diff.add_data(self.__rrset1)
  386. diff.delete_data(self.__rrset2)
  387. self.assertRaises(TestError, diff.commit)
  388. self.assertTrue(self.__broken_called)
  389. self.assertRaises(ValueError, diff.add_data, self.__rrset1)
  390. self.assertRaises(ValueError, diff.delete_data, self.__rrset2)
  391. self.assertRaises(ValueError, diff.commit)
  392. self.assertRaises(ValueError, diff.apply)
  393. def test_raise_add(self):
  394. """
  395. Test the exception from add_rrset is propagated and the diff can't be
  396. used afterwards.
  397. """
  398. self.add_rrset = self.__broken_operation
  399. self.__do_raise_test()
  400. def test_raise_delete(self):
  401. """
  402. Test the exception from delete_rrset is propagated and the diff can't be
  403. used afterwards.
  404. """
  405. self.delete_rrset = self.__broken_operation
  406. self.__do_raise_test()
  407. def test_raise_commit(self):
  408. """
  409. Test the exception from updater's commit gets propagated and it can't be
  410. used afterwards.
  411. """
  412. self.commit = self.__broken_operation
  413. self.__do_raise_test()
  414. def test_ttl(self):
  415. """
  416. Test the TTL handling. A warn function should have been called if they
  417. differ, but that's all, it should not crash or raise.
  418. """
  419. self.orig_logger = isc.xfrin.diff.logger
  420. try:
  421. isc.xfrin.diff.logger = self
  422. diff = Diff(self, Name('example.org.'))
  423. diff.add_data(self.__rrset1)
  424. rrset2 = RRset(Name('a.example.org.'), self.__rrclass,
  425. self.__type, RRTTL(120))
  426. rrset2.add_rdata(Rdata(self.__type, self.__rrclass, '192.10.2.2'))
  427. diff.add_data(rrset2)
  428. rrset2 = RRset(Name('a.example.org.'), self.__rrclass,
  429. self.__type, RRTTL(6000))
  430. rrset2.add_rdata(Rdata(self.__type, self.__rrclass, '192.10.2.3'))
  431. diff.add_data(rrset2)
  432. # They should get compacted together and complain.
  433. diff.compact()
  434. self.assertEqual(1, len(diff.get_buffer()))
  435. # The TTL stays on the first value, no matter if smaller or bigger
  436. # ones come later.
  437. self.assertEqual(self.__ttl, diff.get_buffer()[0][1].get_ttl())
  438. self.assertTrue(self.__warn_called)
  439. finally:
  440. isc.xfrin.diff.logger = self.orig_logger
  441. def test_rrsig_ttl(self):
  442. '''Similar to the previous test, but for RRSIGs of different covered
  443. types.
  444. They shouldn't be compacted.
  445. '''
  446. diff = Diff(self, Name('example.org.'))
  447. rrsig1 = RRset(Name('example.org'), self.__rrclass,
  448. RRType.RRSIG(), RRTTL(3600))
  449. rrsig1.add_rdata(Rdata(RRType.RRSIG(), self.__rrclass,
  450. 'A 5 3 3600 20000101000000 20000201000000 ' +
  451. '0 example.org. FAKEFAKEFAKE'))
  452. diff.add_data(rrsig1)
  453. rrsig2 = RRset(Name('example.org'), self.__rrclass,
  454. RRType.RRSIG(), RRTTL(1800))
  455. rrsig2.add_rdata(Rdata(RRType.RRSIG(), self.__rrclass,
  456. 'AAAA 5 3 3600 20000101000000 20000201000000 ' +
  457. '1 example.org. FAKEFAKEFAKE'))
  458. diff.add_data(rrsig2)
  459. diff.compact()
  460. self.assertEqual(2, len(diff.get_buffer()))
  461. def test_replace(self):
  462. '''
  463. Test that when we want to replace the whole zone, it is propagated.
  464. '''
  465. self.__should_replace = True
  466. diff = Diff(self, "example.org.", True)
  467. self.assertTrue(self.__updater_requested)
  468. def test_get_buffer(self):
  469. '''
  470. Test that the getters raise when used in the wrong mode
  471. '''
  472. diff_multi = Diff(self, Name('example.org.'), single_update_mode=False)
  473. self.assertRaises(ValueError, diff_multi.get_single_update_buffers)
  474. self.assertEqual([], diff_multi.get_buffer())
  475. diff_single = Diff(self, Name('example.org.'), single_update_mode=True)
  476. self.assertRaises(ValueError, diff_single.get_buffer)
  477. self.assertEqual(([], []), diff_single.get_single_update_buffers())
  478. def test_single_update_mode(self):
  479. '''
  480. Test single-update mode. In this mode, updates and deletes can
  481. be done in any order, but there may only be one changeset.
  482. For both updates and deletes, exactly one SOA rr must be given,
  483. and it must be the first change.
  484. '''
  485. # First create some RRsets to play with
  486. soa = RRset(Name('example.org.'), self.__rrclass, RRType.SOA(),
  487. RRTTL(3600))
  488. soa.add_rdata(Rdata(soa.get_type(), soa.get_class(),
  489. "ns.example.org. foo.example.org. 1234 28800 "+
  490. "7200 604800 3600"))
  491. a = RRset(Name('www.example.org.'), self.__rrclass, RRType.A(),
  492. RRTTL(3600))
  493. a.add_rdata(Rdata(a.get_type(), a.get_class(),
  494. "192.0.2.1"))
  495. a2 = RRset(Name('www.example.org.'), self.__rrclass, RRType.A(),
  496. RRTTL(3600))
  497. a2.add_rdata(Rdata(a2.get_type(), a2.get_class(),
  498. "192.0.2.2"))
  499. # full rrset for A (to check compact())
  500. a_1_2 = RRset(Name('www.example.org.'), self.__rrclass, RRType.A(),
  501. RRTTL(3600))
  502. a_1_2.add_rdata(Rdata(a_1_2.get_type(), a_1_2.get_class(),
  503. "192.0.2.1"))
  504. a_1_2.add_rdata(Rdata(a_1_2.get_type(), a_1_2.get_class(),
  505. "192.0.2.2"))
  506. diff = Diff(self, Name('example.org.'), single_update_mode=True)
  507. # adding a first should fail
  508. self.assertRaises(ValueError, diff.add_data, a)
  509. # But soa should work
  510. diff.add_data(soa)
  511. # And then A should as well
  512. diff.add_data(a)
  513. diff.add_data(a2)
  514. # But another SOA should fail again
  515. self.assertRaises(ValueError, diff.add_data, soa)
  516. # Same for delete
  517. self.assertRaises(ValueError, diff.delete_data, a)
  518. diff.delete_data(soa)
  519. diff.delete_data(a)
  520. diff.delete_data(a2)
  521. self.assertRaises(ValueError, diff.delete_data, soa)
  522. # Not compacted yet, so the buffers should be as we
  523. # filled them
  524. (delbuf, addbuf) = diff.get_single_update_buffers()
  525. self.assertEqual([('delete', soa), ('delete', a), ('delete', a2)], delbuf)
  526. self.assertEqual([('add', soa), ('add', a), ('add', a2)], addbuf)
  527. # Compact should compact the A records in both buffers
  528. diff.compact()
  529. (delbuf, addbuf) = diff.get_single_update_buffers()
  530. # need rrset equality again :/
  531. self.assertEqual(2, len(delbuf))
  532. self.assertEqual(2, len(delbuf[0]))
  533. self.assertEqual('delete', delbuf[0][0])
  534. self.assertEqual(soa.to_text(), delbuf[0][1].to_text())
  535. self.assertEqual(2, len(delbuf[1]))
  536. self.assertEqual('delete', delbuf[1][0])
  537. self.assertEqual(a_1_2.to_text(), delbuf[1][1].to_text())
  538. self.assertEqual(2, len(addbuf))
  539. self.assertEqual(2, len(addbuf[0]))
  540. self.assertEqual('add', addbuf[0][0])
  541. self.assertEqual(soa.to_text(), addbuf[0][1].to_text())
  542. self.assertEqual(2, len(addbuf[1]))
  543. self.assertEqual('add', addbuf[1][0])
  544. self.assertEqual(a_1_2.to_text(), addbuf[1][1].to_text())
  545. # Apply should reset the buffers
  546. diff.apply()
  547. (delbuf, addbuf) = diff.get_single_update_buffers()
  548. self.assertEqual([], delbuf)
  549. self.assertEqual([], addbuf)
  550. # Now the change has been applied, and the buffers are cleared,
  551. # Adding non-SOA records should fail again.
  552. self.assertRaises(ValueError, diff.add_data, a)
  553. self.assertRaises(ValueError, diff.delete_data, a)
  554. def test_find(self):
  555. diff = Diff(self, Name('example.org.'))
  556. name = Name('www.example.org.')
  557. rrtype = RRType.A()
  558. self.assertFalse(self.__find_called)
  559. self.assertEqual(None, self.__find_name)
  560. self.assertEqual(None, self.__find_type)
  561. self.assertEqual(None, self.__find_options)
  562. self.assertEqual("find_return", diff.find(name, rrtype))
  563. self.assertTrue(self.__find_called)
  564. self.assertEqual(name, self.__find_name)
  565. self.assertEqual(rrtype, self.__find_type)
  566. self.assertEqual(isc.datasrc.ZoneFinder.FIND_DEFAULT,
  567. self.__find_options)
  568. def test_find_options(self):
  569. diff = Diff(self, Name('example.org.'))
  570. name = Name('foo.example.org.')
  571. rrtype = RRType.TXT()
  572. options = isc.datasrc.ZoneFinder.NO_WILDCARD |\
  573. isc.datasrc.ZoneFinder.FIND_GLUE_OK
  574. self.assertEqual("find_return", diff.find(name, rrtype, options))
  575. self.assertTrue(self.__find_called)
  576. self.assertEqual(name, self.__find_name)
  577. self.assertEqual(rrtype, self.__find_type)
  578. self.assertEqual(options, self.__find_options)
  579. def test_find_all(self):
  580. diff = Diff(self, Name('example.org.'))
  581. name = Name('www.example.org.')
  582. self.assertFalse(self.__find_all_called)
  583. self.assertEqual(None, self.__find_all_name)
  584. self.assertEqual(None, self.__find_all_options)
  585. self.assertEqual("find_all_return", diff.find_all(name))
  586. self.assertTrue(self.__find_all_called)
  587. self.assertEqual(name, self.__find_all_name)
  588. self.assertEqual(isc.datasrc.ZoneFinder.FIND_DEFAULT,
  589. self.__find_all_options)
  590. def test_find_all_options(self):
  591. diff = Diff(self, Name('example.org.'))
  592. name = Name('www.example.org.')
  593. options = isc.datasrc.ZoneFinder.NO_WILDCARD |\
  594. isc.datasrc.ZoneFinder.FIND_GLUE_OK
  595. self.assertFalse(self.__find_all_called)
  596. self.assertEqual(None, self.__find_all_name)
  597. self.assertEqual(None, self.__find_all_options)
  598. self.assertEqual("find_all_return", diff.find_all(name, options))
  599. self.assertTrue(self.__find_all_called)
  600. self.assertEqual(name, self.__find_all_name)
  601. self.assertEqual(options, self.__find_all_options)
  602. if __name__ == "__main__":
  603. isc.log.init("bind10")
  604. isc.log.resetUnitTestRootLogger()
  605. unittest.main()