Skip to content

Commit e343971

Browse files
pfi79denyeart
authored andcommitted
fix fail test with cover flag (release-2.5)
Backport of hyperledger#5117. Signed-off-by: Fedor Partanskiy <fedor.partanskiy@atme.com>
1 parent 0ed9b98 commit e343971

File tree

3 files changed

+32
-10
lines changed

3 files changed

+32
-10
lines changed

.github/workflows/verify-build.yml

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -34,8 +34,6 @@ jobs:
3434
name: Unit Tests
3535
needs: basic-checks
3636
runs-on: ${{ github.repository == 'hyperledger/fabric' && 'fabric-ubuntu-20.04' || 'ubuntu-20.04' }}
37-
env:
38-
GOEXPERIMENT: nocoverageredesign
3937
steps:
4038
- uses: actions/checkout@v4
4139
name: Checkout Fabric Code

core/handlers/library/registry_plugin_test.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ func buildPlugin(t *testing.T, dest, pkg string) {
4747
require.NoError(t, err, "Could not build plugin: "+string(output))
4848
}
4949

50-
func TestLoadAuthPlugin(t *testing.T) {
50+
func TestLoadAuthPluginNoCover(t *testing.T) {
5151
if noplugin {
5252
t.Skip("plugins disabled")
5353
}
@@ -69,7 +69,7 @@ func TestLoadAuthPlugin(t *testing.T) {
6969
require.True(t, endorser.invoked, "Expected filter to invoke endorser on invoke")
7070
}
7171

72-
func TestLoadDecoratorPlugin(t *testing.T) {
72+
func TestLoadDecoratorPluginNoCover(t *testing.T) {
7373
if noplugin {
7474
t.Skip("plugins disabled")
7575
}
@@ -92,7 +92,7 @@ func TestLoadDecoratorPlugin(t *testing.T) {
9292
require.True(t, proto.Equal(decoratedInput, testInput), "Expected chaincode input to remain unchanged")
9393
}
9494

95-
func TestEndorsementPlugin(t *testing.T) {
95+
func TestEndorsementPluginNoCover(t *testing.T) {
9696
if noplugin {
9797
t.Skip("plugins disabled")
9898
}
@@ -117,7 +117,7 @@ func TestEndorsementPlugin(t *testing.T) {
117117
require.Equal(t, []byte{1, 2, 3}, output)
118118
}
119119

120-
func TestValidationPlugin(t *testing.T) {
120+
func TestValidationPluginNoCover(t *testing.T) {
121121
if noplugin {
122122
t.Skip("plugins disabled")
123123
}

scripts/run-unit-tests.sh

Lines changed: 28 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,10 @@ serial_packages=(
1818
"github.com/hyperledger/fabric/gossip/..."
1919
)
2020

21+
no_coverage_packages=(
22+
"github.com/hyperledger/fabric/core/handlers/library"
23+
)
24+
2125
# packages which need to be tested with build tag pkcs11
2226
pkcs11_packages=(
2327
"github.com/hyperledger/fabric/bccsp/factory"
@@ -132,10 +136,18 @@ serial_test_packages() {
132136
fi
133137
}
134138

139+
no_coverage_test_packages() {
140+
local filter
141+
filter=$(package_filter "${no_coverage_packages[@]}")
142+
if [ -n "$filter" ]; then
143+
join_by $'\n' "$@" | grep -E "$filter" || true
144+
fi
145+
}
146+
135147
# "go test" the provided packages. Packages that are not present in the serial package list
136148
# will be tested in parallel
137149
run_tests() {
138-
local -a flags=("-cover")
150+
local -a flags
139151
if [ -n "${VERBOSE}" ]; then
140152
flags+=("-v")
141153
fi
@@ -153,21 +165,33 @@ run_tests() {
153165
local -a serial
154166
while IFS= read -r pkg; do serial+=("$pkg"); done < <(serial_test_packages "$@")
155167
if [ "${#serial[@]}" -ne 0 ]; then
156-
go test "${flags[@]}" -failfast -tags "$GO_TAGS" "${serial[@]}" -short -p 1 -timeout=20m
168+
go test -cover "${flags[@]}" -failfast -tags "$GO_TAGS" "${serial[@]}" -short -p 1 -timeout=20m
157169
fi
158170

159171
local -a parallel
160172
while IFS= read -r pkg; do parallel+=("$pkg"); done < <(parallel_test_packages "$@")
161173
if [ "${#parallel[@]}" -ne 0 ]; then
162-
go test "${flags[@]}" "${race_flags[@]}" -tags "$GO_TAGS" "${parallel[@]}" -short -timeout=20m
174+
go test -cover "${flags[@]}" "${race_flags[@]}" -tags "$GO_TAGS" "${parallel[@]}" -short -timeout=20m -skip=NoCover
175+
fi
176+
177+
# The -cover flag changes the import table of the test and the plugin
178+
# so that they cannot interact with each other.
179+
# In the name of tests that work with plugins we added the suffix NoCover
180+
# and do not run these tests with the -cover flag.
181+
# We run such tests separately, without the -cover flag.
182+
local -a no_coverage
183+
while IFS= read -r pkg; do no_coverage+=("$pkg"); done < <(no_coverage_test_packages "$@")
184+
if [ "${#no_coverage[@]}" -ne 0 ]; then
185+
echo "test with no coverage"
186+
go test "${flags[@]}" "${race_flags[@]}" -tags "$GO_TAGS" "${no_coverage[@]}" -short -timeout=20m -run=NoCover
163187
fi
164188
}
165189
}
166190

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

0 commit comments

Comments
 (0)