def test_consul_restore(self): 'test if we can restore a Consul key/value store from backup' from utils import get_consul_session from utils import get_records_from_consul from utils import consul_restore_from_backup # File from which to restore backup current_dir = os.path.dirname(os.path.realpath(__file__)) backup_file = '%s/adsabs_consul_kv.2015-10-21.json' % current_dir # and check that it really exists self.assertTrue(os.path.exists(backup_file)) # Get session session = get_consul_session('localhost', '8500') # Did we get a proper Consul session object back? expected = 'Consul' class_name = session.__class__.__name__ self.assertEqual(class_name, expected) # Assuming we successfully connected to the Consul cluster, # we should be able to ask for "peers" peers = session.status.peers() # which should be a list self.assertTrue(isinstance(peers, list)) # of length 3 self.assertEqual(len(peers), 3) # Restore from backup and then test querying URL consul_restore_from_backup(session, backup_file) # Get the records from Consul records = get_records_from_consul(session) # and check whether they are what we expect expected_records = '[["key1", 0, "value1"], ["key2", 0, "value2"], ["key3", 0, "value3"]]' self.assertEqual(json.dumps(records), expected_records)
def test_consul_restore(self): 'test if we can restore a Consul key/value store from backup' from utils import get_consul_session from utils import get_records_from_consul from utils import consul_restore_from_backup # File from which to restore backup current_dir = os.path.dirname(os.path.realpath(__file__)) backup_file = '%s/adsabs_consul_kv.2015-10-21.json' % current_dir # and check that it really exists self.assertTrue(os.path.exists(backup_file)) # Get session session = get_consul_session('localhost', '8500') # Did we get a proper Consul session object back? expected = 'Consul' class_name = session.__class__.__name__ self.assertEqual(class_name, expected) # Assuming we successfully connected to the Consul cluster, # we should be able to ask for "peers" peers = session.status.peers() # which should be a list self.assertTrue(isinstance(peers, list)) # of length 3 self.assertEqual(len(peers), 3) # Restore from backup and then test querying URL consul_restore_from_backup(session, backup_file, False) # Get the records from Consul records = get_records_from_consul(session) # and check whether they are what we expect expected_records = '[["key1", 0, "value1"], ["key2", 0, "value2"], ["key3", 0, "value3"]]' self.assertEqual(json.dumps(records), expected_records) # Now we "accidentally" change one of the key/value pairs in the store session.kv.set("key1","oops") # First check that this actually happened self.assertEqual(session.kv.get("key1"), "oops") # Now perform a restore without overwriting records consul_restore_from_backup(session, backup_file, False) # The "accidental" change should still be there self.assertEqual(session.kv.get("key1"), "oops") # Now perform the restore, allowing overwriting records consul_restore_from_backup(session, backup_file, True) # Now the value from the backup should have overwritten the "accidental" one self.assertEqual(session.kv.get("key1"), "value1")