instrument main() for tests (#1399)
This commit is contained in:
parent
b202e387cf
commit
dad22a6aba
10 changed files with 77 additions and 26 deletions
4
.gitignore
vendored
4
.gitignore
vendored
|
@ -7,8 +7,9 @@
|
||||||
*~
|
*~
|
||||||
.pc
|
.pc
|
||||||
|
|
||||||
# Test binary, built with `go test -c`
|
# Test binaries, built with `go test -c`
|
||||||
*.test
|
*.test
|
||||||
|
*.cover
|
||||||
|
|
||||||
# Output of the go coverage tool, specifically when used with LiteIDE
|
# Output of the go coverage tool, specifically when used with LiteIDE
|
||||||
*.out
|
*.out
|
||||||
|
@ -20,7 +21,6 @@
|
||||||
# Dependency directories (remove the comment below to include it)
|
# Dependency directories (remove the comment below to include it)
|
||||||
# vendor/
|
# vendor/
|
||||||
|
|
||||||
|
|
||||||
# crowdsec binaries
|
# crowdsec binaries
|
||||||
cmd/crowdsec-cli/cscli
|
cmd/crowdsec-cli/cscli
|
||||||
cmd/crowdsec/crowdsec
|
cmd/crowdsec/crowdsec
|
||||||
|
|
6
Makefile
6
Makefile
|
@ -109,9 +109,15 @@ clean: testclean
|
||||||
cscli: goversion
|
cscli: goversion
|
||||||
@GOARCH=$(GOARCH) GOOS=$(GOOS) $(MAKE) -C $(CSCLI_FOLDER) build --no-print-directory
|
@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
|
crowdsec: goversion
|
||||||
@GOARCH=$(GOARCH) GOOS=$(GOOS) $(MAKE) -C $(CROWDSEC_FOLDER) build --no-print-directory
|
@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
|
http-plugin: goversion
|
||||||
@GOARCH=$(GOARCH) GOOS=$(GOOS) $(MAKE) -C $(HTTP_PLUGIN_FOLDER) build --no-print-directory
|
@GOARCH=$(GOARCH) GOOS=$(GOOS) $(MAKE) -C $(HTTP_PLUGIN_FOLDER) build --no-print-directory
|
||||||
|
|
||||||
|
|
|
@ -4,7 +4,10 @@ GOBUILD=$(GOCMD) build
|
||||||
GOCLEAN=$(GOCMD) clean
|
GOCLEAN=$(GOCMD) clean
|
||||||
GOTEST=$(GOCMD) test
|
GOTEST=$(GOCMD) test
|
||||||
GOGET=$(GOCMD) get
|
GOGET=$(GOCMD) get
|
||||||
|
|
||||||
BINARY_NAME=cscli
|
BINARY_NAME=cscli
|
||||||
|
# names longer than 15 chars break 'pgrep'
|
||||||
|
BINARY_NAME_COVER=$(BINARY_NAME).cover
|
||||||
PREFIX?="/"
|
PREFIX?="/"
|
||||||
BIN_PREFIX = $(PREFIX)"/usr/local/bin/"
|
BIN_PREFIX = $(PREFIX)"/usr/local/bin/"
|
||||||
|
|
||||||
|
@ -14,6 +17,9 @@ all: clean build
|
||||||
build: clean
|
build: clean
|
||||||
@$(GOBUILD) $(LD_OPTS) -o $(BINARY_NAME) -v
|
@$(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
|
static: clean
|
||||||
@$(GOBUILD) $(LD_OPTS_STATIC) -o $(BINARY_NAME) -v -a -tags netgo
|
@$(GOBUILD) $(LD_OPTS_STATIC) -o $(BINARY_NAME) -v -a -tags netgo
|
||||||
|
|
||||||
|
@ -30,4 +36,4 @@ uninstall:
|
||||||
@$(RM) -r $(BIN_PREFIX)$(BINARY_NAME)
|
@$(RM) -r $(BIN_PREFIX)$(BINARY_NAME)
|
||||||
|
|
||||||
clean:
|
clean:
|
||||||
@$(RM) $(BINARY_NAME)
|
@$(RM) $(BINARY_NAME) $(BINARY_NAME_COVER)
|
||||||
|
|
13
cmd/crowdsec-cli/main_test.go
Normal file
13
cmd/crowdsec-cli/main_test.go
Normal file
|
@ -0,0 +1,13 @@
|
||||||
|
//go:build testrunmain
|
||||||
|
|
||||||
|
package main
|
||||||
|
|
||||||
|
import (
|
||||||
|
"github.com/confluentinc/bincover"
|
||||||
|
|
||||||
|
"testing"
|
||||||
|
)
|
||||||
|
|
||||||
|
func TestBincoverRunMain(t *testing.T) {
|
||||||
|
bincover.RunTest(main)
|
||||||
|
}
|
|
@ -6,6 +6,8 @@ GOTEST=$(GOCMD) test
|
||||||
GOGET=$(GOCMD) get
|
GOGET=$(GOCMD) get
|
||||||
|
|
||||||
CROWDSEC_BIN=crowdsec
|
CROWDSEC_BIN=crowdsec
|
||||||
|
# names longer than 15 chars break 'pgrep'
|
||||||
|
CROWDSEC_BIN_COVER=$(CROWDSEC_BIN).cover
|
||||||
PREFIX?="/"
|
PREFIX?="/"
|
||||||
CFG_PREFIX = $(PREFIX)"/etc/crowdsec/config/"
|
CFG_PREFIX = $(PREFIX)"/etc/crowdsec/config/"
|
||||||
BIN_PREFIX = $(PREFIX)"/usr/local/bin/"
|
BIN_PREFIX = $(PREFIX)"/usr/local/bin/"
|
||||||
|
@ -20,6 +22,9 @@ all: clean test build
|
||||||
build: clean
|
build: clean
|
||||||
$(GOBUILD) $(LD_OPTS) -o $(CROWDSEC_BIN) -v
|
$(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
|
static: clean
|
||||||
$(GOBUILD) $(LD_OPTS_STATIC) -o $(CROWDSEC_BIN) -v -a -tags netgo
|
$(GOBUILD) $(LD_OPTS_STATIC) -o $(CROWDSEC_BIN) -v -a -tags netgo
|
||||||
|
|
||||||
|
@ -27,7 +32,7 @@ test:
|
||||||
$(GOTEST) $(LD_OPTS) -v ./...
|
$(GOTEST) $(LD_OPTS) -v ./...
|
||||||
|
|
||||||
clean:
|
clean:
|
||||||
@$(RM) $(CROWDSEC_BIN)
|
@$(RM) $(CROWDSEC_BIN) $(CROWDSEC_BIN).test $(CROWDSEC_BIN_COVER)
|
||||||
|
|
||||||
.PHONY: install
|
.PHONY: install
|
||||||
install: install-conf install-bin
|
install: install-conf install-bin
|
||||||
|
|
|
@ -266,6 +266,8 @@ func main() {
|
||||||
|
|
||||||
defer types.CatchPanic("crowdsec/main")
|
defer types.CatchPanic("crowdsec/main")
|
||||||
|
|
||||||
|
log.Debugf("os.Args: %v", os.Args)
|
||||||
|
|
||||||
// Handle command line arguments
|
// Handle command line arguments
|
||||||
flags = &Flags{}
|
flags = &Flags{}
|
||||||
flags.Parse()
|
flags.Parse()
|
||||||
|
@ -301,8 +303,10 @@ func main() {
|
||||||
go registerPrometheus(cConfig.Prometheus)
|
go registerPrometheus(cConfig.Prometheus)
|
||||||
}
|
}
|
||||||
|
|
||||||
if err := Serve(cConfig); err != nil {
|
if rc, err := Serve(cConfig); err != nil {
|
||||||
log.Fatalf(err.Error())
|
if err != nil {
|
||||||
|
log.Errorf(err.Error())
|
||||||
|
os.Exit(rc)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
13
cmd/crowdsec/main_test.go
Normal file
13
cmd/crowdsec/main_test.go
Normal file
|
@ -0,0 +1,13 @@
|
||||||
|
//go:build testrunmain
|
||||||
|
|
||||||
|
package main
|
||||||
|
|
||||||
|
import (
|
||||||
|
"github.com/confluentinc/bincover"
|
||||||
|
|
||||||
|
"testing"
|
||||||
|
)
|
||||||
|
|
||||||
|
func TestBincoverRunMain(t *testing.T) {
|
||||||
|
bincover.RunTest(main)
|
||||||
|
}
|
|
@ -177,10 +177,11 @@ func shutdown(sig os.Signal, cConfig *csconfig.Config) error {
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func HandleSignals(cConfig *csconfig.Config) {
|
func HandleSignals(cConfig *csconfig.Config) int {
|
||||||
signalChan := make(chan os.Signal, 1)
|
signalChan := make(chan os.Signal, 1)
|
||||||
signal.Notify(signalChan,
|
signal.Notify(signalChan,
|
||||||
syscall.SIGHUP,
|
syscall.SIGHUP,
|
||||||
|
syscall.SIGINT,
|
||||||
syscall.SIGTERM)
|
syscall.SIGTERM)
|
||||||
|
|
||||||
exitChan := make(chan int)
|
exitChan := make(chan int)
|
||||||
|
@ -198,8 +199,8 @@ func HandleSignals(cConfig *csconfig.Config) {
|
||||||
if err := reloadHandler(s, cConfig); err != nil {
|
if err := reloadHandler(s, cConfig); err != nil {
|
||||||
log.Fatalf("Reload handler failure : %s", err)
|
log.Fatalf("Reload handler failure : %s", err)
|
||||||
}
|
}
|
||||||
// kill -SIGTERM XXXX
|
// ctrl+C, kill -SIGINT XXXX, kill -SIGTERM XXXX
|
||||||
case syscall.SIGTERM:
|
case syscall.SIGINT, syscall.SIGTERM:
|
||||||
log.Warningf("SIGTERM received, shutting down")
|
log.Warningf("SIGTERM received, shutting down")
|
||||||
if err := shutdown(s, cConfig); err != nil {
|
if err := shutdown(s, cConfig); err != nil {
|
||||||
log.Fatalf("failed shutdown : %s", err)
|
log.Fatalf("failed shutdown : %s", err)
|
||||||
|
@ -211,10 +212,10 @@ func HandleSignals(cConfig *csconfig.Config) {
|
||||||
|
|
||||||
code := <-exitChan
|
code := <-exitChan
|
||||||
log.Warningf("Crowdsec service shutting down")
|
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{}
|
acquisTomb = tomb.Tomb{}
|
||||||
parsersTomb = tomb.Tomb{}
|
parsersTomb = tomb.Tomb{}
|
||||||
bucketsTomb = tomb.Tomb{}
|
bucketsTomb = tomb.Tomb{}
|
||||||
|
@ -225,7 +226,7 @@ func Serve(cConfig *csconfig.Config) error {
|
||||||
if !cConfig.DisableAPI {
|
if !cConfig.DisableAPI {
|
||||||
apiServer, err := initAPIServer(cConfig)
|
apiServer, err := initAPIServer(cConfig)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return errors.Wrap(err, "api server init")
|
return 1, errors.Wrap(err, "api server init")
|
||||||
}
|
}
|
||||||
if !flags.TestMode {
|
if !flags.TestMode {
|
||||||
serveAPIServer(apiServer)
|
serveAPIServer(apiServer)
|
||||||
|
@ -235,7 +236,7 @@ func Serve(cConfig *csconfig.Config) error {
|
||||||
if !cConfig.DisableAgent {
|
if !cConfig.DisableAgent {
|
||||||
csParsers, err := initCrowdsec(cConfig)
|
csParsers, err := initCrowdsec(cConfig)
|
||||||
if err != nil {
|
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 it's just linting, we're done */
|
||||||
if !flags.TestMode {
|
if !flags.TestMode {
|
||||||
|
@ -254,18 +255,17 @@ func Serve(cConfig *csconfig.Config) error {
|
||||||
log.Errorf("Failed to notify(sent: %v): %v", sent, err)
|
log.Errorf("Failed to notify(sent: %v): %v", sent, err)
|
||||||
}
|
}
|
||||||
/*wait for signals*/
|
/*wait for signals*/
|
||||||
HandleSignals(cConfig)
|
return HandleSignals(cConfig), nil
|
||||||
} else {
|
}
|
||||||
for {
|
|
||||||
select {
|
for {
|
||||||
case <-apiTomb.Dead():
|
select {
|
||||||
log.Infof("api shutdown")
|
case <-apiTomb.Dead():
|
||||||
os.Exit(0)
|
log.Infof("api shutdown")
|
||||||
case <-crowdsecTomb.Dead():
|
os.Exit(0)
|
||||||
log.Infof("crowdsec shutdown")
|
case <-crowdsecTomb.Dead():
|
||||||
os.Exit(0)
|
log.Infof("crowdsec shutdown")
|
||||||
}
|
os.Exit(0)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return nil
|
|
||||||
}
|
}
|
||||||
|
|
1
go.mod
1
go.mod
|
@ -78,6 +78,7 @@ require (
|
||||||
github.com/asaskevich/govalidator v0.0.0-20200907205600-7a23bdc65eef // indirect
|
github.com/asaskevich/govalidator v0.0.0-20200907205600-7a23bdc65eef // indirect
|
||||||
github.com/beorn7/perks v1.0.1 // indirect
|
github.com/beorn7/perks v1.0.1 // indirect
|
||||||
github.com/cespare/xxhash/v2 v2.1.2 // 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/containerd/containerd v1.4.3 // indirect
|
||||||
github.com/cpuguy83/go-md2man/v2 v2.0.1 // indirect
|
github.com/cpuguy83/go-md2man/v2 v2.0.1 // indirect
|
||||||
github.com/creack/pty v1.1.11 // indirect
|
github.com/creack/pty v1.1.11 // indirect
|
||||||
|
|
3
go.sum
3
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/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/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/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 h1:ijQT13JedHSHrQGWFcGEwzcNKrAGIiZ+jSD5QQG07SY=
|
||||||
github.com/containerd/containerd v1.4.3/go.mod h1:bC6axHOhabU15QhwfG7w5PipXdVtMXFTttgp+kVtyUA=
|
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=
|
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.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.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.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.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.3.0/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=
|
||||||
gopkg.in/yaml.v2 v2.4.0 h1:D8xgwECY7CYvx+Y2n4sBz93Jn9JRvxdiyyo8CTfuKaY=
|
gopkg.in/yaml.v2 v2.4.0 h1:D8xgwECY7CYvx+Y2n4sBz93Jn9JRvxdiyyo8CTfuKaY=
|
||||||
|
|
Loading…
Reference in a new issue