Skip to content

Commit

Permalink
add github actions tests (#557)
Browse files Browse the repository at this point in the history
  • Loading branch information
dashpole authored Jan 9, 2023
1 parent 3943ef3 commit 42d2ee6
Show file tree
Hide file tree
Showing 5 changed files with 222 additions and 54 deletions.
146 changes: 146 additions & 0 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,146 @@
name: ci
on:
push:
branches:
- main
pull_request:
env:
# Path to where test results will be saved.
TEST_RESULTS: /tmp/test-results
# Default minimum version of Go to support.
DEFAULT_GO_VERSION: 1.18
jobs:
lint:
runs-on: ubuntu-latest
steps:
- name: Install Go
uses: actions/setup-go@v3
with:
go-version: ${{ env.DEFAULT_GO_VERSION }}
- name: Checkout Repo
uses: actions/checkout@v3
- name: Setup Environment
run: |
echo "GOPATH=$(go env GOPATH)" >> $GITHUB_ENV
echo "$(go env GOPATH)/bin" >> $GITHUB_PATH
- name: Module cache
uses: actions/cache@v3
env:
cache-name: go-mod-cache
with:
path: ~/go/pkg/mod
key: ${{ runner.os }}-${{ env.cache-name }}-${{ hashFiles('**/go.sum') }}
- name: Tools cache
uses: actions/cache@v3
env:
cache-name: go-tools-cache
with:
path: ~/.tools
key: ${{ runner.os }}-${{ env.cache-name }}-${{ hashFiles('./internal/tools/**') }}
- name: Generate
run: make generate
- name: Build
run: make build
- name: Run linters
run: make lint
- name: Fixtures
run: make fixtures
- name: Check clean repository
run: make check-clean-work-tree

test-race:
runs-on: ubuntu-latest
steps:
- name: Install Go
uses: actions/setup-go@v3
with:
go-version: ${{ env.DEFAULT_GO_VERSION }}
- name: Checkout Repo
uses: actions/checkout@v3
- name: Setup Environment
run: |
echo "GOPATH=$(go env GOPATH)" >> $GITHUB_ENV
echo "$(go env GOPATH)/bin" >> $GITHUB_PATH
- name: Module cache
uses: actions/cache@v3
env:
cache-name: go-mod-cache
with:
path: ~/go/pkg/mod
key: ${{ runner.os }}-${{ env.cache-name }}-${{ hashFiles('**/go.sum') }}
- name: Run tests with race detector
run: make test

test-coverage:
runs-on: ubuntu-latest
steps:
- name: Install Go
uses: actions/setup-go@v3
with:
go-version: ${{ env.DEFAULT_GO_VERSION }}
- name: Checkout Repo
uses: actions/checkout@v3
- name: Setup Environment
run: |
echo "GOPATH=$(go env GOPATH)" >> $GITHUB_ENV
echo "$(go env GOPATH)/bin" >> $GITHUB_PATH
- name: Module cache
uses: actions/cache@v3
env:
cache-name: go-mod-cache
with:
path: ~/go/pkg/mod
key: ${{ runner.os }}-${{ env.cache-name }}-${{ hashFiles('**/go.sum') }}
- name: Run coverage tests
run: |
make test-coverage
mkdir $TEST_RESULTS
cp coverage.out $TEST_RESULTS
cp coverage.txt $TEST_RESULTS
cp coverage.html $TEST_RESULTS
- name: Upload coverage report
uses: codecov/[email protected]
with:
file: ./coverage.txt
fail_ci_if_error: true
verbose: true
- name: Store coverage test output
uses: actions/upload-artifact@v3
with:
name: opentelemetry-go-test-output
path: ${{ env.TEST_RESULTS }}

compatibility-test:
strategy:
matrix:
go-version: [1.19, 1.18]
os: [ubuntu-latest, macos-latest, windows-latest]
arch: ["386", amd64]
exclude:
# Not a supported Go OS/architecture.
- os: macos-latest
arch: "386"
runs-on: ${{ matrix.os }}
steps:
- name: Install Go
uses: actions/setup-go@v3
with:
go-version: ${{ matrix.go-version }}
- name: Checkout code
uses: actions/checkout@v3
- name: Setup Environment
run: |
echo "GOPATH=$(go env GOPATH)" >> $GITHUB_ENV
echo "$(go env GOPATH)/bin" >> $GITHUB_PATH
shell: bash
- name: Module cache
uses: actions/cache@v3
env:
cache-name: go-mod-cache
with:
path: ~/go/pkg/mod
key: ${{ runner.os }}-${{ env.cache-name }}-${{ hashFiles('**/go.sum') }}
- name: Run tests
env:
GOARCH: ${{ matrix.arch }}
run: make test-short
126 changes: 72 additions & 54 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -17,68 +17,84 @@ ifeq ($(UNAME_S),Darwin)
endif
endif

GOTEST_MIN = go test -v -timeout 70s
GOTEST = $(GOTEST_MIN) -race
GOTEST_WITH_COVERAGE = $(GOTEST) -coverprofile=coverage.txt -covermode=atomic
GOTEST = go test -v -timeout 70s
GOTEST_SHORT = $(GOTEST) -short
GOTEST_RACE = $(GOTEST) -race
GOTEST_WITH_COVERAGE = $(GOTEST_RACE) -coverprofile=coverage.txt -covermode=atomic

.DEFAULT_GOAL := precommit

.PHONY: precommit

TOOLS_DIR := $(abspath ./.tools)
TOOLS = $(CURDIR)/.tools

$(TOOLS_DIR)/golangci-lint: $(TOOLS_MOD_DIR)/go.mod $(TOOLS_MOD_DIR)/go.sum $(TOOLS_MOD_DIR)/tools.go
$(TOOLS):
@mkdir -p $@
$(TOOLS)/%: | $(TOOLS)
cd $(TOOLS_MOD_DIR) && \
go build -o $(TOOLS_DIR)/golangci-lint github.com/golangci/golangci-lint/cmd/golangci-lint
go build -o $@ $(PACKAGE)

$(TOOLS_DIR)/misspell: $(TOOLS_MOD_DIR)/go.mod $(TOOLS_MOD_DIR)/go.sum $(TOOLS_MOD_DIR)/tools.go
cd $(TOOLS_MOD_DIR) && \
go build -o $(TOOLS_DIR)/misspell github.com/client9/misspell/cmd/misspell
GOLANGCI_LINT = $(TOOLS)/golangci-lint
$(TOOLS)/golangci-lint: PACKAGE=github.com/golangci/golangci-lint/cmd/golangci-lint

$(TOOLS_DIR)/stringer: $(TOOLS_MOD_DIR)/go.mod $(TOOLS_MOD_DIR)/go.sum $(TOOLS_MOD_DIR)/tools.go
cd $(TOOLS_MOD_DIR) && \
go build -o $(TOOLS_DIR)/stringer golang.org/x/tools/cmd/stringer
MISSPELL = $(TOOLS)/misspell
$(TOOLS)/misspell: PACKAGE=github.com/client9/misspell/cmd/misspell

$(TOOLS_DIR)/gojq: $(TOOLS_MOD_DIR)/go.mod $(TOOLS_MOD_DIR)/go.sum $(TOOLS_MOD_DIR)/tools.go
cd $(TOOLS_MOD_DIR) && \
go build -o $(TOOLS_DIR)/gojq github.com/itchyny/gojq/cmd/gojq
STRINGER = $(TOOLS)/stringer
$(TOOLS)/stringer: PACKAGE=golang.org/x/tools/cmd/stringer

$(TOOLS_DIR)/protoc-gen-go: $(TOOLS_MOD_DIR)/go.mod $(TOOLS_MOD_DIR)/go.sum $(TOOLS_MOD_DIR)/tools.go
cd $(TOOLS_MOD_DIR) && \
go build -o $(TOOLS_DIR)/protoc-gen-go google.golang.org/protobuf/cmd/protoc-gen-go
GOJQ = $(TOOLS)/gojq
$(TOOLS)/gojq: PACKAGE=github.com/itchyny/gojq/cmd/gojq

$(TOOLS_DIR)/fieldalignment: $(TOOLS_MOD_DIR)/go.mod $(TOOLS_MOD_DIR)/go.sum $(TOOLS_MOD_DIR)/tools.go
cd $(TOOLS_MOD_DIR) && \
go build -o $(TOOLS_DIR)/fieldalignment golang.org/x/tools/go/analysis/passes/fieldalignment/cmd/fieldalignment
PROTOC_GEN_GO = $(TOOLS)/protoc-gen-go
$(TOOLS)/protoc-gen-go: PACKAGE=google.golang.org/protobuf/cmd/protoc-gen-go

FIELDALIGNMENT = $(TOOLS)/fieldalignment
$(TOOLS)/fieldalignment: PACKAGE=golang.org/x/tools/go/analysis/passes/fieldalignment/cmd/fieldalignment

GOCOVMERGE = $(TOOLS)/gocovmerge
$(TOOLS)/gocovmerge: PACKAGE=github.com/wadey/gocovmerge

.PHONY: tools
tools: $(GOLANGCI_LINT) $(MISSPELL) $(GOCOVMERGE) $(STRINGER) $(GOJQ) $(FIELDALIGNMENT) $(PROTOC_GEN_GO)

PROTOBUF_VERSION = 3.19.0
PROTOBUF_OS = linux
ifeq ($(UNAME_S),Darwin)
PROTOBUF_OS = osx
endif

$(TOOLS_DIR)/protoc: $(TOOLS_DIR)/protoc-gen-go
PROTOC = $(TOOLS)/protoc
$(TOOLS)/protoc: $(PROTOC_GEN_GO)
tmpdir=$$(mktemp -d) && \
cd $$tmpdir && \
curl -L https://github.com/protocolbuffers/protobuf/releases/download/v$(PROTOBUF_VERSION)/protoc-$(PROTOBUF_VERSION)-$(PROTOBUF_OS)-x86_64.zip \
-o protoc.zip && \
unzip protoc.zip bin/protoc && \
cp bin/protoc $(TOOLS_DIR)/ ; \
cp bin/protoc $(TOOLS)/ ; \
rm -rf $$tmpdir

precommit: generate build lint test fixtures
precommit: generate build lint test-race fixtures

.PHONY: test-with-coverage
test-with-coverage:
set -e; for dir in $(ALL_COVERAGE_MOD_DIRS); do \
echo "go test ./... + coverage in $${dir}"; \
COVERAGE_MODE = atomic
COVERAGE_PROFILE = coverage.out
.PHONY: test-coverage
test-coverage: | $(GOCOVMERGE)
@set -e; \
printf "" > coverage.txt; \
for dir in $(ALL_COVERAGE_MOD_DIRS); do \
echo "go test -coverpkg=go.opentelemetry.io/otel/... -covermode=$(COVERAGE_MODE) -coverprofile="$(COVERAGE_PROFILE)" $${dir}/..."; \
(cd "$${dir}" && \
$(GOTEST_WITH_COVERAGE) ./... && \
go tool cover -html=coverage.txt -o coverage.html); \
done
go list ./... \
| grep -v third_party \
| grep -v 'semconv/v.*' \
| xargs go test -coverpkg=./... -covermode=$(COVERAGE_MODE) -coverprofile="$(COVERAGE_PROFILE)" && \
go tool cover -html=coverage.out -o coverage.html); \
done; \
$(GOCOVMERGE) $$(find . -name coverage.out) > coverage.txt

