From 8512145d7c65d56a35994ec1e3470ce2e2e49af7 Mon Sep 17 00:00:00 2001
From: adrianvillin <avillin@cisco.com>
Date: Thu, 11 Jan 2024 11:59:47 +0100
Subject: [PATCH] hs-test: added targets to makefiles to get coverage from HS
 tests

Type: make

Change-Id: Iae7998692890264dfeea98c165617d0efa024d42
Signed-off-by: adrianvillin <avillin@cisco.com>
---
 Makefile                           | 29 +++++++++++++++++++++++++++++
 extras/hs-test/Makefile            | 23 +++++++++++++++++++++++
 extras/hs-test/script/build_hst.sh |  2 ++
 test/Makefile                      | 15 +++++++++------
 4 files changed, 63 insertions(+), 6 deletions(-)

diff --git a/Makefile b/Makefile
index d41ff36904a..1ab387e94d4 100644
--- a/Makefile
+++ b/Makefile
@@ -245,6 +245,7 @@ help:
 	@echo " build                - build debug binaries"
 	@echo " build-release        - build release binaries"
 	@echo " build-coverity       - build coverity artifacts"
+	@echo " build-vpp-gcov 		 - build gcov vpp only"
 	@echo " rebuild              - wipe and build debug binaries"
 	@echo " rebuild-release      - wipe and build release binaries"
 	@echo " run                  - run debug binary"
@@ -252,6 +253,8 @@ help:
 	@echo " debug                - run debug binary with debugger"
 	@echo " debug-release        - run release binary with debugger"
 	@echo " test                 - build and run tests"
+	@echo " test-cov-hs   		 - build and run host stack tests with coverage"
+	@echo " test-cov-both	  	 - build and run python and host stack tests, merge coverage data"
 	@echo " test-help            - show help on test framework"
 	@echo " run-vat              - run vpp-api-test tool"
 	@echo " pkg-deb              - build DEB packages"
@@ -440,6 +443,10 @@ rebuild: wipe build
 build-release: $(BR)/.deps.ok
 	$(call make,$(PLATFORM),$(addsuffix -install,$(TARGETS)))
 
+.PHONY: build-vpp-gcov
+build-vpp-gcov:
+	$(call test,vpp_gcov)
+
 .PHONY: wipe-release
 wipe-release: test-wipe $(BR)/.deps.ok
 	$(call make,$(PLATFORM),$(addsuffix -wipe,$(TARGETS)))
@@ -486,6 +493,20 @@ test-cov:
 	$(eval TEST_GCOV=1)
 	$(call test,vpp_gcov,cov)
 
+.PHONY: test-cov-hs
+test-cov-hs:
+	@make -C extras/hs-test build-cov
+	@make -C extras/hs-test test-cov
+
+.PHONY: test-cov-both
+test-cov-both:
+	@echo "Running Python, Golang tests and merging coverage reports."
+	find $(BR) -name '*.gcda' -delete
+	@make test-cov
+	find $(BR) -name '*.gcda' -delete
+	@make test-cov-hs
+	@make cov-merge
+
 .PHONY: test-cov-build
 test-cov-build:
 	$(eval CC=gcc)
@@ -502,6 +523,14 @@ test-cov-post:
 	$(eval CC=gcc)
 	$(call test,vpp_gcov,cov-post)
 
+.PHONY: cov-merge
+cov-merge:
+	@lcov --add-tracefile $(BR)/test-coverage-merged/coverage-filtered.info \
+		-a $(BR)/test-coverage-merged/coverage-filtered1.info -o $(BR)/test-coverage-merged/coverage-merged.info
+	@genhtml $(BR)/test-coverage-merged/coverage-merged.info \
+		--output-directory $(BR)/test-coverage-merged/html
+	@echo "Code coverage report is in $(BR)/test-coverage-merged/html/index.html"
+
 .PHONY: test-all
 test-all:
 	$(eval EXTENDED_TESTS=1)
diff --git a/extras/hs-test/Makefile b/extras/hs-test/Makefile
index 0db3958604e..b809b170e00 100644
--- a/extras/hs-test/Makefile
+++ b/extras/hs-test/Makefile
@@ -16,6 +16,10 @@ ifeq ($(TEST),)
 TEST=all
 endif
 
+ifeq ($(TEST-HS),)
+TEST-HS=all
+endif
+
 ifeq ($(DEBUG),)
 DEBUG=false
 endif
@@ -53,6 +57,7 @@ help:
 	@echo " test                 - run tests"
 	@echo " test-debug           - run tests (vpp debug image)"
 	@echo " build                - build test infra"
+	@echo " build-cov			 - coverage build of VPP and Docker images"
 	@echo " build-debug          - build test infra (vpp debug image)"
 	@echo " build-go             - just build golang files"
 	@echo " fixstyle             - format .go source files"
@@ -88,6 +93,10 @@ build-vpp-release:
 build-vpp-debug:
 	@make -C ../.. build
 
+.PHONY: build-vpp-gcov
+build-vpp-gcov:
+	@make -C ../.. build-vpp-gcov
+
 .build.ok: build
 	@touch .build.ok
 
@@ -112,6 +121,14 @@ test-debug: .deps.ok .build_debug.ok
 		--vppsrc=$(VPPSRC) --parallel=$(PARALLEL) --repeat=$(REPEAT)
 	@bash ./script/compress.sh
 
