Skip to content

Commit

Permalink
Run code coverage tests and upload to codecov
Browse files Browse the repository at this point in the history
Signed-off-by: Adrian Reber <areber@redhat.com>
  • Loading branch information
adrianreber committed Oct 29, 2021
1 parent 130a18c commit 83407fb
Show file tree
Hide file tree
Showing 8 changed files with 158 additions and 9 deletions.
10 changes: 9 additions & 1 deletion .github/workflows/main.yml
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ jobs:
strategy:
fail-fast: false
matrix:
go-version: [1.14.x, 1.15.x, 1.16.x]
go-version: [1.15.x, 1.16.x, 1.17.x]
criu_branch: [master, criu-dev]

steps:
Expand All @@ -35,3 +35,11 @@ jobs:
sudo make test phaul-test
# This builds crit-go
sudo make -C crit-go/magic-gen build magicgen test
- name: Check code coverage
if: matrix.go-version == '1.17.x' && matrix.criu_branch == 'criu-dev'
run: |
# Run actual test as root as it uses CRIU.
sudo make coverage
# Upload coverage results to codecov
sudo -E make codecov
2 changes: 1 addition & 1 deletion .github/workflows/verify.yml
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ jobs:
- uses: golangci/golangci-lint-action@v2
with:
# must be specified without patch version
version: v1.36
version: v1.42
# Only show new issues for a pull request.
only-new-issues: true

Expand Down
4 changes: 3 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
test/test
test/test.coverage
test/piggie/piggie
test/phaul
test/phaul/phaul
test/phaul/phaul.coverage
image
51 changes: 45 additions & 6 deletions Makefile
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
SHELL = /bin/bash
GO ?= go
CC ?= gcc
COVERAGE_PATH ?= $(shell pwd)/.coverage

all: build test phaul-test

Expand All @@ -9,13 +11,15 @@ lint:
build:
$(GO) build -v ./...

TEST_BINARIES := test/test test/piggie/piggie test/phaul/phaul
TEST_PAYLOAD := test/piggie/piggie
TEST_BINARIES := test/test $(TEST_PAYLOAD) test/phaul/phaul
COVERAGE_BINARIES := test/test.coverage test/phaul/phaul.coverage
test-bin: $(TEST_BINARIES)

test/piggie/piggie: test/piggie/piggie.c
$(CC) $^ -o $@

test/test: test/*.go
test/test: test/main.go
$(GO) build -v -o $@ $^

test: $(TEST_BINARIES)
Expand All @@ -27,7 +31,7 @@ test: $(TEST_BINARIES)
}
rm -rf image

test/phaul/phaul: test/phaul/*.go
test/phaul/phaul: test/phaul/main.go
$(GO) build -v -o $@ $^

phaul-test: $(TEST_BINARIES)
Expand All @@ -37,9 +41,39 @@ phaul-test: $(TEST_BINARIES)
pkill -9 piggie; \
}

test/test.coverage: test/*.go
$(GO) test \
-covermode=count \
-coverpkg=./... \
-mod=vendor \
-tags coverage \
-buildmode=pie -c -o $@ $^

test/phaul/phaul.coverage: test/phaul/*.go
$(GO) test \
-covermode=count \
-coverpkg=./... \
-mod=vendor \
-tags coverage \
-buildmode=pie -c -o $@ $^

coverage: $(COVERAGE_BINARIES) $(TEST_PAYLOAD)
mkdir -p $(COVERAGE_PATH)
mkdir -p image
PID=$$(test/piggie/piggie) && { \
test/test.coverage -test.coverprofile=coverprofile.integration.$$RANDOM -test.outputdir=${COVERAGE_PATH} COVERAGE dump $$PID image && \
test/test.coverage -test.coverprofile=coverprofile.integration.$$RANDOM -test.outputdir=${COVERAGE_PATH} COVERAGE restore image; \
pkill -9 piggie; \
}
rm -rf image
PID=$$(test/piggie/piggie) && { \
test/phaul/phaul.coverage -test.coverprofile=coverprofile.integration.$$RANDOM -test.outputdir=${COVERAGE_PATH} COVERAGE $$PID; \
pkill -9 piggie; \
}

clean:
@rm -f $(TEST_BINARIES)
@rm -rf image
@rm -f $(TEST_BINARIES) $(COVERAGE_BINARIES) codecov
@rm -rf image $(COVERAGE_PATH)

rpc/rpc.proto:
curl -sSL https://raw.githubusercontent.com/checkpoint-restore/criu/master/images/rpc.proto -o $@
Expand All @@ -58,4 +92,9 @@ vendor:
GO111MODULE=on $(GO) mod vendor
GO111MODULE=on $(GO) mod verify

.PHONY: build test phaul-test test-bin clean lint vendor
codecov:
curl -Os https://uploader.codecov.io/latest/linux/codecov
chmod +x codecov
./codecov -f '.coverage/*'

.PHONY: build test phaul-test test-bin clean lint vendor coverage codecov
39 changes: 39 additions & 0 deletions test/main_coverage_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
// +build coverage

package main

import (
"os"
"os/signal"
"strings"
"testing"
)

// NOTE: do not use this in production. Binaries built with this file are
// merely useful to collect coverage data.
func TestCoverageMain(_ *testing.T) {
var args []string

for _, arg := range os.Args {
switch {
case strings.HasPrefix(arg, "COVERAGE"):
// Dummy argument to enable global flags.
case strings.HasPrefix(arg, "-test"):
// Make sure we don't pass `go test` specific flags to
// main.
default:
args = append(args, arg)
}
}

signal.Reset()
os.Args = args
main() // "run" to

// Make sure that std{err,out} write to /dev/null so we prevent the
// testing backend to print "PASS" along with the coverage. We really
// want the coverage to be set via the `-test.coverprofile=$path` flag.
null, _ := os.Open(os.DevNull)
os.Stdout = null
os.Stderr = null
}
11 changes: 11 additions & 0 deletions test/main_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package main_test

import (
"testing"
)

func TestMain(t *testing.T) {
// Do nothing. We just need dummy to make `ginkgo` happy. Without that,
// `ginkgo` would try to execute the _coverage_test.go _despite_ the
t.Parallel()
}
39 changes: 39 additions & 0 deletions test/phaul/main_coverage_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
// +build coverage

package main

import (
"os"
"os/signal"
"strings"
"testing"
)

// NOTE: do not use this in production. Binaries built with this file are
// merely useful to collect coverage data.
func TestCoverageMain(_ *testing.T) {
var args []string

for _, arg := range os.Args {
switch {
case strings.HasPrefix(arg, "COVERAGE"):
// Dummy argument to enable global flags.
case strings.HasPrefix(arg, "-test"):
// Make sure we don't pass `go test` specific flags to
// main.
default:
args = append(args, arg)
}
}

signal.Reset()
os.Args = args
main() // "run" it

// Make sure that std{err,out} write to /dev/null so we prevent the
// testing backend to print "PASS" along with the coverage. We really
// want the coverage to be set via the `-test.coverprofile=$path` flag.
null, _ := os.Open(os.DevNull)
os.Stdout = null
os.Stderr = null
}
11 changes: 11 additions & 0 deletions test/phaul/main_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package main_test

import (
"testing"
)

func TestMain(t *testing.T) {
// Do nothing. We just need dummy to make `ginkgo` happy. Without that,
// `ginkgo` would try to execute the _coverage_test.go _despite_ the
t.Parallel()
}

0 comments on commit 83407fb

Please sign in to comment.