.PHONY: ci
ci: precommit check-clean-work-tree test-with-coverage test-386
ci: precommit check-clean-work-tree test-coverage test-race

.PHONY: check-clean-work-tree
check-clean-work-tree:
Expand All @@ -101,54 +117,56 @@ build:
go test -run xxxxxMatchNothingxxxxx ./... >/dev/null); \
done

.PHONY: test
test:
.PHONY: test-race
test-race:
set -e; for dir in $(ALL_GO_MOD_DIRS); do \
echo "go test ./... + race in $${dir}"; \
(cd "$${dir}" && \
$(GOTEST) ./...); \
$(GOTEST_RACE) ./...); \
done

.PHONY: integrationtest
integrationtest:
set -e; for dir in $(ALL_GO_MOD_DIRS); do \
echo "go test ./... + race in $${dir}"; \
(cd "$${dir}" && \
$(GOTEST) -tags=integrationtest -run=TestIntegration ./...); \
$(GOTEST_RACE) -tags=integrationtest -run=TestIntegration ./...); \
done

.PHONY: test-386
test-386:
if [ $(SKIP_386_TEST) = true ] ; then \
echo "skipping the test for GOARCH 386 as it is not supported on the current OS"; \
else \
set -e; for dir in $(ALL_GO_MOD_DIRS); do \
echo "go test ./... GOARCH 386 in $${dir}"; \
(cd "$${dir}" && \
GOARCH=386 $(GOTEST_MIN) ./...); \
done; \
fi
.PHONY: test-short
test-short:
set -e; for dir in $(ALL_GO_MOD_DIRS); do \
echo "go test ./... in $${dir}"; \
(cd "$${dir}" && $(GOTEST_SHORT) ./...); \
done

