def testNodeControlRemovesBackups(monkeypatch, tdir, looper):
    msg = 'test'
    stdout = 'teststdout'
    currentVersion = Upgrader.getVersion()
    backupWasRemoved = m.Value('b', False)

    def testRemoveBackup(tool, version):
        backupWasRemoved.value = True
        tool._remove_backup_test(version)

    def transform(tool):
        nodeControlGeneralMonkeypatching(tool, monkeypatch, tdir, stdout)
        tool._remove_backup_test = tool._remove_backup
        monkeypatch.setattr(tool, '_remove_backup', functools.partial(testRemoveBackup, tool))

    def checkBackupRemoved(tool):
        assert backupWasRemoved.value

    nct = NCT(transform = transform)
    try:
        sendUpgradeMessage(msg)
        looper.run(eventually(checkBackupRemoved, nct.tool))
        assert not os.path.exists('{}.{}'.format(nct.tool._backup_name(currentVersion), nct.tool.backup_format))
    finally:
        nct.stop()
def testNodeControlRestoresFromBackups(monkeypatch, tdir, looper):
    msg = 'test'
    stdout = 'teststdout'
    currentVersion = Upgrader.getVersion()
    backupWasRestored = m.Value('b', False)
    testFile = 'testFile'

    def testRestoreBackup(tool, version):
        backupWasRestored.value = True
        tool._restore_from_backup_test(version)

    def mockMigrate(tool):
        with open(os.path.join(tool.sovrin_dir, testFile), 'w') as f:
            f.write('random')
        raise Exception('test')

    def transform(tool):
        nodeControlGeneralMonkeypatching(tool, monkeypatch, tdir, stdout)
        tool._restore_from_backup_test = tool._restore_from_backup
        monkeypatch.setattr(tool, '_migrate', functools.partial(mockMigrate, tool))
        monkeypatch.setattr(tool, '_restore_from_backup', functools.partial(testRestoreBackup, tool))

    def checkBackupRestored(tool):
        assert backupWasRestored.value

    nct = NCT(transform = transform)
    try:
        sendUpgradeMessage(msg)
        looper.run(eventually(checkBackupRestored, nct.tool))
        assert not os.path.exists(os.path.join(nct.tool.sovrin_dir, testFile))
    finally:
        nct.stop()
Example #3
0
def testNodeControlRestoresFromBackups(monkeypatch, tdir, looper):
    msg = 'test'
    stdout = 'teststdout'
    currentVersion = Upgrader.getVersion()
    backupWasRestored = m.Value('b', False)
    testFile = 'testFile'

    def testRestoreBackup(tool, version):
        backupWasRestored.value = True
        tool._restore_from_backup_test(version)

    def mockMigrate(tool):
        with open(os.path.join(tool.sovrin_dir, testFile), 'w') as f:
            f.write('random')
        raise Exception('test')

    def transform(tool):
        nodeControlGeneralMonkeypatching(tool, monkeypatch, tdir, stdout)
        tool._restore_from_backup_test = tool._restore_from_backup
        monkeypatch.setattr(tool, '_migrate',
                            functools.partial(mockMigrate, tool))
        monkeypatch.setattr(tool, '_restore_from_backup',
                            functools.partial(testRestoreBackup, tool))

    def checkBackupRestored(tool):
        assert backupWasRestored.value

    nct = NCT(transform=transform)
    try:
        sendUpgradeMessage(msg)
        looper.run(eventually(checkBackupRestored, nct.tool))
        assert not os.path.exists(os.path.join(nct.tool.sovrin_dir, testFile))
    finally:
        nct.stop()
