Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

*: support generate the test coverage file and using it in ci #7991

Merged
merged 3 commits into from
May 17, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 21 additions & 8 deletions .github/workflows/pd-tests.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -25,24 +25,33 @@ jobs:
strategy:
fail-fast: true
matrix:
worker_id: [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13]
include:
- worker_id: 1
name: 'Unit Test(1)'
- worker_id: 2
name: 'Unit Test(2)'
- worker_id: 3
name: 'Tools Test'
- worker_id: 4
name: 'Client Integration Test'
- worker_id: 5
name: 'TSO Integration Test'
- worker_id: 6
name: 'MicroService Integration Test'
outputs:
job-total: 13
job-total: 6
steps:
- name: Checkout code
uses: actions/checkout@v4
- uses: actions/setup-go@v5
with:
go-version: '1.21'
- name: Make Test
- name: ${{ matrix.name }}
env:
WORKER_ID: ${{ matrix.worker_id }}
WORKER_COUNT: 13
JOB_COUNT: 9 # 10 is tools test, 11, 12, 13 are for other integrations jobs
run: |
make ci-test-job JOB_COUNT=$(($JOB_COUNT)) JOB_INDEX=$WORKER_ID
make ci-test-job JOB_INDEX=$WORKER_ID
mv covprofile covprofile_$WORKER_ID
sed -i "/failpoint_binding/d" covprofile_$WORKER_ID
- name: Upload coverage result ${{ matrix.worker_id }}
uses: actions/upload-artifact@v4
with:
Expand All @@ -62,7 +71,11 @@ jobs:
- name: Merge
env:
TOTAL_JOBS: ${{needs.chunks.outputs.job-total}}
run: for i in $(seq 1 $TOTAL_JOBS); do cat covprofile_$i >> covprofile; done
run: |
for i in $(seq 1 $TOTAL_JOBS); do cat covprofile_$i >> covprofile; done
sed -i "/failpoint_binding/d" covprofile
# only keep the first line(`mode: aomic`) of the coverage profile
sed -i '2,${/mode: atomic/d;}' covprofile
- name: Send coverage
uses: codecov/[email protected]
with:
Expand Down
4 changes: 2 additions & 2 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -127,7 +127,7 @@ regions-dump:
stores-dump:
cd tools && CGO_ENABLED=0 go build -gcflags '$(GCFLAGS)' -ldflags '$(LDFLAGS)' -o $(BUILD_BIN_PATH)/stores-dump stores-dump/main.go
pd-ut: pd-xprog
cd tools && GOEXPERIMENT=$(BUILD_GOEXPERIMENT) CGO_ENABLED=$(BUILD_TOOL_CGO_ENABLED) go build -gcflags '$(GCFLAGS)' -ldflags '$(LDFLAGS)' -o $(BUILD_BIN_PATH)/pd-ut pd-ut/ut.go
cd tools && GOEXPERIMENT=$(BUILD_GOEXPERIMENT) CGO_ENABLED=$(BUILD_TOOL_CGO_ENABLED) go build -gcflags '$(GCFLAGS)' -ldflags '$(LDFLAGS)' -o $(BUILD_BIN_PATH)/pd-ut pd-ut/ut.go pd-ut/coverProfile.go
pd-xprog:
cd tools && GOEXPERIMENT=$(BUILD_GOEXPERIMENT) CGO_ENABLED=$(BUILD_TOOL_CGO_ENABLED) go build -tags xprog -gcflags '$(GCFLAGS)' -ldflags '$(LDFLAGS)' -o $(BUILD_BIN_PATH)/xprog pd-ut/xprog.go

Expand Down Expand Up @@ -251,7 +251,7 @@ basic-test: install-tools
go test $(BASIC_TEST_PKGS) || { $(FAILPOINT_DISABLE); exit 1; }
@$(FAILPOINT_DISABLE)

