Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

added lint and test gha #6

Merged
merged 3 commits into from
Mar 1, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 17 additions & 0 deletions .github/workflows/test.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
name: Test
on:
push:
branches:
- main
pull_request:
jobs:
lint_and_test:
name: lint_and_test
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v3
- uses: actions/setup-go@v3
with:
go-version-file: 'go.mod'
cache: false
- run: make ci
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,4 @@ bin/
*.env
.idea
.vscode
cover.out
26 changes: 23 additions & 3 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -11,15 +11,15 @@ GEN_DIR := $(CURDIR)/proto/genpb

.PHONY: help
help: ## prints this help
@ grep -hE '^[\.a-zA-Z_-]+:.*?## .*$$' $(MAKEFILE_LIST) | awk 'BEGIN {FS = ":.*?## "}; {printf "${_YELLOW}%-24s${_NC} %s\n", $$1, $$2}'
@grep -hE '^[\.a-zA-Z_-]+:.*?## .*$$' $(MAKEFILE_LIST) | awk 'BEGIN {FS = ":.*?## "}; {printf "${_YELLOW}%-24s${_NC} %s\n", $$1, $$2}'

.PHONY: clean-tools
clean-tools:
@ rm -rf $(TOOLS_BIN_DIR)
@rm -rf $(TOOLS_BIN_DIR)

.PHONY: lint
lint: $(GOLANGCI_LINT) ## lint you some go code but dont fix things
@ $(GOLANGCI_LINT) run --out-format=github-actions --config=.golangci.yaml
@$(GOLANGCI_LINT) run --out-format=github-actions --config=.golangci.yaml

.PHONY: lint-fix
lint-fix: $(GOLANGCI_LINT) ## lint you some go code and fix things
Expand Down Expand Up @@ -47,6 +47,17 @@ buf: $(BUF) ## generate proto artifacts with buf
rm -rf $(GEN_DIR) ; \
}

.PHONY: test-unit
test-unit: ## run unit tests
@go test -v -race -coverprofile=cover.out `go list ./... | grep -v /examples`

.PHONY: test-integration
test-integration: ## run integration tests
@echo "TODO: add integration tests"

.PHONY: test
test: test-unit test-integration ## run all tests

.PHONY: ready
ready: ## generate all artifacts, clean, format, and vet code...get ready for a PR
@{\
Expand All @@ -60,3 +71,12 @@ ready: ## generate all artifacts, clean, format, and vet code...get ready for a
$(MAKE) lint-fix && \
printf " ✅ \n" ; \
}

# ci-only targets
.PHONY: lint-ci
lint-ci:
@go install github.com/golangci/golangci-lint/cmd/[email protected]
@golangci-lint run --out-format=github-actions --config=.golangci.yaml

.PHONY: ci
ci: lint-ci test-unit
2 changes: 1 addition & 1 deletion errors.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,4 @@ package health

import "errors"

var ErrAddCheckAlreadyRunning = errors.New("health: cannot add a health check to a running health instance")
var ErrHealth = errors.New("health")
4 changes: 2 additions & 2 deletions go.mod
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
module github.com/schigh/health

go 1.20.0
go 1.21.0

