Skip to content

Commit

Permalink
Merge #13298
Browse files Browse the repository at this point in the history
13298: ci: Track code coverage r=abhinav a=abhinav

**Overview**

This re-enables tracking of code coverage.
For Go, there are two kinds of coverage at play:
unit test and integration test coverage.

Unit tests follow the usual pattern of running
`go test -cover -coverprofile=whatever.cov`.

For integration tests, we use the new integration test profiling support
[added in Go 1.20](https://go.dev/testing/coverage/).
In short, the way it works is:

    # Build a coverage instrumented binary:
    go build -cover

    # Set GOCOVERDIR to a directory and run the integration tests
    # that will invoke this coverage-instrumented binary.
    GOCOVERDIR=$(pwd)/coverage
    go test ./tests

    # $GOCOVERDIR will now be filled with coverage data
    # from every invocation of the coverage-instrumented binary.
    # Combine it into a single coverage file:
    go tool covdata textfmt -i=$(GOCOVERDIR) -o=out.cov

    # The resulting file can be uploaded to codecov as-is.

The above replaces the prior, partially working hacks we had in place
to get coverage-instrumented binaries with `go test -c`
and hijacking the TestMain.

**Notable changes**

- TestMain hijacking is deleted from the Pulumi CLI.
  We no longer need this to build coverage-instrumented binaries.
- ProgramTest no longer tracks or passes PULUMI_TEST_COVERAGE_PATH
  because the Pulumi binary no longer accepts a test.coverprofile flag.
  This information is now in the GOCOVERDIR environment variable.
- We add an `enable-coverage` parameter to the `ci-build-binaries`
  workflow to mirror some of the other workflows.
  It will produce coverage-instrumented binaries if this is true.
  These binaries are then used by `ci-run-test` which will set
  `GOCOVERDIR` and merge the coverage results from it.
- Coverage configuration no longer counts tests, testdata,
  and Protobuf-generated code against coverage.
- go-wrapper.sh:
  Because we're no longer relying on the `go test -c` hack,
  this no longer excludes Windows and language providers
  from coverage tracking.
- go-test.py and go-wrapper.sh will include pulumi-language-go and
  pulumi-language-nodejs in covered packages.

*Other changes*

- go-test.py:
  Fixed a bug where `args` parameters added for coverage were ignored.

Note that this change DOES NOT track coverage for calls made to Pulumi
packages by plugins downloaded from external sources,
e.g. provider plugins. Arguably, that's out of scope of coverage
trackcing for the Pulumi repository.

Resolves #8615, #11419

Co-authored-by: Abhinav Gupta <abhinav@pulumi.com>
  • Loading branch information
bors[bot] and abhinav committed Jun 29, 2023
2 parents 1a92341 + 6af5f0b commit af1cbee
Show file tree
Hide file tree
Showing 12 changed files with 111 additions and 253 deletions.
6 changes: 6 additions & 0 deletions .github/workflows/ci-build-binaries.yml
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,11 @@ on:
required: false
description: "Set of language versions to use for builds, lints, releases, etc."
type: string
enable-coverage:
description: "Build coverage instrumented binaries"
default: false
required: false
type: boolean

defaults:
run:
Expand Down Expand Up @@ -110,6 +115,7 @@ jobs:
shell: bash
env:
GORELEASER_CURRENT_TAG: v${{ inputs.version }}
PULUMI_BUILD_MODE: ${{ inputs.enable-coverage && 'coverage' || 'normal' }}
run: |
set -euxo pipefail
# Spurious, this command requires piping via stdin
Expand Down
23 changes: 16 additions & 7 deletions .github/workflows/ci-run-test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -129,9 +129,12 @@ jobs:
run: |
./scripts/versions.sh | tee -a "$GITHUB_ENV"
- name: Enable code coverage
if: ${{ inputs.enable-coverage && runner.os != 'Windows' }}
if: ${{ inputs.enable-coverage }}
run: |
echo "PULUMI_TEST_COVERAGE_PATH=$(pwd)/coverage" >> "$GITHUB_ENV"
# Post integration test coverage to a temporary directory.
# This will be merged at a later step.
echo "GOCOVERDIR=$(mktemp -d)" >> "$GITHUB_ENV"
- name: Configure Go Cache Key
env:
CACHE_KEY: "${{ fromJson(inputs.version-set).go }}-${{ runner.os }}-${{ runner.arch }}"
Expand Down Expand Up @@ -312,19 +315,25 @@ jobs:
run: |
cd sdk/nodejs
if [ -e .nyc_output ]; then yarn run nyc report -r cobertura --report-dir "$PULUMI_TEST_COVERAGE_PATH"; fi
- name: Merge Go coverage data
if: ${{ inputs.enable-coverage && runner.os != 'Windows' }}
- name: Merge integration test code coverage
if: ${{ inputs.enable-coverage }}
run: |
pulumictl cover merge --in ./coverage --out ./coverage/go-all.txt
rm ./coverage/*.cov || true
# Merge coverage data from coverage-instrumented binaries
# if available.
if [ -n "$(ls -A "$GOCOVERDIR")" ]; then
# Cross-platform way to get milliseconds since Unix epoch.
TS=$(python -c 'import time; print(int(time.time() * 1000))')
go tool covdata textfmt -i="$GOCOVERDIR" -o="$PULUMI_TEST_COVERAGE_PATH/integration.$TS.cov"
fi
- name: Upload code coverage
if: ${{ inputs.enable-coverage && runner.os != 'Windows' }}
if: ${{ inputs.enable-coverage }}
uses: codecov/codecov-action@v3
with:
directory: coverage
files: "*"
files: "*,!.gitkeep"
fail_ci_if_error: false
verbose: true
token: ${{ secrets.CODECOV_TOKEN }}
- name: Summarize Test Time by Package
continue-on-error: true
env:
Expand Down
7 changes: 4 additions & 3 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -238,6 +238,7 @@ jobs:
arch: ${{ matrix.target.arch }}
build-platform: ${{ matrix.target.build-platform }}
version-set: ${{ needs.matrix.outputs.version-set }}
enable-coverage: ${{ inputs.enable-coverage }}
secrets: inherit

build-sdks:
Expand Down Expand Up @@ -273,7 +274,7 @@ jobs:
test-name: ${{ matrix.test-suite.name || matrix.test-suite.command }} on ${{ matrix.platform }}/${{ matrix.version-set.name }}
test-command: ${{ matrix.test-suite.command }}
is-integration-test: false
enable-coverage: false # TODO: ${{ matrix.enable-coverage }}
enable-coverage: ${{ inputs.enable-coverage }}
# require-build: false # TODO, remove ${{ matrix.require-build || false }}

version-set: ${{ toJson(matrix.version-set) }}
Expand All @@ -298,7 +299,7 @@ jobs:
test-name: ${{ matrix.test-suite.name || matrix.test-suite.command }} on ${{ matrix.platform }}/${{ matrix.version-set.name }}
test-command: ${{ matrix.test-suite.command }}
is-integration-test: true # TODO: set to true here
enable-coverage: false # TODO: ${{ matrix.enable-coverage }}
enable-coverage: ${{ inputs.enable-coverage }}
# require-build: false # TODO, remove ${{ matrix.require-build || false }}

version-set: ${{ toJson(matrix.version-set) }}
Expand All @@ -324,7 +325,7 @@ jobs:
test-name: ${{ matrix.test-suite.name || matrix.test-suite.command }} on ${{ matrix.platform }}/${{ matrix.version-set.name }}
test-command: ${{ matrix.test-suite.command }}
is-integration-test: true # TODO: set to true here
enable-coverage: false # TODO: ${{ matrix.enable-coverage }}
enable-coverage: ${{ inputs.enable-coverage }}
# require-build: false # TODO, remove ${{ matrix.require-build || false }}

version-set: ${{ toJson(matrix.version-set) }}
Expand Down
15 changes: 4 additions & 11 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -62,29 +62,22 @@ generate::
$(call STEP_MESSAGE)
echo "This command does not do anything anymore. It will be removed in a future version."

ifeq ($(PULUMI_TEST_COVERAGE_PATH),)
build:: build-proto go.ensure
cd pkg && go install -ldflags "-X github.com/pulumi/pulumi/pkg/v3/version.Version=${VERSION}" ${PROJECT}

install:: .ensure.phony go.ensure
cd pkg && GOBIN=$(PULUMI_BIN) go install -ldflags "-X github.com/pulumi/pulumi/pkg/v3/version.Version=${VERSION}" ${PROJECT}
else
build:: build_cover ensure_cover

ensure_cover::
mkdir -p $(PULUMI_TEST_COVERAGE_PATH)

install:: install_cover
endif

build_debug::
cd pkg && go install -gcflags="all=-N -l" -ldflags "-X github.com/pulumi/pulumi/pkg/v3/version.Version=${VERSION}" ${PROJECT}

build_cover::
cd pkg && go test -coverpkg github.com/pulumi/pulumi/pkg/v3/...,github.com/pulumi/pulumi/sdk/v3/... -cover -c -o $(shell go env GOPATH)/bin/pulumi -ldflags "-X github.com/pulumi/pulumi/pkg/v3/version.Version=${VERSION}" ${PROJECT}
cd pkg && go build -cover -o ../bin/pulumi \
-coverpkg github.com/pulumi/pulumi/pkg/v3/...,github.com/pulumi/pulumi/sdk/v3/... \
-ldflags "-X github.com/pulumi/pulumi/pkg/v3/version.Version=${VERSION}" ${PROJECT}

install_cover:: build_cover
cp $(shell go env GOPATH)/bin/pulumi $(PULUMI_BIN)
cp bin/pulumi $(PULUMI_BIN)

developer_docs::
cd developer-docs && make html
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
changes:
- type: chore
scope: pkg/testing
description: ProgramTest dropped the CoverProfile option as it's no longer necessary.
44 changes: 33 additions & 11 deletions codecov.yml
Original file line number Diff line number Diff line change
@@ -1,32 +1,54 @@
comment: false

coverage:
status:

# Project tracks and reports the project-level coverage.
project:
default: false
default:
informational: true

# Pulumi core
core:
informational: true
paths:
- "pkg/*"
- "sdk/go/common/*"
- "sdk/go/auto/*"
- "sdk/proto/*"
- "pkg"
- "sdk/go/common"
- "sdk/go/auto"

sdk-go:
informational: true
paths:
- "sdk/go/pulumi/*"
- "sdk/go/pulumi-language-go/*"
- "sdk/go/pulumi"
- "sdk/go/pulumi-language-go"
sdk-nodejs:
informational: true
paths:
- "sdk/nodejs/*"
- "sdk/nodejs"
sdk-python:
informational: true
paths:
- "sdk/python/*"
- "sdk/python"

# Patch tracks the coverage of the changes in a single patch.
patch:
default:
informational: true

ignore:
# Integration tests and codegen test data
# should not count against coverage.
- "tests/"
- "pkg/codegen/testing/test/testdata"

# Don't count protobuf-generated code against coverage.
- "sdk/proto"

# More generally, none of the test data
# should count against coverage.
- "**/testdata"

# Don't comment on PRs.
comment: false

# Don't post annotations to GitHub.
github_checks:
annotations: false
24 changes: 0 additions & 24 deletions pkg/cmd/pulumi/main_1.17_test.go

This file was deleted.

23 changes: 0 additions & 23 deletions pkg/cmd/pulumi/main_1.18_test.go

This file was deleted.

121 changes: 0 additions & 121 deletions pkg/cmd/pulumi/main_test.go

This file was deleted.

0 comments on commit af1cbee

Please sign in to comment.