To generate code coverage for GoLang applications using the RKTracer tool, you need to add the RKTracer runtime library to the GoLang installation folder, be it Windows or Linux platform, and instrument the application with the RKTracer tool.
Adding the RKTracer runtime library to the GoLang installation
Add the RKTracer runtime library rk4go to the GOROOT, i.e., the Go installation folder. It is a one-time setup.
Step 1. Open a command prompt and run the command.
go env GOROOT
If GoLang is in the path, then you should see the following output. In my case, it is in path /usr/lib/go-1.13.
Step 2. The command output gives us the path where GoLang is installed, and now create rk4go in the src GoLang installation folder and copy rk4go from the RKTracer installation folder to the rk4go folder.
sudo cp /home/username/share/rktracer/rk4go.go /usr/lib/go-1.13/src/rk4go/
Enable the RKTracer tool
Identify the project folder that needs to be instrumented and generate coverage data. Please make sure the project is configured and you are able to test it.
Then execute the following command to Turn ON (instrument) or Turn OFF (un-instrument) the RKTracer tool. The command will instrument all GoLang source files in a folder recursively.
rktracer -on path-toproject/module/ go -v rktracer -off path-toproject/module/ go -v
The above command will instrument all the GoLang source files in the project folder recursively.
Rebuild the application with the go clean and go build commands.
go clean go build
Run tests on the instrumented application
Testing can be unit-testing or functional or integration testing of the application.
Once the testing is completed, you should see the file rk-coverage.txt file. Now copy this file to the project folder or the path where you have executed the command rktracer -on path-to project/module/ go -v to instrument the project folder using the RKTracer tool.
You should also see the rktracer folder with RKTracer tool intermediate files. The rktracer folder will get generated at the time of the instrumentation. You need to copy the rk-coverage.txt file parallel to the rktracer folder in the project.
Generate code coverage reports
Once you have the rk-coverage.txt file, you need to run the rkresults command to generate HTML reports. Ensure that you run the rkresults command in the application root folder or where you have the rktracer folder generated at the time of build/instrumentation.
rkresults
The rkresults command will search the coverage data file rk-coverage.txt and map with JSON files (generated during instrumentation) in the rktracer folder and generate the HTML reports. You can manually open the HTML report using index.html.
After generating code coverage, make sure to turn off (un-instrument) the RKTracer tool for the application that we instrument to create reports.
rktracer -off path-toproject/module/ go -v
Enable coverage for selected files
Suppose you need code coverage for source files from three different folders, i.e., core, keys, power, and ignore coverage for folders firmware_loader and lockdown. Edit rktracer.config in the RKTracer installation folder and go to the end of the file, add the following information.
ignore *.go instrument */power/* */core/* */keys/* never */firmware_loader/* */lockdown/*
ignore *.goignores all GoLang source files from instrumentation.instrumentinstruments source files from given folders.neverignores the selected folder.
Suppose you need code coverage for selected functions from three different files. Edit rktracer.config in the RKTracer installation folder and go to the end of the file and set the following variables as shown below.
ignore *.go instrument *file-X.go *file-Y.go *file-Z.go function-ignore * function-instrument fun_X() fun_Y() fun_Z()
ignore *.goignores all GoLang programming source files.instrument *file-X.go *file-Y.go *file-Z.goinstruments only these three source files.function-ignore *then ignores all functions in the above three files.function-instrument fun_X() fun_Y() fun_Z()does not ignore these three functions from these three files from instrumentation.