def testScheduleNodeUpgrade(nodeSet): """ Tests that upgrade scheduling works. For that it starts mock control service, schedules upgrade for near future and then checks that service received notification. """ from sovrin_common.config_util import getConfig config = getConfig() loop = asyncio.get_event_loop() server, indicator = loop.run_until_complete( _createServer(host=config.controlServiceHost, port=config.controlServicePort)) indicator.add_done_callback(_stopServer(server)) node = nodeSet[0] # ATTENTION! nodeId and ledger must not be None, but there # we do not call methods that use them, so we can pass None # We do it because node from nodeSet is some testable object, not real # node, so it has no nodeId and ledger that we can use upgrader = Upgrader(nodeId=None, nodeName=None, dataDir=node.dataLocation, config=config, ledger=None) upgrader._callUpgradeAgent(time.time(), "1.2", failTimeout=1000, upgrade_id=None) result = loop.run_until_complete(eventuallySoon(_checkFuture(indicator))) expectedResult = UpgradeMessage(version) assert result == expectedResult.toJson()
def testVersions(): assert Upgrader.isVersionHigher('0.0.5', '0.0.6') assert not Upgrader.isVersionHigher('0.0.9', '0.0.8') assert Upgrader.isVersionHigher('0.0.9', '0.1.0') assert Upgrader.isVersionHigher('0.20.30', '1.0.0') versions = ['0.0.1', '0.10.11', '0.0.2', '1.9.0', '9.10.0', '9.1.0'] assert Upgrader.versionsDescOrder(versions) == [ '9.10.0', '9.1.0', '1.9.0', '0.10.11', '0.0.2', '0.0.1' ]
def _compare_migration_scripts(migration1, migration2): migration_original_version1, migration_new_version1 = _get_migration_versions( migration1) migration_original_version2, migration_new_version2 = _get_migration_versions( migration2) if Upgrader.compareVersions(migration_original_version2, migration_original_version1) == 0: return Upgrader.compareVersions(migration_new_version2, migration_new_version1) return Upgrader.compareVersions(migration_original_version2, migration_original_version1)
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 getUpgrader(self): return Upgrader(self.id, self.name, self.dataLocation, self.config, self.configLedger, upgradeFailedCallback=self.postConfigLedgerCaughtUp)
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()
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()
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 _get_relevant_migrations(migration_scripts, current_version, new_version): relevant_migrations = [] for migration in migration_scripts: migration_original_version, migration_new_version = _get_migration_versions( migration) if not migration_original_version or not migration_new_version: continue if Upgrader.compareVersions(current_version, new_version) >= 0: if Upgrader.compareVersions(migration_new_version, migration_original_version) > 0: continue if Upgrader.compareVersions(migration_original_version, current_version) <= 0 \ and Upgrader.compareVersions(migration_new_version, new_version) >= 0: relevant_migrations.append(migration) else: if Upgrader.compareVersions(migration_original_version, migration_new_version) > 0: continue if Upgrader.compareVersions(migration_original_version, current_version) >= 0 \ and Upgrader.compareVersions(migration_new_version, new_version) <= 0: relevant_migrations.append(migration) relevant_migrations = sorted( relevant_migrations, key=cmp_to_key(_compare_migration_scripts)) return relevant_migrations
def comparator_test(lower_versrion, higher_version): assert Upgrader.compareVersions(lower_versrion, higher_version) == 1 assert Upgrader.compareVersions(higher_version, lower_versrion) == -1 assert Upgrader.compareVersions(higher_version, higher_version) == 0 assert not Upgrader.is_version_upgradable(higher_version, higher_version) assert Upgrader.is_version_upgradable(higher_version, higher_version, reinstall=True) assert Upgrader.is_version_upgradable(lower_versrion, higher_version) assert Upgrader.is_version_upgradable(higher_version, lower_versrion)
def _upgrade(self, new_version, migrate=True, rollback=True): try: current_version = Upgrader.getVersion() logger.info('Trying to upgrade from {} to {}'.format(current_version, new_version)) self._call_upgrade_script(new_version) if migrate: self._do_migration(current_version, new_version) self._call_restart_node_script() except Exception as e: logger.error("Unexpected error in _upgrade {}, trying to rollback to the previous version {}".format(e, current_version)) if rollback: self._upgrade(current_version, rollback=False)
def testVersions(): assert Upgrader.isVersionHigher('0.0.5', '0.0.6') assert not Upgrader.isVersionHigher('0.0.9', '0.0.8') assert Upgrader.isVersionHigher('0.0.9', '0.1.0') assert Upgrader.isVersionHigher('0.20.30', '1.0.0') assert Upgrader.isVersionHigher('1.3.19', '1.3.20') versions = ['0.0.1', '0.10.11', '0.0.10', '0.0.2', '1.9.0', '9.10.0', '9.1.0'] assert Upgrader.versionsDescOrder(versions) == \ ['9.10.0', '9.1.0', '1.9.0', '0.10.11', '0.0.10', '0.0.2', '0.0.1']
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 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 getUpgrader(self): return Upgrader(self.id, self.config, self.dataLocation, self.configLedger)
def codeVersion(): return Upgrader.getVersion()