def test_repo_setfilters(self, shell): """ Test do_repo_setfilters with wrong filters syntax :param shell: :return: """ shell.help_repo_setfilters = MagicMock() shell.client.channel.software.setRepoFilters = MagicMock() mprint = MagicMock() logger = MagicMock() with patch("spacecmd.repo.print", mprint) as prn, \ patch("spacecmd.repo.logging", logger) as lgr: out = spacecmd.repo.do_repo_setfilters(shell, "repo +emacs -vim") assert out is 0 assert not mprint.called assert not shell.help_repo_setfilters.called assert not logger.error.called assert shell.client.channel.software.setRepoFilters.call_count == 1 assert shell.client.channel.software.setRepoFilters.called assert_args_expect( shell.client.channel.software.setRepoFilters.call_args_list, [((shell.session, 'repo', [{ 'filter': 'emacs', 'flag': '+' }, { 'filter': 'vim', 'flag': '-' }]), {})])
def test_errata_publish_no_interactive(self, shell): """ Test do_errata_publish publish to channel without interactive mode :param shell: :return: """ shell.help_errata_publish = MagicMock() shell.expand_errata = MagicMock(return_value=["one", "two", "three"]) shell.user_confirm = MagicMock() shell.client.errata.publish = MagicMock() shell.options.yes = True mprint = MagicMock() logger = MagicMock() with patch("spacecmd.errata.print", mprint) as prt, \ patch("spacecmd.errata.logging", logger) as lgr: spacecmd.errata.do_errata_publish(shell, "CVE-1 base_channel") assert not logger.warning.called assert not shell.help_errata_publish.called assert not shell.user_confirm.called assert shell.client.errata.publish.called assert mprint.called assert shell.expand_errata.called assert_expect(mprint.call_args_list, 'one\nthree\ntwo') assert_args_expect(shell.client.errata.publish.call_args_list, [((shell.session, 'one', ['base_channel']), {}), ((shell.session, 'two', ['base_channel']), {}), ((shell.session, 'three', ['base_channel']), {})])
def test_kickstart_delete_some_invalid_profile(self, shell): """ Test do_kickstart_delete invalid profile (not found). :param shell: :return: """ shell.do_kickstart_list = MagicMock(return_value=[ "first_profile", "second_profile", "third_profile" ]) logger = MagicMock() with patch("spacecmd.kickstart.logging", logger) as lgr: spacecmd.kickstart.do_kickstart_delete( shell, "fourth_profile zero_profile first_profile second_profile") assert not shell.client.kickstart.deleteProfile.called assert not shell.help_kickstart_delete.called assert logger.error.called assert shell.do_kickstart_list.called assert logger.debug.called assert_expect(logger.debug.call_args_list, "Got labels to delete of ['first_profile', 'second_profile']") assert_args_expect(logger.error.call_args_list, [(('The following kickstart labels are invalid:', 'fourth_profile, zero_profile'), {})])
def test_kickstart_clone_interactive_wrong_profile_entered(self, shell): """ Test do_kickstart_clone interactive. Wrong profile has been entered. :param shell: :return: """ mprint = MagicMock() logger = MagicMock() prompter = MagicMock(side_effect=[ "posix_compliance_problem", "POSIX"]) shell.do_kickstart_list = MagicMock(return_value=[ "default_kickstart_profile", "some_other_profile"]) with patch("spacecmd.kickstart.print", mprint) as prt, \ patch("spacecmd.kickstart.logging", logger) as lgr, \ patch("spacecmd.kickstart.prompt_user", prompter) as pmt: spacecmd.kickstart.do_kickstart_clone(shell, "") assert not shell.client.kickstart.cloneProfile.called assert mprint.called assert prompter.called assert logger.error.called assert_expect(logger.error.call_args_list, "Kickstart profile you've entered was not found") assert_list_args_expect(mprint.call_args_list, ['', 'Kickstart Profiles', '------------------', 'default_kickstart_profile\nsome_other_profile', '']) assert_args_expect(prompter.call_args_list, [(('Original Profile:',), {"noblank": True}), (('Cloned Profile:',), {"noblank": True})])
def test_kickstart_delete_profile_interactive(self, shell): """ Test do_kickstart_delete profile, yes=false. Should start interactive confirmation prompt. :param shell: :return: """ shell.options.yes = False shell.user_confirm = MagicMock(return_value=True) shell.do_kickstart_list = MagicMock(return_value=[ "first_profile", "second_profile", "third_profile" ]) logger = MagicMock() with patch("spacecmd.kickstart.logging", logger) as lgr: spacecmd.kickstart.do_kickstart_delete( shell, "first_profile second_profile") assert not shell.help_kickstart_delete.called assert not logger.error.called assert shell.user_confirm.called assert shell.client.kickstart.deleteProfile.called assert shell.do_kickstart_list.called assert logger.debug.called assert_args_expect(shell.client.kickstart.deleteProfile.call_args_list, [((shell.session, "first_profile"), {}), ((shell.session, "second_profile"), {})])
def test_kickstart_delete_invalid_profile(self, shell): """ Test do_kickstart_delete invalid profile (not found). :param shell: :return: """ shell.do_kickstart_list = MagicMock(return_value=[ "first_profile", "second_profile", "third_profile" ]) logger = MagicMock() with patch("spacecmd.kickstart.logging", logger) as lgr: spacecmd.kickstart.do_kickstart_delete(shell, "fourth_profile") assert not shell.client.kickstart.deleteProfile.called assert logger.error.called assert logger.debug.called assert shell.do_kickstart_list.called assert shell.help_kickstart_delete.called assert_expect(logger.error.call_args_list, 'No valid kickstart labels passed as arguments!') assert_expect(logger.debug.call_args_list, 'Got labels to delete of []') assert_args_expect(shell.do_kickstart_list.call_args_list, [(('', True), {})])
def test_repo_updatessl_non_interactive(self, shell): """ Test do_repo_updatessl non-interactive. :param shell: :return: """ shell.help_repo_rename = MagicMock() shell.client.channel.software.updateRepoSsl = MagicMock() shell.do_repo_list = MagicMock() mprint = MagicMock() logger = MagicMock() prompter = MagicMock(return_value="") with patch("spacecmd.repo.print", mprint) as prn, \ patch("spacecmd.repo.prompt_user", prompter) as prn, \ patch("spacecmd.repo.logging", logger) as lgr: out = spacecmd.repo.do_repo_updatessl( shell, "--name name --ca ca --cert cert --key key") assert out is 0 assert not mprint.called assert not logger.error.called assert shell.client.channel.software.updateRepoSsl.called assert_args_expect( shell.client.channel.software.updateRepoSsl.call_args_list, [((shell.session, "name", "ca", "cert", "key"), {})])
def test_user_create_no_password_with_pam(self, shell): """ Test do_user_create, password should be ignored if user opted for PAM :param shell: :return: """ shell.client.user.create = MagicMock() shell.user_confirm = MagicMock(return_value=True) logger = MagicMock() getps = MagicMock(return_value="1234567890") prompter = MagicMock(side_effect=Exception("Should not happen")) with patch("spacecmd.user.logging", logger) as lgr, \ patch("spacecmd.user.prompt_user", prompter) as pmt, \ patch("spacecmd.user.getpass", getps) as gpw: spacecmd.user.do_user_create( shell, "-u lksw -f Luke -l Skywalker --pam " "-e [email protected] -p 123123123") assert not logger.error.called assert shell.client.user.create.called assert logger.warning.called assert_expect(logger.warning.call_args_list, "Note: password was ignored due to PAM mode") assert_args_expect(shell.client.user.create.call_args_list, [((shell.session, 'lksw', '', 'Luke', 'Skywalker', '*****@*****.**', 1), {})])
def test_kickstart_clone_args(self, shell): """ Test do_kickstart_clone with args. :param shell: :return: """ mprint = MagicMock() logger = MagicMock() prompter = MagicMock() shell.do_kickstart_list = MagicMock(return_value=[ "default_kickstart_profile", "some_other_profile"]) name, clone = "default_kickstart_profile", "new_default_profile" with patch("spacecmd.kickstart.print", mprint) as prt, \ patch("spacecmd.kickstart.logging", logger) as lgr, \ patch("spacecmd.kickstart.prompt_user", prompter) as pmt: spacecmd.kickstart.do_kickstart_clone( shell, "-n {} -c {}".format(name, clone)) assert not prompter.called assert not mprint.called assert not logger.error.called assert shell.client.kickstart.cloneProfile.called assert_args_expect(shell.client.kickstart.cloneProfile.call_args_list, [((shell.session, name, clone), {})])
def test_org_addtrust_no_src_org(self, shell): """ Test do_org_addtrust, source org not found :param shell: :return: """ shell.help_org_addtrust = MagicMock() shell.get_org_id = MagicMock(side_effect=[None, 0]) shell.client.org.trusts.addTrust = MagicMock() logger = MagicMock() mprint = MagicMock() with patch("spacecmd.org.print", mprint) as prn, \ patch("spacecmd.org.logging", logger) as lgr: spacecmd.org.do_org_addtrust(shell, "trust me") assert not shell.client.org.trusts.addTrust.called assert not shell.help_org_addtrust.called assert shell.get_org_id.called assert mprint.called assert logger.warning.called assert_expect(mprint.call_args_list, "Organisation 'trust' was not found") assert_args_expect(logger.warning.call_args_list, [(( 'No organisation found for the name %s', 'trust', ), {})])
def test_org_trustdetails_no_org_found(self, shell): """ Test for do_org_trustdetails no org found. :param shell: :return: """ shell.help_org_trustdetails = MagicMock() shell.get_org_id = MagicMock(return_value=None) shell.client.org.trusts.getDetails = MagicMock() shell.client.org.trusts.listChannelsConsumed = MagicMock() shell.client.org.trusts.listChannelsProvided = MagicMock() logger = MagicMock() mprint = MagicMock() with patch("spacecmd.org.print", mprint) as prn, \ patch("spacecmd.org.logging", logger) as lgr: spacecmd.org.do_org_trustdetails(shell, "notfound") assert not shell.client.org.trusts.getDetails.called assert not shell.client.org.trusts.listChannelsConsumed.called assert not shell.client.org.trusts.listChannelsProvided.called assert not shell.help_org_trustdetails.called assert shell.get_org_id.called assert mprint.called assert logger.warning.called assert_expect(mprint.call_args_list, "Trusted organisation 'notfound' was not found") assert_args_expect(logger.warning.call_args_list, [ (('No trusted organisation found for the name %s', 'notfound'), {}) ])
def test_save_package_caches(self, shell): """ Test saving package caches. :param shell: :return: """ savecache = MagicMock() shell.all_packages = {"emacs-41-1": [42]} shell.all_packages_short = {"emacs": ""} shell.all_packages_by_id = {42: "emacs-41-1"} shell.packages_short_cache_file = "/tmp/psc.f" shell.packages_long_cache_file = "/tmp/plc.f" shell.packages_by_id_cache_file = "/tmp/bic.f" tst = datetime.datetime(2019, 1, 1, 0, 0) shell.package_cache_expire = tst with patch("spacecmd.misc.save_cache", savecache) as savc: spacecmd.misc.save_package_caches(shell) assert shell.package_cache_expire == tst assert_args_expect(savecache.call_args_list, [ (('/tmp/psc.f', { 'emacs': '' }, tst), {}), (('/tmp/plc.f', { 'emacs-41-1': [42] }, tst), {}), (('/tmp/bic.f', { 42: 'emacs-41-1' }, tst), {}), ])
def test_repo_create_interactive(self, shell): """ Test do_repo_create interactive. :param shell: :return: """ shell.client.channel.software.createRepo = MagicMock() mprint = MagicMock() logger = MagicMock() prompter = MagicMock(side_effect=[ "name", "http://something", "type", "ca", "cert", "key" ]) with patch("spacecmd.repo.print", mprint) as prn, \ patch("spacecmd.repo.prompt_user", prompter) as prn, \ patch("spacecmd.repo.logging", logger) as lgr: out = spacecmd.repo.do_repo_create(shell, "") assert out is 0 assert not mprint.called assert shell.client.channel.software.createRepo.called assert not logger.error.called assert_args_expect( shell.client.channel.software.createRepo.call_args_list, [((shell.session, 'name', 'type', 'http://something', 'ca', 'cert', 'key'), {})])
def test_org_listusers_no_org(self, shell): """ Test do_org_listusers where org was not found. :param shell: :return: """ shell.help_org_listusers = MagicMock() shell.client.org.listUsers = MagicMock() shell.get_org_id = MagicMock(return_value=None) logger = MagicMock() mprint = MagicMock() with patch("spacecmd.org.print", mprint) as prn, \ patch("spacecmd.org.logging", logger) as lgr: spacecmd.org.do_org_listusers(shell, "foo") assert not shell.client.org.listUsers.called assert not shell.help_org_listusers.called assert shell.get_org_id.called assert mprint.called assert logger.warning.called assert_expect(mprint.call_args_list, "Organisation 'foo' was not found") assert_args_expect( logger.warning.call_args_list, [(('No organisation found for the name %s', 'foo'), {})])
def test_get_system_id_handle_duplicates(self, shell): """ Test getting system ID with duplicates. :param shell: :return: """ shell.all_systems = { 100100: "douchebox", 100200: "sloppy", 100300: "douchebox" } logger = MagicMock() with patch("spacecmd.misc.logging", logger) as lgr: assert spacecmd.misc.get_system_id(shell, "douchebox") == 0 assert_args_expect( logger.warning.call_args_list, [(('Duplicate system profile names found!', ), {}), (('Please reference systems by ID or resolve the', ), {}), (("underlying issue with 'system_delete' or 'system_rename'", ), {}), (('', ), {}), (('douchebox = 100100, 100300', ), {})]) assert logger.warning.called
def test_repo_create_non_interactive_type_required(self, shell): """ Test do_repo_create non-interactive, type is missing :param shell: :return: """ shell.client.channel.software.createRepo = MagicMock() mprint = MagicMock() logger = MagicMock() prompter = MagicMock(return_value="") with patch("spacecmd.repo.print", mprint) as prn, \ patch("spacecmd.repo.prompt_user", prompter) as prn, \ patch("spacecmd.repo.logging", logger) as lgr: out = spacecmd.repo.do_repo_create( shell, "-n name -u http://something " "--ca ca --cert cert --key key") assert out is 0 assert not mprint.called assert not logger.error.called assert shell.client.channel.software.createRepo.called assert_args_expect( shell.client.channel.software.createRepo.call_args_list, [((shell.session, 'name', 'yum', 'http://something', 'ca', 'cert', 'key'), {})])
def test_user_create_args(self, shell): """ Test do_user_create parameters/arguments mode. :param shell: :return: """ shell.client.user.create = MagicMock() shell.user_confirm = MagicMock(return_value=True) logger = MagicMock() getps = MagicMock(return_value="1234567890") prompter = MagicMock(side_effect=Exception("Should not happen")) with patch("spacecmd.user.logging", logger) as lgr, \ patch("spacecmd.user.prompt_user", prompter) as pmt, \ patch("spacecmd.user.getpass", getps) as gpw: spacecmd.user.do_user_create( shell, "-u lksw -f Luke -l Skywalker " "-e [email protected] -p 1234567890") assert shell.client.user.create.called assert not logger.error.called assert not logger.warning.called assert_args_expect(shell.client.user.create.call_args_list, [((shell.session, 'lksw', '1234567890', 'Luke', 'Skywalker', '*****@*****.**', 0), {})])
def test_org_removetrust_no_dst(self, shell): """ Test for do_org_removetrust destination org not found. :param shell: :return: """ shell.help_org_removetrust = MagicMock() shell.get_org_id = MagicMock(side_effect=[0, None]) shell.client.org.trusts.listSystemsAffected = MagicMock() logger = MagicMock() mprint = MagicMock() with patch("spacecmd.org.print", mprint) as prn, \ patch("spacecmd.org.logging", logger) as lgr: spacecmd.org.do_org_removetrust(shell, "trust bad-guys") assert not shell.client.org.trusts.removeTrust.called assert not shell.help_org_removetrust.called assert shell.get_org_id.called assert logger.warning.called assert mprint.called assert_expect(mprint.call_args_list, "Organisation 'bad-guys' to trust for, was not found") assert_args_expect(logger.warning.call_args_list, [ (('No trust organisation found for the name %s', 'bad-guys'), {}) ])
def test_group_addsystems(self, shell): """ Test do_group_addsystems with API call to find systems. :param shell: :return: """ shell.help_group_addsystems = MagicMock() shell.get_system_id = MagicMock(side_effect=["1000010000", "1000010001"]) shell.expand_systems = MagicMock(return_value=["one", "two"]) shell.client.systemgroup.addOrRemoveSystems = MagicMock() shell.ssm.keys = MagicMock() mprint = MagicMock() logger = MagicMock() with patch("spacecmd.group.print", mprint) as prn, \ patch("spacecmd.group.logging", logger) as lgr: spacecmd.group.do_group_addsystems(shell, "groupname something*") assert not mprint.called assert not logger.error.called assert not shell.help_group_addsystems.called assert not shell.ssm.keys.called assert shell.get_system_id.called assert shell.client.systemgroup.addOrRemoveSystems.called assert shell.expand_systems.called assert_args_expect(shell.client.systemgroup.addOrRemoveSystems.call_args_list, [((shell.session, 'groupname', ['1000010000', '1000010001'], True), {})])
def test_errata_details_erratum_failure(self, shell): """ Test do_errata_details erratum failure. :param shell: :return: """ shell.help_errata_details = MagicMock() shell.client.errata.getDetails = MagicMock( side_effect=xmlrpclib.Fault(faultCode=42, faultString="Kaboom!")) shell.client.errata.listPackages = MagicMock() shell.client.errata.listAffectedSystems = MagicMock() shell.client.errata.listCves = MagicMock() shell.client.errata.applicableToChannels = MagicMock() shell.expand_errata = MagicMock(return_value=["cve-one", "cve-two"]) mprint = MagicMock() logger = MagicMock() with patch("spacecmd.errata.print", mprint) as prt, \ patch("spacecmd.errata.logging", logger) as lgr: spacecmd.errata.do_errata_details(shell, "cve*") assert shell.client.errata.getDetails.called assert not shell.client.errata.listPackages.called assert not shell.client.errata.listAffectedSystems.called assert not shell.client.errata.listCves.called assert not shell.client.errata.applicableToChannels.called assert not mprint.called assert logger.warning.called assert not shell.help_errata_details.called assert_args_expect(logger.warning.call_args_list, [ (("cve-one is not a valid erratum", ), {}), (("cve-two is not a valid erratum", ), {}), ])
def test_group_removesystems_nossm_sys(self, shell): """ Test do_group_removesystems with filters and found systems. :param shell: :return: """ shell.help_group_removesystems = MagicMock() shell.get_system_id = MagicMock(side_effect=["1000010000", "1000010001"]) shell.expand_systems = MagicMock(return_value=["one", "two"]) shell.client.systemgroup.addOrRemoveSystems = MagicMock() shell.ssm.keys = MagicMock() shell.user_confirm = MagicMock(return_value=True) mprint = MagicMock() logger = MagicMock() with patch("spacecmd.group.print", mprint) as prn, \ patch("spacecmd.group.logging", logger) as lgr: spacecmd.group.do_group_removesystems(shell, "somegroup somesystem") assert not logger.error.called assert not shell.help_group_removesystems.called assert not shell.ssm.keys.called assert shell.get_system_id.called assert shell.user_confirm.called assert mprint.called assert shell.expand_systems.called assert shell.client.systemgroup.addOrRemoveSystems.called assert_args_expect(shell.client.systemgroup.addOrRemoveSystems.call_args_list, [((shell.session, 'somegroup', ['1000010000', '1000010001'], False), {})]) assert_list_args_expect(mprint.call_args_list, ["Systems", "-------", "one\ntwo"])
def test_group_backup_all_group_list(self, shell): """ Test do_group_backup with all groups lookup :param shell: :return: """ def exp_user(path): """ Fake expand user :param path: :return: """ return os.path.join("/opt/spacecmd", path.replace("~", "").strip("/")) shell.help_group_backup = MagicMock() shell.do_group_list = MagicMock(return_value=["group-a", "group-b"]) shell.client.systemgroup.getDetails = MagicMock( side_effect=[ {"description": "Group A description"}, {"description": "Group B description"}, ] ) mprint = MagicMock() logger = MagicMock() opener = MagicMock() _open = MagicMock(return_value=opener) _datetime = MagicMock() _datetime.now = MagicMock(return_value=datetime.datetime(2019, 1, 1)) with patch("spacecmd.group.print", mprint) as prn, \ patch("spacecmd.group.logging", logger) as lgr, \ patch("spacecmd.group.os.path.expanduser", exp_user) as exu, \ patch("spacecmd.group.open", _open) as opr, \ patch("spacecmd.group.datetime", _datetime) as dtm: spacecmd.group.do_group_backup(shell, "ALL") assert not logger.called assert not shell.help_group_backup.called assert shell.do_group_list.called assert shell.client.systemgroup.getDetails.called assert mprint.called assert opener.write.called assert opener.close.called assert_list_args_expect(mprint.call_args_list, ['Backup Group: group-a', 'Output File: /opt/spacecmd/spacecmd-backup/group/2019-01-01/group-a', 'Backup Group: group-b', 'Output File: /opt/spacecmd/spacecmd-backup/group/2019-01-01/group-b' ]) assert_list_args_expect(opener.write.call_args_list, ["Group A description", "Group B description"]) assert_args_expect(_open.call_args_list, [(('/opt/spacecmd/spacecmd-backup/group/2019-01-01/group-a', 'w'), {}), (('/opt/spacecmd/spacecmd-backup/group/2019-01-01/group-b', 'w'), {}),])
def test_login_reuse_cached_session(self, shell): """ Test handling cached available session. :param shell: :return: """ mprint = MagicMock() logger = MagicMock() prompter = MagicMock() gpass = MagicMock() mkd = MagicMock() client = MagicMock() rpc_server = MagicMock(return_value=client) shell.session = None shell.options.debug = 2 shell.options.password = None shell.options.username = None shell.config = {"server": "no.mans.land"} shell.conf_dir = "/tmp" shell.MINIMUM_API_VERSION = 10.8 client.api.getVersion = MagicMock(return_value=11.5) with patch("spacecmd.misc.print", mprint) as prt, \ patch("spacecmd.misc.prompt_user", prompter) as pmt, \ patch("spacecmd.misc.getpass", gpass) as gtp, \ patch("spacecmd.misc.os.mkdir", mkd) as mkdr, \ patch("spacecmd.misc.xmlrpclib.Server", rpc_server) as rpcs, \ patch("spacecmd.misc.open", new_callable=mock_open, read_data="bofh:5adf5cc50929f71a899b81c2c2eb0979") as fmk, \ patch("spacecmd.misc.logging", logger) as lgr: out = spacecmd.misc.do_login(shell, "") assert not client.auth.login.called assert not gpass.called assert not prompter.called assert not mprint.called assert not mkd.called assert not logger.warning.called assert not logger.error.called assert logger.info.called assert client.user.listAssignableRoles.called assert shell.load_caches.called assert shell.load_config_section.called assert logger.debug.called assert shell.client is not None assert shell.session == "5adf5cc50929f71a899b81c2c2eb0979" assert shell.current_user == "bofh" assert shell.server == "no.mans.land" assert out assert_args_expect(logger.info.call_args_list, [(('Connected to %s as %s', 'https://no.mans.land/rpc/api', 'bofh'), {})]) assert_args_expect(logger.debug.call_args_list, [(('Connecting to %s', 'https://no.mans.land/rpc/api'), {}), (('Server API Version = %s', 11.5), {}), (('Using cached credentials from %s', '/tmp/no.mans.land/session'), {})])
def test_generate_package_cache_duplicate_ids(self, shell): """ Test generate package cache, handling duplicate IDs. :param shell: :return: """ tst = datetime.datetime(2000, 1, 1, 0, 0) logger = MagicMock() shell.options.quiet = False shell.all_packages = {} shell.all_packages_short = {} shell.all_packages_by_id = {} shell.package_cache_expire = tst shell.PACKAGE_CACHE_TTL = 8000 shell.client.channel.software.listAllPackages = MagicMock( side_effect=[ [ {"name": "emacs", "version": 42, "release": 3, "id": 42}, {"name": "gedit", "version": 1, "release": 2, "id": 69}, {"name": "vim", "version": 1, "release": 2, "id": 69}, ], xmlrpclib.Fault(faultString="Interrupt configuration interference error", faultCode=13) ] ) shell.client.channel.listSoftwareChannels = MagicMock( return_value=[ {"label": "basic_channel"}, {"label": "locked_channel"}, ] ) with patch("spacecmd.misc.logging", logger) as lgr: spacecmd.misc.generate_package_cache(shell, force=False) assert logger.debug.called assert shell.client.channel.software.listAllPackages.called assert shell.client.channel.listSoftwareChannels.called assert shell.replace_line_buffer.called assert shell.save_package_caches.called assert shell.package_cache_expire != tst assert shell.package_cache_expire is not None assert_args_expect(logger.debug.call_args_list, [(('No access to %s', 'locked_channel',), {}), (('Non-unique package id "69" is detected. ' 'Taking "vim-1-2" instead of "gedit-1-2"',), {})]) for pkgname, pkgid in [("emacs-42-3", 42), ("vim-1-2", 69)]: assert pkgname in shell.all_packages assert shell.all_packages[pkgname] == [pkgid] assert pkgid in shell.all_packages_by_id assert shell.all_packages_by_id[pkgid] == pkgname assert pkgname.split("-")[0] in shell.all_packages_short
def test_login_connection_error(self, shell): """ Test handling connection error at login. :param shell: :return: """ mprint = MagicMock() logger = MagicMock() prompter = MagicMock() gpass = MagicMock() mkd = MagicMock() client = MagicMock() rpc_server = MagicMock(return_value=client) shell.session = None shell.options.debug = 2 shell.config = {"server": "no.mans.land"} shell.conf_dir = "/tmp" client.api.getVersion = MagicMock(side_effect=Exception("Insert coin")) with patch("spacecmd.misc.print", mprint) as prt, \ patch("spacecmd.misc.prompt_user", prompter) as pmt, \ patch("spacecmd.misc.getpass", gpass) as gtp, \ patch("spacecmd.misc.os.mkdir", mkd) as mkdr, \ patch("spacecmd.misc.xmlrpclib.Server", rpc_server) as rpcs, \ patch("spacecmd.misc.logging", logger) as lgr: out = spacecmd.misc.do_login(shell, "") assert not logger.info.called assert not client.user.listAssignableRoles.called assert not client.auth.login.called assert not gpass.called assert not prompter.called assert not mprint.called assert not mkd.called assert not shell.load_caches.called assert not out assert not logger.warning.called assert shell.load_config_section.called assert logger.debug.called assert logger.error.called assert shell.client is None assert_args_expect(logger.error.call_args_list, [ (('Failed to connect to %s', 'https://no.mans.land/rpc/api'), {}) ]) assert_args_expect( logger.debug.call_args_list, [(('Connecting to %s', 'https://no.mans.land/rpc/api'), {}), (('Error while connecting to the server %s: %s', 'https://no.mans.land/rpc/api', 'Insert coin'), {})])
def test_kickstart_addcryptokeys(self, shell): """ Test do_kickstart_addcryptokeys :param shell: :return: """ spacecmd.kickstart.do_kickstart_addcryptokeys(shell, "my_profile key1 key2") assert not shell.help_kickstart_addcryptokeys.called assert shell.client.kickstart.profile.system.addKeys.called assert_args_expect(shell.client.kickstart.profile.system.addKeys.call_args_list, [((shell.session, "my_profile", ["key1", "key2"]), {})])
def test_group_backup_all_group_list_custom_destination(self, shell): """ Test do_group_backup with all groups lookup, custom destination, handling error. :param shell: :return: """ def exp_user(path): """ Fake expand user :param path: :return: """ return os.path.join("/opt/spacecmd", path.replace("~", "").strip("/")) shell.help_group_backup = MagicMock() shell.do_group_list = MagicMock(return_value=["group-a", "group-b"]) shell.client.systemgroup.getDetails = MagicMock( side_effect=[ {"description": "Group A description"}, {"description": "Group B description"}, ] ) mprint = MagicMock() logger = MagicMock() opener = MagicMock() _open = MagicMock(return_value=opener) _datetime = MagicMock() _datetime.now = MagicMock(return_value=datetime.datetime(2019, 1, 1, 15, 0, 0)) with patch("spacecmd.group.print", mprint) as prn, \ patch("spacecmd.group.logging", logger) as lgr, \ patch("spacecmd.group.os.path.expanduser", exp_user) as exu, \ patch("spacecmd.group.open", _open) as opr, \ patch("spacecmd.group.datetime", _datetime) as dtm: spacecmd.group.do_group_backup(shell, "ALL /dev/null/%Y-%m-%T") assert not shell.help_group_backup.called assert not shell.client.systemgroup.getDetails.called assert not mprint.called assert not opener.write.called assert not opener.close.called assert logger.error.called assert shell.do_group_list.called assert_args_expect(logger.error.call_args_list, [(('Could not create output directory: %s', '/opt/spacecmd/dev/null/2019-01-15:00:00'), {})])
def test_generate_errata_cache_force(self, shell): """ Test generate errata cache, forced :return: """ shell.ERRATA_CACHE_TTL = 86400 shell.all_errata = {} shell.options.quiet = False shell.errata_cache_expire = datetime.datetime(2099, 1, 1) shell.client.channel.listSoftwareChannels = MagicMock( return_value=[{ "label": "locked_channel" }, { "label": "base_channel" }]) shell.client.channel.software.listErrata = MagicMock(side_effect=[ xmlrpclib.Fault( faultCode=42, faultString="Sales staff sold a product we don't offer"), [{ "id": 123, "advisory_name": "cve-123", "advisory_type": "mockery", "date": "2019.1.1", "advisory_synopsis": "some text here", }] ]) logger = MagicMock() with patch("spacecmd.misc.logging", logger) as lgr: spacecmd.misc.generate_errata_cache(shell, force=True) assert logger.debug.called assert spacecmd.misc.generate_errata_cache(shell) is None assert shell.client.channel.listSoftwareChannels.called assert shell.client.channel.software.listErrata.called assert shell.replace_line_buffer.called assert shell.save_errata_cache.called assert "cve-123" in shell.all_errata assert shell.all_errata["cve-123"]["id"] == 123 assert shell.all_errata["cve-123"]["advisory_type"] == "mockery" assert shell.all_errata["cve-123"][ "advisory_synopsis"] == "some text here" assert shell.all_errata["cve-123"]["advisory_name"] == "cve-123" assert shell.all_errata["cve-123"]["date"] == "2019.1.1" assert_args_expect( logger.debug.call_args_list, [(('No access to %s (%s): %s', 'locked_channel', 42, "Sales staff sold a product we don't offer"), {})])
def test_login_api_version_mismatch(self, shell): """ Test handling API version mismatch error at login. :param shell: :return: """ mprint = MagicMock() logger = MagicMock() prompter = MagicMock() gpass = MagicMock() mkd = MagicMock() client = MagicMock() rpc_server = MagicMock(return_value=client) shell.session = None shell.options.debug = 2 shell.config = {"server": "no.mans.land"} shell.conf_dir = "/tmp" shell.MINIMUM_API_VERSION = 10.8 client.api.getVersion = MagicMock(return_value=1.5) with patch("spacecmd.misc.print", mprint) as prt, \ patch("spacecmd.misc.prompt_user", prompter) as pmt, \ patch("spacecmd.misc.getpass", gpass) as gtp, \ patch("spacecmd.misc.os.mkdir", mkd) as mkdr, \ patch("spacecmd.misc.xmlrpclib.Server", rpc_server) as rpcs, \ patch("spacecmd.misc.logging", logger) as lgr: out = spacecmd.misc.do_login(shell, "") assert not logger.info.called assert not client.user.listAssignableRoles.called assert not client.auth.login.called assert not gpass.called assert not prompter.called assert not mprint.called assert not mkd.called assert not shell.load_caches.called assert not out assert not logger.warning.called assert shell.load_config_section.called assert logger.debug.called assert logger.error.called assert shell.client is None assert_args_expect( logger.error.call_args_list, [(('API (%s) is too old (>= %s required)', 1.5, 10.8), {})])
def test_user_removerole(self, shell): """ Test do_user_removerole, with correct arguments of user and role :param shell: :return: """ shell.client.user.removeRole = MagicMock() shell.help_user_removerole = MagicMock() spacecmd.user.do_user_removerole(shell, "bofh coffee") assert not shell.help_user_removerole.called assert shell.client.user.removeRole.called assert_args_expect(shell.client.user.removeRole.call_args_list, [((shell.session, "bofh", "coffee"), {})])