Chore: Add Enterprise unit testing path (#101748)

* Add backend unit tests for Grafana and Grafana Enterprise

---------

Co-authored-by: Kevin Minehart <kmineh0151@gmail.com>
This commit is contained in:
Jev Forsberg
2025-03-12 12:00:13 -05:00
committed by GitHub
co-authored by Kevin Minehart
parent f910238185
commit 0233c39a7f
5 changed files with 209 additions and 56 deletions
+2
View File
@@ -756,6 +756,7 @@ embed.go @grafana/grafana-as-code
/.github/pr-checks.json @tolzhabayev /.github/pr-checks.json @tolzhabayev
/.github/pr-commands.json @tolzhabayev /.github/pr-commands.json @tolzhabayev
/.github/renovate.json5 @grafana/frontend-ops /.github/renovate.json5 @grafana/frontend-ops
/.github/actions/setup-enterprise/action.yml @grafana/grafana-backend-group
/.github/actions/test-coverage-processor/action.yml @grafana/grafana-backend-group /.github/actions/test-coverage-processor/action.yml @grafana/grafana-backend-group
/.github/actions/setup-grafana-bench/ @Proximyst /.github/actions/setup-grafana-bench/ @Proximyst
/.github/workflows/add-to-whats-new.yml @grafana/docs-tooling /.github/workflows/add-to-whats-new.yml @grafana/docs-tooling
@@ -792,6 +793,7 @@ embed.go @grafana/grafana-as-code
/.github/workflows/pr-commands.yml @tolzhabayev /.github/workflows/pr-commands.yml @tolzhabayev
/.github/workflows/pr-patch-check.yml @grafana/grafana-developer-enablement-squad /.github/workflows/pr-patch-check.yml @grafana/grafana-developer-enablement-squad
/.github/workflows/pr-backend-unit-tests.yml @grafana/grafana-backend-group /.github/workflows/pr-backend-unit-tests.yml @grafana/grafana-backend-group
/.github/workflows/pr-backend-coverage.yml @grafana/grafana-backend-group
/.github/workflows/sync-mirror.yml @grafana/grafana-developer-enablement-squad /.github/workflows/sync-mirror.yml @grafana/grafana-developer-enablement-squad
/.github/workflows/publish-technical-documentation-next.yml @grafana/docs-tooling /.github/workflows/publish-technical-documentation-next.yml @grafana/docs-tooling
/.github/workflows/publish-technical-documentation-release.yml @grafana/docs-tooling /.github/workflows/publish-technical-documentation-release.yml @grafana/docs-tooling
@@ -0,0 +1,48 @@
name: 'Setup Grafana Enterprise'
description: 'Clones and sets up Grafana Enterprise repository for testing'
inputs:
github-app-name:
description: 'Name of the GitHub App in Vault'
required: false
default: 'grafana-ci-bot'
runs:
using: "composite"
steps:
- name: Retrieve GitHub App secrets
id: get-secrets
uses: grafana/shared-workflows/actions/get-vault-secrets@get-vault-secrets-v1.0.1
with:
repo_secrets: |
APP_ID=${{ inputs.github-app-name }}:app-id
APP_INSTALLATION_ID=${{ inputs.github-app-name }}:app-installation-id
PRIVATE_KEY=${{ inputs.github-app-name }}:private-key
- name: Generate GitHub App token
id: generate_token
uses: actions/create-github-app-token@v1
with:
app-id: ${{ env.APP_ID }}
private-key: ${{ env.PRIVATE_KEY }}
repositories: "grafana-enterprise"
owner: "grafana"
- name: Setup Enterprise
shell: bash
env:
GH_TOKEN: ${{ steps.generate_token.outputs.token }}
run: |
git clone https://x-access-token:${GH_TOKEN}@github.com/grafana/grafana-enterprise.git ../grafana-enterprise;
cd ../grafana-enterprise
if git checkout ${GITHUB_HEAD_REF}; then
echo "checked out ${GITHUB_HEAD_REF}"
elif git checkout ${GITHUB_BASE_REF}; then
echo "checked out ${GITHUB_BASE_REF}"
else
git checkout main
fi
./build.sh
+69
View File
@@ -0,0 +1,69 @@
name: Coverage
on:
workflow_dispatch:
push:
branches:
- main
paths-ignore:
- 'docs/**'
- '**/*.md'
permissions:
contents: read
id-token: write
env:
EDITION: 'oss'
WIRE_TAGS: 'oss'
jobs:
main:
name: Backend Unit Tests
runs-on: ubuntu-latest-8-cores
steps:
- name: Checkout code
uses: actions/checkout@v4
- name: Setup Go
uses: actions/setup-go@v5
with:
go-version-file: go.mod
cache: true
- name: Install dependencies
run: |
sudo apt-get update
sudo apt-get install -y build-essential shared-mime-info
go install github.com/mfridman/tparse@c1754a1f484ac5cd422697b0fec635177ddc8507 # v0.17.0
- name: Generate Go code
run: make gen-go
- name: Run unit tests
run: COVER_OPTS="-coverprofile=be-unit.cov -coverpkg=github.com/grafana/grafana/..." GO_TEST_OUTPUT="/tmp/unit.log" make test-go-unit-cov
- name: Process and upload coverage
uses: ./.github/actions/test-coverage-processor
with:
test-type: 'be-unit'
# Needs to be named 'unit.cov' based on the Makefile command `make test-go-unit`
coverage-file: 'unit.cov'
codecov-token: ${{ secrets.CODECOV_TOKEN }}
codecov-flag: 'be-unit'
codecov-name: 'be-unit'
- name: Install Grafana Bench
# We can't allow forks here, as we need secret access.
if: ${{ github.event_name != 'pull_request' }}
uses: ./.github/actions/setup-grafana-bench
- name: Process output for Bench
if: ${{ github.event_name != 'pull_request' }}
run: |
grafana-bench report \
--trigger pr-backend-unit-tests-oss \
--report-input go \
--report-output log \
--grafana-version "$(git rev-parse HEAD)" \
--suite-name grafana-oss-unit-tests \
/tmp/unit.log
concurrency:
group: ${{ github.workflow }}-${{ github.ref }}
cancel-in-progress: false
+79 -53
View File
@@ -1,78 +1,104 @@
name: Backend Unit Tests name: Backend Unit Tests
on: on:
workflow_dispatch:
push:
branches:
- main
paths-ignore:
- 'docs/**'
- '**/*.md'
pull_request: pull_request:
paths-ignore: paths-ignore:
- 'docs/**' - 'docs/**'
- '**/*.md' - '**/*.md'
concurrency:
group: ${{ github.workflow }}-${{ github.ref }}
cancel-in-progress: true
permissions: permissions:
contents: read contents: read
id-token: write id-token: write
env:
EDITION: 'oss'
WIRE_TAGS: 'oss'
jobs: jobs:
backend-testing-coverage: grafana:
name: Backend Testing & Coverage # Run this workflow only for PRs from forks; if it gets merged into `main` or `release-*`,
runs-on: ubuntu-latest # the `pr-backend-unit-tests-enterprise` workflow will run instead
if: github.event_name == 'pull_request' && github.event.pull_request.head.repo.fork == true
name: Grafana
runs-on: ubuntu-latest-8-cores
continue-on-error: true
steps: steps:
- name: Checkout code - name: Checkout code
uses: actions/checkout@v4 uses: actions/checkout@v4
- name: Setup Go - name: Setup Go
uses: actions/setup-go@v5 uses: actions/setup-go@v5
with: with:
go-version-file: go.mod go-version-file: go.mod
cache: true - name: Restore GOCACHE
uses: actions/cache/restore@v4
- name: Install dependencies with:
run: | key: go-test-cache-${{ github.ref_name }}
sudo apt-get update restore-keys: |
sudo apt-get install -y build-essential shared-mime-info go-test-cache-${{ github.base_ref }}
go install github.com/mfridman/tparse@c1754a1f484ac5cd422697b0fec635177ddc8507 # v0.17.0 go-test-cache-main
path: /home/runner/.cache/go-build
- name: Generate Go code - name: Generate Go code
run: make gen-go run: make gen-go
- name: Run unit tests - name: Run unit tests
run: COVER_OPTS="-coverprofile=be-unit.cov -coverpkg=github.com/grafana/grafana/..." GO_TEST_OUTPUT="/tmp/unit.log" make test-go-unit run: make test-go-unit
- name: "Generate token"
- name: Process and upload coverage id: generate_token
uses: ./.github/actions/test-coverage-processor uses: tibdex/github-app-token@b62528385c34dbc9f38e5f4225ac829252d1ea92
with: with:
test-type: 'be-unit' app_id: ${{ secrets.GRAFANA_DELIVERY_BOT_APP_ID }}
# Needs to be named 'unit.cov' based on the Makefile command `make test-go-unit` private_key: ${{ secrets.GRAFANA_DELIVERY_BOT_APP_PEM }}
coverage-file: 'unit.cov' - name: Clear GOCACHE
codecov-token: ${{ secrets.CODECOV_TOKEN }} run: gh cache delete go-test-cache-${{ github.ref_name }}
codecov-flag: 'be-unit' continue-on-error: true
codecov-name: 'be-unit' env:
GH_TOKEN: ${{ steps.generate_token.outputs.token }}
- name: Save GOCACHE
uses: actions/cache/save@v4
with:
key: go-test-cache-${{ github.ref_name }}
path: /home/runner/.cache/go-build
- name: Install Grafana Bench grafana-enterprise:
# We can't allow forks here, as we need secret access. # Run this workflow for non-PR events (like pushes to `main` or `release-*`) OR for internal PRs (PRs not from forks)
if: ${{ github.event_name != 'pull_request' }} if: github.event_name != 'pull_request' || github.event.pull_request.head.repo.fork == false
uses: ./.github/actions/setup-grafana-bench name: Grafana Enterprise
runs-on: ubuntu-latest-8-cores
- name: Process output for Bench steps:
if: ${{ github.event_name != 'pull_request' }} - name: Checkout code
run: | uses: actions/checkout@v4
grafana-bench report \ - name: Setup Go
--trigger pr-backend-unit-tests-oss \ uses: actions/setup-go@v5
--report-input go \ with:
--report-output log \ go-version-file: go.mod
--grafana-version "$(git rev-parse HEAD)" \ - name: Restore GOCACHE
--suite-name grafana-oss-unit-tests \ uses: actions/cache/restore@v4
/tmp/unit.log with:
key: go-test-cache-${{ github.ref_name }}-enterprise
concurrency: restore-keys: |
group: ${{ github.workflow }}-${{ github.ref }} go-test-cache-${{ github.base_ref }}-enterprise
cancel-in-progress: true go-test-cache-main-enterprise
path: /home/runner/.cache/go-build
- name: Setup Enterprise
uses: ./.github/actions/setup-enterprise
with:
github-app-name: 'grafana-ci-bot'
- name: Generate Go code
run: make gen-go
- name: Run unit tests
run: make test-go-unit
- name: "Generate token"
id: generate_token
uses: tibdex/github-app-token@b62528385c34dbc9f38e5f4225ac829252d1ea92
with:
app_id: ${{ secrets.GRAFANA_DELIVERY_BOT_APP_ID }}
private_key: ${{ secrets.GRAFANA_DELIVERY_BOT_APP_PEM }}
- name: Clear GOCACHE
run: gh cache delete go-test-cache-${{ github.ref_name }}-enterprise
continue-on-error: true
env:
GH_TOKEN: ${{ steps.generate_token.outputs.token }}
- name: Save GOCACHE
uses: actions/cache/save@v4
with:
key: go-test-cache-${{ github.ref_name }}-enterprise
path: /home/runner/.cache/go-build
+11 -3
View File
@@ -18,6 +18,9 @@ GO_BUILD_FLAGS += $(if $(GO_BUILD_DEV),-dev)
GO_BUILD_FLAGS += $(if $(GO_BUILD_TAGS),-build-tags=$(GO_BUILD_TAGS)) GO_BUILD_FLAGS += $(if $(GO_BUILD_TAGS),-build-tags=$(GO_BUILD_TAGS))
GO_BUILD_FLAGS += $(GO_RACE_FLAG) GO_BUILD_FLAGS += $(GO_RACE_FLAG)
GO_TEST_OUTPUT := $(shell [ -n "$(GO_TEST_OUTPUT)" ] && echo '-json | tee $(GO_TEST_OUTPUT) | tparse -all') GO_TEST_OUTPUT := $(shell [ -n "$(GO_TEST_OUTPUT)" ] && echo '-json | tee $(GO_TEST_OUTPUT) | tparse -all')
GO_UNIT_COVERAGE ?= true
GO_UNIT_COVER_PROFILE ?= unit.cov
GO_INTEGRATION_COVER_PROFILE ?= integration.cov
GIT_BASE = remotes/origin/main GIT_BASE = remotes/origin/main
# GNU xargs has flag -r, and BSD xargs (e.g. MacOS) has that behaviour by default # GNU xargs has flag -r, and BSD xargs (e.g. MacOS) has that behaviour by default
@@ -246,8 +249,13 @@ test-go: test-go-unit test-go-integration
.PHONY: test-go-unit .PHONY: test-go-unit
test-go-unit: ## Run unit tests for backend with flags. test-go-unit: ## Run unit tests for backend with flags.
@echo "test backend unit tests" @echo "backend unit tests"
$(GO) test $(GO_RACE_FLAG) -short -covermode=atomic -coverprofile=unit.cov -timeout=30m $(GO_TEST_FILES) $(GO_TEST_OUTPUT) $(GO) test $(GO_RACE_FLAG) -v -short -timeout=30m $(GO_TEST_FILES) $(GO_TEST_OUTPUT)
.PHONY: test-go-unit-cov
test-go-unit-cov: ## Run unit tests for backend with flags and coverage
@echo "backend unit tests with coverage"
$(GO) test $(GO_RACE_FLAG) -v -short $(if $(filter true,$(GO_UNIT_COVERAGE)),-covermode=atomic -coverprofile=$(GO_UNIT_COVER_PROFILE) $(if $(GO_UNIT_TEST_COVERPKG),-coverpkg=$(GO_UNIT_TEST_COVERPKG)),) -timeout=30m $(GO_TEST_FILES) $(GO_TEST_OUTPUT)
.PHONY: test-go-unit-pretty .PHONY: test-go-unit-pretty
test-go-unit-pretty: check-tparse test-go-unit-pretty: check-tparse
@@ -260,7 +268,7 @@ test-go-unit-pretty: check-tparse
.PHONY: test-go-integration .PHONY: test-go-integration
test-go-integration: ## Run integration tests for backend with flags. test-go-integration: ## Run integration tests for backend with flags.
@echo "test backend integration tests" @echo "test backend integration tests"
$(GO) test $(GO_RACE_FLAG) -count=1 -run "^TestIntegration" -covermode=atomic -coverprofile=integration.cov -timeout=5m $(GO_INTEGRATION_TESTS) $(GO_TEST_OUTPUT) $(GO) test $(GO_RACE_FLAG) -count=1 -run "^TestIntegration" -covermode=atomic -coverprofile=$(GO_INTEGRATION_COVER_PROFILE) -timeout=5m $(GO_INTEGRATION_TESTS) $(GO_TEST_OUTPUT)
.PHONY: test-go-integration-alertmanager .PHONY: test-go-integration-alertmanager
test-go-integration-alertmanager: ## Run integration tests for the remote alertmanager (config taken from the mimir_backend block). test-go-integration-alertmanager: ## Run integration tests for the remote alertmanager (config taken from the mimir_backend block).