ci-test-job: install-tools dashboard-ui
ci-test-job: install-tools dashboard-ui pd-ut
@$(FAILPOINT_ENABLE)
./scripts/ci-subtask.sh $(JOB_COUNT) $(JOB_INDEX) || { $(FAILPOINT_DISABLE); exit 1; }
@$(FAILPOINT_DISABLE)
Expand Down
6 changes: 0 additions & 6 deletions codecov.yml
Original file line number Diff line number Diff line change
Expand Up @@ -24,9 +24,3 @@ flag_management:
target: 74% # increase it if you want to enforce higher coverage for project, current setting as 74% is for do not let the error be reported and lose the meaning of warning.
- type: patch
target: 74% # increase it if you want to enforce higher coverage for project, current setting as 74% is for do not let the error be reported and lose the meaning of warning.

ignore:
# Ignore the tool tests
- tests/dashboard
- tests/pdbackup
- tests/pdctl
88 changes: 29 additions & 59 deletions scripts/ci-subtask.sh
Original file line number Diff line number Diff line change
Expand Up @@ -3,63 +3,33 @@
# ./ci-subtask.sh <TOTAL_TASK_N> <TASK_INDEX>

ROOT_PATH_COV=$(pwd)/covprofile

if [[ $2 -gt 9 ]]; then
# run tools tests
if [[ $2 -eq 10 ]]; then
# Currently, we only have 3 integration tests, so we can hardcode the task index.
integrations_dir=$(pwd)/tests/integrations

case $1 in
1)
# unit tests ignore `tests`
./bin/pd-ut run --race --ignore tests --coverprofile $ROOT_PATH_COV || exit 1
;;
2)
# unit tests only in `tests`
./bin/pd-ut run tests --race --coverprofile $ROOT_PATH_COV || exit 1
;;
3)
# tools tests
cd ./tools && make ci-test-job && cat covprofile >> $ROOT_PATH_COV || exit 1
exit
fi

# Currently, we only have 3 integration tests, so we can hardcode the task index.
integrations_dir=$(pwd)/tests/integrations
integrations_tasks=($(find "$integrations_dir" -mindepth 1 -maxdepth 1 -type d))
for t in "${integrations_tasks[@]}"; do
if [[ "$t" = "$integrations_dir/client" && $2 -eq 11 ]]; then
cd ./client && make ci-test-job && cat covprofile >> $ROOT_PATH_COV || exit 1
cd $integrations_dir && make ci-test-job test_name=client && cat ./client/covprofile >> $ROOT_PATH_COV || exit 1
elif [[ "$t" = "$integrations_dir/tso" && $2 -eq 12 ]]; then
cd $integrations_dir && make ci-test-job test_name=tso && cat ./tso/covprofile >> $ROOT_PATH_COV || exit 1
elif [[ "$t" = "$integrations_dir/mcs" && $2 -eq 13 ]]; then
cd $integrations_dir && make ci-test-job test_name=mcs && cat ./mcs/covprofile >> $ROOT_PATH_COV || exit 1
fi
done
else
# Get package test list.
packages=($(go list ./...))
dirs=($(find . -iname "*_test.go" -exec dirname {} \; | sort -u | sed -e "s/^\./github.com\/tikv\/pd/"))
tasks=($(comm -12 <(printf "%s\n" "${packages[@]}") <(printf "%s\n" "${dirs[@]}")))

weight() {
[[ $1 == "github.com/tikv/pd/server/api" ]] && return 30
[[ $1 == "github.com/tikv/pd/pkg/schedule" ]] && return 30
[[ $1 == "github.com/tikv/pd/pkg/core" ]] && return 30
[[ $1 == "github.com/tikv/pd/tests/server/api" ]] && return 30
[[ $1 =~ "pd/tests" ]] && return 5
return 1
}

# Create an associative array to store the weight of each task.
declare -A task_weights
for t in ${tasks[@]}; do
weight $t
task_weights[$t]=$?
done

# Sort tasks by weight in descending order.
tasks=($(printf "%s\n" "${tasks[@]}" | sort -rn))

scores=($(seq "$1" | xargs -I{} echo 0))

