From 758416a9aa1918be814564dd5379b49771ed3f0d Mon Sep 17 00:00:00 2001 From: Kadin Sayani Date: Tue, 8 Oct 2024 16:03:32 -0600 Subject: [PATCH 1/3] Makefile: Add `-cover` go build step for coverage Signed-off-by: Kadin Sayani --- Makefile | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/Makefile b/Makefile index e81567bb..f4233fdd 100644 --- a/Makefile +++ b/Makefile @@ -1,4 +1,5 @@ GOMIN=1.22.7 +GOCOVERDIR ?= $(shell go env GOCOVERDIR) .PHONY: default default: build @@ -6,8 +7,13 @@ default: build # Build targets. .PHONY: build build: +ifeq "$(GOCOVERDIR)" "" go install -tags=agent -v ./cmd/microcloud go install -tags=agent -v ./cmd/microcloudd +else + go install -tags=agent -v -cover ./cmd/microcloud + go install -tags=agent -v -cover ./cmd/microcloudd +endif # Testing targets. .PHONY: check @@ -15,7 +21,11 @@ check: check-static check-unit check-system .PHONY: check-unit check-unit: +ifeq "$(GOCOVERDIR)" "" go test ./... +else + go test ./... -cover -test.gocoverdir="${GOCOVERDIR}" +endif .PHONY: check-system check-system: From 52fbc5fc409c3033bded6580343e677cac72291a Mon Sep 17 00:00:00 2001 From: Kadin Sayani Date: Wed, 9 Oct 2024 14:38:16 -0600 Subject: [PATCH 2/3] github: Add tiobe tics job for system and unit test coverage Signed-off-by: Kadin Sayani --- .github/workflows/tests.yml | 122 +++++++++++++++++++++++++++++++++--- test/includes/microcloud.sh | 8 ++- test/main.sh | 14 +++++ 3 files changed, 135 insertions(+), 9 deletions(-) diff --git a/.github/workflows/tests.yml b/.github/workflows/tests.yml index 597c2147..206526f2 100644 --- a/.github/workflows/tests.yml +++ b/.github/workflows/tests.yml @@ -6,6 +6,10 @@ on: branches: - main pull_request: + workflow_dispatch: + +env: + GOCOVERDIR: ${{ ( github.event_name == 'workflow_dispatch' || github.event_name == 'schedule' ) && '/home/runner/work/microcloud/microcloud/cover' || '' }} permissions: contents: read @@ -73,8 +77,30 @@ jobs: - name: Run static analysis run: make check-static + - name: Make GOCOVERDIR + run: mkdir -p "${GOCOVERDIR}" + if: env.GOCOVERDIR != '' + - name: Unit tests - run: make check-unit + run: | + set -eux + make check-unit + + - name: Upload coverage data + uses: actions/upload-artifact@v4 + with: + name: coverage-unit + path: ${{env.GOCOVERDIR}} + if: env.GOCOVERDIR != '' + + - name: Upload system test dependencies + uses: actions/upload-artifact@v4 + with: + name: system-test-deps + path: | + /home/runner/go/bin/microcloud + /home/runner/go/bin/microcloudd + retention-days: 1 system-tests: env: @@ -82,7 +108,8 @@ jobs: SKIP_VM_LAUNCH: "1" SNAPSHOT_RESTORE: "1" name: System - runs-on: GitHubMicrocloud + runs-on: GitHubMicrocloud + needs: code-tests strategy: fail-fast: false matrix: @@ -193,12 +220,21 @@ jobs: sudo apt-get update sudo apt-get install --no-install-recommends -y libdqlite-dev pkg-config - - name: Build + - name: Download system test dependencies + uses: actions/download-artifact@v4 + with: + name: system-test-deps + merge-multiple: true + path: /home/runner/go/bin + + - name: Make GOCOVERDIR + run: mkdir -p "${GOCOVERDIR}" + if: env.GOCOVERDIR != '' + + - name: Sideload debug binaries run: | set -eux - make - # Binaries to sideload export MICROCLOUD_DEBUG_PATH=~/go/bin/microcloud export MICROCLOUDD_DEBUG_PATH=~/go/bin/microcloudd @@ -259,7 +295,7 @@ jobs: export MICROCLOUD_SNAP_CHANNEL="${{ matrix.microcloud }}" cd test - sudo --preserve-env=DEBUG,GITHUB_ACTIONS,MICROCLOUD_DEBUG_PATH,MICROCLOUDD_DEBUG_PATH,SKIP_VM_LAUNCH,SNAPSHOT_RESTORE,TEST_STORAGE_SOURCE,TESTBED_READY,BASE_OS,LXD_SNAP_CHANNEL,MICROCEPH_SNAP_CHANNEL,MICROOVN_SNAP_CHANNEL,MICROCLOUD_SNAP_CHANNEL ./main.sh setup + sudo --preserve-env=GOCOVERDIR,DEBUG,GITHUB_ACTIONS,MICROCLOUD_DEBUG_PATH,MICROCLOUDD_DEBUG_PATH,SKIP_VM_LAUNCH,SNAPSHOT_RESTORE,TEST_STORAGE_SOURCE,TESTBED_READY,BASE_OS,LXD_SNAP_CHANNEL,MICROCEPH_SNAP_CHANNEL,MICROOVN_SNAP_CHANNEL,MICROCLOUD_SNAP_CHANNEL ./main.sh setup echo "TESTBED_READY=1" >> "${GITHUB_ENV}" echo "BASE_OS=${BASE_OS}" >> "${GITHUB_ENV}" @@ -273,7 +309,79 @@ jobs: set -eux chmod +x ~ cd test - sudo --preserve-env=DEBUG,GITHUB_ACTIONS,MICROCLOUD_DEBUG_PATH,MICROCLOUDD_DEBUG_PATH,SKIP_VM_LAUNCH,SNAPSHOT_RESTORE,TEST_STORAGE_SOURCE,TESTBED_READY,BASE_OS,LXD_SNAP_CHANNEL,MICROCEPH_SNAP_CHANNEL,MICROOVN_SNAP_CHANNEL,MICROCLOUD_SNAP_CHANNEL ./main.sh ${{ matrix.suite }} + sudo --preserve-env=GOCOVERDIR,DEBUG,GITHUB_ACTIONS,MICROCLOUD_DEBUG_PATH,MICROCLOUDD_DEBUG_PATH,SKIP_VM_LAUNCH,SNAPSHOT_RESTORE,TEST_STORAGE_SOURCE,TESTBED_READY,BASE_OS,LXD_SNAP_CHANNEL,MICROCEPH_SNAP_CHANNEL,MICROOVN_SNAP_CHANNEL,MICROCLOUD_SNAP_CHANNEL ./main.sh ${{ matrix.suite }} + echo "TIMESTAMP=$(date +%Y%m%d_%H%M%S_%N)" >> "${GITHUB_ENV}" + + - name: Upload coverage data + uses: actions/upload-artifact@v4 + with: + name: coverage-${{ matrix.go }}-${{ matrix.suite }}-${{ env.TIMESTAMP }} + path: ${{ env.GOCOVERDIR }} + if: env.GOCOVERDIR != '' + + tics: + name: Tiobe TICS + runs-on: ubuntu-22.04 + needs: system-tests + if: ${{ ( github.event_name == 'workflow_dispatch' || github.event_name == 'schedule' ) && github.ref_name == 'main' && github.repository == 'canonical/microcloud' }} + steps: + - name: Checkout + uses: actions/checkout@v4 + with: + fetch-depth: 0 + + - name: Install Go + uses: actions/setup-go@v5 + with: + go-version: 1.23.x + + - name: Download coverage data + uses: actions/download-artifact@v4 + with: + pattern: coverage-* + path: ${{env.GOCOVERDIR}} + merge-multiple: true + + - name: Extract coverage data + run: | + find ${{ env.GOCOVERDIR }}/micro*/cover/ -type f -exec mv {} ${{ env.GOCOVERDIR }} \; + rm -rf ${{ env.GOCOVERDIR }}/micro* + ls -la ${{ env.GOCOVERDIR }} + + - name: Download system test dependencies + uses: actions/download-artifact@v4 + with: + name: system-test-deps + merge-multiple: true + path: /home/runner/go/bin + + - name: Install dependencies + run: | + sudo add-apt-repository ppa:dqlite/dev -y --no-update + sudo apt-get update + sudo apt-get install --no-install-recommends -y libdqlite-dev pkg-config + go install github.com/axw/gocov/gocov@latest + go install github.com/AlekSi/gocov-xml@latest + go install honnef.co/go/tools/cmd/staticcheck@latest + + - name: Convert coverage files + run: | + go tool covdata textfmt -i="${GOCOVERDIR}" -o "${GOCOVERDIR}"/coverage.out + gocov convert "${GOCOVERDIR}"/coverage.out > "${GOCOVERDIR}"/coverage.json + gocov-xml < "${GOCOVERDIR}"/coverage.json > "${GOCOVERDIR}"/coverage-go.xml + go tool covdata percent -i="${GOCOVERDIR}" + + - name: Run TICS + uses: tiobe/tics-github-action@v3 + with: + mode: qserver + project: microcloud + viewerUrl: https://canonical.tiobe.com/tiobeweb/TICS/api/cfg?name=default + branchdir: ${{ github.workspace }} + ticsAuthToken: ${{ secrets.TICS_AUTH_TOKEN }} + installTics: true + calc: ALL + tmpdir: /tmp/tics documentation-checks: uses: canonical/documentation-workflows/.github/workflows/documentation-checks.yaml@main diff --git a/test/includes/microcloud.sh b/test/includes/microcloud.sh index 4d79c6f7..7973b428 100644 --- a/test/includes/microcloud.sh +++ b/test/includes/microcloud.sh @@ -277,8 +277,8 @@ set_debug_binaries() { lxc exec "${name}" -- rm -f /var/snap/microcloud/common/microcloudd.debug lxc exec "${name}" -- rm -f /var/snap/microcloud/common/microcloud.debug - lxc file push --quiet "${MICROCLOUDD_DEBUG_PATH}" "${name}"/var/snap/microcloud/common/microcloudd.debug - lxc file push --quiet "${MICROCLOUD_DEBUG_PATH}" "${name}"/var/snap/microcloud/common/microcloud.debug + lxc file push --quiet "${MICROCLOUDD_DEBUG_PATH}" "${name}"/var/snap/microcloud/common/microcloudd.debug --mode 0755 + lxc file push --quiet "${MICROCLOUD_DEBUG_PATH}" "${name}"/var/snap/microcloud/common/microcloud.debug --mode 0755 lxc exec "${name}" -- systemctl restart snap.microcloud.daemon || true fi @@ -888,6 +888,8 @@ cluster_reset() { # reset_systems: Concurrently or sequentially resets the specified number of systems. reset_systems() { + collect_go_cover_files + if [ "${SNAPSHOT_RESTORE}" = 1 ]; then # shellcheck disable=SC2048,SC2086 restore_systems ${*} @@ -944,6 +946,8 @@ reset_systems() { restore_systems() { echo "::group::restore_systems" + collect_go_cover_files + num_vms=3 num_disks=3 num_extra_ifaces=1 diff --git a/test/main.sh b/test/main.sh index c201a9b4..8ad95226 100755 --- a/test/main.sh +++ b/test/main.sh @@ -192,6 +192,8 @@ run_test() { ${TEST_CURRENT} END_TIME="$(date +%s)" + collect_go_cover_files + echo "::notice::==> TEST DONE: ${TEST_CURRENT_DESCRIPTION} ($((END_TIME - START_TIME))s)" } @@ -208,6 +210,18 @@ testbed_setup() { echo "::notice::==> SETUP DONE ($((END_TIME - START_TIME))s)" } +collect_go_cover_files() { + if [ -n "${GOCOVERDIR}" ]; then + echo "==> Collecting Go coverage files" + lxc list -c n -f csv | xargs --no-run-if-empty -I {} sh -c " + container_name=\"{}\" + timestamp=\$(date +%Y%m%d_%H%M%S_%N) + destination=\"${GOCOVERDIR}/\${container_name}_\${timestamp}\" + lxc file pull -r \"\${container_name}/var/snap/microcloud/common/data/cover\" \"\${destination}\" || true + " + fi +} + # test groups run_add_tests() { run_test test_add_interactive "add interactive" From 6e93959ae5a723c858b9830e0f8e821886e09f6f Mon Sep 17 00:00:00 2001 From: Kadin Sayani Date: Mon, 21 Oct 2024 08:56:11 -0600 Subject: [PATCH 3/3] staticcheck: Add configuration file to ignore check ST1005 Signed-off-by: Kadin Sayani --- staticcheck.conf | 3 +++ 1 file changed, 3 insertions(+) create mode 100644 staticcheck.conf diff --git a/staticcheck.conf b/staticcheck.conf new file mode 100644 index 00000000..4adf37e3 --- /dev/null +++ b/staticcheck.conf @@ -0,0 +1,3 @@ +# Checks being ignored: +# ST1005: error strings should not be capitalized +checks = ["inherit", "-ST1005"]