def test_commit(self): """ If we call a commit, it should first apply whatever changes are left (we hook into that instead of checking the effect) and then the commit on the updater should have been called. Then we check it raises value error for whatever operation we try. """ diff = Diff(self, Name('example.org.')) diff.add_data(self.__rrset1) orig_apply = diff.apply diff.apply = self.__mock_apply diff.commit() self.assertTrue(self.__apply_called) self.assertTrue(self.__commit_called) # The data should be handled by apply which we replaced. self.assertEqual([], self.__data_operations) # Now check all range of other methods raise ValueError self.assertRaises(ValueError, diff.commit) self.assertRaises(ValueError, diff.add_data, self.__rrset2) self.assertRaises(ValueError, diff.delete_data, self.__rrset1) self.assertRaises(ValueError, diff.find, Name('foo.example.org.'), RRType.A()) self.assertRaises(ValueError, diff.find_all, Name('foo.example.org.')) diff.apply = orig_apply self.assertRaises(ValueError, diff.apply) # This one does not state it should raise, so check it doesn't # But it is NOP in this situation anyway diff.compact()
def test_apply(self): """ Schedule few additions and check the apply works by passing the data into the updater. """ # Prepare the diff diff = Diff(self, Name('example.org.')) diff.add_data(self.__rrset1) diff.delete_data(self.__rrset2) dlist = [('add', self.__rrset1), ('delete', self.__rrset2)] self.assertEqual(dlist, diff.get_buffer()) # Do the apply, hook the compact method diff.compact = self.__mock_compact diff.apply() # It should call the compact self.assertTrue(self.__compact_called) # And pass the data. Our local history of what happened is the same # format, so we can check the same way self.assertEqual(dlist, self.__data_operations) # And the buffer in diff should become empty, as everything # got inside. self.assertEqual([], diff.get_buffer())
def test_autoapply(self): """ Test the apply is called all by itself after 100 tasks are added. """ diff = Diff(self, Name('example.org.')) # A method to check the apply is called _after_ the 100th element # is added. We don't use it anywhere else, so we define it locally # as lambda function def check(): self.assertEqual(100, len(diff.get_buffer())) self.__mock_apply() orig_apply = diff.apply diff.apply = check # If we put 99, nothing happens yet for i in range(0, 99): diff.add_data(self.__rrset1) expected = [('add', self.__rrset1)] * 99 self.assertEqual(expected, diff.get_buffer()) self.assertFalse(self.__apply_called) # Now we push the 100th and it should call the apply method # This will _not_ flush the data yet, as we replaced the method. # It, however, would in the real life. diff.add_data(self.__rrset1) # Now the apply method (which is replaced by our check) should # have been called. If it wasn't, this is false. If it was, but # still with 99 elements, the check would complain self.assertTrue(self.__apply_called) # Reset the buffer by calling the original apply. orig_apply() self.assertEqual([], diff.get_buffer()) # Similar with delete self.__apply_called = False for i in range(0, 99): diff.delete_data(self.__rrset2) expected = [('delete', self.__rrset2)] * 99 self.assertEqual(expected, diff.get_buffer()) self.assertFalse(self.__apply_called) diff.delete_data(self.__rrset2) self.assertTrue(self.__apply_called)
def test_single_update_mode(self): ''' Test single-update mode. In this mode, updates and deletes can be done in any order, but there may only be one changeset. For both updates and deletes, exactly one SOA rr must be given, and it must be the first change. ''' # full rrset for A (to check compact()) txt = RRset(Name('c.example.org.'), self.__rrclass, RRType.TXT(), RRTTL(3600)) txt.add_rdata(Rdata(txt.get_type(), txt.get_class(), "one")) txt.add_rdata(Rdata(txt.get_type(), txt.get_class(), "two")) txt.add_rdata(Rdata(txt.get_type(), txt.get_class(), "three")) a = RRset(Name('d.example.org.'), self.__rrclass, RRType.A(), RRTTL(3600)) a.add_rdata(Rdata(a.get_type(), a.get_class(), "192.0.2.1")) a.add_rdata(Rdata(a.get_type(), a.get_class(), "192.0.2.2")) diff = Diff(self, Name('example.org.'), single_update_mode=True) # adding a first should fail self.assertRaises(ValueError, diff.add_data, a) # But soa should work diff.add_data(self.__rrset_soa) # And then A should as well diff.add_data(self.__rrset3) diff.add_data(self.__rrset4) diff.add_data(self.__rrset5) # But another SOA should fail again self.assertRaises(ValueError, diff.add_data, self.__rrset_soa) # Same for delete self.assertRaises(ValueError, diff.delete_data, self.__rrset6) diff.delete_data(self.__rrset_soa) diff.delete_data(self.__rrset6) diff.delete_data(self.__rrset7) self.assertRaises(ValueError, diff.delete_data, self.__rrset_soa) # Not compacted yet, so the buffers should be as we # filled them (delbuf, addbuf) = diff.get_single_update_buffers() self.assertEqual([('delete', self.__rrset_soa), ('delete', self.__rrset6), ('delete', self.__rrset7)], delbuf) self.assertEqual([('add', self.__rrset_soa), ('add', self.__rrset3), ('add', self.__rrset4), ('add', self.__rrset5)], addbuf) # Compact should compact the A records in both buffers diff.compact() (delbuf, addbuf) = diff.get_single_update_buffers() # need rrset equality again :/ self.assertEqual(2, len(delbuf)) self.assertEqual(2, len(delbuf[0])) self.assertEqual('delete', delbuf[0][0]) self.assertEqual(self.__rrset_soa.to_text(), delbuf[0][1].to_text()) self.assertEqual(2, len(delbuf[1])) self.assertEqual('delete', delbuf[1][0]) self.assertEqual(a.to_text(), delbuf[1][1].to_text()) self.assertEqual(2, len(addbuf)) self.assertEqual(2, len(addbuf[0])) self.assertEqual('add', addbuf[0][0]) self.assertEqual(self.__rrset_soa.to_text(), addbuf[0][1].to_text()) self.assertEqual(2, len(addbuf[1])) self.assertEqual('add', addbuf[1][0]) self.assertEqual(txt.to_text(), addbuf[1][1].to_text()) # Apply should reset the buffers diff.apply() (delbuf, addbuf) = diff.get_single_update_buffers() self.assertEqual([], delbuf) self.assertEqual([], addbuf) # Now the change has been applied, and the buffers are cleared, # Adding non-SOA records should fail again. self.assertRaises(ValueError, diff.add_data, a) self.assertRaises(ValueError, diff.delete_data, a)