Example #4
0
def testNodeControlRemovesBackups(monkeypatch, tdir, looper):
    msg = 'test'
    stdout = 'teststdout'
    currentVersion = Upgrader.getVersion()
    backupWasRemoved = m.Value('b', False)

    def testRemoveBackup(tool, version):
        backupWasRemoved.value = True
        tool._remove_backup_test(version)

    def transform(tool):
        nodeControlGeneralMonkeypatching(tool, monkeypatch, tdir, stdout)
        tool._remove_backup_test = tool._remove_backup
        monkeypatch.setattr(tool, '_remove_backup',
                            functools.partial(testRemoveBackup, tool))

    def checkBackupRemoved(tool):
        assert backupWasRemoved.value

    nct = NCT(transform=transform)
    try:
        sendUpgradeMessage(msg)
        looper.run(eventually(checkBackupRemoved, nct.tool))
        assert not os.path.exists('{}.{}'.format(
            nct.tool._backup_name(currentVersion), nct.tool.backup_format))
    finally:
        nct.stop()
def testNodeControlCreatesBackups(monkeypatch, tdir, looper):
    msg = 'test'
    stdout = 'teststdout'
    currentVersion = Upgrader.getVersion()

    def transform(tool):
        nodeControlGeneralMonkeypatching(tool, monkeypatch, tdir, stdout)
        monkeypatch.setattr(tool, '_remove_backup', lambda *x: None)

    def checkBackup(tool):
        assert os.path.isfile('{}.{}'.format(tool._backup_name(currentVersion), tool.backup_format))
    nct = NCT(transform = transform)
    try:
        sendUpgradeMessage(msg)
        looper.run(eventually(checkBackup, nct.tool))
    finally:
        nct.stop()
def testNodeControlReceivesMessages(monkeypatch, looper):
    received = m.list()
    msg = 'test'

    def transform(tool):
        monkeypatch.setattr(tool, '_process_data', received.append)

    def checkMessage():
        assert len(received) == 1
        assert received[0] == composeUpgradeMessage(msg)

    nct = NCT(transform = transform)
    try:
        sendUpgradeMessage(msg)
        looper.run(eventually(checkMessage))
    finally:
        nct.stop()
Example #7
0
def testNodeControlReceivesMessages(monkeypatch, looper):
    received = m.list()
    msg = 'test'

    def transform(tool):
        monkeypatch.setattr(tool, '_process_data', received.append)

    def checkMessage():
        assert len(received) == 1
        assert received[0] == composeUpgradeMessage(msg)

    nct = NCT(transform=transform)
    try:
        sendUpgradeMessage(msg)
        looper.run(eventually(checkMessage))
    finally:
        nct.stop()
Example #8
0
def testNodeControlCreatesBackups(monkeypatch, tdir, looper):
    msg = 'test'
    stdout = 'teststdout'
    currentVersion = Upgrader.getVersion()

    def transform(tool):
        nodeControlGeneralMonkeypatching(tool, monkeypatch, tdir, stdout)
        monkeypatch.setattr(tool, '_remove_backup', lambda *x: None)

    def checkBackup(tool):
        assert os.path.isfile('{}.{}'.format(tool._backup_name(currentVersion),
                                             tool.backup_format))

    nct = NCT(transform=transform)
    try:
        sendUpgradeMessage(msg)
        looper.run(eventually(checkBackup, nct.tool))
    finally:
        nct.stop()
def testNodeControlRestoresFromBackups(monkeypatch, tdir, looper):
    msg = 'test'
    stdout = 'teststdout'
    currentVersion = Upgrader.getVersion()
    backupWasRestored = m.Value('b', False)
    testFile = 'testFile'
    original_text = '1'
    new_text = '2'

    def testRestoreBackup(tool, version):
        tool._restore_from_backup_test(version)
        backupWasRestored.value = True

    def mockMigrate(tool, *args):
        monkeypatch.setattr(tool, '_migrate', lambda *args: None)
        with open(os.path.join(tool.sovrin_dir, testFile), 'w') as f:
            f.write(new_text)
        raise Exception('test')

    def transform(tool):
        nodeControlGeneralMonkeypatching(tool, monkeypatch, tdir, stdout)
        tool._restore_from_backup_test = tool._restore_from_backup
        monkeypatch.setattr(tool, '_migrate',
                            functools.partial(mockMigrate, tool))
        monkeypatch.setattr(tool, '_restore_from_backup',
                            functools.partial(testRestoreBackup, tool))

    def checkBackupRestored(tool):
        assert backupWasRestored.value

    nct = NCT(transform=transform)
    try:
        with open(os.path.join(nct.tool.sovrin_dir, testFile), 'w') as f:
            f.write(original_text)
        sendUpgradeMessage(msg)
        looper.run(eventually(checkBackupRestored, nct.tool))
        with open(os.path.join(nct.tool.sovrin_dir, testFile)) as f:
            assert original_text == f.read()
    finally:
        clean_dir(nct.tool.base_dir)
        nct.stop()
