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_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_common_node_disconnect_and_local_modifications(self): """Check that remote modifications take over local modifications for a common node""" self.conf.read.return_value = {"enabled": "1"} z = ZkFarmJoiner(self.client, "/services/db", self.conf, True) z.loop(3, timeout=self.TIMEOUT) self.expire_session() self.conf.read.return_value = {"enabled": "22"} z.dispatch(FakeFileEvent()) z.loop(10, timeout=self.TIMEOUT) self.assertEqual(json.loads(self.client.get("/services/db/common")[0]), {"enabled": "1"})
def test_disconnect_while_local_modification(self): """Test we can disconnect and have a local 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.conf.read.return_value = {"enabled": "22", "hostname": self.NAME} z.dispatch(FakeFileEvent()) z.loop(10, timeout=self.TIMEOUT) self.assertEqual(json.loads(self.client.get("/services/db/%s" % self.IP)[0]), {"enabled": "22", "hostname": self.NAME})
def test_local_modification(self): """Check if ZooKeeper is updated after a local 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.conf.read.return_value = {"enabled": "0", "hostname": self.NAME} z.dispatch(FakeFileEvent()) z.loop(4, timeout=self.TIMEOUT) self.assertFalse(self.conf.write.called) self.assertEqual(json.loads(self.client.get("/services/db/%s" % self.IP)[0]), {"enabled": "0", "hostname": self.NAME})
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})
def test_detect_file_moved(self): """Test if we can detect a file moved into our location.""" self.conf.read.return_value = {"enabled": "56", "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": "57", "hostname": self.NAME} f = FakeFileEvent() f.src_path="/unrelated/path" f.dst_path="/fake/root" z.dispatch(f) z.loop(4, timeout=self.TIMEOUT) self.assertFalse(self.conf.write.called) self.assertEqual(json.loads(self.client.get("/services/db/%s" % self.IP)[0]), {"enabled": "57", "hostname": self.NAME})
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 })