From dad22a6aba12f5abc14f263c72044520ccaa66ef Mon Sep 17 00:00:00 2001 From: mmetc <92726601+mmetc@users.noreply.github.com> Date: Fri, 1 Apr 2022 11:17:45 +0200 Subject: [PATCH] instrument main() for tests (#1399) --- .gitignore | 4 ++-- Makefile | 6 ++++++ cmd/crowdsec-cli/Makefile | 8 +++++++- cmd/crowdsec-cli/main_test.go | 13 ++++++++++++ cmd/crowdsec/Makefile | 7 ++++++- cmd/crowdsec/main.go | 10 ++++++--- cmd/crowdsec/main_test.go | 13 ++++++++++++ cmd/crowdsec/serve.go | 38 +++++++++++++++++------------------ go.mod | 1 + go.sum | 3 +++ 10 files changed, 77 insertions(+), 26 deletions(-) create mode 100644 cmd/crowdsec-cli/main_test.go create mode 100644 cmd/crowdsec/main_test.go diff --git a/.gitignore b/.gitignore index 04221a070..f67aa1c47 100644 --- a/.gitignore +++ b/.gitignore @@ -7,8 +7,9 @@ *~ .pc -# Test binary, built with `go test -c` +# Test binaries, built with `go test -c` *.test +*.cover # Output of the go coverage tool, specifically when used with LiteIDE *.out @@ -20,7 +21,6 @@ # Dependency directories (remove the comment below to include it) # vendor/ - # crowdsec binaries cmd/crowdsec-cli/cscli cmd/crowdsec/crowdsec diff --git a/Makefile b/Makefile index 46f63ae8e..ceb586453 100644 --- a/Makefile +++ b/Makefile @@ -109,9 +109,15 @@ clean: testclean cscli: goversion @GOARCH=$(GOARCH) GOOS=$(GOOS) $(MAKE) -C $(CSCLI_FOLDER) build --no-print-directory +cscli-bincover: goversion + @GOARCH=$(GOARCH) GOOS=$(GOOS) $(MAKE) -C $(CSCLI_FOLDER) build-bincover --no-print-directory + crowdsec: goversion @GOARCH=$(GOARCH) GOOS=$(GOOS) $(MAKE) -C $(CROWDSEC_FOLDER) build --no-print-directory +crowdsec-bincover: goversion + @GOARCH=$(GOARCH) GOOS=$(GOOS) $(MAKE) -C $(CROWDSEC_FOLDER) build-bincover --no-print-directory + http-plugin: goversion @GOARCH=$(GOARCH) GOOS=$(GOOS) $(MAKE) -C $(HTTP_PLUGIN_FOLDER) build --no-print-directory diff --git a/cmd/crowdsec-cli/Makefile b/cmd/crowdsec-cli/Makefile index b5e639819..4bdf675a3 100644 --- a/cmd/crowdsec-cli/Makefile +++ b/cmd/crowdsec-cli/Makefile @@ -4,7 +4,10 @@ GOBUILD=$(GOCMD) build GOCLEAN=$(GOCMD) clean GOTEST=$(GOCMD) test GOGET=$(GOCMD) get + BINARY_NAME=cscli +# names longer than 15 chars break 'pgrep' +BINARY_NAME_COVER=$(BINARY_NAME).cover PREFIX?="/" BIN_PREFIX = $(PREFIX)"/usr/local/bin/" @@ -14,6 +17,9 @@ all: clean build build: clean @$(GOBUILD) $(LD_OPTS) -o $(BINARY_NAME) -v +build-bincover: clean + $(GOTEST) . -tags testrunmain -coverpkg=all -covermode=atomic $(LD_OPTS) -c -o $(BINARY_NAME_COVER) + static: clean @$(GOBUILD) $(LD_OPTS_STATIC) -o $(BINARY_NAME) -v -a -tags netgo @@ -30,4 +36,4 @@ uninstall: @$(RM) -r $(BIN_PREFIX)$(BINARY_NAME) clean: - @$(RM) $(BINARY_NAME) + @$(RM) $(BINARY_NAME) $(BINARY_NAME_COVER) diff --git a/cmd/crowdsec-cli/main_test.go b/cmd/crowdsec-cli/main_test.go new file mode 100644 index 000000000..da7241e1d --- /dev/null +++ b/cmd/crowdsec-cli/main_test.go @@ -0,0 +1,13 @@ +//go:build testrunmain + +package main + +import ( + "github.com/confluentinc/bincover" + + "testing" +) + +func TestBincoverRunMain(t *testing.T) { + bincover.RunTest(main) +} diff --git a/cmd/crowdsec/Makefile b/cmd/crowdsec/Makefile index eff0e6eb5..894efba0a 100644 --- a/cmd/crowdsec/Makefile +++ b/cmd/crowdsec/Makefile @@ -6,6 +6,8 @@ GOTEST=$(GOCMD) test GOGET=$(GOCMD) get CROWDSEC_BIN=crowdsec +# names longer than 15 chars break 'pgrep' +CROWDSEC_BIN_COVER=$(CROWDSEC_BIN).cover PREFIX?="/" CFG_PREFIX = $(PREFIX)"/etc/crowdsec/config/" BIN_PREFIX = $(PREFIX)"/usr/local/bin/" @@ -20,6 +22,9 @@ all: clean test build build: clean $(GOBUILD) $(LD_OPTS) -o $(CROWDSEC_BIN) -v +build-bincover: clean + $(GOTEST) . -tags testrunmain -coverpkg=all -covermode=atomic $(LD_OPTS) -c -o $(CROWDSEC_BIN_COVER) + static: clean $(GOBUILD) $(LD_OPTS_STATIC) -o $(CROWDSEC_BIN) -v -a -tags netgo @@ -27,7 +32,7 @@ test: $(GOTEST) $(LD_OPTS) -v ./... clean: - @$(RM) $(CROWDSEC_BIN) + @$(RM) $(CROWDSEC_BIN) $(CROWDSEC_BIN).test $(CROWDSEC_BIN_COVER) .PHONY: install install: install-conf install-bin diff --git a/cmd/crowdsec/main.go b/cmd/crowdsec/main.go index d0e8f57fa..0cde76d63 100644 --- a/cmd/crowdsec/main.go +++ b/cmd/crowdsec/main.go @@ -266,6 +266,8 @@ func main() { defer types.CatchPanic("crowdsec/main") + log.Debugf("os.Args: %v", os.Args) + // Handle command line arguments flags = &Flags{} flags.Parse() @@ -301,8 +303,10 @@ func main() { go registerPrometheus(cConfig.Prometheus) } - if err := Serve(cConfig); err != nil { - log.Fatalf(err.Error()) + if rc, err := Serve(cConfig); err != nil { + if err != nil { + log.Errorf(err.Error()) + os.Exit(rc) + } } - } diff --git a/cmd/crowdsec/main_test.go b/cmd/crowdsec/main_test.go new file mode 100644 index 000000000..da7241e1d --- /dev/null +++ b/cmd/crowdsec/main_test.go @@ -0,0 +1,13 @@ +//go:build testrunmain + +package main + +import ( + "github.com/confluentinc/bincover" + + "testing" +) + +func TestBincoverRunMain(t *testing.T) { + bincover.RunTest(main) +} diff --git a/cmd/crowdsec/serve.go b/cmd/crowdsec/serve.go index d76bcec6d..42676ad39 100644 --- a/cmd/crowdsec/serve.go +++ b/cmd/crowdsec/serve.go @@ -177,10 +177,11 @@ func shutdown(sig os.Signal, cConfig *csconfig.Config) error { return nil } -func HandleSignals(cConfig *csconfig.Config) { +func HandleSignals(cConfig *csconfig.Config) int { signalChan := make(chan os.Signal, 1) signal.Notify(signalChan, syscall.SIGHUP, + syscall.SIGINT, syscall.SIGTERM) exitChan := make(chan int) @@ -198,8 +199,8 @@ func HandleSignals(cConfig *csconfig.Config) { if err := reloadHandler(s, cConfig); err != nil { log.Fatalf("Reload handler failure : %s", err) } - // kill -SIGTERM XXXX - case syscall.SIGTERM: + // ctrl+C, kill -SIGINT XXXX, kill -SIGTERM XXXX + case syscall.SIGINT, syscall.SIGTERM: log.Warningf("SIGTERM received, shutting down") if err := shutdown(s, cConfig); err != nil { log.Fatalf("failed shutdown : %s", err) @@ -211,10 +212,10 @@ func HandleSignals(cConfig *csconfig.Config) { code := <-exitChan log.Warningf("Crowdsec service shutting down") - os.Exit(code) + return code } -func Serve(cConfig *csconfig.Config) error { +func Serve(cConfig *csconfig.Config) (int, error) { acquisTomb = tomb.Tomb{} parsersTomb = tomb.Tomb{} bucketsTomb = tomb.Tomb{} @@ -225,7 +226,7 @@ func Serve(cConfig *csconfig.Config) error { if !cConfig.DisableAPI { apiServer, err := initAPIServer(cConfig) if err != nil { - return errors.Wrap(err, "api server init") + return 1, errors.Wrap(err, "api server init") } if !flags.TestMode { serveAPIServer(apiServer) @@ -235,7 +236,7 @@ func Serve(cConfig *csconfig.Config) error { if !cConfig.DisableAgent { csParsers, err := initCrowdsec(cConfig) if err != nil { - return errors.Wrap(err, "crowdsec init") + return 1, errors.Wrap(err, "crowdsec init") } /* if it's just linting, we're done */ if !flags.TestMode { @@ -254,18 +255,17 @@ func Serve(cConfig *csconfig.Config) error { log.Errorf("Failed to notify(sent: %v): %v", sent, err) } /*wait for signals*/ - HandleSignals(cConfig) - } else { - for { - select { - case <-apiTomb.Dead(): - log.Infof("api shutdown") - os.Exit(0) - case <-crowdsecTomb.Dead(): - log.Infof("crowdsec shutdown") - os.Exit(0) - } + return HandleSignals(cConfig), nil + } + + for { + select { + case <-apiTomb.Dead(): + log.Infof("api shutdown") + os.Exit(0) + case <-crowdsecTomb.Dead(): + log.Infof("crowdsec shutdown") + os.Exit(0) } } - return nil } diff --git a/go.mod b/go.mod index e685a06af..8e37cd87c 100644 --- a/go.mod +++ b/go.mod @@ -78,6 +78,7 @@ require ( github.com/asaskevich/govalidator v0.0.0-20200907205600-7a23bdc65eef // indirect github.com/beorn7/perks v1.0.1 // indirect github.com/cespare/xxhash/v2 v2.1.2 // indirect + github.com/confluentinc/bincover v0.2.0 // indirect github.com/containerd/containerd v1.4.3 // indirect github.com/cpuguy83/go-md2man/v2 v2.0.1 // indirect github.com/creack/pty v1.1.11 // indirect diff --git a/go.sum b/go.sum index 0a421f5c5..0025248b8 100644 --- a/go.sum +++ b/go.sum @@ -172,6 +172,8 @@ github.com/cockroachdb/apd v1.1.0 h1:3LFP3629v+1aKXU5Q37mxmRxX/pIu1nijXydLShEq5I github.com/cockroachdb/apd v1.1.0/go.mod h1:8Sl8LxpKi29FqWXR16WEFZRNSz3SoPzUzeMeY4+DwBQ= github.com/cockroachdb/datadriven v0.0.0-20190809214429-80d97fb3cbaa/go.mod h1:zn76sxSg3SzpJ0PPJaLDCu+Bu0Lg3sKTORVIj19EIF8= github.com/codahale/hdrhistogram v0.0.0-20161010025455-3a0bb77429bd/go.mod h1:sE/e/2PUdi/liOCUjSTXgM1o87ZssimdTWN964YiIeI= +github.com/confluentinc/bincover v0.2.0 h1:WSS3MqzwJbosCLMOuF3tJ0pMpALzBfrm80Tb+/3gbQs= +github.com/confluentinc/bincover v0.2.0/go.mod h1:qeI1wx0RxdGTZtrJY0HVlgJ4NqC/X2Z+fHbvy87tgHE= github.com/containerd/containerd v1.4.3 h1:ijQT13JedHSHrQGWFcGEwzcNKrAGIiZ+jSD5QQG07SY= github.com/containerd/containerd v1.4.3/go.mod h1:bC6axHOhabU15QhwfG7w5PipXdVtMXFTttgp+kVtyUA= github.com/coreos/go-semver v0.2.0/go.mod h1:nnelYz7RCh+5ahJtPPxZlU+153eP4D4r3EedlOD2RNk= @@ -1539,6 +1541,7 @@ gopkg.in/yaml.v2 v2.2.2/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI= gopkg.in/yaml.v2 v2.2.3/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI= gopkg.in/yaml.v2 v2.2.4/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI= gopkg.in/yaml.v2 v2.2.5/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI= +gopkg.in/yaml.v2 v2.2.7/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI= gopkg.in/yaml.v2 v2.2.8/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI= gopkg.in/yaml.v2 v2.3.0/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI= gopkg.in/yaml.v2 v2.4.0 h1:D8xgwECY7CYvx+Y2n4sBz93Jn9JRvxdiyyo8CTfuKaY=