Beispiel #1
0
def test_create_bundle_archive(mock_gwc, mock_set_request, deps_present, tmpdir):
    # Make the bundles and sources dir configs point to under the pytest managed temp dir
    bundles_dir = tmpdir.mkdir('bundles')
    mock_gwc.return_value.cachito_bundles_dir = str(bundles_dir)
    request_id = 3
    request_bundle_dir = bundles_dir.mkdir('temp').mkdir(str(request_id))

    # Create the extracted application source
    app_archive_contents = {
        'app/.git': b'some content',
        'app/pizza.go': b'Cheese Pizza',
        'app/all_systems.go': b'All Systems Go',
    }

    request_bundle_dir.mkdir('app')
    for name, data in app_archive_contents.items():
        file_path = os.path.join(str(request_bundle_dir), name)
        with open(file_path, 'wb') as f:
            f.write(data)

    # Create the dependencies cache from the call to add_deps_to_bundle call from resolve_gomod
    deps_archive_contents = {
        'deps/gomod/pkg/mod/cache/download/server.com/dep1/@v/dep1.zip': b'dep1 archive',
        'deps/gomod/pkg/mod/cache/download/server.com/dep2/@v/dep2.zip': b'dep2 archive',
    }

    if deps_present:
        for name, data in deps_archive_contents.items():
            path = request_bundle_dir.join(name)
            os.makedirs(os.path.dirname(path), exist_ok=True)
            open(path, 'wb').write(data)

    # Test the bundle is created when create_bundle_archive is called
    tasks.create_bundle_archive(request_id)
    bundle_archive_path = str(bundles_dir.join(f'{request_id}.tar.gz'))
    assert os.path.exists(bundle_archive_path)

    # Verify the contents of the assembled bundle archive
    with tarfile.open(bundle_archive_path, mode='r:*') as bundle_archive:
        bundle_contents = set([
            path for path in bundle_archive.getnames()
            if pathlib.Path(path).suffix in ('.go', '.zip')
        ])

        # Always make sure there is a deps directory. This will be empty if no deps were present.
        assert 'deps' in bundle_archive.getnames()

    expected = set(app_archive_contents.keys())
    # The .git folder must be excluded
    expected.remove('app/.git')
    if deps_present:
        expected |= set(deps_archive_contents.keys())

    assert bundle_contents == expected

    mock_set_request.assert_called_once_with(
        request_id, 'in_progress', 'Assembling the bundle archive')
Beispiel #2
0
def test_create_bundle_archive(mock_gwc, mock_set_request_state, deps_present,
                               include_git_dir, tmpdir):
    flags = ["include-git-dir"] if include_git_dir else []

    # Make the bundles and sources dir configs point to under the pytest managed temp dir
    bundles_dir = tmpdir.mkdir("bundles")
    mock_gwc.return_value.cachito_bundles_dir = str(bundles_dir)
    request_id = 3
    request_bundle_dir = bundles_dir.mkdir("temp").mkdir(str(request_id))

    # Create the extracted application source
    app_archive_contents = {
        "app/.git": b"some content",
        "app/pizza.go": b"Cheese Pizza",
        "app/all_systems.go": b"All Systems Go",
    }

    request_bundle_dir.mkdir("app")
    for name, data in app_archive_contents.items():
        file_path = os.path.join(str(request_bundle_dir), name)
        with open(file_path, "wb") as f:
            f.write(data)

    # Create the dependencies cache from the call to add_deps_to_bundle call from resolve_gomod
    deps_archive_contents = {
        "deps/gomod/pkg/mod/cache/download/server.com/dep1/@v/dep1.zip":
        b"dep1 archive",
        "deps/gomod/pkg/mod/cache/download/server.com/dep2/@v/dep2.zip":
        b"dep2 archive",
    }

    if deps_present:
        for name, data in deps_archive_contents.items():
            path = request_bundle_dir.join(name)
            os.makedirs(os.path.dirname(path), exist_ok=True)
            open(path, "wb").write(data)

    # Test the bundle is created when create_bundle_archive is called
    tasks.create_bundle_archive(request_id, flags)

    bundle_archive_path = str(bundles_dir.join(f"{request_id}.tar.gz"))
    assert os.path.exists(bundle_archive_path)

    # Verify the contents of the assembled bundle archive
    with tarfile.open(bundle_archive_path, mode="r:*") as bundle_archive:
        bundle_contents = set([
            path for path in bundle_archive.getnames()
            if pathlib.Path(path).suffix in (
                ".go", ".zip") or os.path.basename(path) == ".git"
        ])

        # Always make sure there is a deps directory. This will be empty if no deps were present.
        assert "deps" in bundle_archive.getnames()

    expected = set(app_archive_contents.keys())
    if not include_git_dir:
        # The .git folder must be excluded unless flag is used
        expected.remove("app/.git")
    if deps_present:
        expected |= set(deps_archive_contents.keys())

    assert bundle_contents == expected
    assert mock_set_request_state.call_count == 1
    mock_set_request_state.assert_called_once_with(
        request_id, "in_progress", "Assembling the bundle archive")
