def serve_networks(): """Start the HIL networking server""" from hil import model, deferred from time import sleep config.setup() server.init() server.register_drivers() server.validate_state() model.init_db() migrations.check_db_schema() # Check if config contains usable sleep_time if (cfg.has_section('network-daemon') and cfg.has_option('network-daemon', 'sleep_time')): try: sleep_time = cfg.getfloat('network-daemon', 'sleep_time') except (ValueError): sys.exit("Error: sleep_time set to non-float value") if sleep_time <= 0 or sleep_time >= 3600: sys.exit("Error: sleep_time not within bounds " "0 < sleep_time < 3600") if sleep_time > 60: logger.warn('sleep_time greater than 1 minute.') else: sleep_time = 2 while True: # Empty the journal until it's empty; then delay so we don't tight # loop. while deferred.apply_networking(): pass sleep(sleep_time)
def run(self): logger = logging.getLogger(__name__) server.init() server.register_drivers() server.validate_state() migrations.check_db_schema() # Check if config contains usable sleep_time if (config.cfg.has_section('network-daemon') and config.cfg.has_option('network-daemon', 'sleep_time')): try: sleep_time = config.cfg.getfloat('network-daemon', 'sleep_time') except (ValueError): sys.exit("Error: sleep_time set to non-float value") if sleep_time <= 0 or sleep_time >= 3600: sys.exit("Error: sleep_time not within bounds " "0 < sleep_time < 3600") if sleep_time > 60: logger.warn('sleep_time greater than 1 minute.') else: sleep_time = 2 while True: # Empty the journal until it's empty; then delay so we don't tight # loop. while deferred.apply_networking(): pass sleep(sleep_time)
def run(self): logger = logging.getLogger(__name__) server.init() server.register_drivers() server.validate_state() migrations.check_db_schema() # Check if config contains usable sleep_time if (config.cfg.has_section('network-daemon') and config.cfg.has_option('network-daemon', 'sleep_time')): try: sleep_time = config.cfg.getfloat( 'network-daemon', 'sleep_time') except (ValueError): sys.exit("Error: sleep_time set to non-float value") if sleep_time <= 0 or sleep_time >= 3600: sys.exit("Error: sleep_time not within bounds " "0 < sleep_time < 3600") if sleep_time > 60: logger.warn('sleep_time greater than 1 minute.') else: sleep_time = 2 while True: # Empty the journal until it's empty; then delay so we don't tight # loop. while deferred.apply_networking(): pass sleep(sleep_time)
def test_db_eq(filename, make_objects, extra_config): """Migrating from a snapshot should create the same objects as a new db. `make_objects` is a function that, when run against the latest version of a schema, will create some set of objects. `filename` is the name of an sql dump of a previous database, whose contents were created with the then-current version of `make_objects`. `extra_config` specifies modifications to the hil.cfg under which the test is run. this is passed to `config_merge`. The test does the following: * Restore the database snapshot and run the migration scripts to update its contents to the current schema. * Create a fresh database according to the current schema, and execute `make_objects`. * Compare the two resulting databases. The test passes if and only if they are the same. """ config_merge(extra_config) load_extensions() server.register_drivers() server.validate_state() def run_fn(f): """Run the function f and return a representation of its db.""" # We can't just do db.drop_all(), since db's metadata won't # necessarily reflect the contents of the database. If there are # tables it doesn't know about, it may raise an exception. with app.app_context(): drop_tables() f() return get_db_state() upgraded = run_fn(lambda: load_dump(filename)) fresh = run_fn(lambda: fresh_create(make_objects)) drop_tables() def censor_nondeterminism(string): """Censor parts of the output whose values are non-deterministic. Certain objects (currently just uuids) are generated non-deterministically, and will thus be different between databases even if everything is working. This function censors the relevant parts of `string`, so that they don't cause the tests to fail. """ hex_re = r'[0-9a-f]' uuid_re = hex_re * 8 + ('-' + hex_re * 4) * 3 + '-' + hex_re * 12 return re.sub(uuid_re, '<<UUID>>', string) differ = difflib.Differ() upgraded = censor_nondeterminism(pformat(upgraded)).split('\n') fresh = censor_nondeterminism(pformat(fresh)).split('\n') assert upgraded == fresh, ("Databases are different!\n\n" + pformat(list(differ.compare(upgraded, fresh))))
def server_init(): server.register_drivers() server.validate_state()
def server_init(): server.register_drivers() server.validate_state()
def server_init(): """Fixture to do server-specific setup.""" server.register_drivers() server.validate_state()
def server_init(): """Fixture to do server-specific setup.""" server.register_drivers() server.validate_state()
def test_db_eq(filename, make_objects, extra_config): """Migrating from a snapshot should create the same objects as a new db. `make_objects` is a function that, when run against the latest version of a schema, will create some set of objects. `filename` is the name of an sql dump of a previous database, whose contents were created with the then-current version of `make_objects`. `extra_config` specifies modifications to the hil.cfg under which the test is run. this is passed to `config_merge`. The test does the following: * Restore the database snapshot and run the migration scripts to update its contents to the current schema. * Create a fresh database according to the current schema, and execute `make_objects`. * Compare the two resulting databases. The test passes if and only if they are the same. """ config_merge(extra_config) load_extensions() server.register_drivers() server.validate_state() def run_fn(f): """Run the function f and return a representation of its db.""" # We can't just do db.drop_all(), since db's metadata won't # necessarily reflect the contents of the database. If there are # tables it doesn't know about, it may raise an exception. with app.app_context(): drop_tables() f() return get_db_state() upgraded = run_fn(lambda: load_dump(filename)) fresh = run_fn(lambda: fresh_create(make_objects)) drop_tables() def censor_nondeterminism(string): """Censor parts of the output whose values are non-deterministic. Certain objects (currently just uuids) are generated non-deterministically, and will thus be different between databases even if everything is working. This function censors the relevant parts of `string`, so that they don't cause the tests to fail. """ hex_re = r'[0-9a-f]' uuid_re = hex_re * 8 + ('-' + hex_re * 4) * 3 + '-' + hex_re * 12 return re.sub(uuid_re, '<<UUID>>', string) differ = difflib.Differ() upgraded = censor_nondeterminism(pformat(upgraded)).split('\n') fresh = censor_nondeterminism(pformat(fresh)).split('\n') assert upgraded == fresh, ( "Databases are different!\n\n" + pformat(list(differ.compare(upgraded, fresh))) )