Exemplo n.º 1
0
def fetch_gomod_source(app_archive_path, request_id_to_update=None):
    """
    Resolve and fetch gomod dependencies for given app source archive.

    :param str app_archive_path: the full path to the application source code
    :param int request_id_to_update: the Cachito request ID this is for; if specified, this will
        update the request's state
    :return: the full path to the application source code
    :rtype: str
    """
    log.info('Fetching gomod dependencies for "%s"', app_archive_path)
    if request_id_to_update:
        set_request_state(request_id_to_update, 'in_progress',
                          'Fetching the golang dependencies')

    try:
        deps = resolve_gomod_deps(app_archive_path, request_id_to_update)
    except CachitoError:
        log.exception('Failed to fetch gomod dependencies for "%s"',
                      app_archive_path)
        raise

    if request_id_to_update:
        env_vars = {}
        if len(deps):
            env_vars['GOPATH'] = env_vars['GOCACHE'] = 'deps/gomod'
        update_request_with_deps(request_id_to_update, deps, env_vars)

    return app_archive_path
def test_resolve_gomod_deps(
    mock_tarfile_open, mock_run, mock_temp_dir, mock_add_deps, request_id, tmpdir, sample_deps,
):
    archive_path = '/this/is/path/to/archive.tar.gz'
    # Mock the tempfile.TemporaryDirectory context manager
    mock_temp_dir.return_value.__enter__.return_value = str(tmpdir)

    # Mock the "subprocess.run" calls
    mock_run.side_effect = [
        mock.Mock(returncode=0, stdout=None),   # go mod download
        mock.Mock(returncode=0, stdout=mock_cmd_output)  # go list -m all
    ]

    # Mock the opening of the tar file containing application source code
    mock_final_tarfile = mock.Mock()
    mock_final_tarfile.extractall.return_value = None
    mock_tarfile_open.return_value.__enter__.side_effect = [
        mock_final_tarfile,
    ]

    resolved_deps = resolve_gomod_deps(archive_path, request_id)

    assert resolved_deps == sample_deps
    if request_id:
        mock_add_deps.assert_called_once()
        assert mock_add_deps.call_args[0][0].endswith('pkg/mod/cache/download')
        assert mock_add_deps.call_args[0][1] == 'gomod/pkg/mod/cache/download'
        assert mock_add_deps.call_args[0][2] == 3
    else:
        mock_add_deps.assert_not_called()
Exemplo n.º 3
0
def fetch_gomod_source(app_archive_path,
                       copy_cache_to=None,
                       request_id_to_update=None):
    """
    Resolve and fetch gomod dependencies for given app source archive.

    :param str app_archive_path: the full path to the application source code
    :param str copy_cache_to: path to copy artifacts from gomod cache
    :param int request_id_to_update: the Cachito request ID this is for; if specified, this will
        update the request's state
    """
    log.info('Fetching gomod dependencies for "%s"', app_archive_path)
    if request_id_to_update:
        set_request_state(request_id_to_update, 'in_progress',
                          'Fetching the golang dependencies')

    try:
        deps = resolve_gomod_deps(app_archive_path, copy_cache_to)
    except CachitoError:
        log.exception('Failed to fetch gomod dependencies for "%s"',
                      app_archive_path)
        raise

    if request_id_to_update:
        update_request_with_deps(request_id_to_update, deps)

    return app_archive_path
Exemplo n.º 4
0
def test_go_list_cmd_failure(mock_tarfile_open, mock_run, mock_temp_dir,
                             tmpdir, go_mod_rc, go_list_rc):
    archive_path = '/this/is/path/to/archive.tar.gz'
    # Mock the tempfile.TemporaryDirectory context manager
    mock_temp_dir.return_value.__enter__.return_value = str(tmpdir)

    # Mock the "subprocess.run" calls
    mock_run.side_effect = [
        mock.Mock(returncode=go_mod_rc, stdout=None),  # go mod download
        mock.Mock(returncode=go_list_rc,
                  stdout=mock_cmd_output)  # go list -m all
    ]

    # Mock the opening of the tar file containing application source code
    mock_final_tarfile = mock.Mock()
    mock_final_tarfile.extractall.return_value = None
    mock_tarfile_open.return_value.__enter__.side_effect = [
        mock_final_tarfile,
    ]

    with pytest.raises(CachitoError) as exc_info:
        resolve_gomod_deps(archive_path)
    assert str(exc_info.value) == 'Processing gomod dependencies failed'
Exemplo n.º 5
0
def test_resolve_gomod_deps_with_copy_cache(mock_tarfile_open, mock_run,
                                            mock_temp_dir, tmpdir,
                                            sample_deps):
    archive_path = '/this/is/path/to/archive.tar.gz'
    # Mock the tempfile.TemporaryDirectory context manager
    mock_temp_dir.return_value.__enter__.return_value = str(tmpdir)

    dep_cache_partial_path = os.path.join('pkg', 'mod', 'cache', 'download',
                                          'server.com', 'dep1', '@v',
                                          'dep1.zip')

    def side_effect(*args, **kwargs):
        if 'list' not in args[0]:
            # "go list" command
            return mock.Mock(returncode=0, stdout=None)

        # "go mod" command - generate dummy dependency cache
        dep1_path = os.path.join(str(tmpdir), dep_cache_partial_path)
        os.makedirs(os.path.dirname(dep1_path), exist_ok=True)
        with open(dep1_path, 'wb') as f:
            f.write(b'dep1 archive')
        return mock.Mock(returncode=0, stdout=mock_cmd_output)

    mock_run.side_effect = side_effect

    # Mock the opening of the tar file containing application source code
    mock_final_tarfile = mock.Mock()
    mock_final_tarfile.extractall.return_value = None
    mock_tarfile_open.return_value.__enter__.side_effect = [
        mock_final_tarfile,
    ]

    copy_cache_to = os.path.join(str(tmpdir), 'the-cache')
    resolved_deps = resolve_gomod_deps(archive_path,
                                       copy_cache_to=copy_cache_to)

    assert resolved_deps == sample_deps
    # Verify cache has been copied to the provided copy_cache_to location under the gomod dir
    assert os.path.exists(
        os.path.join(copy_cache_to, 'gomod', dep_cache_partial_path))
Exemplo n.º 6
0
def test_resolve_gomod_deps(mock_tarfile_open, mock_run, mock_temp_dir, tmpdir,
                            sample_deps):
    archive_path = '/this/is/path/to/archive.tar.gz'
    # Mock the tempfile.TemporaryDirectory context manager
    mock_temp_dir.return_value.__enter__.return_value = str(tmpdir)

    # Mock the "subprocess.run" calls
    mock_run.side_effect = [
        mock.Mock(returncode=0, stdout=None),  # go mod download
        mock.Mock(returncode=0, stdout=mock_cmd_output)  # go list -m all
    ]

    # Mock the opening of the tar file containing application source code
    mock_final_tarfile = mock.Mock()
    mock_final_tarfile.extractall.return_value = None
    mock_tarfile_open.return_value.__enter__.side_effect = [
        mock_final_tarfile,
    ]

    resolved_deps = resolve_gomod_deps(archive_path)

    assert resolved_deps == sample_deps