Beispiel #1
0
 def test_change_mirror(self):
     """Test change mirror"""
     if os.getuid() != 0:
         return self.skipTest("root privileges required to opt in")
     updater = AptMirrorUpdater()
     updater.change_mirror()
     assert have_package_lists()
Beispiel #2
0
 def test_create_chroot(self):
     """Test create chroot"""
     if os.getuid() != 0:
         return self.skipTest("root privileges required to opt in")
     updater = AptMirrorUpdater()
     updater.create_chroot('/test_chroot')
     assert 'Filename:' in updater.context.capture('apt-cache', 'show',
                                                   'python')
Beispiel #3
0
    def test_trusty_eol(self):
        """
        Test 'trusty' for `issue #9`_.

        .. _issue #9: https://github.com/xolox/python-apt-mirror-updater/issues/9
        """
        updater = AptMirrorUpdater(
            distributor_id='ubuntu',
            distribution_codename='trusty',
            architecture='amd64',
        )
        assert updater.release_is_eol == (updater.validate_mirror(
            updater.old_releases_url) == MirrorStatus.AVAILABLE)
Beispiel #4
0
    def test_debian_lts_eol_date(self):
        """
        Regression test for `issue #5`_.

        .. _issue #5: https://github.com/xolox/python-apt-mirror-updater/issues/5
        """
        updater = AptMirrorUpdater(
            distributor_id='debian',
            distribution_codename='jessie',
            architecture='amd64',
        )
        eol_expected = (time.time() >= 1593468000)
        assert updater.release_is_eol == eol_expected
Beispiel #5
0
 def test_dumb_update(self):
     """Test that our dumb ``apt-get update`` wrapper works."""
     if os.getuid() != 0:
         return self.skipTest("root privileges required to opt in")
     updater = AptMirrorUpdater()
     # Remove all existing package lists.
     updater.clear_package_lists()
     # Verify that package lists aren't available.
     assert not have_package_lists()
     # Run `apt-get update' to download the package lists.
     updater.dumb_update()
     # Verify that package lists are again available.
     assert have_package_lists()
Beispiel #6
0
    def test_smart_update(self):
        """
        Test that our smart ``apt-get update`` wrapper works.

        Currently this test simply ensures coverage of the happy path.
        Ideally it will evolve to test the handled edge cases as well.
        """
        if os.getuid() != 0:
            return self.skipTest("root privileges required to opt in")
        updater = AptMirrorUpdater()
        # Remove all existing package lists.
        updater.clear_package_lists()
        # Verify that package lists aren't available.
        assert not have_package_lists()
        # Run `apt-get update' to download the package lists.
        updater.smart_update()
        # Verify that package lists are again available.
        assert have_package_lists()
Beispiel #7
0
def main():
    """Command line interface for the ``apt-smart`` program."""
    # Initialize logging to the terminal and system log.
    coloredlogs.install(syslog=True)
    # Command line option defaults.
    context = LocalContext()
    updater = AptMirrorUpdater(context=context)
    limit = MAX_MIRRORS
    url_char_len = URL_CHAR_LEN
    actions = []
    # Parse the command line arguments.
    try:
        options, arguments = getopt.getopt(sys.argv[1:],
                                           'r:fF:blL:c:aux:m:vVR:qh', [
                                               'remote-host=',
                                               'find-current-mirror',
                                               'find-best-mirror',
                                               'file-to-read=',
                                               'list-mirrors',
                                               'url-char-len=',
                                               'change-mirror',
                                               'auto-change-mirror',
                                               'update',
                                               'update-package-lists',
                                               'exclude=',
                                               'max=',
                                               'verbose',
                                               'version',
                                               'create-chroot=',
                                               'quiet',
                                               'help',
                                           ])
        for option, value in options:
            if option in ('-r', '--remote-host'):
                if actions:
                    msg = "The %s option should be the first option given on the command line!"
                    raise Exception(msg % option)
                context = RemoteContext(value)
                updater = AptMirrorUpdater(context=context)
            elif option in ('-f', '--find-current-mirror'):
                actions.append(
                    functools.partial(report_current_mirror, updater))
            elif option in ('-F', '--file-to-read'):
                updater.custom_mirror_file_path = value
            elif option in ('-b', '--find-best-mirror'):
                actions.append(functools.partial(report_best_mirror, updater))
            elif option in ('-l', '--list-mirrors'):
                actions.append(
                    functools.partial(report_available_mirrors, updater))
            elif option in ('-L', '--url-char-len'):
                url_char_len = int(value)
            elif option in ('-c', '--change-mirror'):
                if value.strip().startswith(('http://', 'https://', 'ftp://')):
                    actions.append(
                        functools.partial(updater.change_mirror, value))
                else:
                    raise Exception("\'%s\' is not a valid mirror URL" % value)
            elif option in ('-a', '--auto-change-mirror'):
                actions.append(updater.change_mirror)
            elif option in ('-u', '--update', '--update-package-lists'):
                actions.append(updater.smart_update)
            elif option in ('-x', '--exclude'):
                actions.insert(0,
                               functools.partial(updater.ignore_mirror, value))
            elif option in ('-m', '--max'):
                limit = int(value)
            elif option in ('-v', '--verbose'):
                coloredlogs.increase_verbosity()
            elif option in ('-V', '--version'):
                output("Version: %s on Python %i.%i", updater_version,
                       sys.version_info[0], sys.version_info[1])
                return
            elif option in ('-R', '--create-chroot'):
                actions.append(functools.partial(updater.create_chroot, value))
            elif option in ('-q', '--quiet'):
                coloredlogs.decrease_verbosity()
            elif option in ('-h', '--help'):
                usage(__doc__)
                return
            else:
                assert False, "Unhandled option!"
        if not actions:
            usage(__doc__)
            return
        # Propagate options to the Python API.
        updater.max_mirrors = limit
        updater.url_char_len = url_char_len
    except Exception as e:
        warning("Error: Failed to parse command line arguments! (%s)" % e)
        sys.exit(1)
    # Perform the requested action(s).
    try:
        for callback in actions:
            callback()
    except Exception:
        logger.exception("Encountered unexpected exception! Aborting ..")
        sys.exit(1)
Beispiel #8
0
 def test_best_mirror_selection(self):
     """Test the selection of a "best" mirror."""
     updater = AptMirrorUpdater()
     check_mirror_url(updater.best_mirror)
Beispiel #9
0
 def test_mirror_ranking(self):
     """Test the ranking of discovered mirrors."""
     updater = AptMirrorUpdater()
     # Make sure that multiple discovered mirrors are available.
     assert sum(m.is_available for m in updater.ranked_mirrors) > 9
Beispiel #10
0
 def test_adaptive_mirror_discovery(self):
     """Test the discovery of mirrors for the current type of system."""
     updater = AptMirrorUpdater()
     assert len(updater.available_mirrors) > 10
     for candidate in updater.available_mirrors:
         check_mirror_url(candidate.mirror_url)