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")
def test_consul_backup(self): 'test making backup of Consul key/value store' from utils import get_consul_session from utils import get_records_from_consul from utils import save_records import json # 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) # Now add some data to the key/value store for key, value in test_data.items(): session.kv[key] = value # Now retrieve the records records = get_records_from_consul(session) # Did we get the expected data back? expected_records = '[["key1", 0, "value1"], ["key2", 0, "value2"], ["key3", 0, "value3"]]' self.assertEqual(json.dumps(records), expected_records) # Now test file creation current_dir = os.path.dirname(os.path.realpath(__file__)) backup_file = '%s/test_backup.json' % current_dir save_records(records, backup_file) # Check that the file was created self.assertTrue(os.path.exists(backup_file)) # Now check if its contents are what we expect backup = open(backup_file).read().strip() self.assertEqual(backup, expected_records) # Delete the backup file os.remove(backup_file)
def test_consul_backup(self): 'test making backup of Consul key/value store' from utils import get_consul_session from utils import get_records_from_consul from utils import save_records import json # 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) # Now add some data to the key/value store for key,value in test_data.items(): session.kv[key] = value # Now retrieve the records records = get_records_from_consul(session) # Did we get the expected data back? expected_records = '[["key1", 0, "value1"], ["key2", 0, "value2"], ["key3", 0, "value3"]]' self.assertEqual(json.dumps(records), expected_records) # Now test file creation current_dir = os.path.dirname(os.path.realpath(__file__)) backup_file = '%s/test_backup.json' % current_dir save_records(records, backup_file) # Check that the file was created self.assertTrue(os.path.exists(backup_file)) # Now check if its contents are what we expect backup = open(backup_file).read().strip() self.assertEqual(backup, expected_records) # Delete the backup file os.remove(backup_file)
action = os.environ.get('ACTION') if not action: action = 'backup' # If we need to restore, we need to be able to identify the backup file to be restored # and if this has not been set, we need to exit if action == 'restore': restore_id = os.environ.get('RESTORE_ID') if not restore_id: logging.error('Restore was requested, but no RESTORE_ID was set!') sys.exit(2) # See if we are in "overwrite" mode overwrite = os.environ.get('OVERWRITE',False) # Register the Consul client with the server logging.info("Registering consulate with %s on port %s"%(consul_host, consul_port)) try: session = get_consul_session(consul_host, consul_port) except Exception,e: logging.error("Failed to register consulate: %s"%e) sys.exit(2) # Request status info to check that we established a connection try: leader = session.status.leader() except Exception, e: logging.error("Failed to register consulate: %s"%e) sys.exit(2) # Now take the appropriate action depending on the value of the ACTION variable if action == 'backup': # Get today's date for the time stamp now = str(datetime.datetime.now().date()) # Construct the name of the backup file fname = os.environ.get('BACKUP_FILE','adsabs_consul_kv')
tmp_dir = os.environ.get('TMP_DIR','/tmp') # If no action was specified, the default action is backup action = os.environ.get('ACTION') if not action: action = 'backup' # If we need to restore, we need to be able to identify the backup file to be restored # and if this has not been set, we need to exit if action == 'restore': restore_id = os.environ.get('RESTORE_ID') if not restore_id: logging.error('Restore was requested, but no RESTORE_ID was set!') sys.exit(2) # Register the Consul client with the server logging.info("Registering consulate with %s on port %s"%(consul_host, consul_port)) try: session = get_consul_session(consul_host, consul_port) except Exception,e: logging.error("Failed to register consulate: %s"%e) sys.exit(2) # Request status info to check that we established a connection try: leader = session.status.leader() except Exception, e: logging.error("Failed to register consulate: %s"%e) sys.exit(2) # Now take the appropriate action depending on the value of the ACTION variable if action == 'backup': # Get today's date for the time stamp now = str(datetime.datetime.now().date()) # Construct the name of the backup file backup_file = '%s/adsabs_consul_kv.%s.json' % (tmp_dir,now)