Skip to content

Commit

Permalink
test: add code coverage
Browse files Browse the repository at this point in the history
Signed-off-by: Alessio Greggi <[email protected]>
  • Loading branch information
alegrey91 committed Jul 16, 2023
1 parent d6ff300 commit b6b95b2
Show file tree
Hide file tree
Showing 3 changed files with 114 additions and 2 deletions.
2 changes: 0 additions & 2 deletions .github/workflows/release.yml
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,6 @@ jobs:
go-version: '1.18'
check-latest: true
- run: go version
- name: Test with go cli
run: go test -v ./...
- name: Run GoReleaser
uses: goreleaser/goreleaser-action@b508e2e3ef3b19d4e4146d4f8fb3ba9db644a757 # ratchet:goreleaser/goreleaser-action@v3
with:
Expand Down
95 changes: 95 additions & 0 deletions .github/workflows/tests.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,95 @@
name: tests

on: [push]

jobs:
unit-test:

runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v3

- name: Set up Go
uses: actions/setup-go@v4
with:
go-version: '1.20'

- name: Run Unit-Test
run: |
mkdir /tmp/unit/
go test \
-v \
-cover ./... \
-args -test.gocoverdir=/tmp/unit/
- name: Upload cover profiles
uses: actions/upload-artifact@v3
with:
name: unit-test
path: /tmp/unit/

integration-test:

runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v3

- name: Set up Go
uses: actions/setup-go@v4
with:
go-version: '1.20'

- name: Build coverage-instrumented binary
run: |
go build \
-cover \
-v \
-o fwdctl .
- name: Run integration test
run: |
sudo ./hack/integration-test.sh
- name: Upload cover profiles
uses: actions/upload-artifact@v3
with:
name: integration-test
path: /tmp/integration/

code-coverage:

runs-on: ubuntu-latest
needs: [unit-test,integration-test]
steps:
- uses: actions/checkout@v3

- uses: actions/download-artifact@v3
with:
name: unit-test
path: /tmp/unit-test

- uses: actions/download-artifact@v3
with:
name: integration-test
path: /tmp/integration-test

- name: list files
run: |
ls -lah /tmp/unit-test
ls -lah /tmp/integration-test
- name: Set up Go
uses: actions/setup-go@v4
with:
go-version: '1.20'

- name: Calculate total coverage
run: |
go tool \
covdata \
textfmt \
-i=/tmp/unit-test,/tmp/integration-test \
-o code-coverage
go tool \
cover \
-func code-coverage
19 changes: 19 additions & 0 deletions hack/integration-test.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
#!/bin/sh
#
set -e

# create directory to store cover profiles
INTEGDIR=/tmp/integration
mkdir -p $INTEGDIR
export GOCOVERDIR=$INTEGDIR

# list of subcommands we want to test
command_list="version|list|help|generate rules -O /tmp/|generate systemd -O /tmp/ -f /tmp/rules.yaml -p /tmp/ -t fork|add -d 8080 -i lo -P tcp -s 127.0.0.1 -p 9090|delete -n 1"

IFS="|"
for cmd in $command_list;
do
command="./fwdctl $cmd"
echo "$command"
eval "$command"
done

0 comments on commit b6b95b2

Please sign in to comment.