From adb3beff0c98059543b3b053a4ccf4cbf81be0b4 Mon Sep 17 00:00:00 2001 From: Radostin Stoyanov Date: Sat, 15 Apr 2023 12:54:10 +0100 Subject: [PATCH] test: enable coverage of crit unit tests This moves the unit tests in the crit package and enables code coverage. Signed-off-by: Radostin Stoyanov --- .github/workflows/main.yml | 1 + .gitignore | 1 + crit/Makefile | 20 ++++++++++++++++++-- {test/crit => crit}/stats_test.go | 12 ++++-------- test/Makefile | 3 +++ test/crit/Makefile | 7 ++----- 6 files changed, 29 insertions(+), 15 deletions(-) rename {test/crit => crit}/stats_test.go (63%) diff --git a/.github/workflows/main.yml b/.github/workflows/main.yml index 567fffb9c..db2d4217e 100644 --- a/.github/workflows/main.yml +++ b/.github/workflows/main.yml @@ -54,6 +54,7 @@ jobs: make -C crit gen-proto sudo -E make -C crit all fi + sudo -E make -C crit unit-test sudo -E make -C test crit-test - name: Check code coverage diff --git a/.gitignore b/.gitignore index f5817091e..5fc93424a 100644 --- a/.gitignore +++ b/.gitignore @@ -13,3 +13,4 @@ scripts/*.h scripts/expected.go scripts/output.go crit/bin +crit/test-imgs/ diff --git a/crit/Makefile b/crit/Makefile index 814407ae7..f72082390 100644 --- a/crit/Makefile +++ b/crit/Makefile @@ -1,5 +1,7 @@ GO ?= go +CRIU ?= criu + # The import path that protoc will use if a proto file imports another one import_path := github.com/checkpoint-restore/go-criu/crit/images # Path to .proto source files @@ -39,7 +41,21 @@ gen-proto: bin/crit: cmd/cli.go $(GO) build -o $@ $^ +../test/loop/loop: ../test/loop/loop.c + $(CC) $^ -o $@ + +test-imgs: ../test/loop/loop + $(eval PID := $(shell ../test/loop/loop)) + mkdir -p $@ + $(CRIU) dump -v4 -o dump.log -D $@ -t $(PID) + $(CRIU) restore -v4 -o restore.log -D $@ -d + pkill -9 loop + +unit-test: test-imgs + go test ${GOFLAGS} -v ./... + clean: - rm -f bin/crit + @rm -f bin/crit + @rm -rf test-imgs -.PHONY: all gen-proto update-proto clean +.PHONY: all gen-proto update-proto unit-test clean diff --git a/test/crit/stats_test.go b/crit/stats_test.go similarity index 63% rename from test/crit/stats_test.go rename to crit/stats_test.go index 9072f8748..3da5876d8 100644 --- a/test/crit/stats_test.go +++ b/crit/stats_test.go @@ -1,13 +1,9 @@ -package main +package crit -import ( - "testing" - - "github.com/checkpoint-restore/go-criu/v6/crit" -) +import "testing" func TestGetDumpStats(t *testing.T) { - dumpStats, err := crit.GetDumpStats("test-imgs") + dumpStats, err := GetDumpStats("test-imgs") if err != nil { t.Error("Failed to get stats") } @@ -17,7 +13,7 @@ func TestGetDumpStats(t *testing.T) { } func TestGetRestoreStats(t *testing.T) { - restoreStats, err := crit.GetRestoreStats("test-imgs") + restoreStats, err := GetRestoreStats("test-imgs") if err != nil { t.Error("Failed to get stats") } diff --git a/test/Makefile b/test/Makefile index 7564a07ad..ec194acaf 100644 --- a/test/Makefile +++ b/test/Makefile @@ -88,10 +88,13 @@ coverage: check-go-version $(COVERAGE_BINARIES) $(TEST_PAYLOAD) pkill -9 piggie; \ } cd crit/ && GOCOVERDIR=${COVERAGE_PATH} ./crit-test.coverage + $(MAKE) -C ../crit/ unit-test GOFLAGS="-coverprofile=${COVERAGE_PATH}/coverprofile-crit-unit-test" $(MAKE) -C crit/ clean # Print coverage from this run $(GO) tool covdata percent -i=${COVERAGE_PATH} $(GO) tool covdata textfmt -i=${COVERAGE_PATH} -o ${COVERAGE_PATH}/coverage.out + cat .coverage/coverprofile* | \ + grep -v mode: | sort -r | awk '{if($$1 != last) {print $$0;last=$$1}}' >> ${COVERAGE_PATH}/coverage.out codecov: curl -Os https://uploader.codecov.io/latest/linux/codecov diff --git a/test/crit/Makefile b/test/crit/Makefile index da7e3d4ce..9f32d8d46 100644 --- a/test/crit/Makefile +++ b/test/crit/Makefile @@ -2,10 +2,7 @@ CC ?= gcc GO ?= go CRIU ?= criu -all: unit-test integration-test e2e-test clean - -unit-test: test-imgs - go test -v ./... +all: integration-test e2e-test clean integration-test: test-imgs crit-test @echo "Running integration test" @@ -34,4 +31,4 @@ crit-test: main.go clean: @rm -rf test-imgs -.PHONY: all test unit-test integration-test e2e-test clean +.PHONY: all test integration-test e2e-test clean