def testNodeControlRemovesBackups(monkeypatch, tdir, looper):
    msg = 'test'
    stdout = 'teststdout'
    currentVersion = Upgrader.getVersion()
    backupsWereRemoved = m.Value('b', False)

    def testRemoveOldBackups(tool):
        assert len(tool._get_backups()) == (tool.backup_num + 1)
        tool._remove_old_backups_test()
        backupsWereRemoved.value = True

    def transform(tool):
        nodeControlGeneralMonkeypatching(tool, monkeypatch, tdir, stdout)
        tool._remove_old_backups_test = tool._remove_old_backups
        monkeypatch.setattr(tool, '_remove_old_backups',
                            functools.partial(testRemoveOldBackups, tool))

    def checkOldBackupsRemoved():
        assert backupsWereRemoved.value

    nct = NCT(transform=transform)
    try:
        assert len(nct.tool._get_backups()) == 0
        for i in range(nct.tool.backup_num):
            file = os.path.join(nct.tool.base_dir,
                                '{}{}'.format(nct.tool.backup_name_prefix, i))
            with open(file, 'w') as f:
                f.write('random')
        assert len(nct.tool._get_backups()) == nct.tool.backup_num
        sendUpgradeMessage(msg)
        looper.run(eventually(checkOldBackupsRemoved))
        assert os.path.exists('{}.{}'.format(
            nct.tool._backup_name(currentVersion), nct.tool.backup_format))
        assert len(nct.tool._get_backups()) == nct.tool.backup_num
    finally:
        clean_dir(nct.tool.base_dir)
        nct.stop()
def testNodeControlPerformsMigrations(monkeypatch, tdir, looper):
    msg = 'test'
    stdout = 'teststdout'
    migrationFile = 'migrationProof'
    migrationText = 'testMigrations'

    def mock_migrate(*args):
        with open(os.path.join(tdir, migrationFile), 'w') as f:
            f.write(migrationText)

    def transform(tool):
        nodeControlGeneralMonkeypatching(tool, monkeypatch, tdir, stdout)
        monkeypatch.setattr(tool, '_migrate', mock_migrate)

    def checkMigration():
        with open(os.path.join(tdir, migrationFile)) as f:
            assert f.read() == migrationText

    nct = NCT(transform = transform)
    try:
        sendUpgradeMessage(msg)
        looper.run(eventually(checkMigration))
    finally:
        nct.stop()
Example #12
0
def testNodeControlPerformsMigrations(monkeypatch, tdir, looper):
    msg = 'test'
    stdout = 'teststdout'
    migrationFile = 'migrationProof'
    migrationText = 'testMigrations'

    def mock_migrate(*args):
        with open(os.path.join(tdir, migrationFile), 'w') as f:
            f.write(migrationText)

    def transform(tool):
        nodeControlGeneralMonkeypatching(tool, monkeypatch, tdir, stdout)
        monkeypatch.setattr(tool, '_migrate', mock_migrate)

    def checkMigration():
        with open(os.path.join(tdir, migrationFile)) as f:
            assert f.read() == migrationText

    nct = NCT(transform=transform)
    try:
        sendUpgradeMessage(msg)
        looper.run(eventually(checkMigration))
    finally:
        nct.stop()