Skip to content

Commit 8b11133

Browse files
authored
Merge pull request #58 from adrianreber/2021-10-29-coverage
Run code coverage tests and upload to codecov
2 parents df3662d + 83407fb commit 8b11133

File tree

8 files changed

+158
-9
lines changed

8 files changed

+158
-9
lines changed

.github/workflows/main.yml

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ jobs:
88
strategy:
99
fail-fast: false
1010
matrix:
11-
go-version: [1.14.x, 1.15.x, 1.16.x]
11+
go-version: [1.15.x, 1.16.x, 1.17.x]
1212
criu_branch: [master, criu-dev]
1313

1414
steps:
@@ -35,3 +35,11 @@ jobs:
3535
sudo make test phaul-test
3636
# This builds crit-go
3737
sudo make -C crit-go/magic-gen build magicgen test
38+
39+
- name: Check code coverage
40+
if: matrix.go-version == '1.17.x' && matrix.criu_branch == 'criu-dev'
41+
run: |
42+
# Run actual test as root as it uses CRIU.
43+
sudo make coverage
44+
# Upload coverage results to codecov
45+
sudo -E make codecov

.github/workflows/verify.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ jobs:
99
- uses: golangci/golangci-lint-action@v2
1010
with:
1111
# must be specified without patch version
12-
version: v1.36
12+
version: v1.42
1313
# Only show new issues for a pull request.
1414
only-new-issues: true
1515

.gitignore

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
11
test/test
2+
test/test.coverage
23
test/piggie/piggie
3-
test/phaul
4+
test/phaul/phaul
5+
test/phaul/phaul.coverage
46
image

Makefile

Lines changed: 45 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
1+
SHELL = /bin/bash
12
GO ?= go
23
CC ?= gcc
4+
COVERAGE_PATH ?= $(shell pwd)/.coverage
35

46
all: build test phaul-test
57

@@ -9,13 +11,15 @@ lint:
911
build:
1012
$(GO) build -v ./...
1113

12-
TEST_BINARIES := test/test test/piggie/piggie test/phaul/phaul
14+
TEST_PAYLOAD := test/piggie/piggie
15+
TEST_BINARIES := test/test $(TEST_PAYLOAD) test/phaul/phaul
16+
COVERAGE_BINARIES := test/test.coverage test/phaul/phaul.coverage
1317
test-bin: $(TEST_BINARIES)
1418

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