+.PHONY: test-cov
+test-cov: .deps.ok .build.ok
+	-bash ./test --persist=$(PERSIST) --verbose=$(VERBOSE) \
+		--unconfigure=$(UNCONFIGURE) --debug=$(DEBUG) --test=$(TEST-HS) --cpus=$(CPUS) \
+		--vppsrc=$(VPPSRC)
+	@make -C ../.. test-cov-post HS_TEST=1
+	@bash ./script/compress.sh
+
 .PHONY: build-go
 build-go:
 	go build ./tools/http_server
@@ -122,6 +139,12 @@ build: .deps.ok build-vpp-release build-go
 	bash ./script/build_hst.sh release
 	@touch .build.ok
 
+.PHONY: build-cov
+build-cov: .deps.ok build-vpp-gcov build-go
+	@rm -f .build.vpp
+	bash ./script/build_hst.sh gcov
+	@touch .build.vpp
+
 .PHONY: build-debug
 build-debug: .deps.ok build-vpp-debug build-go
 	@rm -f .build.ok
diff --git a/extras/hs-test/script/build_hst.sh b/extras/hs-test/script/build_hst.sh
index cc2d00b6cbd..3b4bc28e275 100755
--- a/extras/hs-test/script/build_hst.sh
+++ b/extras/hs-test/script/build_hst.sh
@@ -21,6 +21,8 @@ export VPP_WS=../..
 
 if [ "$1" == "debug" ]; then
 	VPP_BUILD_ROOT=${VPP_WS}/build-root/build-vpp_debug-native/vpp
+elif [ "$1" == "gcov" ]; then
+  VPP_BUILD_ROOT=${VPP_WS}/build-root/build-vpp_gcov-native/vpp
 else
 	VPP_BUILD_ROOT=${VPP_WS}/build-root/build-vpp-native/vpp
 fi
diff --git a/test/Makefile b/test/Makefile
index 9b9cc178cf6..203a12e6e9b 100644
--- a/test/Makefile
+++ b/test/Makefile
@@ -354,12 +354,12 @@ cov-prep: test-dep
 cov-post: wipe-cov $(BUILD_COV_DIR)
 	@lcov --capture \
 		--directory $(VPP_BUILD_DIR) \
-		--output-file $(BUILD_COV_DIR)/coverage.info
+		--output-file $(BUILD_COV_DIR)/coverage$(HS_TEST).info
 	@test -z "$(EXTERN_COV_DIR)" || \
 		lcov --capture \
 		--directory $(EXTERN_COV_DIR) \
-		--output-file $(BUILD_COV_DIR)/extern-coverage.info
-	@lcov --remove $(BUILD_COV_DIR)/coverage.info \
+		--output-file $(BUILD_COV_DIR)/extern-coverage$(HS_TEST).info
+	@lcov --remove $(BUILD_COV_DIR)/coverage$(HS_TEST).info \
 		"/usr/include/*" "*/build-root/*" "/opt/*" "/usr/lib/*" \
 		"*_test.*" "*vat*" "*rdma*" "*/vpp-api/client/*" "*/plugins/af_packet/*" \
 		"*/plugins/af_xdp/*" "*/plugins/avf/*" "*/plugins/dma_intel/*" \
@@ -367,15 +367,17 @@ cov-post: wipe-cov $(BUILD_COV_DIR)
 		"*/plugins/perfmon/arm*" "*/plugins/perfmon/intel/*" "*/vlib/vmbus/*" \
 		"*/vnet/dev/*" "*/plugins/dev_ena/*" "*/plugins/builtinurl/*" "*/vnet/flow/*" \
 		"*/plugins/http_static/builtinurl/*" "*/plugins/dev_iavf/*" \
-		-o $(BUILD_COV_DIR)/coverage-filtered.info
-	@genhtml $(BUILD_COV_DIR)/coverage-filtered.info \
+		-o $(BUILD_COV_DIR)/coverage-filtered$(HS_TEST).info
+	@genhtml $(BUILD_COV_DIR)/coverage-filtered$(HS_TEST).info \
 		--output-directory $(BUILD_COV_DIR)/html
 	@test -z "$(EXTERN_COV_DIR)" || \
-		genhtml $(BUILD_COV_DIR)/extern-coverage.info \
+		genhtml $(BUILD_COV_DIR)/extern-coverage$(HS_TEST).info \
 			--output-directory $(BUILD_COV_DIR)/extern-html
 	@echo
 	@echo "Build finished. Code coverage report is in $(BUILD_COV_DIR)/html/index.html"
 	@test -z "$(EXTERN_COV_DIR)" || echo "Code coverage report for out-of-tree objects is in $(BUILD_COV_DIR)/extern-html/index.html"
+	@mkdir -p $(BR)/test-coverage-merged
+	@cp -f $(BUILD_COV_DIR)/coverage-filtered$(HS_TEST).info $(BR)/test-coverage-merged
 
 .PHONY: cov
 cov:
@@ -434,6 +436,7 @@ help:
 	@echo " test-cov-prep          - coverage phase #1 : prepare lcov"
 	@echo " test-cov-build         - coverage phase #2 : build gcov image & run tests against it (use TEST=)"
 	@echo " test-cov-post          - coverage phase #3 : generate lcov html report"
+	@echo " test-cov-both          - generate and merge code coverage report for Python and Golang tests"
 	@echo " test-all               - build and run functional and extended tests"
 	@echo " test-all-debug         - build and run functional and extended tests (debug build)"
 	@echo " test-all-cov           - generate code coverage report for functional and extended tests"
-- 
2.16.6