Skip to content

Commit

Permalink
Add test coverage GH action
Browse files Browse the repository at this point in the history
  • Loading branch information
efiacor committed May 28, 2024
1 parent a03a8d2 commit 1644410
Show file tree
Hide file tree
Showing 5 changed files with 135 additions and 19 deletions.
64 changes: 64 additions & 0 deletions .github/.testcoverage.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
# Copyright 2024 The Nephio Authors
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

# (mandatory)
# Path to coverprofile file (output of `go test -coverprofile` command).
#
# For cases where there are many coverage profiles, such as when running
# unit tests and integration tests separately, you can combine all those
# profiles into one. In this case, the profile should have a comma-separated list
# of profile files, e.g., 'cover_unit.out,cover_integration.out'.
profile: lcov.info

# (optional; but recommended to set)
# When specified reported file paths will not contain local prefix in the output
local-prefix: "github.com/nephio-project/porch"

# Holds coverage thresholds percentages, values should be in range [0-100]
threshold:
# (optional; default 0)
# The minimum coverage that each file should have
file: 70

# (optional; default 0)
# The minimum coverage that each package should have
package: 80

# (optional; default 0)
# The minimum total coverage project should have
total: 80

# Holds regexp rules which will override thresholds for matched files or packages
# using their paths.
#
# First rule from this list that matches file or package is going to apply
# new threshold to it. If project has multiple rules that match same path,
# override rules should be listed in order from specific to more general rules.
override:
# # Increase coverage threshold to 100% for `foo` package
# # (default is 80, as configured above in this example)
# - threshold: 100
# path: ^pkg/lib/foo$

# Holds regexp rules which will exclude matched files or packages
# from coverage statistics
exclude:
# Exclude files or packages matching their paths
paths:
- \.pb\.go$ # excludes all protobuf generated files
- ^pkg/bar # exclude package `pkg/bar`

# NOTES:
# - symbol `/` in all path regexps will be replaced by current OS file path separator
# to properly work on Windows
58 changes: 58 additions & 0 deletions .github/workflows/porch-go-covergae.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
# Copyright 2024 The Nephio Authors
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
name: "Go test coverage"
on:
push:
paths-ignore:
- "build/**"
- "deployments/**"
- "docs/**"
- "examples/**"
- "release/**"
- "scripts/**"
pull_request:
paths-ignore:
- "build/**"
- "deployments/**"
- "docs/**"
- "examples/**"
- "release/**"
- "scripts/**"

permissions:
contents: write

jobs:
coverage:
name: Go test coverage check
runs-on: ubuntu-latest
steps:
- name: checkout repo
uses: actions/checkout@v4

- name: setup go
uses: actions/setup-go@v5
with:
go-version: '1.22.2'

- name: generate test coverage
run: make test

- name: check test coverage
uses: vladopajic/go-test-coverage@v2
with:
# Configure action using config file (option 1)
config: ./.github/.testcoverage.yaml
git-branch: badges
git-token: ${{ github.ref_name == 'main' && secrets.GITHUB_TOKEN || '' }}
11 changes: 0 additions & 11 deletions .prow.yaml

This file was deleted.

2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
# Package Orchestration Server

[![coverage](https://raw.githubusercontent.com/nephio-project/porch/badges/.badges/main/coverage.svg)](/.github/.testcoverage.yaml)

Package Orchestration Server (a.k.a. Porch) is a k8s extension apiserver
which manages the lifecycle of KRM configuration packages.

Expand Down
19 changes: 11 additions & 8 deletions default-go-test.mk
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# Copyright 2023 The Nephio Authors.
# Copyright 2023-2024 The Nephio Authors.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
Expand All @@ -14,6 +14,7 @@

GO_VERSION ?= 1.22.2
TEST_COVERAGE_FILE=lcov.info
TEST_COVERAGE_TMP_FILE=lcov.tmp.info
TEST_COVERAGE_HTML_FILE=coverage_unit.html
TEST_COVERAGE_FUNC_FILE=func_coverage.out
GIT_ROOT_DIR ?= $(dir $(lastword $(MAKEFILE_LIST)))
Expand All @@ -26,13 +27,15 @@ unit: test
test: ## Run unit tests (go test)
ifeq ($(CONTAINER_RUNNABLE), 0)
$(RUN_CONTAINER_COMMAND) docker.io/nephio/gotests:1782782171367346176 \
sh -e -c "git config --global --add user.name test; \
git config --global --add user.email test@nephio.org; \
go test ./... -v -coverprofile ${TEST_COVERAGE_FILE}; \
go tool cover -html=${TEST_COVERAGE_FILE} -o ${TEST_COVERAGE_HTML_FILE}; \
go tool cover -func=${TEST_COVERAGE_FILE} -o ${TEST_COVERAGE_FUNC_FILE}"
sh -e -c "git config --global --add user.name test; \
git config --global --add user.email test@nephio.org; \
go test ./... -v -covermode=atomic -coverpkg=./... -coverprofile ${TEST_COVERAGE_TMP_FILE}; \
cat ${TEST_COVERAGE_TMP_FILE} | grep -v 'test\|api' > ${TEST_COVERAGE_FILE}; \
go tool cover -html=${TEST_COVERAGE_FILE} -o ${TEST_COVERAGE_HTML_FILE}; \
go tool cover -func=${TEST_COVERAGE_FILE} -o ${TEST_COVERAGE_FUNC_FILE}"
else
go test ./... -v -coverprofile ${TEST_COVERAGE_FILE}
go test ./... -v -covermode=atomic -coverpkg=./... -coverprofile ${TEST_COVERAGE_TMP_FILE}; \
cat ${TEST_COVERAGE_TMP_FILE} | grep -v 'test\|api' > ${TEST_COVERAGE_FILE}; \
go tool cover -html=${TEST_COVERAGE_FILE} -o ${TEST_COVERAGE_HTML_FILE}
go tool cover -func=${TEST_COVERAGE_FILE} -o ${TEST_COVERAGE_FUNC_FILE}
endif
Expand All @@ -42,4 +45,4 @@ unit-clean: ## Clean up the artifacts created by the unit tests
ifeq ($(CONTAINER_RUNNABLE), 0)
$(CONTAINER_RUNTIME) system prune -f
endif
rm -f ${TEST_COVERAGE_FILE} ${TEST_COVERAGE_HTML_FILE} ${TEST_COVERAGE_FUNC_FILE} > /dev/null 2>&1
rm -f ${TEST_COVERAGE_TMP_FILE} ${TEST_COVERAGE_FILE} ${TEST_COVERAGE_HTML_FILE} ${TEST_COVERAGE_FUNC_FILE} > /dev/null 2>&1

0 comments on commit 1644410

Please sign in to comment.