.PHONY: test
test:
set -e; for dir in $(ALL_GO_MOD_DIRS); do \
echo "go test ./... in $${dir}"; \
(cd "$${dir}" && $(GOTEST) ./...); \
done

.PHONY: lint
lint: $(TOOLS_DIR)/golangci-lint $(TOOLS_DIR)/misspell
lint: $(GOLANGCI_LINT) $(MISSPELL)
set -e; for dir in $(ALL_GO_MOD_DIRS); do \
echo "golangci-lint in $${dir}"; \
(cd "$${dir}" && \
$(TOOLS_DIR)/golangci-lint --config $(CURDIR)/golangci.yml run --fix && \
$(TOOLS_DIR)/golangci-lint --config $(CURDIR)/golangci.yml run); \
$(GOLANGCI_LINT) --config $(CURDIR)/golangci.yml run --fix && \
$(GOLANGCI_LINT) --config $(CURDIR)/golangci.yml run); \
done
$(TOOLS_DIR)/misspell -w $(ALL_DOCS)
$(MISSPELL) -w $(ALL_DOCS)
set -e; for dir in $(ALL_GO_MOD_DIRS) $(TOOLS_MOD_DIR); do \
echo "go mod tidy -compat=1.18 in $${dir}"; \
(cd "$${dir}" && \
go mod tidy -compat=1.18); \
done

