In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-29 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Servers >
Share
Shulou(Shulou.com)06/02 Report--
Author | Liu Lu, Senior Test Development engineer of Aliyun Intelligent Business Group
PouchContainer is Alibaba's open source rich container technology. GA was officially released in September 2018 and has fully reached the production level. PouchContainer has always attached great importance to project quality, and project developers need to provide the corresponding single test and integration test code when submitting the PR. This requirement, on the one hand, ensures the quality of regression, but also reduces the cost of code review and improves the efficiency of cooperation. (more reference: PouchContainer open source version and build consistency practice)
Initially, PouchContainer combined TravisCI and Codecov tools to run tests and demonstrate unit test coverage for each PR submission. For some PR that add integration tests, the test coverage changes brought about by the increase or decrease of integration tests are not included in the test coverage statistics.
The lack of integration test coverage makes developers lack a more complete understanding of project test coverage. In order to more fully demonstrate the test coverage of PouchContainer, now PouchContainer has added the statistical function of integrated test coverage. This paper mainly introduces the implementation of integrated test coverage statistics in PouchContainer.
Go test coverage
Before introducing the implementation of integrated test coverage statistics, we need to understand the principles of Golang coverage statistics. Golang coverage statistics is done by rewriting the source code of the package before compilation, adding statistics, and then compiling, running, and collecting test coverage. About the principle of Go test coverage, please refer to The cover story (https://blog.golang.org/cover), the following content, mainly refer to the above article, and list the implementation process in detail.
First of all, give a Size () function to be tested, which has multiple switch branches, the code is as follows:
Package size
Func Size (an int) string {
Switch {
Case a < 0:
Return "negative"
Case a = = 0:
Return "zero"
Case a < 10:
Return "small"
}
Return "enormous"
}
The corresponding test code is as follows:
$cat size_test.go
Package size
Import (
"testing"
"fmt"
)
Type Test struct {
In int
Out string
}
Var tests = [] Test {
{- 1, "negative"}
{5, "small"}
}
Func TestSize (t * testing.T) {
Fmt.Println ("a")
For I, test: = range tests {
Size: = Size (test.in)
If size! = test.out {
T.Errorf ("#% d: Size (% d) =% s; want% s", I, test.in, size, test.out)
}
}
}
Execute the go test-x-cover-coverprofile=./size.out command, run the test, and count the test coverage. The-x parameter prints the execution process of the above command (note: the information of the printed execution steps is incomplete. If you execute the output steps manually, it will fail because some of the execution steps of go test do not print information), the-cover parameter enables the test coverage statistics function, and the-coverprofile parameter specifies that the test coverage file is stored. The run results are as follows:
$go test-x-cover-coverprofile=./size.out
WORK=/var/folders/d2/0gxc6wf16hb6t8ng0w00czpm0000gn/T/go-build982568783
Mkdir-p $WORK/test/_test/
Mkdir-p $WORK/test/_test/_obj_test/
Cd $WORK/test/_test/_obj_test/
/ usr/local/go/pkg/tool/darwin_amd64/cover-mode set-var GoCover_0-o. Size.go / Users/letty/work/code/go/src/test/size.go
Cd / Users/letty/work/code/go/src/test
/ usr/local/go/pkg/tool/darwin_amd64/compile-o $WORK/test/_test/test.a-trimpath $WORK-p test-complete-buildid 6033df309978241f19d83a0e6bad252ee3ba376e-D _ / Users/letty/work/code/go/src/test-I $WORK-pack $WORK/test/_test/_obj_test/size.go. / size_test.go
Cd $WORK/test/_test
/ usr/local/go/pkg/tool/darwin_amd64/compile-o. / main.a-trimpath $WORK-p main-complete-D ""-I. -I $WORK-pack. / _ testmain.go
Cd.
/ usr/local/go/pkg/tool/darwin_amd64/link-o $WORK/test/_test/test.test-L $WORK/test/_test-L $WORK-w-extld=clang-buildmode=exe $WORK/test/_test/main.a
$WORK/test/_test/test.test-test.coverprofile=./size.out-test.outputdir / Users/letty/work/code/go/src/test
A
PASS
Coverage: 60.0% of statements
Ok test 0.006s
From the penultimate line of the above output, the test coverage is 60%. To analyze the execution steps of go test, the fifth line calls the / usr/local/go/pkg/tool/darwin_amd64/cover tool, which rewrites the source code to be tested and adds counting points to the code to count the test coverage. Lines 8-13 compile the file to be tested and the _ testmain.go file (this file is generated by the go test tool, see https://github.com/golang/go/blob/3f150934e274f9ce167e1ed565fb3e60b8ea8223/src/cmd/go/internal/test/test.go#L1887 for implementation details) to generate the test.test test execution file. Line 13, execute the test.test test file, pass in the relevant parameters of the test, and you can run the test.
View the help information for the cover command, and execute the cover command again to view the rewritten test code:
$cat .size.go
Package size
Func Size (an int) string {
GoCover_0.Count [0] = 1
Switch {
Case a < 0:
GoCover_0.Count [2] = 1
Return "negative"
Case a = = 0:
GoCover_0.Count [3] = 1
Return "zero"
Case a < 10:
GoCover_0.Count [4] = 1
Return "small"
}
GoCover_0.Count [1] = 1
Return "enormous"
}
Var GoCover_0 = struct {
Count [5] uint32
Pos [3 * 5] uint32
NumStmt [5] uint16
} {
Pos: [3 * 5] uint32 {
3, 4, 0x9001a, / / [0]
12, 12, 0x130002, / / [1]
5, 6, 0x14000d, / / [2]
7, 8, 0x10000e, / / [3]
9, 10, 0x11000e, / / [4]
}
NumStmt: [5] uint16 {
1, / / 0
1, / / 1
1, / / 2
1, / / 3
1, / / 4
}
}
View the coverage statistics file after go test runs the test, and the information is as follows:
$cat size.out
Mode: set
Test/size.go:3.26,4.9 1 1
Test/size.go:12.2,12.19 1 0
Test/size.go:5.13,6.20 1 1
Test/size.go:7.14,8.16 1 0
Test/size.go:9.14,10.17 1 1
The first line of the file identifies the coverage statistics mode which provides three modes: set, count and atomic for set,go test:
Set mode only counts whether the statement is running
Count mode counts the number of times the statement is run
The atomic mode is similar to count in that it counts the number of runs of statements and is suitable for multithreaded testing.
The second line begins in the format: name.go:line.column,line.column numberOfStatements count, that is, the file name, the starting position of the code, the number of lines of the statement, and the number of times it has been run. In this sample code, there are 5 lines of statements to be counted, the statistical mode is set, and a total of 3 count are set to 1 (readers can set covermode to count to observe the changes in count output), so the final test coverage is 60%.
PouchContainer test coverage
PouchContainer integrates the CodeCov tool. Each time you run TravisCI, you will upload the test coverage file to the CodeCov website to complete the visual display and continuous tracking of the coverage.
TravisCI and CodeCov can be easily integrated. You only need to generate a coverage.txt name coverage statistics file under the test path, and call the CodeCov script in the .tarvis.yml file to upload the coverage statistics file. For specific commands, please refer to the implementation of TEST_FLAGS= make build-integration-test in Makefile. Interested students can also view the CodeCov script directly to learn about its implementation details.
Next, we elaborate the implementation details of PouchContainer from two aspects: single test and integrated test coverage statistics.
Single-test coverage statistics PouchContianer collection single-test coverage is relatively simple, only need to execute the make unit-test command to achieve coverage statistics collection. For the implementation of single test coverage statistics, you can refer to Makefile. It should be noted that some irrelevant package should be excluded from coverage statistics, such as vendor directory, types directory, etc., otherwise the accuracy of test coverage will be affected. Integration test coverage statistics PouchContainer integration testing is to test daemon API and command line by starting pouch daemon and then executing pouch command line or sending API request directly. Under normal circumstances, the pouch daemon to be tested is compiled by go build, and no counters are inserted into the source code, so the test coverage cannot be counted.
For the PR that counts the test coverage of pouch daemon, see https://github.com/alibaba/pouch/pull/1338. In this PR (the latest code location has changed due to continuous iterations of the code, please refer to the commit code corresponding to this article), we have done the following:
Add main_test.go test files to the root directory
In hack/build script, a new testserver function is added to compile main package and generate executable test files
In the hack/make.sh script, start the test file generated in step 2 in the background and run the API and command line tests
At the end of the test, send a signal to the test process and collect test coverage
Next, we will describe the implementation details in detail. First, add a main_test.go test file and define a test function TestMain in the file, as follows:
Package main
Import (
"os"
"os/signal"
"strings"
"syscall"
"testing"
)
Func TestMain (t * testing.T) {
Var (
Args [] string
)
For _, arg: = range os.Args {
Switch {
Case strings.HasPrefix (arg, "DEVEL"):
Case strings.HasPrefix (arg, "- test"):
Default:
Args = append (args, arg)
}
}
WaitCh: = make (chan int, 1)
Os.Args = args
Go func () {
Main ()
Close (waitCh)
} ()
SignalCh: = make (chan os.Signal, 1)
Signal.Notify (signalCh, syscall.SIGINT, syscall.SIGQUIT, syscall.SIGTERM, syscall.SIGHUP)
Select {
Case
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.