18-
test/test: test/*.go
22+
test/test: test/main.go
1923
$(GO) build -v -o $@ $^
2024

2125
test: $(TEST_BINARIES)
@@ -27,7 +31,7 @@ test: $(TEST_BINARIES)
2731
}
2832
rm -rf image
2933

30-
test/phaul/phaul: test/phaul/*.go
34+
test/phaul/phaul: test/phaul/main.go
3135
$(GO) build -v -o $@ $^
3236

3337
phaul-test: $(TEST_BINARIES)
@@ -37,9 +41,39 @@ phaul-test: $(TEST_BINARIES)
3741
pkill -9 piggie; \
3842
}
3943

44+
test/test.coverage: test/*.go
45+
$(GO) test \
46+
-covermode=count \
47+
-coverpkg=./... \
48+
-mod=vendor \
49+
-tags coverage \
50+
-buildmode=pie -c -o $@ $^
51+
52+
test/phaul/phaul.coverage: test/phaul/*.go
53+
$(GO) test \
54+
-covermode=count \
55+
-coverpkg=./... \
56+
-mod=vendor \
57+
-tags coverage \
58+
-buildmode=pie -c -o $@ $^
59+
60+
coverage: $(COVERAGE_BINARIES) $(TEST_PAYLOAD)
61+
mkdir -p $(COVERAGE_PATH)
62+
mkdir -p image
63+
PID=$$(test/piggie/piggie) && { \
64+
test/test.coverage -test.coverprofile=coverprofile.integration.$$RANDOM -test.outputdir=${COVERAGE_PATH} COVERAGE dump $$PID image && \
65+
test/test.coverage -test.coverprofile=coverprofile.integration.$$RANDOM -test.outputdir=${COVERAGE_PATH} COVERAGE restore image; \
66+
pkill -9 piggie; \
67+
}
68+
rm -rf image
69+
PID=$$(test/piggie/piggie) && { \
70+
test/phaul/phaul.coverage -test.coverprofile=coverprofile.integration.$$RANDOM -test.outputdir=${COVERAGE_PATH} COVERAGE $$PID; \
71+
pkill -9 piggie; \
72+
}
73+
4074
clean:
41-
@rm -f $(TEST_BINARIES)
42-
@rm -rf image
75+
@rm -f $(TEST_BINARIES) $(COVERAGE_BINARIES) codecov
76+
@rm -rf image $(COVERAGE_PATH)
4377

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

61-
.PHONY: build test phaul-test test-bin clean lint vendor
95+
codecov:
96+
curl -Os https://uploader.codecov.io/latest/linux/codecov
97+
chmod +x codecov
98+
./codecov -f '.coverage/*'
99+
100+
.PHONY: build test phaul-test test-bin clean lint vendor coverage codecov

test/main_coverage_test.go

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
// +build coverage
2+
3+
package main
4+
5+
import (
6+
"os"
7+
"os/signal"
8+
"strings"
9+
"testing"
10+
)
11+
12+
// NOTE: do not use this in production. Binaries built with this file are
13+
// merely useful to collect coverage data.
14+
func TestCoverageMain(_ *testing.T) {
15+
var args []string
16+
17+
for _, arg := range os.Args {
18+
switch {
19+
case strings.HasPrefix(arg, "COVERAGE"):
20+
// Dummy argument to enable global flags.
21+
case strings.HasPrefix(arg, "-test"):
22+
// Make sure we don't pass `go test` specific flags to
23+
// main.
24+
default:
25+
args = append(args, arg)
26+
}
27+
}
28+
29+
signal.Reset()
30+
os.Args = args
31+
main() // "run" to
32+
33+
// Make sure that std{err,out} write to /dev/null so we prevent the
34+
// testing backend to print "PASS" along with the coverage. We really
35+
// want the coverage to be set via the `-test.coverprofile=$path` flag.
36+
null, _ := os.Open(os.DevNull)
37+
os.Stdout = null
38+
os.Stderr = null
39+
}

test/main_test.go

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
package main_test
2+
3+
import (
4+
"testing"
5+
)
6+
7+
func TestMain(t *testing.T) {
8+
// Do nothing. We just need dummy to make `ginkgo` happy. Without that,
9+
// `ginkgo` would try to execute the _coverage_test.go _despite_ the
10+
t.Parallel()
11+
}

test/phaul/main_coverage_test.go

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
// +build coverage
2+
3+
package main
4+
5+
import (
6+
"os"
7+
"os/signal"
8+
"strings"
9+
"testing"
10+
)
11+
12+
// NOTE: do not use this in production. Binaries built with this file are
13+
// merely useful to collect coverage data.
14+
func TestCoverageMain(_ *testing.T) {
15+
var args []string
16+
17+
for _, arg := range os.Args {
18+
switch {
19+
case strings.HasPrefix(arg, "COVERAGE"):
20+
// Dummy argument to enable global flags.
21+
case strings.HasPrefix(arg, "-test"):
22+
// Make sure we don't pass `go test` specific flags to
23+
// main.
24+
default:
25+
args = append(args, arg)
26+
}
27+
}
28+
29+
signal.Reset()
30+
os.Args = args
31+
main() // "run" it
32+
33+
// Make sure that std{err,out} write to /dev/null so we prevent the
34+
// testing backend to print "PASS" along with the coverage. We really
35+
// want the coverage to be set via the `-test.coverprofile=$path` flag.
36+
null, _ := os.Open(os.DevNull)
37+
os.Stdout = null
38+
os.Stderr = null
39+
}

test/phaul/main_test.go

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
package main_test
2+
3+
import (
4+
"testing"
5+
)
6+
7+
func TestMain(t *testing.T) {
8+
// Do nothing. We just need dummy to make `ginkgo` happy. Without that,
9+
// `ginkgo` would try to execute the _coverage_test.go _despite_ the
10+
t.Parallel()
11+
}

0 commit comments

Comments
 (0)