def test_create_bundle_archive(mock_get_worker_config, mock_set_request,
                               deps_present, tmpdir):
    # Make the bundles and sources dir configs point to under the pytest managed temp dir
    bundles_dir = tmpdir.mkdir('bundles')
    sources_dir = tmpdir.mkdir('sources')
    mock_get_worker_config.return_value = mock.Mock(
        cachito_bundles_dir=str(bundles_dir),
        cachito_sources_dir=str(sources_dir),
    )

    # Create the mocked application source archive (app.tar.gz)
    app_archive_path = (sources_dir.mkdir('release-engineering').mkdir(
        'some_app').join('app.tar.gz'))
    app_archive_contents = {
        'app/pizza.go': b'Cheese Pizza',
        'app/all_systems.go': b'All Systems Go',
    }

    with tarfile.open(app_archive_path, mode='w:gz') as app_archive:
        for name, data in app_archive_contents.items():
            fileobj = io.BytesIO(data)
            tarinfo = tarfile.TarInfo(name)
            tarinfo.size = len(fileobj.getvalue())
            app_archive.addfile(tarinfo, fileobj=fileobj)

    # Create the dependencies cache from the call to add_deps_to_bundle call from resolve_gomod_deps
    deps_archive_contents = {
        'deps/gomod/pkg/mod/cache/download/server.com/dep1/@v/dep1.zip':
        b'dep1 archive',
        'deps/gomod/pkg/mod/cache/download/server.com/dep2/@v/dep2.zip':
        b'dep2 archive',
    }

    request_id = 3
    if deps_present:
        temp_bundle_path = bundles_dir.mkdir('temp').mkdir(str(request_id))
        for name, data in deps_archive_contents.items():
            path = temp_bundle_path.join(name)
            os.makedirs(os.path.dirname(path), exist_ok=True)
            open(path, 'wb').write(data)

    # Test the bundle is created when create_bundle_archive is called
    tasks.create_bundle_archive(app_archive_path, request_id)
    bundle_archive_path = str(bundles_dir.join(f'{request_id}.tar.gz'))
    assert os.path.exists(bundle_archive_path)

    # Verify the contents of the assembled bundle archive
    with tarfile.open(bundle_archive_path, mode='r:*') as bundle_archive:
        bundle_contents = set([
            path for path in bundle_archive.getnames()
            if pathlib.Path(path).suffix in ('.go', '.zip')
        ])

        # Always make sure there is a deps directory. This will be empty if no deps were present.
        assert 'deps' in bundle_archive.getnames()

    expected = set(app_archive_contents.keys())
    if deps_present:
        expected |= set(deps_archive_contents.keys())

    assert bundle_contents == expected

    mock_set_request.assert_called_once_with(request_id, 'in_progress',
                                             'Assembling the bundle archive')