Skip to content

Commit

Permalink
fix fail test with cover flag
Browse files Browse the repository at this point in the history
Signed-off-by: Fedor Partanskiy <[email protected]>
  • Loading branch information
pfi79 authored and denyeart committed Jan 24, 2025
1 parent 9336215 commit d8bb207
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 10 deletions.
2 changes: 0 additions & 2 deletions .github/workflows/verify-build.yml
Original file line number Diff line number Diff line change
Expand Up @@ -37,8 +37,6 @@ jobs:
name: Unit Tests
needs: basic-checks
runs-on: ${{ github.repository == 'hyperledger/fabric' && 'fabric-ubuntu-22.04' || 'ubuntu-22.04' }}
env:
GOEXPERIMENT: nocoverageredesign
steps:
- uses: actions/checkout@v4
with:
Expand Down
8 changes: 4 additions & 4 deletions core/handlers/library/registry_plugin_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ func buildPlugin(t *testing.T, dest, pkg string) {
require.NoError(t, err, "Could not build plugin: "+string(output))
}

func TestLoadAuthPlugin(t *testing.T) {
func TestLoadAuthPluginNoCover(t *testing.T) {
if noplugin {
t.Skip("plugins disabled")
}
Expand All @@ -65,7 +65,7 @@ func TestLoadAuthPlugin(t *testing.T) {
require.True(t, endorser.invoked, "Expected filter to invoke endorser on invoke")
}

func TestLoadDecoratorPlugin(t *testing.T) {
func TestLoadDecoratorPluginNoCover(t *testing.T) {
if noplugin {
t.Skip("plugins disabled")
}
Expand All @@ -86,7 +86,7 @@ func TestLoadDecoratorPlugin(t *testing.T) {
require.True(t, proto.Equal(decoratedInput, testInput), "Expected chaincode input to remain unchanged")
}

func TestEndorsementPlugin(t *testing.T) {
func TestEndorsementPluginNoCover(t *testing.T) {
if noplugin {
t.Skip("plugins disabled")
}
Expand All @@ -109,7 +109,7 @@ func TestEndorsementPlugin(t *testing.T) {
require.Equal(t, []byte{1, 2, 3}, output)
}

func TestValidationPlugin(t *testing.T) {
func TestValidationPluginNoCover(t *testing.T) {
if noplugin {
t.Skip("plugins disabled")
}
Expand Down
32 changes: 28 additions & 4 deletions scripts/run-unit-tests.sh
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,10 @@ serial_packages=(
"github.com/hyperledger/fabric/gossip/..."
)

no_coverage_packages=(
"github.com/hyperledger/fabric/core/handlers/library"
)

# packages which need to be tested with build tag pkcs11
pkcs11_packages=(
"github.com/hyperledger/fabric/bccsp/factory"
Expand Down Expand Up @@ -132,10 +136,18 @@ serial_test_packages() {
fi
}

no_coverage_test_packages() {
local filter
filter=$(package_filter "${no_coverage_packages[@]}")
if [ -n "$filter" ]; then
join_by $'\n' "$@" | grep -E "$filter" || true
fi
}

# "go test" the provided packages. Packages that are not present in the serial package list
# will be tested in parallel
run_tests() {
local -a flags=("-cover")
local -a flags
if [ -n "${VERBOSE}" ]; then
flags+=("-v")
fi
Expand All @@ -153,21 +165,33 @@ run_tests() {
local -a serial
while IFS= read -r pkg; do serial+=("$pkg"); done < <(serial_test_packages "$@")
if [ "${#serial[@]}" -ne 0 ]; then
go test "${flags[@]}" -failfast -tags "$GO_TAGS" "${serial[@]}" -short -p 1 -timeout=20m
go test -cover "${flags[@]}" -failfast -tags "$GO_TAGS" "${serial[@]}" -short -p 1 -timeout=20m
fi

local -a parallel
while IFS= read -r pkg; do parallel+=("$pkg"); done < <(parallel_test_packages "$@")
if [ "${#parallel[@]}" -ne 0 ]; then
go test "${flags[@]}" "${race_flags[@]}" -tags "$GO_TAGS" "${parallel[@]}" -short -timeout=20m
go test -cover "${flags[@]}" "${race_flags[@]}" -tags "$GO_TAGS" "${parallel[@]}" -short -timeout=20m -skip=NoCover
fi

# The -cover flag changes the import table of the test and the plugin
# so that they cannot interact with each other.
# In the name of tests that work with plugins we added the suffix NoCover
# and do not run these tests with the -cover flag.
# We run such tests separately, without the -cover flag.
local -a no_coverage
while IFS= read -r pkg; do no_coverage+=("$pkg"); done < <(no_coverage_test_packages "$@")
if [ "${#no_coverage[@]}" -ne 0 ]; then
echo "test with no coverage"
go test "${flags[@]}" "${race_flags[@]}" -tags "$GO_TAGS" "${no_coverage[@]}" -short -timeout=20m -run=NoCover
fi
}
}

# "go test" the provided packages and generate code coverage reports.
run_tests_with_coverage() {
# run the tests serially
time go test -p 1 -cover -coverprofile=profile_tmp.cov -tags "$GO_TAGS" "$@" -timeout=20m
time go test -p 1 -cover -coverprofile=profile_tmp.cov -tags "$GO_TAGS" "$@" -timeout=20m -skip=NoCover
tail -n +2 profile_tmp.cov >> profile.cov && rm profile_tmp.cov
}

Expand Down

0 comments on commit d8bb207

Please sign in to comment.