Example #1
0
 def setUpClass(cls):
     cls.connection = helper.setup_connection()
     cls.cursor = cls.connection.cursor()
def main():
    helper.require_confirm_or_user_input('Test backup and restore logic')

    # Run migrations
    sys.path.append('../src')
    import run_migrations
    run_migrations.MIGRATIONS_PATH = os.path.join(
        '../src/', run_migrations.MIGRATIONS_PATH)
    run_migrations.main(migrations_dir='../src/migrations')

    name = secrets.token_urlsafe(8)
    print(f'Creating test user {name}')

    conn = helper.setup_connection()
    cursor = conn.cursor()
    users = Table('users')
    cursor.execute(
        Query.into(users).columns('username').insert(
            Parameter('%s')).get_sql(), (name, ))
    conn.commit()

    print('Initiating backup...')
    import create_backup
    create_backup.main(args=[])

    print('Initiating download...')
    import download_backup
    download_backup.main(args=['--meta', 'uploaded.json'])

    print('Verifying uploaded matches downloaded...')
    if not filecmp.cmp('uploaded.dump', 'downloaded.dump'):
        print('Files are different!')
        sys.exit(1)

    print('Deleting the test user...')
    cursor.execute(
        Query.from_(users).where(
            users.username == Parameter('%s')).delete().get_sql(), (name, ))
    conn.commit()

    print('Verifying the user does not exist')
    if _exists(conn, cursor, users, name):
        print('User exists!')
        sys.exit(1)

    print('Disconnecting...')
    cursor.close()
    conn.close()
    cursor = None
    conn = None

    print('Initiating restore...')
    import restore_backup
    restore_backup.main(args=['--confirm', 'downloaded.dump'])

    print('Reconnecting...')
    conn = helper.setup_connection()
    cursor = conn.cursor()

    print('Verifying the user does exist')
    if not _exists(conn, cursor, users, name):
        print('User does not exist!')
        sys.exit(1)

    print('All done')
 def setUp(self):
     self.connection = helper.setup_connection()
     self.cursor = self.connection.cursor()
Example #4
0
def main():
    helper.require_confirm_or_user_input('Run migration tests')

    print('Loading migrations...')
    sys.path.append('../src')
    files = sorted(os.listdir('../src/migrations'))
    files = [f[:-3] for f in files if f.endswith('.py')]
    print('Loading all the modules to verify...')
    for f in files:
        mod = importlib.import_module(f'migrations.{f}')
        if not hasattr(mod, 'up'):
            print(f'Module {mod} is missing the up function!')
            sys.exit(1)
        down_test_mod = importlib.import_module(f'migrations.{f}_down')
        if not hasattr(down_test_mod, 'DownTest'):
            print(f'Module {mod} down test missing class DownTest')
            sys.exit(1)
        up_test_mod = importlib.import_module(f'migrations.{f}_up')
        if not hasattr(up_test_mod, 'UpTest'):
            print(f'Module {mod} up test missing class UpTest')
            sys.exit(1)

    conn = helper.setup_connection()
    cursor = conn.cursor()
    print('Dropping all tables...')
    drop_all_tables(conn)
    print('Success!')
    print()
    runner = unittest.TextTestRunner()
    for f in files:
        mod = importlib.import_module(f'migrations.{f}')
        up_test_mod = importlib.import_module(f'migrations.{f}_up')
        down_test_mod = importlib.import_module(f'migrations.{f}_down')

        print(f'Running down test for {f}')
        result = run_tests(runner, down_test_mod.DownTest)
        if result.failures or result.errors:
            sys.exit(1)
        print(f'Applying migration {f}')
        mod.up(conn, cursor)
        conn.commit()
        print(f'Running up test for {f}')
        result = run_tests(runner, up_test_mod.UpTest)
        if result.failures or result.errors:
            sys.exit(1)
        if hasattr(mod, 'down'):
            print(f'Rolling back migration {f}')
            mod.down(conn, cursor)
            conn.commit()
            print(f'Running down test for {f}')
            result = run_tests(runner, down_test_mod.DownTest)
            if result.failures or result.errors:
                sys.exit(1)
            print(f'Reapplying migration {f}')
            mod.up(conn, cursor)
            conn.commit()
            print(f'Running up test for {f}')
            result = run_tests(runner, up_test_mod.UpTest)
            if result.failures or result.errors:
                sys.exit(1)

    print()
    print('All migrations completed successfully!')
    print('Dropping tables...')
    drop_all_tables(conn)
    conn.commit()
    print('All done')
    conn.close()
 def setUpClass(cls):
     cls.connection = helper.setup_connection()
     cls.cursor = cls.connection.cursor()
     cls.apps = Table('log_applications')
     cls.idens = Table('log_identifiers')
     cls.events = Table('log_events')