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 haas.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 serve_networks(): """Start the HaaS networking server""" from haas import model, deferred from time import sleep server.init() server.register_drivers() server.validate_state() model.init_db() while True: # Empty the journal until it's empty; then delay so we don't tight # loop. while deferred.apply_networking(): pass sleep(2)
def test_db_eq(filename, extra_config): """Verify that each function in fns creates the same database.""" 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(fresh_create) 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 test_db_eq(filename, extra_config): """Verify that each function in fns creates the same database.""" 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(fresh_create) 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 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 haas.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))) )