pulumi/scripts/go-test.py

112 lines
3.3 KiB
Python
Raw Normal View History

#!/usr/bin/env python3
"""
Wraps `go test`.
"""
from datetime import datetime
from typing import List
from integration_test_subsets import INTEGRATION_TESTS
import os
import pathlib
import platform
import shutil
import subprocess as sp
import sys
import uuid
import threading
ci: Track code coverage **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
2023-06-27 16:57:36 +00:00
cover_packages = [
"github.com/pulumi/pulumi/pkg/v3/...",
"github.com/pulumi/pulumi/sdk/v3/...",
"github.com/pulumi/pulumi/sdk/go/pulumi-language-go/v3/...",
"github.com/pulumi/pulumi/sdk/nodejs/cmd/pulumi-language-nodejs/v3/...",
"github.com/pulumi/pulumi/sdk/python/cmd/pulumi-language-python/v3/...",
ci: Track code coverage **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
2023-06-27 16:57:36 +00:00
]
dryrun = os.environ.get("PULUMI_TEST_DRYRUN", None) == "true"
def options(options_and_packages: List[str]):
return [o for o in options_and_packages if not o.startswith('github.com/pulumi/pulumi')]
def packages(options_and_packages: List[str]):
return [o for o in options_and_packages if o.startswith('github.com/pulumi/pulumi')]
root = pathlib.Path(__file__).absolute().parent.parent
integration_test_subset = os.environ.get('PULUMI_INTEGRATION_TESTS', None)
args = sys.argv[1:]
ci: Track code coverage **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
2023-06-27 16:57:36 +00:00
covdir = os.environ.get('PULUMI_TEST_COVERAGE_PATH', None)
covprofile = None
if covdir is not None:
covprofile = f'{covdir}/go-test-{os.urandom(4).hex()}.cov'
elif '-cover' in args:
wd = os.getcwd()
covprofile = f'{wd}/go-test-{os.urandom(4).hex()}.cov'
if covprofile is not None:
coverpkg = ','.join(cover_packages)
args += [f'-coverprofile={covprofile}', f'-coverpkg={coverpkg}']
if integration_test_subset:
print(f"Using test subset: {integration_test_subset}")
args += ['-run', INTEGRATION_TESTS[integration_test_subset]]
if os.environ.get("CI") != "true":
args += ['-v']
class RepeatTimer(threading.Timer):
def run(self):
while not self.finished.wait(self.interval):
self.function(*self.args, **self.kwargs)
windows = platform.system() == 'Windows'
heartbeat_str = '💓' if not windows else 'heartbeat'
start_time = datetime.now()
def heartbeat():
if not sys:
# occurs during interpreter shutdown
return
print(heartbeat_str + " " + str(datetime.now()), file=sys.stderr) # Ensures GitHub receives stdout during long, silent package tests.
sys.stdout.flush()
sys.stderr.flush()
timer = RepeatTimer(10, heartbeat)
timer.daemon = True
timer.start()
if shutil.which('gotestsum') is not None:
test_run = str(uuid.uuid4())
opts = options(args)
pkgs = " ".join(packages(args))
test_results_dir = root.joinpath('test-results')
if not test_results_dir.is_dir():
os.mkdir(str(test_results_dir))
json_file = str(test_results_dir.joinpath(f'{test_run}.json'))
args = ['gotestsum', '--jsonfile', json_file, '--packages', pkgs, '--'] + \
opts
else:
args = ['go', 'test'] + args
if not dryrun:
try:
print("Running: " + ' '.join(args))
sp.check_call(args, shell=False)
print("Completed: " + ' '.join(args))
except sp.CalledProcessError as e:
print("Failed: " + ' '.join(args))
raise e
else:
print("Would have run: " + ' '.join(args))
timer.cancel()
# rarely the python runtime will shut down, closing sys.stderr and crashing with a write to a closed
# sys.stderr. ensure that we exit with an OK status code, in case we race on the write in the timer:
sys.exit(0)