def testNodeControlCreatesBackups(monkeypatch, tdir, looper, tconf): version = bumpedVersion() stdout = 'teststdout' curr_src_ver = Upgrader.get_src_version() def transform(tool): nodeControlGeneralMonkeypatching(tool, monkeypatch, tdir, stdout) monkeypatch.setattr(tool, '_remove_old_backups', lambda *x: None) def checkBackup(tool): assert os.path.isfile('{}.{}'.format( tool._backup_name(curr_src_ver.release), tool.backup_format)) monkeypatch.setattr( NodeControlUtil, 'get_latest_pkg_version', lambda *x, **y: DebianVersion( version, upstream_cls=src_version_cls()) ) nct = NCT(backup_dir=tdir, backup_target=tdir, transform=transform, config=tconf) try: sendUpgradeMessage(version) looper.run(eventually(checkBackup, nct.tool)) finally: clean_dir(nct.tool.base_dir) nct.stop()
def patch_packet_mgr_output(monkeypatch, pkg_name, pkg_version, new_version): if pkg_name != APP_NAME: node_package = (APP_NAME, '0.0.1') EXT_TOP_PKT_DEPS = [("aa", "1.1.1"), ("bb", "2.2.2")] PACKAGE_MNG_EXT_PTK_OUTPUT = ( "Package: {}\nVersion: {}\n" "Depends: {}, {} (= {}), {} (= {})\n".format( pkg_name, pkg_version, APP_NAME, *EXT_TOP_PKT_DEPS[0], *EXT_TOP_PKT_DEPS[1])) top_level_package = (pkg_name, pkg_version) plenum_package = ('indy-plenum', '0.0.3') top_level_package_with_version = '{}={}'.format(*top_level_package) top_level_package_dep1_with_version = '{}={}'.format( *EXT_TOP_PKT_DEPS[0]) top_level_package_dep2_with_version = '{}={}'.format( *EXT_TOP_PKT_DEPS[1]) node_package_with_version = '{}={}'.format(*node_package) plenum_package_with_version = '{}={}'.format(*plenum_package) mock_info = { top_level_package_with_version: "{}{} (= {}) {} (= {}), {} (= {})".format(randomText(100), *node_package, *EXT_TOP_PKT_DEPS[0], *EXT_TOP_PKT_DEPS[1]), node_package_with_version: '{}{} (= {}){}{}'.format(randomText(100), *plenum_package, randomText(100), randomText(100)), plenum_package_with_version: '{}'.format(randomText(100)), top_level_package_dep1_with_version: '{}{} (= {})'.format(randomText(100), *plenum_package), top_level_package_dep2_with_version: '{}{} (= {})'.format(randomText(100), *node_package) } def mock_get_info_from_package_manager(package): if package.startswith(pkg_name): return mock_info.get(top_level_package_with_version, "") return mock_info.get(package, "") #monkeypatch.setattr(NodeControlUtil, '_get_info_from_package_manager', # lambda x: mock_get_info_from_package_manager(x)) monkeypatch.setattr(NodeControlUtil, '_get_curr_info', lambda *x: PACKAGE_MNG_EXT_PTK_OUTPUT) else: #monkeypatch.setattr( # NodeControlUtil, '_get_info_from_package_manager', # lambda package: "Package: {}\nVersion: {}\n".format(APP_NAME, pkg_version) if package == APP_NAME else "" #) monkeypatch.setattr( NodeControlUtil, '_get_curr_info', lambda *x: "Package: {}\nVersion: {}\n".format( APP_NAME, pkg_version)) monkeypatch.setattr(NodeControlUtil, 'update_package_cache', lambda *x: None) monkeypatch.setattr( NodeControlUtil, 'get_latest_pkg_version', lambda *x, **y: DebianVersion(new_version, upstream_cls=src_version_cls(pkg_name)))
def test_curr_pkg_info(monkeypatch): output = 'Version: 1.2.3\nDepends: aaa (= 1.2.4), bbb (>= 1.2.5), ccc, aaa' expected_deps = ['aaa=1.2.4', 'bbb=1.2.5', 'ccc'] monkeypatch.setattr(NodeControlUtil, 'run_shell_command', lambda *_: output) for pkg_name in [APP_NAME, 'any_package']: upstream_cls = src_version_cls(pkg_name) expected_version = DebianVersion('1.2.3', upstream_cls=upstream_cls) pkg_info = NodeControlUtil.curr_pkg_info(pkg_name) assert expected_version == pkg_info[0] assert isinstance(expected_version, type(pkg_info[0])) assert isinstance(expected_version.upstream, type(pkg_info[0].upstream)) assert expected_deps == pkg_info[1]
def testNodeControlRemovesBackups(monkeypatch, tdir, looper, tconf): version = bumpedVersion() stdout = 'teststdout' curr_src_ver = Upgrader.get_src_version() backupsWereRemoved = m.Value('b', False) def testRemoveOldBackups(tool): assert len(tool._get_backups()) == (tool.backup_num + 1) #looper = Looper(debug=True) 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 def check_backups_files_exists(): assert os.path.exists('{}.{}'.format( nct.tool._backup_name(curr_src_ver.release), nct.tool.backup_format)) monkeypatch.setattr( NodeControlUtil, 'get_latest_pkg_version', lambda *x, **y: DebianVersion(version, upstream_cls=src_version_cls())) nct = NCT(backup_dir=tdir, backup_target=tdir, 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(version) looper.run(eventually(checkOldBackupsRemoved)) looper.run(eventually(check_backups_files_exists)) assert len(nct.tool._get_backups()) == nct.tool.backup_num finally: clean_dir(nct.tool.base_dir) nct.stop()
def testNodeControlPerformsMigrations(monkeypatch, tdir, looper, tconf): version = bumpedVersion() stdout = 'teststdout' migrationFile = 'migrationProof' migrationText = "{} {}".format(releaseVersion(), version) old_call_upgrade_script = None def mock_call_upgrade_script(*args, **kwargs): old_call_upgrade_script(*args, **kwargs) monkeypatch.setattr( NodeControlUtil, '_get_curr_info', lambda *x, **y: ( "Package: {}\nVersion: {}\n".format(APP_NAME, version) ) ) def mock_migrate(curr_src_ver: str, new_src_ver: str): with open(os.path.join(tdir, migrationFile), 'w') as f: f.write("{} {}".format(curr_src_ver, new_src_ver)) def transform(tool): nonlocal old_call_upgrade_script nodeControlGeneralMonkeypatching(tool, monkeypatch, tdir, stdout) monkeypatch.setattr(tool, '_do_migration', mock_migrate) old_call_upgrade_script = getattr(tool, '_call_upgrade_script') monkeypatch.setattr(tool, '_call_upgrade_script', mock_call_upgrade_script) def checkMigration(): with open(os.path.join(tdir, migrationFile)) as f: assert f.read() == migrationText monkeypatch.setattr( NodeControlUtil, 'get_latest_pkg_version', lambda *x, **y: DebianVersion( version, upstream_cls=src_version_cls()) ) nct = NCT(backup_dir=tdir, backup_target=tdir, transform=transform, config=tconf) try: sendUpgradeMessage(version) looper.run(eventually(checkMigration)) finally: nct.stop()
def testNodeControlRestoresFromBackups(monkeypatch, tdir, looper, tconf): version = bumpedVersion() stdout = 'teststdout' backupWasRestored = m.Value('b', False) testFile = 'testFile' original_text = '1' new_text = '2' def testRestoreBackup(tool, src_ver: str): tool._restore_from_backup_test(src_ver) backupWasRestored.value = True def mockMigrate(tool, *args): monkeypatch.setattr(tool, '_do_migration', lambda *args: None) with open(os.path.join(tool.indy_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, '_do_migration', functools.partial(mockMigrate, tool)) monkeypatch.setattr(tool, '_restore_from_backup', functools.partial(testRestoreBackup, tool)) def checkBackupRestored(tool): assert backupWasRestored.value monkeypatch.setattr( NodeControlUtil, 'get_latest_pkg_version', lambda *x, **y: DebianVersion(version, upstream_cls=src_version_cls())) nct = NCT(backup_dir=tdir, backup_target=tdir, transform=transform) try: with open(os.path.join(nct.tool.indy_dir, testFile), 'w') as f: f.write(original_text) sendUpgradeMessage(version) looper.run(eventually(checkBackupRestored, nct.tool)) with open(os.path.join(nct.tool.indy_dir, testFile)) as f: assert original_text == f.read() finally: clean_dir(nct.tool.base_dir) nct.stop()
def pkg_version(pool_upgrade_request): return DebianVersion('1.1.1', upstream_cls=src_version_cls( pool_upgrade_request.operation[PACKAGE]))
' '.join(packages)) def test_get_latest_pkg_version_invalid_args(): pkg_name = 'any_package' with pytest.raises(TypeError) as excinfo: NodeControlUtil.get_latest_pkg_version( pkg_name, upstream=DigitDotVersion('1.2.3'), update_cache=False) assert ("should be instance of {}".format(src_version_cls(pkg_name)) in str(excinfo.value)) @pytest.mark.parametrize('output,expected', [ ('', None), ('Version: 1.2.3\nVersion: 1.2.4', DebianVersion('1.2.4', upstream_cls=src_version_cls('any_package'))), ], ids=lambda s: s.replace('\n', '_').replace(' ', '_')) def test_get_latest_pkg_version(monkeypatch, output, expected): def _f(command, *args, **kwargs): if not output: raise ShellError(1, command) else: return output monkeypatch.setattr(NodeControlUtil, 'run_shell_script_extended', _f) assert expected == NodeControlUtil.get_latest_pkg_version( 'any_package', update_cache=False) def test_get_latest_pkg_version_for_unknown_package():
def test_default_upstream_cls(): assert isinstance(DebianVersion('1.2.3').upstream, GenericVersion)