res=()
for t in ${tasks[@]}; do
min_i=0
for i in ${!scores[@]}; do
[[ ${scores[i]} -lt ${scores[$min_i]} ]] && min_i=$i
done
scores[$min_i]=$((${scores[$min_i]} + ${task_weights[$t]}))
[[ $(($min_i + 1)) -eq $2 ]] && res+=($t)
done

CGO_ENABLED=1 go test -timeout=15m -tags deadlock -race -cover -covermode=atomic -coverprofile=$ROOT_PATH_COV -coverpkg=./... ${res[@]}
fi
;;
4)
# integration test client
cd ./client && make ci-test-job && cat covprofile >> $ROOT_PATH_COV || exit 1
cd $integrations_dir && make ci-test-job test_name=client && cat ./client/covprofile >> $ROOT_PATH_COV || exit 1
;;
5)
# integration test tso
cd $integrations_dir && make ci-test-job test_name=tso && cat ./tso/covprofile >> $ROOT_PATH_COV || exit 1
;;
6)
# integration test mcs
cd $integrations_dir && make ci-test-job test_name=mcs && cat ./mcs/covprofile >> $ROOT_PATH_COV || exit 1
;;
esac
2 changes: 1 addition & 1 deletion tests/scheduling_cluster.go
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,7 @@ func (tc *TestSchedulingCluster) WaitForPrimaryServing(re *require.Assertions) *
}
}
return false
}, testutil.WithWaitFor(5*time.Second), testutil.WithTickInterval(50*time.Millisecond))
}, testutil.WithWaitFor(10*time.Second), testutil.WithTickInterval(50*time.Millisecond))

return primary
}
Expand Down
7 changes: 3 additions & 4 deletions tests/server/cluster/cluster_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -753,20 +753,19 @@ func TestConcurrentHandleRegion(t *testing.T) {
re.NoError(err)
peerID, err := id.Alloc()
re.NoError(err)
regionID, err := id.Alloc()
re.NoError(err)
peer := &metapb.Peer{Id: peerID, StoreId: store.GetId()}
regionReq := &pdpb.RegionHeartbeatRequest{
Header: testutil.NewRequestHeader(clusterID),
Region: &metapb.Region{
Id: regionID,
// mock error msg to trigger stream.Recv()
Id: 0,
Peers: []*metapb.Peer{peer},
},
Leader: peer,
}
err = stream.Send(regionReq)
re.NoError(err)
// make sure the first store can receive one response
// make sure the first store can receive one response(error msg)
if i == 0 {
wg.Add(1)
}
Expand Down
2 changes: 1 addition & 1 deletion tests/testutil.go
Original file line number Diff line number Diff line change
Expand Up @@ -157,7 +157,7 @@ func WaitForPrimaryServing(re *require.Assertions, serverMap map[string]bs.Serve
}
}
return false
}, testutil.WithWaitFor(5*time.Second), testutil.WithTickInterval(50*time.Millisecond))
}, testutil.WithWaitFor(10*time.Second), testutil.WithTickInterval(50*time.Millisecond))

return primary
}
Expand Down
2 changes: 1 addition & 1 deletion tools/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ require (
go.uber.org/goleak v1.3.0
go.uber.org/zap v1.27.0
golang.org/x/text v0.14.0
golang.org/x/tools v0.14.0
google.golang.org/grpc v1.62.1
)

Expand Down Expand Up @@ -172,7 +173,6 @@ require (
golang.org/x/sync v0.6.0 // indirect
golang.org/x/sys v0.18.0 // indirect
golang.org/x/time v0.5.0 // indirect
golang.org/x/tools v0.14.0 // indirect
google.golang.org/appengine v1.6.8 // indirect
google.golang.org/genproto v0.0.0-20240401170217-c3f982113cda // indirect
google.golang.org/genproto/googleapis/api v0.0.0-20240401170217-c3f982113cda // indirect
Expand Down
4 changes: 4 additions & 0 deletions tools/pd-ut/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -63,4 +63,8 @@ pd-ut run --junitfile xxx

// test with race flag
pd-ut run --race

// test with coverprofile
pd-ut run --coverprofile xxx
go tool cover --func=xxx
```
Loading