generate: $(TOOLS_DIR)/stringer $(TOOLS_DIR)/protoc
$(MAKE) for-all-mod PATH="$(TOOLS_DIR):$${PATH}" CMD="go generate ./..."
generate: $(STRINGER) $(PROTOC)
$(MAKE) for-all-mod PATH="$(TOOLS):$${PATH}" CMD="go generate ./..."

.PHONY: fieldalignment
fieldalignment: $(TOOLS_DIR)/fieldalignment
fieldalignment: $(FIELDALIGNMENT)
$(MAKE) for-all-package CMD="fieldalignment -fix ."


Expand Down
1 change: 1 addition & 0 deletions tools/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ require (
github.com/client9/misspell v0.3.4
github.com/golangci/golangci-lint v1.46.1
github.com/itchyny/gojq v0.12.1
github.com/wadey/gocovmerge v0.0.0-20160331181800-b5bfa59ec0ad
golang.org/x/tools v0.1.11-0.20220316014157-77aa08bb151a
google.golang.org/protobuf v1.28.0
)
Expand Down
2 changes: 2 additions & 0 deletions tools/go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -807,6 +807,8 @@ github.com/urfave/cli v1.22.1/go.mod h1:Gos4lmkARVdJ6EkW0WaNv/tZAAMe9V7XWyB60NtX
github.com/uudashr/gocognit v1.0.5 h1:rrSex7oHr3/pPLQ0xoWq108XMU8s678FJcQ+aSfOHa4=
github.com/uudashr/gocognit v1.0.5/go.mod h1:wgYz0mitoKOTysqxTDMOUXg+Jb5SvtihkfmugIZYpEA=
github.com/viki-org/dnscache v0.0.0-20130720023526-c70c1f23c5d8/go.mod h1:dniwbG03GafCjFohMDmz6Zc6oCuiqgH6tGNyXTkHzXE=
github.com/wadey/gocovmerge v0.0.0-20160331181800-b5bfa59ec0ad h1:W0LEBv82YCGEtcmPA3uNZBI33/qF//HAAs3MawDjRa0=
github.com/wadey/gocovmerge v0.0.0-20160331181800-b5bfa59ec0ad/go.mod h1:Hy8o65+MXnS6EwGElrSRjUzQDLXreJlzYLlWiHtt8hM=
github.com/xiang90/probing v0.0.0-20190116061207-43a291ad63a2/go.mod h1:UETIi67q53MR2AWcXfiuqkDkRtnGDLqkBTpCHuJHxtU=
github.com/xo/terminfo v0.0.0-20210125001918-ca9a967f8778/go.mod h1:2MuV+tbUrU1zIOPMxZ5EncGwgmMJsa+9ucAQZXxsObs=
github.com/xordataexchange/crypt v0.0.3-0.20170626215501-b2862e3d0a77/go.mod h1:aYKd//L2LvnjZzWKhF00oedf4jCCReLcmhLdhm1A27Q=
Expand Down
1 change: 1 addition & 0 deletions tools/tools.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,4 +23,5 @@ import (
_ "golang.org/x/tools/cmd/stringer"
_ "golang.org/x/tools/go/analysis/passes/fieldalignment/cmd/fieldalignment"
_ "google.golang.org/protobuf/cmd/protoc-gen-go"
_ "github.com/wadey/gocovmerge"
)

0 comments on commit 42d2ee6

Please sign in to comment.