def test_disconnect_while_remote_modification(self): """Test we can disconnect and have a remote modification while disconnected""" self.conf.read.return_value = {"enabled": "1", "hostname": self.NAME} z = ZkFarmJoiner(self.client, "/services/db", self.conf) z.loop(3, timeout=self.TIMEOUT) self.expire_session() self.client.ensure_path( "/services/db/%s" % self.IP) # Disconnected, the path does not exist self.client.set("/services/db/%s" % self.IP, json.dumps({ "enabled": "22", "hostname": self.NAME })) z.loop(10, timeout=self.TIMEOUT) try: self.assertEqual( json.loads(self.client.get("/services/db/%s" % self.IP)[0]), { "enabled": "22", "hostname": self.NAME }) except AssertionError: # This test could be fixed but we won't because we # consider that the local filesystem is authoritative. If # this test succeeds, we can make it fail by stopping # zkfarmer before reconnection to ZooKeeper. In this case, # on next start, ZooKeeper modifications would be # lost. Moreover, the znode is ephemeral, so no "remote" # modifications can happend while the node is down. raise SkipTest("Fuzzy test")
def test_remote_modification_should_not_cancel_local_one(self): """Test if a received remote modification does not cancel a local one. This may happen if we have a local modification while processing the echo of a previous modification. For example, we set a value to 1021, we receive back a remote change about this value being set to 1021 but we have a local modification at the same time putting the value to 1022. """ self.conf.read.return_value = { "enabled": "1", "hostname": self.NAME, "counter": 1000 } z = ZkFarmJoiner(self.client, "/services/db", self.conf) z.loop(3, timeout=self.TIMEOUT) self.conf.reset_mock() self.conf.read.return_value = { "enabled": "1", "hostname": self.NAME, "counter": 1001 } z.dispatch(FakeFileEvent()) z.loop(1, timeout=self.TIMEOUT) # Here comes the local modification that won't be noticed now self.conf.reset_mock() self.conf.read.return_value = { "enabled": "1", "hostname": self.NAME, "counter": 1002 } z.loop(3, timeout=self.TIMEOUT) self.assertFalse(self.conf.write.called)
def test_no_write_when_no_modification(self): """Check we don't write modification if not needed""" self.conf.read.return_value = {"enabled": "1", "hostname": self.NAME} z = ZkFarmJoiner(self.client, "/services/db", self.conf) z.loop(3, timeout=self.TIMEOUT) self.conf.reset_mock() self.conf.read.return_value = {"enabled": "0", "hostname": self.NAME} self.client.set("/services/db/%s" % self.IP, json.dumps({ "enabled": "0", "hostname": self.NAME })) z.loop(2, timeout=self.TIMEOUT) self.assertFalse(self.conf.write.called)
def test_zookeeper_modification(self): """Check if local configuration is updated after remote modification""" self.conf.read.return_value = {"enabled": "1", "hostname": self.NAME} z = ZkFarmJoiner(self.client, "/services/db", self.conf) z.loop(3, timeout=self.TIMEOUT) self.conf.reset_mock() self.client.set("/services/db/%s" % self.IP, json.dumps({ "enabled": "0", "hostname": self.NAME })) z.loop(2, timeout=self.TIMEOUT) self.conf.write.assert_called_once_with({ "enabled": "0", "hostname": self.NAME })
def test_updated_handler_called(self): """Test the appropriate handler is called on modification""" self.conf.read.return_value = {"enabled": "1", "hostname": self.NAME} handler = Mock() z = ZkFarmJoiner(self.client, "/services/db", self.conf, updated_handler=handler) z.loop(3, timeout=self.TIMEOUT) handler.reset_mock() self.client.set("/services/db/%s" % self.IP, json.dumps({ "enabled": "0", "hostname": self.NAME })) z.loop(2, timeout=self.TIMEOUT) handler.assert_called_once_with()
def test_no_update_handler_when_no_modification(self): """Check we don't call handler if not needed""" self.conf.read.return_value = {"enabled": "1", "hostname": self.NAME} handler = Mock() z = ZkFarmJoiner(self.client, "/services/db", self.conf, updated_handler=handler) z.loop(3, timeout=self.TIMEOUT) handler.reset_mock() self.conf.read.return_value = {"enabled": "0", "hostname": self.NAME} self.client.set("/services/db/%s" % self.IP, json.dumps({ "enabled": "0", "hostname": self.NAME })) z.loop(2, timeout=self.TIMEOUT) self.assertFalse(handler.called)
def test_disconnect_while_both_remote_and_local_modification(self): self.conf.read.return_value = {"enabled": "1", "hostname": self.NAME} z = ZkFarmJoiner(self.client, "/services/db", self.conf) z.loop(3, timeout=self.TIMEOUT) self.expire_session() z.dispatch(FakeFileEvent()) self.conf.read.return_value = {"enabled": "56", "hostname": self.NAME} self.client.ensure_path( "/services/db/%s" % self.IP) # Disconnected, the path does not exist self.client.set("/services/db/%s" % self.IP, json.dumps({ "enabled": "22", "hostname": self.NAME })) z.loop(10, timeout=self.TIMEOUT) self.assertEqual( json.loads(self.client.get("/services/db/%s" % self.IP)[0]), { "enabled": "56", "hostname": self.NAME })