def test_firefox_downloader_downloading(mock_stdout, mock_urlopen, tmpdir):
    """Test the download function"""
    del mock_stdout

    # This test is checking caching behavior, hence:
    # Using a test-specific test directory to not wipe regular cache.
    test_tmp_dir = str(tmpdir.join("download_test"))

    fdl = fd.FirefoxDownloader(test_tmp_dir, cache_timeout=1)

    mock_req = mock.Mock()
    mock_read = mock.Mock(side_effect=(b"foo", b"bar", None))
    mock_info = mock.Mock()
    mock_get = mock.Mock(return_value="6")
    mock_info.return_value = mock.Mock(get=mock_get)
    mock_req.info = mock_info
    mock_req.read = mock_read
    mock_urlopen.return_value = mock_req

    output_file_name = fdl.download("nightly", "linux", use_cache=True)
    assert mock_get.call_args_list == [(("Content-Length",),)],\
        "only checks content length (assumed by test mock)"
    expected_url = """https://download.mozilla.org/?product=firefox-nightly-latest&os=linux64&lang=en-US"""
    assert mock_urlopen.call_args_list == [((expected_url,),)], "downloads the expected URL"
    assert len(mock_read.call_args_list) == 3, "properly calls read()"
    assert output_file_name.endswith("firefox-nightly_linux.tar.bz2"), "uses expected file name"
    assert output_file_name.startswith(test_tmp_dir), "writes file to expected directory"
    assert os.path.isfile(output_file_name), "creates proper file"
    with open(output_file_name, "rb") as f:
        content = f.read()
    assert content == b"foobar", "downloads expected content"

    # Test caching by re-downloading
    mock_read.reset_mock()
    mock_read.side_effect = (b"foo", b"bar", None)
    second_output_file_name = fdl.download("nightly", "linux", use_cache=True)
    assert not mock_read.called, "does not re-download"
    assert output_file_name == second_output_file_name, "uses cached file"

    # Test purging on obsolete cache. Cache is purged on fdl init.
    sleep(1.1)
    mock_read.reset_mock()
    mock_read.side_effect = (b"foo", b"bar", None)
    fdl = fd.FirefoxDownloader(test_tmp_dir, cache_timeout=1)
    fdl.download("nightly", "linux", use_cache=True)
    assert mock_read.called, "re-downloads when cache is stale"

    # Test caching when file changes upstream (checks file size).
    mock_get.reset_mock()
    mock_get.return_value = "7"
    mock_read.reset_mock()
    mock_read.side_effect = (b"foo", b"barr", None)
    fdl.download("nightly", "linux", use_cache=True)
    assert mock_read.called, "re-downloads when upstream changes"
Exemplo n.º 2
0
def test_firefox_download_dummy():
    """Downloading firefox instance for tests"""
    global test_app, test_archive
    # Get ourselves a Firefox app for the local platform.
    fdl = fd.FirefoxDownloader(tmp_dir)
    test_archive = fdl.download("nightly", use_cache=True)
    test_app = fe.extract(test_archive, tmp_dir)
def test_firefox_downloader_instance(tmpdir):
    """FirefoxDownloader instances sanity check"""

    fdl = fd.FirefoxDownloader(tmpdir)

    build_list, platform_list, test_default, base_default = fdl.list()
    assert "nightly" in build_list and "release" in build_list, "build list looks sane"
    assert "linux" in platform_list and "osx" in platform_list, "platform list looks sane"
    assert test_default in build_list and base_default in build_list, "defaults are valid builds"
def test_firefox_downloader_exceptions(tmpdir):
    """Test handling of invalid parameters"""

    fdl = fd.FirefoxDownloader(tmpdir)
    build_list, platform_list, test_default, base_default = fdl.list()

    assert "foobar" not in build_list and "foobar" not in platform_list

    with pytest.raises(Exception):
        fdl.download("foobar", platform_list[0])

    with pytest.raises(Exception):
        fdl.download(test_default, "foobar")
def test_firefox_downloader_exceptions():
    """Test handling of invalid parameters"""

    # This test is checking caching behavior, hence:
    # Using a test-specific test directory to not wipe regular cache.
    test_tmp_dir = os.path.join(tests.tmp_dir, "download_test")

    fdl = fd.FirefoxDownloader(test_tmp_dir, cache_timeout=1)
    build_list, platform_list, test_default, base_default = fdl.list()

    assert_true("foobar" not in build_list and "foobar" not in platform_list)
    assert_raises(Exception, fdl.download, "foobar", platform_list[0])
    assert_raises(Exception, fdl.download, test_default, "foobar")