require (
github.com/go-chi/chi/v5 v5.0.12
Expand All @@ -9,5 +9,5 @@ require (

require (
go.uber.org/mock v0.4.0
golang.org/x/xerrors v0.0.0-20200804184101-5ec99f83aff1 // indirect
golang.org/x/xerrors v0.0.0-20231012003039-104605ab7028 // indirect
)
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -5,5 +5,7 @@ go.uber.org/mock v0.4.0 h1:VcM4ZOtdbR4f6VXfiOpwpVJDL6lCReaZ6mw31wqh7KU=
go.uber.org/mock v0.4.0/go.mod h1:a6FSlNadKUHUa9IP5Vyt1zh4fC7uAwxMutEAscFbkZc=
golang.org/x/xerrors v0.0.0-20200804184101-5ec99f83aff1 h1:go1bK/D/BFZV2I8cIQd1NKEZ+0owSTG1fDTci4IqFcE=
golang.org/x/xerrors v0.0.0-20200804184101-5ec99f83aff1/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
golang.org/x/xerrors v0.0.0-20231012003039-104605ab7028 h1:+cNy6SZtPcJQH3LJVLOSmiC7MMxXNOb3PU/VUEz+EhU=
golang.org/x/xerrors v0.0.0-20231012003039-104605ab7028/go.mod h1:NDW/Ps6MPRej6fsCIbMTohpP40sJ/P/vI1MoTEGwX90=
google.golang.org/protobuf v1.32.0 h1:pPC6BG5ex8PDFnkbrGU3EixyhKcQ2aDuBS36lqK/C7I=
google.golang.org/protobuf v1.32.0/go.mod h1:c6P6GXX6sHbq/GpV6MGZEdwhWPcYBgnhAHhKbcUYpos=
4 changes: 4 additions & 0 deletions logger.go
Original file line number Diff line number Diff line change
@@ -1,12 +1,16 @@
package health

// Logger defines the logging interface used internally.
// This interface is implemented by log/slog, but can be
// easily adapted from other loggers.
type Logger interface {
Debug(msg string, args ...any)
Info(msg string, args ...any)
Warn(msg string, args ...any)
Error(msg string, args ...any)
}

// NoOpLogger is used to suppress log output.
type NoOpLogger struct{}

func (n NoOpLogger) Debug(_ string, _ ...any) {}
Expand Down
16 changes: 8 additions & 8 deletions manager/std/manager.go
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ func (m *Manager) running() bool {

func (m *Manager) AddCheck(name string, checker health.Checker, opts ...health.AddCheckOption) error {
if m.running() {
return health.ErrAddCheckAlreadyRunning
return fmt.Errorf("%w.manager.std: cannot add a health check to a running health instance", health.ErrHealth)
}

o := health.AddCheckOptions{}
Expand Down Expand Up @@ -109,21 +109,21 @@ func (m *Manager) Run(ctx context.Context) <-chan error {
}

if m.errChan == nil {
// we use a buffered channel here so we can push a
// we use a buffered channel here, so we can push a
// startup error straight away if we have to
m.errChan = make(chan error, 1)
}

// make sure we have at least one checker and one reporters
if m.checkers == nil || m.checkers.Size() == 0 {
shouldReset = true
m.errChan <- errors.New("health.manager.std: there are no checkers specified for this manager")
m.errChan <- fmt.Errorf("%w.manager.std: there are no checkers specified for this manager", health.ErrHealth)
return m.errChan
}

if m.reporters == nil || m.reporters.Size() == 0 {
shouldReset = true
m.errChan <- errors.New("health.manager.std: there are no reporters specified for this manager")
m.errChan <- fmt.Errorf("%w.manager.std: there are no reporters specified for this manager", health.ErrHealth)
return m.errChan
}

Expand Down Expand Up @@ -174,7 +174,7 @@ func (m *Manager) Stop(ctx context.Context) error {
m.reporters.Each(func(key string, reporter health.Reporter) bool {
rErr := reporter.Stop(ctx)
if rErr != nil && !errors.Is(rErr, context.Canceled) {
errs = append(errs, fmt.Errorf("health.manager.std: reporter '%s' failed to stop: %w", key, rErr))
errs = append(errs, fmt.Errorf("%w.manager.std: reporter '%s' failed to stop: %w", health.ErrHealth, key, rErr))
}
return true
})
Expand All @@ -186,15 +186,15 @@ func (m *Manager) Stop(ctx context.Context) error {
}

// TODO: come up with a better way to convey 0...N errors at once
return errors.New(strings.Join(errStrs, "\n"))
return fmt.Errorf("%s.manager.std: "+strings.Join(errStrs, "\n"), health.ErrHealth)
}

return nil
}

func (m *Manager) AddReporter(name string, r health.Reporter) error {
if m.running() {
return errors.New("health.manager.std: cannot add a reporters to a running health instance")
return fmt.Errorf("%w.manager.std: cannot add a reporters to a running health instance", health.ErrHealth)
}

if m.reporters == nil {
Expand Down Expand Up @@ -426,7 +426,7 @@ func (m *Manager) evaluateFitness(ctx context.Context) {
}
}

// cant be ready if you arent live
// cant be ready if you aren't live
actuallyReady = actuallyReady && actuallyLive

// this indicates that liveness or readiness have changed
Expand Down
Loading
Loading