def test_firefox_downloader_instance():
    """FirefoxDownloader instances sanity check"""

    # This test is checking caching behavior, hence:
    # Using a test-specific test directory to not wipe regular cache.
    test_tmp_dir = os.path.join(tests.tmp_dir, "download_test")

    fdl = fd.FirefoxDownloader(test_tmp_dir)

    build_list, platform_list, test_default, base_default = fdl.list()
    assert_true("nightly" in build_list and "release" in build_list, "build list looks sane")
    assert_true("linux" in platform_list and "osx" in platform_list, "platform list looks sane")
    assert_true(test_default in build_list and base_default in build_list, "defaults are valid builds")
def caching_firefox_downloader(tmpdir_factory):
    return fd.FirefoxDownloader(tmpdir_factory.mktemp("caching_downloader"))
Exemplo n.º 8
0
    def get_test_candidate(self, build):
        """
        Download and extract a build candidate. build may either refer
        to a Firefox release identifier, package, or build directory.
        :param build: str with firefox build
        :return: two FirefoxApp objects for test and base candidate
        """
        global logger

        platform = fd.FirefoxDownloader.detect_platform()
        if platform is None:
            logger.error("Unsupported platform: `%s`" % sys.platform)
            sys.exit(5)

        # `build` may refer to a build reference as defined in FirefoxDownloader,
        # a local Firefox package as produced by `mach build`, or a local build tree.
        if build in fd.FirefoxDownloader.build_urls:
            # Download test candidate by Firefox release ID
            logger.info("Downloading Firefox `%s` build for platform `%s`" % (build, platform))
            fdl = fd.FirefoxDownloader(self.args.workdir, cache_timeout=1 * 60 * 60)
            build_archive_file = fdl.download(build, platform)
            if build_archive_file is None:
                sys.exit(-1)
            # Extract candidate archive
            candidate_app = fe.extract(build_archive_file, self.args.workdir, cache_timeout=1 * 60 * 60)
            candidate_app.package_origin = fdl.get_download_url(build, platform)
        elif os.path.isfile(build):
            # Extract firefox build from archive
            logger.info("Using file `%s` as Firefox package" % build)
            candidate_app = fe.extract(build, self.args.workdir, cache_timeout=1 * 60 * 60)
            candidate_app.package_origin = build
            logger.debug("Build candidate executable is `%s`" % candidate_app.exe)
        elif os.path.isfile(os.path.join(build, "mach")):
            logger.info("Using Firefox build tree at `%s`" % build)
            dist_globs = sorted(glob.glob(os.path.join(build, "obj-*", "dist")))
            if len(dist_globs) == 0:
                logger.critical("`%s` looks like a Firefox build directory, but can't find a build in it" % build)
                sys.exit(5)
            logger.debug("Potential globs for dist directory: %s" % dist_globs)
            dist_dir = dist_globs[-1]
            logger.info("Using `%s` as build distribution directory" % dist_dir)
            if "apple-darwin" in dist_dir.split("/")[-2]:
                # There is a special case for OS X dist directories:
                # FirefoxApp expects OS X .dmg packages to contain the .app folder inside
                # another directory. However, that directory isn't there in build trees,
                # thus we need to point to the parent for constructing the app.
                logger.info("Looks like this is an OS X build tree")
                candidate_app = fa.FirefoxApp(os.path.abspath(os.path.dirname(dist_dir)))
                candidate_app.package_origin = os.path.abspath(build)
            else:
                candidate_app = fa.FirefoxApp(os.path.abspath(dist_dir))
                candidate_app.package_origin = os.path.abspath(build)
        else:
            logger.critical("`%s` specifies neither a Firefox release, package file, or build directory" % build)
            logger.critical("Valid Firefox release identifiers are: %s" % ", ".join(fd.FirefoxDownloader.list()[0]))
            sys.exit(5)

        logger.debug("Build candidate executable is `%s`" % candidate_app.exe)
        if candidate_app.platform != platform:
            logger.warning("Platform mismatch detected")
            logger.critical("Running a Firefox binary for `%s` on a `%s` platform will probably fail" %
                            (candidate_app.platform, platform))
        return candidate_app