From c78c833400cf453e596113bb8ecccbd2708407f0 Mon Sep 17 00:00:00 2001 From: mmetc <92726601+mmetc@users.noreply.github.com> Date: Fri, 17 Jun 2022 16:12:49 +0200 Subject: [PATCH] CI: colored test output, colored crowdsec and crowdsec-api logs, full final db dump for mysql and sqlite (#1596) * github-ci: color unit test output and logs * new config option: force_color_logs (useful in CI) * bats: show sqlite/mysql dump at the end * removed "-v" (print package names) from "go build" * general workflow cleanup --- .github/workflows/.yamllint | 1 + .github/workflows/bats-hub.yml | 5 +- .github/workflows/bats-mysql.yml | 33 ++++++++++--- .github/workflows/bats-postgres.yml | 17 ++++--- .github/workflows/bats-sqlite-coverage.yml | 30 +++++++++--- .github/workflows/{ci_tests.yml => bats.yml} | 37 ++++++-------- .github/workflows/ci-go-test-windows.yml | 40 --------------- .github/workflows/ci-windows-build-msi.yml | 2 +- .../workflows/ci_golangci-lint-windows.yml | 7 ++- .github/workflows/ci_golangci-lint.yml | 4 +- .github/workflows/ci_release-drafter.yml | 1 - .github/workflows/codeql-analysis.yml | 1 + .../workflows/dispatch_create_branch_hub.yaml | 2 +- .../workflows/dispatch_delete_branch_hub.yaml | 2 +- .github/workflows/go-tests-windows.yml | 49 +++++++++++++++++++ .github/workflows/go-tests.yml | 42 ++++++++++------ .github/workflows/release_publish-package.yml | 4 +- .../release_publish_docker-image-debian.yml | 2 + .../release_publish_docker-image.yml | 2 + .yamllint | 43 ++++++++++++++++ cmd/crowdsec-cli/Makefile | 4 +- cmd/crowdsec/Makefile | 4 +- cmd/crowdsec/run_in_svc.go | 2 +- cmd/crowdsec/run_in_svc_windows.go | 2 +- cmd/crowdsec/serve.go | 2 +- cmd/crowdsec/win_service.go | 2 +- pkg/apiserver/apiserver_test.go | 4 +- pkg/csconfig/common.go | 21 ++++---- pkg/types/utils.go | 4 +- plugins/notifications/dummy/Makefile | 4 +- plugins/notifications/email/Makefile | 4 +- plugins/notifications/http/Makefile | 4 +- plugins/notifications/slack/Makefile | 4 +- plugins/notifications/splunk/Makefile | 4 +- tests/bats/01_base.bats | 36 +++++++------- tests/bats/01_crowdsec.bats | 8 +-- tests/bats/02_nolapi.bats | 18 +++---- tests/generate-hub-tests | 4 +- tests/lib/config/config-global | 3 +- tests/lib/config/config-local | 4 +- tests/lib/setup_file.sh | 10 +++- tests/run-tests | 5 +- 42 files changed, 289 insertions(+), 188 deletions(-) create mode 120000 .github/workflows/.yamllint rename .github/workflows/{ci_tests.yml => bats.yml} (60%) delete mode 100644 .github/workflows/ci-go-test-windows.yml create mode 100644 .github/workflows/go-tests-windows.yml create mode 100644 .yamllint diff --git a/.github/workflows/.yamllint b/.github/workflows/.yamllint new file mode 120000 index 000000000..4a4652c1f --- /dev/null +++ b/.github/workflows/.yamllint @@ -0,0 +1 @@ +../../.yamllint \ No newline at end of file diff --git a/.github/workflows/bats-hub.yml b/.github/workflows/bats-hub.yml index e5dab9136..cd5507d6d 100644 --- a/.github/workflows/bats-hub.yml +++ b/.github/workflows/bats-hub.yml @@ -8,6 +8,9 @@ on: GIST_BADGES_ID: required: true +env: + PREFIX_TEST_NAMES_WITH_FILE: true + jobs: build: name: "Build + tests" @@ -34,7 +37,7 @@ jobs: - name: "Install bats dependencies" run: | - sudo apt install -y -qq build-essential daemonize jq netcat-openbsd + sudo apt -qq -y -o=Dpkg::Use-Pty=0 install build-essential daemonize jq netcat-openbsd go install github.com/mikefarah/yq/v4@latest go install github.com/cloudflare/cfssl/cmd/cfssl@latest go install github.com/cloudflare/cfssl/cmd/cfssljson@latest diff --git a/.github/workflows/bats-mysql.yml b/.github/workflows/bats-mysql.yml index 0ae0faba2..5172b1c93 100644 --- a/.github/workflows/bats-mysql.yml +++ b/.github/workflows/bats-mysql.yml @@ -1,4 +1,4 @@ -name: Functional tests with MySQL +name: Functional tests (MySQL) on: workflow_call: @@ -10,6 +10,9 @@ on: DATABASE_PASSWORD: required: true +env: + PREFIX_TEST_NAMES_WITH_FILE: true + jobs: build: @@ -37,7 +40,7 @@ jobs: go-version: 1.17 id: go - - name: "Clone CrowdSec" + - name: "Check out CrowdSec repository" uses: actions/checkout@v3 with: fetch-depth: 0 @@ -45,16 +48,15 @@ jobs: - name: "Install bats dependencies" run: | - sudo apt install -y -qq build-essential daemonize jq netcat-openbsd + sudo apt -qq -y -o=Dpkg::Use-Pty=0 install build-essential daemonize jq netcat-openbsd go install github.com/mikefarah/yq/v4@latest go install github.com/cloudflare/cfssl/cmd/cfssl@latest go install github.com/cloudflare/cfssl/cmd/cfssljson@latest - sudo cp -u ~/go/bin/yq /usr/local/bin/ - sudo cp -u ~/go/bin/cfssl /usr/local/bin - sudo cp -u ~/go/bin/cfssljson /usr/local/bin + sudo cp -u ~/go/bin/yq ~/go/bin/cfssl ~/go/bin/cfssljson /usr/local/bin/ - name: "Build crowdsec and fixture" - run: make bats-clean bats-build bats-fixture + run: | + make clean bats-build bats-fixture env: DB_BACKEND: mysql MYSQL_HOST: 127.0.0.1 @@ -71,6 +73,22 @@ jobs: MYSQL_PASSWORD: ${{ secrets.DATABASE_PASSWORD }} MYSQL_USER: root + # + # In case you need to inspect the database status after the failure of a given test + # + # - name: "Run specified tests" + # run: ./tests/run-tests tests/bats/.bats -f "" + + - name: Show database dump + run: ./tests/instance-db dump /dev/fd/1 + env: + DB_BACKEND: mysql + MYSQL_HOST: 127.0.0.1 + MYSQL_PORT: 3306 + MYSQL_PASSWORD: ${{ secrets.DATABASE_PASSWORD }} + MYSQL_USER: root + if: ${{ always() }} + - name: "Show crowdsec logs" run: for file in $(find ./tests/local/var/log -type f); do echo ">>>>> $file"; cat $file; echo; done @@ -79,4 +97,3 @@ jobs: - name: "Show database logs" run: docker logs "${{ job.services.database.id }}" if: ${{ always() }} - diff --git a/.github/workflows/bats-postgres.yml b/.github/workflows/bats-postgres.yml index 8e6e380b1..a7fa6e0ee 100644 --- a/.github/workflows/bats-postgres.yml +++ b/.github/workflows/bats-postgres.yml @@ -1,4 +1,4 @@ -name: Functional tests with PostgreSQL +name: Functional tests (Postgres) on: workflow_call: @@ -6,6 +6,9 @@ on: DATABASE_PASSWORD: required: true +env: + PREFIX_TEST_NAMES_WITH_FILE: true + jobs: build: @@ -38,7 +41,7 @@ jobs: go-version: 1.17 id: go - - name: "Clone CrowdSec" + - name: "Check out CrowdSec repository" uses: actions/checkout@v3 with: fetch-depth: 0 @@ -46,16 +49,15 @@ jobs: - name: "Install bats dependencies" run: | - sudo apt install -y -qq build-essential daemonize jq netcat-openbsd + sudo apt -qq -y -o=Dpkg::Use-Pty=0 install build-essential daemonize jq netcat-openbsd go install github.com/mikefarah/yq/v4@latest go install github.com/cloudflare/cfssl/cmd/cfssl@latest go install github.com/cloudflare/cfssl/cmd/cfssljson@latest - sudo cp -u ~/go/bin/yq /usr/local/bin/ - sudo cp -u ~/go/bin/cfssl /usr/local/bin - sudo cp -u ~/go/bin/cfssljson /usr/local/bin + sudo cp -u ~/go/bin/yq ~/go/bin/cfssl ~/go/bin/cfssljson /usr/local/bin/ - name: "Build crowdsec and fixture (DB_BACKEND: pgx)" - run: make clean bats-build bats-fixture + run: | + make clean bats-build bats-fixture env: DB_BACKEND: pgx PGHOST: 127.0.0.1 @@ -98,4 +100,3 @@ jobs: - name: "Show database logs" run: docker logs "${{ job.services.database.id }}" if: ${{ always() }} - diff --git a/.github/workflows/bats-sqlite-coverage.yml b/.github/workflows/bats-sqlite-coverage.yml index 4b3c3213f..c2217f25b 100644 --- a/.github/workflows/bats-sqlite-coverage.yml +++ b/.github/workflows/bats-sqlite-coverage.yml @@ -1,8 +1,12 @@ -name: Functional tests with sqlite +name: Functional tests (sqlite) on: workflow_call: +env: + PREFIX_TEST_NAMES_WITH_FILE: true + TEST_COVERAGE: true + jobs: build: @@ -23,7 +27,7 @@ jobs: go-version: 1.17 id: go - - name: "Clone CrowdSec" + - name: "Check out CrowdSec repository" uses: actions/checkout@v3 with: fetch-depth: 0 @@ -31,19 +35,32 @@ jobs: - name: "Install bats dependencies" run: | - sudo apt install -y -qq build-essential daemonize jq netcat-openbsd + sudo apt -qq -y -o=Dpkg::Use-Pty=0 install build-essential daemonize jq netcat-openbsd go install github.com/mikefarah/yq/v4@latest go install github.com/cloudflare/cfssl/cmd/cfssl@latest go install github.com/cloudflare/cfssl/cmd/cfssljson@latest + sudo cp -u ~/go/bin/yq ~/go/bin/cfssl ~/go/bin/cfssljson /usr/local/bin/ go install github.com/wadey/gocovmerge@latest - sudo cp -u ~/go/bin/yq ~/go/bin/gocovmerge ~/go/bin/cfssl ~/go/bin/cfssljson /usr/local/bin/ + sudo cp -u ~/go/bin/gocovmerge /usr/local/bin/ - name: "Build crowdsec and fixture" - run: TEST_COVERAGE=true make bats-clean bats-build bats-fixture + run: | + make clean bats-build bats-fixture - name: "Run tests" + run: make bats-test + + # + # In case you need to inspect the database status after the failure of a given test + # + # - name: "Run specified tests" + # run: ./tests/run-tests tests/bats/.bats -f "" + + - name: "Show database dump" run: | - TEST_COVERAGE=true make bats-test + ./tests/instance-crowdsec stop + sqlite3 ./tests/local/var/lib/crowdsec/data/crowdsec.db '.dump' + if: ${{ always() }} - name: "Show crowdsec logs" run: @@ -61,4 +78,3 @@ jobs: with: files: ./tests/local/var/lib/coverage/coverage-cscli.out flags: func-cscli - diff --git a/.github/workflows/ci_tests.yml b/.github/workflows/bats.yml similarity index 60% rename from .github/workflows/ci_tests.yml rename to .github/workflows/bats.yml index 89bbd8ff5..8d70d77ba 100644 --- a/.github/workflows/ci_tests.yml +++ b/.github/workflows/bats.yml @@ -1,39 +1,31 @@ -name: Tests +name: Bats -# Main workflow for tests, it calls all the others through parallel jobs. -# -# A final step collects and merges coverage output, then pushes it to -# coveralls.io +# Main workflow for functional tests, it calls all the others through parallel jobs. # # https://docs.github.com/en/actions/using-workflows/reusing-workflows +# +# There is no need to merge coverage output because codecov.io should take care of that. on: push: - branches: - - master - - testing* + branches: [master] paths-ignore: - 'README.md' pull_request: - branches: - - master - - testing* + branches: [master] paths-ignore: - 'README.md' jobs: - go-tests: - uses: ./.github/workflows/go-tests.yml - - bats-sqlite: + sqlite: uses: ./.github/workflows/bats-sqlite-coverage.yml - # Jobs for Postgres and MySQL can have failing tests on GitHub CI, but they - # pass when run on devs' machines or in the release checks. We disable them - # here by default. Remove the if..false to enable them. + # Jobs for Postgres (and sometimes MySQL) can have failing tests on GitHub + # CI, but they pass when run on devs' machines or in the release checks. We + # disable them here by default. Remove the if..false to enable them. - bats-mariadb: + mariadb: if: ${{ false }} uses: ./.github/workflows/bats-mysql.yml with: @@ -41,7 +33,7 @@ jobs: secrets: DATABASE_PASSWORD: ${{ secrets.DATABASE_PASSWORD}} - bats-mysql: + mysql: if: ${{ false }} uses: ./.github/workflows/bats-mysql.yml with: @@ -49,15 +41,14 @@ jobs: secrets: DATABASE_PASSWORD: ${{ secrets.DATABASE_PASSWORD}} - bats-postgres: + postgres: if: ${{ false }} uses: ./.github/workflows/bats-postgres.yml secrets: DATABASE_PASSWORD: ${{ secrets.DATABASE_PASSWORD}} - bats-hub: + hub: uses: ./.github/workflows/bats-hub.yml secrets: GIST_BADGES_ID: ${{ secrets.GIST_BADGES_ID }} GIST_BADGES_SECRET: ${{ secrets.GIST_BADGES_SECRET }} - diff --git a/.github/workflows/ci-go-test-windows.yml b/.github/workflows/ci-go-test-windows.yml deleted file mode 100644 index 4c8e3df8e..000000000 --- a/.github/workflows/ci-go-test-windows.yml +++ /dev/null @@ -1,40 +0,0 @@ -name: tests-windows - -on: - push: - branches: [ master ] - paths-ignore: - - 'docs/**' - - 'mkdocs.yml' - - 'README.md' - pull_request: - branches: [ master ] - paths-ignore: - - 'docs/**' - - 'mkdocs.yml' - - 'README.md' - -jobs: - - build: - name: Build - runs-on: windows-2022 - steps: - - name: Set up Go 1.17 - uses: actions/setup-go@v1 - with: - go-version: 1.17 - id: go - - name: Check out code into the Go module directory - uses: actions/checkout@v2 - - name: Build - run: make build - - name: All tests - run: go test -coverprofile coverage.out -covermode=atomic ./... - - - name: Upload unit coverage to Codecov - uses: codecov/codecov-action@v2 - with: - files: coverage.out - flags: unit-windows - diff --git a/.github/workflows/ci-windows-build-msi.yml b/.github/workflows/ci-windows-build-msi.yml index 214465d85..913662724 100644 --- a/.github/workflows/ci-windows-build-msi.yml +++ b/.github/workflows/ci-windows-build-msi.yml @@ -1,4 +1,4 @@ -name: build-msi +name: build-msi (windows) on: pull_request: diff --git a/.github/workflows/ci_golangci-lint-windows.yml b/.github/workflows/ci_golangci-lint-windows.yml index d777fd0ef..5f0f0c550 100644 --- a/.github/workflows/ci_golangci-lint-windows.yml +++ b/.github/workflows/ci_golangci-lint-windows.yml @@ -1,4 +1,5 @@ -name: golangci-lint-windows +name: golangci-lint (windows) + on: push: tags: @@ -16,7 +17,7 @@ on: - 'README.md' jobs: golangci: - name: lint-windows + name: lint runs-on: windows-2022 steps: - uses: actions/checkout@v3 @@ -28,5 +29,3 @@ jobs: # Optional: golangci-lint command line arguments. args: --issues-exit-code=0 --timeout 10m only-new-issues: true - - diff --git a/.github/workflows/ci_golangci-lint.yml b/.github/workflows/ci_golangci-lint.yml index 22271e9a7..48a5a1360 100644 --- a/.github/workflows/ci_golangci-lint.yml +++ b/.github/workflows/ci_golangci-lint.yml @@ -1,4 +1,5 @@ -name: golangci-lint +name: golangci-lint (linux) + on: push: tags: @@ -36,4 +37,3 @@ jobs: skip-pkg-cache: false # Optional: if set to true then the action don't cache or restore ~/.cache/go-build. skip-build-cache: false - diff --git a/.github/workflows/ci_release-drafter.yml b/.github/workflows/ci_release-drafter.yml index 34c271527..d4aea0e69 100644 --- a/.github/workflows/ci_release-drafter.yml +++ b/.github/workflows/ci_release-drafter.yml @@ -18,4 +18,3 @@ jobs: # config-name: my-config.yml env: GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} - diff --git a/.github/workflows/codeql-analysis.yml b/.github/workflows/codeql-analysis.yml index 2279c88aa..3cb8d1635 100644 --- a/.github/workflows/codeql-analysis.yml +++ b/.github/workflows/codeql-analysis.yml @@ -1,3 +1,4 @@ +# yamllint disable rule:comments # For most projects, this workflow file will not need changing; you simply need # to commit it to your repository. # diff --git a/.github/workflows/dispatch_create_branch_hub.yaml b/.github/workflows/dispatch_create_branch_hub.yaml index e17a582c9..38d6f11bd 100644 --- a/.github/workflows/dispatch_create_branch_hub.yaml +++ b/.github/workflows/dispatch_create_branch_hub.yaml @@ -1,6 +1,6 @@ name: Dispatch to hub when creating pre-release -on: +on: release: types: prereleased diff --git a/.github/workflows/dispatch_delete_branch_hub.yaml b/.github/workflows/dispatch_delete_branch_hub.yaml index 669b13fe0..6a298691d 100644 --- a/.github/workflows/dispatch_delete_branch_hub.yaml +++ b/.github/workflows/dispatch_delete_branch_hub.yaml @@ -1,6 +1,6 @@ name: Dispatch to hub when deleting pre-release -on: +on: release: types: deleted diff --git a/.github/workflows/go-tests-windows.yml b/.github/workflows/go-tests-windows.yml new file mode 100644 index 000000000..1fbddda67 --- /dev/null +++ b/.github/workflows/go-tests-windows.yml @@ -0,0 +1,49 @@ +name: Go tests (windows) + +on: + push: + branches: [master] + paths-ignore: + - 'README.md' + pull_request: + branches: [master] + paths-ignore: + - 'README.md' + +env: + RICHGO_FORCE_COLOR: 1 + +jobs: + + build: + name: "Build + tests" + runs-on: windows-2022 + + steps: + + - name: "Set up Go 1.17" + uses: actions/setup-go@v3 + with: + go-version: 1.17 + id: go + + - name: Check out CrowdSec repository + uses: actions/checkout@v3 + with: + fetch-depth: 0 + submodules: false + + - name: Build + run: | + make build + + - name: Run tests + run: | + go install github.com/kyoh86/richgo@v0.3.10 + go test -coverprofile coverage.out -covermode=atomic ./... | sed 's/ *coverage:.*of statements in.*//' | richgo testfilter + + - name: Upload unit coverage to Codecov + uses: codecov/codecov-action@v2 + with: + files: coverage.out + flags: unit-windows diff --git a/.github/workflows/go-tests.yml b/.github/workflows/go-tests.yml index 15a9dfd58..125a31606 100644 --- a/.github/workflows/go-tests.yml +++ b/.github/workflows/go-tests.yml @@ -1,20 +1,28 @@ -name: Go tests +name: Go tests (linux w/ localstack) + +on: + push: + branches: [master] + paths-ignore: + - 'README.md' + pull_request: + branches: [master] + paths-ignore: + - 'README.md' # these env variables are for localstack, so we can emulate aws services env: + RICHGO_FORCE_COLOR: 1 AWS_HOST: localstack SERVICES: cloudwatch,logs,kinesis - #those are to mimic aws config + # these are to mimic aws config AWS_ACCESS_KEY_ID: AKIAIOSFODNN7EXAMPLE AWS_SECRET_ACCESS_KEY: wJalrXUtnFEMI/K7MDENG/bPxRfiCYEXAMPLEKEY AWS_REGION: us-east-1 - #and to override our endpoint in aws sdk + # and to override our endpoint in aws sdk AWS_ENDPOINT_FORCE: http://localhost:4566 KINESIS_INITIALIZE_STREAMS: "stream-1-shard:1,stream-2-shards:2" -on: - workflow_call: - jobs: build: @@ -24,7 +32,7 @@ jobs: localstack: image: localstack/localstack:0.13.3 ports: - - 4566:4566 # Localstack exposes all services on same port + - 4566:4566 # Localstack exposes all services on the same port env: SERVICES: ${{ env.SERVICES }} DEBUG: "" @@ -34,8 +42,8 @@ jobs: DOCKER_HOST: unix:///var/run/docker.sock HOST_TMP_FOLDER: "/tmp" KINESIS_INITIALIZE_STREAMS: ${{ env.KINESIS_INITIALIZE_STREAMS }} - HOSTNAME_EXTERNAL: ${{ env.AWS_HOST }} # Required so that resource urls are provided properly - # e.g sqs url will get localhost if we don't set this env to map our service + HOSTNAME_EXTERNAL: ${{ env.AWS_HOST }} # Required so that resource urls are provided properly + # e.g sqs url will get localhost if we don't set this env to map our service options: >- --name=localstack --health-cmd="curl -sS 127.0.0.1:4566 || exit 1" @@ -51,22 +59,26 @@ jobs: go-version: 1.17 id: go - - name: "Clone CrowdSec" + - name: Check out CrowdSec repository uses: actions/checkout@v3 with: fetch-depth: 0 submodules: false - name: Build - run: make build && go get -u github.com/ory/go-acc - - - name: "Run tests" run: | - go run github.com/ory/go-acc ./... -o coverage.out --ignore database,notifications,protobufs,cwversion,cstest,models + make build + + - name: Run tests + run: | + go install github.com/ory/go-acc@v0.2.8 + go install github.com/kyoh86/richgo@v0.3.10 + go-acc ./... -o coverage.out --ignore database,notifications,protobufs,cwversion,cstest,models \ + | sed 's/ *coverage:.*of statements in.*//' \ + | richgo testfilter - name: Upload unit coverage to Codecov uses: codecov/codecov-action@v2 with: files: coverage.out flags: unit-linux - diff --git a/.github/workflows/release_publish-package.yml b/.github/workflows/release_publish-package.yml index 0bd029e48..5c5452672 100644 --- a/.github/workflows/release_publish-package.yml +++ b/.github/workflows/release_publish-package.yml @@ -1,7 +1,7 @@ # .github/workflows/build-docker-image.yml name: build -on: +on: release: types: prereleased @@ -86,7 +86,7 @@ jobs: - name: "Test post-install macines" run: | cd scripts/func_tests/ - ./tests_post-install_3machines.sh + ./tests_post-install_3machines.sh - name: "Test post-install ip management" run: | cd scripts/func_tests/ diff --git a/.github/workflows/release_publish_docker-image-debian.yml b/.github/workflows/release_publish_docker-image-debian.yml index f6c29d81b..620623e24 100644 --- a/.github/workflows/release_publish_docker-image-debian.yml +++ b/.github/workflows/release_publish_docker-image-debian.yml @@ -1,10 +1,12 @@ name: Publish Docker Debian image + on: release: types: - released - prereleased workflow_dispatch: + jobs: push_to_registry: name: Push Docker debian image to Docker Hub diff --git a/.github/workflows/release_publish_docker-image.yml b/.github/workflows/release_publish_docker-image.yml index 37f4d0922..a29b5cb77 100644 --- a/.github/workflows/release_publish_docker-image.yml +++ b/.github/workflows/release_publish_docker-image.yml @@ -1,9 +1,11 @@ name: Publish Docker image + on: release: types: - released - prereleased + jobs: push_to_registry: name: Push Docker image to Docker Hub diff --git a/.yamllint b/.yamllint new file mode 100644 index 000000000..fbdeb80a4 --- /dev/null +++ b/.yamllint @@ -0,0 +1,43 @@ +--- +rules: + braces: + min-spaces-inside: 0 + max-spaces-inside: 1 + brackets: + min-spaces-inside: 0 + max-spaces-inside: 1 + colons: + max-spaces-before: 0 + max-spaces-after: 1 + commas: + max-spaces-before: 0 + min-spaces-after: 1 + max-spaces-after: 1 + comments: + level: warning + require-starting-space: true + min-spaces-from-content: 2 + comments-indentation: + level: warning + document-end: disable + document-start: disable + empty-lines: + max: 2 + max-start: 0 + max-end: 0 + hyphens: + max-spaces-after: 1 + indentation: + spaces: consistent + indent-sequences: whatever + check-multi-line-strings: false + key-duplicates: enable + line-length: + max: 180 + allow-non-breakable-words: true + allow-non-breakable-inline-mappings: false + new-line-at-end-of-file: enable + new-lines: + type: unix + trailing-spaces: enable + truthy: disable diff --git a/cmd/crowdsec-cli/Makefile b/cmd/crowdsec-cli/Makefile index 3076c3b76..b8f2753d3 100644 --- a/cmd/crowdsec-cli/Makefile +++ b/cmd/crowdsec-cli/Makefile @@ -21,13 +21,13 @@ BIN_PREFIX = $(PREFIX)"/usr/local/bin/" all: clean build build: clean - @$(GOBUILD) $(LD_OPTS) -o $(BINARY_NAME) -v + @$(GOBUILD) $(LD_OPTS) -o $(BINARY_NAME) build-bincover: clean $(GOTEST) . -tags testrunmain -coverpkg=$(go list github.com/crowdsecurity/crowdsec/... | grep -v -e 'pkg/database' -e 'plugins/notifications' -e 'pkg/protobufs' -e 'pkg/cwversions' -e 'pkg/cstest' -e 'pkg/models') -covermode=atomic $(LD_OPTS) -c -o $(BINARY_NAME_COVER) static: clean - @$(GOBUILD) $(LD_OPTS_STATIC) -o $(BINARY_NAME) -v -a -tags netgo + @$(GOBUILD) $(LD_OPTS_STATIC) -o $(BINARY_NAME) -a -tags netgo .PHONY: install install: install-conf install-bin diff --git a/cmd/crowdsec/Makefile b/cmd/crowdsec/Makefile index 7a9db1c59..4284db57c 100644 --- a/cmd/crowdsec/Makefile +++ b/cmd/crowdsec/Makefile @@ -26,13 +26,13 @@ SYSTEMD_PATH_FILE="/etc/systemd/system/crowdsec.service" all: clean test build build: clean - $(GOBUILD) $(LD_OPTS) -o $(CROWDSEC_BIN) -v + $(GOBUILD) $(LD_OPTS) -o $(CROWDSEC_BIN) build-bincover: clean $(GOTEST) . -tags testrunmain -coverpkg=$(go list github.com/crowdsecurity/crowdsec/... | grep -v -e 'pkg/database' -e 'plugins/notifications' -e 'pkg/protobufs' -e 'pkg/cwversions' -e 'pkg/cstest' -e 'pkg/models') -covermode=atomic $(LD_OPTS) -c -o $(CROWDSEC_BIN_COVER) static: clean - $(GOBUILD) $(LD_OPTS_STATIC) -o $(CROWDSEC_BIN) -v -a -tags netgo + $(GOBUILD) $(LD_OPTS_STATIC) -o $(CROWDSEC_BIN) -a -tags netgo test: $(GOTEST) $(LD_OPTS) -v ./... diff --git a/cmd/crowdsec/run_in_svc.go b/cmd/crowdsec/run_in_svc.go index 55fb8e810..5fa837700 100644 --- a/cmd/crowdsec/run_in_svc.go +++ b/cmd/crowdsec/run_in_svc.go @@ -36,7 +36,7 @@ func StartRunSvc() error { } // Configure logging if err = types.SetDefaultLoggerConfig(cConfig.Common.LogMedia, cConfig.Common.LogDir, *cConfig.Common.LogLevel, - cConfig.Common.LogMaxSize, cConfig.Common.LogMaxFiles, cConfig.Common.LogMaxAge, cConfig.Common.CompressLogs); err != nil { + cConfig.Common.LogMaxSize, cConfig.Common.LogMaxFiles, cConfig.Common.LogMaxAge, cConfig.Common.CompressLogs, cConfig.Common.ForceColorLogs); err != nil { log.Fatal(err.Error()) } diff --git a/cmd/crowdsec/run_in_svc_windows.go b/cmd/crowdsec/run_in_svc_windows.go index ff97e3c20..de4466384 100644 --- a/cmd/crowdsec/run_in_svc_windows.go +++ b/cmd/crowdsec/run_in_svc_windows.go @@ -76,7 +76,7 @@ func WindowsRun() error { } // Configure logging if err = types.SetDefaultLoggerConfig(cConfig.Common.LogMedia, cConfig.Common.LogDir, *cConfig.Common.LogLevel, - cConfig.Common.LogMaxSize, cConfig.Common.LogMaxFiles, cConfig.Common.LogMaxAge, cConfig.Common.CompressLogs); err != nil { + cConfig.Common.LogMaxSize, cConfig.Common.LogMaxFiles, cConfig.Common.LogMaxAge, cConfig.Common.CompressLogs, cConfig.Common.ForceColorLogs); err != nil { return err } diff --git a/cmd/crowdsec/serve.go b/cmd/crowdsec/serve.go index 4e540de2f..ab1e9304f 100644 --- a/cmd/crowdsec/serve.go +++ b/cmd/crowdsec/serve.go @@ -60,7 +60,7 @@ func reloadHandler(sig os.Signal, cConfig *csconfig.Config) error { } // Configure logging if err = types.SetDefaultLoggerConfig(cConfig.Common.LogMedia, cConfig.Common.LogDir, *cConfig.Common.LogLevel, - cConfig.Common.LogMaxSize, cConfig.Common.LogMaxFiles, cConfig.Common.LogMaxAge, cConfig.Common.CompressLogs); err != nil { + cConfig.Common.LogMaxSize, cConfig.Common.LogMaxFiles, cConfig.Common.LogMaxAge, cConfig.Common.CompressLogs, cConfig.Common.ForceColorLogs); err != nil { return err } diff --git a/cmd/crowdsec/win_service.go b/cmd/crowdsec/win_service.go index 36beed776..169ab06fd 100644 --- a/cmd/crowdsec/win_service.go +++ b/cmd/crowdsec/win_service.go @@ -74,7 +74,7 @@ func runService(name string) error { // Configure logging if err := types.SetDefaultLoggerConfig(cConfig.Common.LogMedia, cConfig.Common.LogDir, *cConfig.Common.LogLevel, - cConfig.Common.LogMaxSize, cConfig.Common.LogMaxFiles, cConfig.Common.LogMaxAge, cConfig.Common.CompressLogs); err != nil { + cConfig.Common.LogMaxSize, cConfig.Common.LogMaxFiles, cConfig.Common.LogMaxAge, cConfig.Common.CompressLogs, cConfig.Common.ForceColorLogs); err != nil { return err } diff --git a/pkg/apiserver/apiserver_test.go b/pkg/apiserver/apiserver_test.go index efdb05e16..415d2660c 100644 --- a/pkg/apiserver/apiserver_test.go +++ b/pkg/apiserver/apiserver_test.go @@ -357,7 +357,7 @@ func TestLoggingDebugToFileConfig(t *testing.T) { cfg.LogLevel = &lvl // Configure logging - if err := types.SetDefaultLoggerConfig(cfg.LogMedia, cfg.LogDir, *cfg.LogLevel, cfg.LogMaxSize, cfg.LogMaxFiles, cfg.LogMaxAge, cfg.CompressLogs); err != nil { + if err := types.SetDefaultLoggerConfig(cfg.LogMedia, cfg.LogDir, *cfg.LogLevel, cfg.LogMaxSize, cfg.LogMaxFiles, cfg.LogMaxAge, cfg.CompressLogs, false); err != nil { t.Fatal(err.Error()) } api, err := NewServer(&cfg) @@ -414,7 +414,7 @@ func TestLoggingErrorToFileConfig(t *testing.T) { cfg.LogLevel = &lvl // Configure logging - if err := types.SetDefaultLoggerConfig(cfg.LogMedia, cfg.LogDir, *cfg.LogLevel, cfg.LogMaxSize, cfg.LogMaxFiles, cfg.LogMaxAge, cfg.CompressLogs); err != nil { + if err := types.SetDefaultLoggerConfig(cfg.LogMedia, cfg.LogDir, *cfg.LogLevel, cfg.LogMaxSize, cfg.LogMaxFiles, cfg.LogMaxAge, cfg.CompressLogs, false); err != nil { t.Fatal(err.Error()) } api, err := NewServer(&cfg) diff --git a/pkg/csconfig/common.go b/pkg/csconfig/common.go index 79a67a155..6add00c05 100644 --- a/pkg/csconfig/common.go +++ b/pkg/csconfig/common.go @@ -10,16 +10,17 @@ import ( /*daemonization/service related stuff*/ type CommonCfg struct { - Daemonize bool - PidDir string `yaml:"pid_dir,omitempty"` // TODO: This is just for backward compat. Remove this later - LogMedia string `yaml:"log_media"` - LogDir string `yaml:"log_dir,omitempty"` //if LogMedia = file - LogLevel *log.Level `yaml:"log_level"` - WorkingDir string `yaml:"working_dir,omitempty"` ///var/run - CompressLogs *bool `yaml:"compress_logs,omitempty"` - LogMaxSize int `yaml:"log_max_size,omitempty"` - LogMaxAge int `yaml:"log_max_age,omitempty"` - LogMaxFiles int `yaml:"log_max_files,omitempty"` + Daemonize bool + PidDir string `yaml:"pid_dir,omitempty"` // TODO: This is just for backward compat. Remove this later + LogMedia string `yaml:"log_media"` + LogDir string `yaml:"log_dir,omitempty"` //if LogMedia = file + LogLevel *log.Level `yaml:"log_level"` + WorkingDir string `yaml:"working_dir,omitempty"` ///var/run + CompressLogs *bool `yaml:"compress_logs,omitempty"` + LogMaxSize int `yaml:"log_max_size,omitempty"` + LogMaxAge int `yaml:"log_max_age,omitempty"` + LogMaxFiles int `yaml:"log_max_files,omitempty"` + ForceColorLogs bool `yaml:"force_color_logs,omitempty"` } func (c *Config) LoadCommon() error { diff --git a/pkg/types/utils.go b/pkg/types/utils.go index 8159d2464..366d1adf6 100644 --- a/pkg/types/utils.go +++ b/pkg/types/utils.go @@ -23,7 +23,7 @@ var logFormatter log.Formatter var LogOutput *lumberjack.Logger //io.Writer var logLevel log.Level -func SetDefaultLoggerConfig(cfgMode string, cfgFolder string, cfgLevel log.Level, maxSize int, maxFiles int, maxAge int, compress *bool) error { +func SetDefaultLoggerConfig(cfgMode string, cfgFolder string, cfgLevel log.Level, maxSize int, maxFiles int, maxAge int, compress *bool, forceColors bool) error { /*Configure logs*/ if cfgMode == "file" { @@ -67,7 +67,7 @@ func SetDefaultLoggerConfig(cfgMode string, cfgFolder string, cfgLevel log.Level } logLevel = cfgLevel log.SetLevel(logLevel) - logFormatter = &log.TextFormatter{TimestampFormat: "02-01-2006 15:04:05", FullTimestamp: true} + logFormatter = &log.TextFormatter{TimestampFormat: "02-01-2006 15:04:05", FullTimestamp: true, ForceColors: forceColors} log.SetFormatter(logFormatter) return nil } diff --git a/plugins/notifications/dummy/Makefile b/plugins/notifications/dummy/Makefile index 0d423e842..71035f3a9 100644 --- a/plugins/notifications/dummy/Makefile +++ b/plugins/notifications/dummy/Makefile @@ -17,7 +17,7 @@ clean: @$(RM) $(BINARY_NAME) $(WIN_IGNORE_ERR) build: clean - @$(GOBUILD) $(LD_OPTS) -o $(BINARY_NAME) -v + @$(GOBUILD) $(LD_OPTS) -o $(BINARY_NAME) static: clean - $(GOBUILD) $(LD_OPTS_STATIC) -o $(BINARY_NAME) -v -a -tags netgo + $(GOBUILD) $(LD_OPTS_STATIC) -o $(BINARY_NAME) -a -tags netgo diff --git a/plugins/notifications/email/Makefile b/plugins/notifications/email/Makefile index eb8e4e2ca..17cef999b 100644 --- a/plugins/notifications/email/Makefile +++ b/plugins/notifications/email/Makefile @@ -17,7 +17,7 @@ clean: @$(RM) $(BINARY_NAME) $(WIN_IGNORE_ERR) build: clean - @$(GOBUILD) $(LD_OPTS) -o $(BINARY_NAME) -v + @$(GOBUILD) $(LD_OPTS) -o $(BINARY_NAME) static: clean - $(GOBUILD) $(LD_OPTS_STATIC) -o $(BINARY_NAME) -v -a -tags netgo + $(GOBUILD) $(LD_OPTS_STATIC) -o $(BINARY_NAME) -a -tags netgo diff --git a/plugins/notifications/http/Makefile b/plugins/notifications/http/Makefile index 3b6d4036a..ad3bea252 100644 --- a/plugins/notifications/http/Makefile +++ b/plugins/notifications/http/Makefile @@ -17,7 +17,7 @@ clean: @$(RM) $(BINARY_NAME) $(WIN_IGNORE_ERR) build: clean - $(GOBUILD) $(LD_OPTS) -o $(BINARY_NAME) -v + $(GOBUILD) $(LD_OPTS) -o $(BINARY_NAME) static: clean - $(GOBUILD) $(LD_OPTS_STATIC) -o $(BINARY_NAME) -v -a -tags netgo + $(GOBUILD) $(LD_OPTS_STATIC) -o $(BINARY_NAME) -a -tags netgo diff --git a/plugins/notifications/slack/Makefile b/plugins/notifications/slack/Makefile index 1273121a8..a101a235a 100644 --- a/plugins/notifications/slack/Makefile +++ b/plugins/notifications/slack/Makefile @@ -13,11 +13,11 @@ GOGET=$(GOCMD) get BINARY_NAME=notification-slack$(EXT) build: clean - @$(GOBUILD) $(LD_OPTS) -o $(BINARY_NAME) -v + @$(GOBUILD) $(LD_OPTS) -o $(BINARY_NAME) clean: @$(RM) $(BINARY_NAME) $(WIN_IGNORE_ERR) static: clean - $(GOBUILD) $(LD_OPTS_STATIC) -o $(BINARY_NAME) -v -a -tags netgo + $(GOBUILD) $(LD_OPTS_STATIC) -o $(BINARY_NAME) -a -tags netgo diff --git a/plugins/notifications/splunk/Makefile b/plugins/notifications/splunk/Makefile index eb0531e55..bd99de357 100644 --- a/plugins/notifications/splunk/Makefile +++ b/plugins/notifications/splunk/Makefile @@ -15,10 +15,10 @@ GOGET=$(GOCMD) get BINARY_NAME=notification-splunk$(EXT) build: clean - $(GOBUILD) $(LD_OPTS) -o $(BINARY_NAME) -v + $(GOBUILD) $(LD_OPTS) -o $(BINARY_NAME) clean: @$(RM) $(BINARY_NAME) $(WIN_IGNORE_ERR) static: clean - $(GOBUILD) $(LD_OPTS_STATIC) -o $(BINARY_NAME) -v -a -tags netgo + $(GOBUILD) $(LD_OPTS_STATIC) -o $(BINARY_NAME) -a -tags netgo diff --git a/tests/bats/01_base.bats b/tests/bats/01_base.bats index 1bdfd0306..7ec68877f 100644 --- a/tests/bats/01_base.bats +++ b/tests/bats/01_base.bats @@ -27,7 +27,7 @@ declare stderr #---------- -@test "${FILE} cscli - usage" { +@test "cscli - usage" { run -0 cscli assert_output --partial "Usage:" assert_output --partial "cscli [command]" @@ -41,7 +41,7 @@ declare stderr refute_output --partial 'unknown command "blahblah" for "cscli"' } -@test "${FILE} cscli version" { +@test "cscli version" { run -0 cscli version assert_output --partial "version:" assert_output --partial "Codename:" @@ -59,7 +59,7 @@ declare stderr assert_output --partial "version:" } -@test "${FILE} cscli help" { +@test "cscli help" { run -0 cscli help assert_line "Available Commands:" assert_line --regexp ".* help .* Help about any command" @@ -70,7 +70,7 @@ declare stderr assert_line "Available Commands:" } -@test "${FILE} cscli alerts list: at startup returns at least one entry: community pull" { +@test "cscli alerts list: at startup returns at least one entry: community pull" { is_db_postgres && skip # it should have been received while preparing the fixture run -0 cscli alerts list -o json @@ -88,7 +88,7 @@ declare stderr # refute_output 0 } -@test "${FILE} cscli capi status" { +@test "cscli capi status" { run -0 cscli capi status assert_output --partial "Loaded credentials from" assert_output --partial "Trying to authenticate with username" @@ -102,7 +102,7 @@ declare stderr assert_output --partial "Local API is disabled, please run this command on the local API machine: loading online client credentials: failed to read api server credentials configuration file '${ONLINE_API_CREDENTIALS_YAML}': open ${ONLINE_API_CREDENTIALS_YAML}: no such file or directory" } -@test "${FILE} cscli config show -o human" { +@test "cscli config show -o human" { run -0 cscli config show -o human assert_output --partial "Global:" assert_output --partial "Crowdsec:" @@ -110,7 +110,7 @@ declare stderr assert_output --partial "Local API Server:" } -@test "${FILE} cscli config show -o json" { +@test "cscli config show -o json" { run -0 cscli config show -o json assert_output --partial '"API":' assert_output --partial '"Common":' @@ -123,7 +123,7 @@ declare stderr assert_output --partial '"Prometheus":' } -@test "${FILE} cscli config show -o raw" { +@test "cscli config show -o raw" { run -0 cscli config show -o raw assert_line "api:" assert_line "common:" @@ -135,12 +135,12 @@ declare stderr assert_line "prometheus:" } -@test "${FILE} cscli config show --key" { +@test "cscli config show --key" { run -0 cscli config show --key Config.API.Server.ListenURI assert_output "127.0.0.1:8080" } -@test "${FILE} cscli config backup / restore" { +@test "cscli config backup / restore" { # test that we need a valid path # disabled because in CI, the empty string is not passed as a parameter ## run -1 --separate-stderr cscli config backup "" @@ -182,7 +182,7 @@ declare stderr rm -rf -- "${backupdir:?}" } -@test "${FILE} cscli lapi status" { +@test "cscli lapi status" { if is_db_postgres; then sleep 4; fi run -0 --separate-stderr cscli lapi status @@ -193,7 +193,7 @@ declare stderr assert_output --partial "You can successfully interact with Local API (LAPI)" } -@test "${FILE} cscli - missing LAPI credentials file" { +@test "cscli - missing LAPI credentials file" { LOCAL_API_CREDENTIALS=$(config_yq '.api.client.credentials_path') rm -f "${LOCAL_API_CREDENTIALS}" run -1 --separate-stderr cscli lapi status @@ -209,7 +209,7 @@ declare stderr assert_output --partial "loading api client: while reading yaml file: open ${LOCAL_API_CREDENTIALS}: no such file or directory" } -@test "${FILE} cscli - empty LAPI credentials file" { +@test "cscli - empty LAPI credentials file" { LOCAL_API_CREDENTIALS=$(config_yq '.api.client.credentials_path') truncate -s 0 "${LOCAL_API_CREDENTIALS}" run -1 --separate-stderr cscli lapi status @@ -225,7 +225,7 @@ declare stderr assert_output --partial "no credentials or URL found in api client configuration '${LOCAL_API_CREDENTIALS}'" } -@test "${FILE} cscli - missing LAPI client settings" { +@test "cscli - missing LAPI client settings" { yq e 'del(.api.client)' -i "${CONFIG_YAML}" run -1 --separate-stderr cscli lapi status run -0 echo "${stderr}" @@ -240,7 +240,7 @@ declare stderr assert_output --partial "loading api client: no API client section in configuration" } -@test "${FILE} cscli - malformed LAPI url" { +@test "cscli - malformed LAPI url" { LOCAL_API_CREDENTIALS=$(config_yq '.api.client.credentials_path') yq e '.url="https://127.0.0.1:-80"' -i "${LOCAL_API_CREDENTIALS}" @@ -260,7 +260,7 @@ declare stderr assert_output --partial 'invalid port \":-80\" after host' } -@test "${FILE} cscli metrics" { +@test "cscli metrics" { run -0 cscli lapi status run -0 --separate-stderr cscli metrics assert_output --partial "ROUTE" @@ -270,7 +270,7 @@ declare stderr assert_output --partial "Local Api Metrics:" } -@test "${FILE} 'cscli completion' with or without configuration file" { +@test "'cscli completion' with or without configuration file" { run -0 cscli completion bash assert_output --partial "# bash completion for cscli" run -0 cscli completion zsh @@ -285,7 +285,7 @@ declare stderr assert_output --partial "# bash completion for cscli" } -@test "${FILE} cscli hub list" { +@test "cscli hub list" { # we check for the presence of some objects. There may be others when we # use $PACKAGE_TESTING, so the order is not important. diff --git a/tests/bats/01_crowdsec.bats b/tests/bats/01_crowdsec.bats index e92c442eb..c9d5126a9 100644 --- a/tests/bats/01_crowdsec.bats +++ b/tests/bats/01_crowdsec.bats @@ -25,7 +25,7 @@ declare stderr #---------- -@test "${FILE} crowdsec (usage)" { +@test "crowdsec (usage)" { run -0 --separate-stderr timeout 2s "${CROWDSEC}" -h run -0 echo "${stderr}" assert_line --regexp "Usage of .*:" @@ -35,21 +35,21 @@ declare stderr assert_line --regexp "Usage of .*:" } -@test "${FILE} crowdsec (unknown flag)" { +@test "crowdsec (unknown flag)" { run -2 --separate-stderr timeout 2s "${CROWDSEC}" --foobar run -0 echo "${stderr}" assert_line "flag provided but not defined: -foobar" assert_line --regexp "Usage of .*" } -@test "${FILE} crowdsec (unknown argument)" { +@test "crowdsec (unknown argument)" { run -2 --separate-stderr timeout 2s "${CROWDSEC}" trololo run -0 echo "${stderr}" assert_line "argument provided but not defined: trololo" assert_line --regexp "Usage of .*" } -@test "${FILE} crowdsec (no api and no agent)" { +@test "crowdsec (no api and no agent)" { run -1 --separate-stderr timeout 2s "${CROWDSEC}" -no-api -no-cs run -0 echo "${stderr}" assert_line --partial "You must run at least the API Server or crowdsec" diff --git a/tests/bats/02_nolapi.bats b/tests/bats/02_nolapi.bats index 1af46597d..0bebc586e 100644 --- a/tests/bats/02_nolapi.bats +++ b/tests/bats/02_nolapi.bats @@ -25,18 +25,16 @@ declare stderr #---------- -@test "$FILE test without -no-api flag" { +@test "test without -no-api flag" { run -124 --separate-stderr timeout 2s "${CROWDSEC}" # from `man timeout`: If the command times out, and --preserve-status is not set, then exit with status 124. } -@test "$FILE crowdsec should not run without LAPI (-no-api flag)" { - skip +@test "crowdsec should not run without LAPI (-no-api flag)" { run -1 --separate-stderr timeout 2s "${CROWDSEC}" -no-api } -@test "$FILE crowdsec should not run without LAPI (no api.server in configuration file)" { - skip +@test "crowdsec should not run without LAPI (no api.server in configuration file)" { yq e 'del(.api.server)' -i "${CONFIG_YAML}" run -1 --separate-stderr timeout 2s "${CROWDSEC}" @@ -44,7 +42,7 @@ declare stderr assert_output --partial "crowdsec local API is disabled" } -@test "$FILE capi status shouldn't be ok without api.server" { +@test "capi status shouldn't be ok without api.server" { yq e 'del(.api.server)' -i "${CONFIG_YAML}" run -1 --separate-stderr cscli capi status @@ -53,7 +51,7 @@ declare stderr assert_output --partial "There is no configuration on 'api.server:'" } -@test "$FILE cscli config show -o human" { +@test "cscli config show -o human" { yq e 'del(.api.server)' -i "${CONFIG_YAML}" run -0 cscli config show -o human assert_output --partial "Global:" @@ -62,7 +60,7 @@ declare stderr refute_output --partial "Local API Server:" } -@test "$FILE cscli config backup" { +@test "cscli config backup" { yq e 'del(.api.server)' -i "${CONFIG_YAML}" backupdir=$(TMPDIR="${BATS_TEST_TMPDIR}" mktemp -u) run -0 cscli config backup "${backupdir}" @@ -75,7 +73,7 @@ declare stderr assert_output --partial "file exists" } -@test "$FILE lapi status shouldn't be ok without api.server" { +@test "lapi status shouldn't be ok without api.server" { yq e 'del(.api.server)' -i "${CONFIG_YAML}" ./instance-crowdsec start || true run -1 --separate-stderr cscli machines list @@ -83,7 +81,7 @@ declare stderr assert_output --partial "Local API is disabled, please run this command on the local API machine" } -@test "$FILE cscli metrics" { +@test "cscli metrics" { skip 'need to trigger metrics with a live parse' yq e 'del(.api.server)' -i "${CONFIG_YAML}" ./instance-crowdsec start diff --git a/tests/generate-hub-tests b/tests/generate-hub-tests index 002388218..56afa6719 100755 --- a/tests/generate-hub-tests +++ b/tests/generate-hub-tests @@ -16,7 +16,7 @@ cscli() { hubdir="${LOCAL_DIR}/hub-tests" git clone --depth 1 https://github.com/crowdsecurity/hub.git "${hubdir}" >/dev/null 2>&1 || (cd "${hubdir}"; git pull) -HUBTESTS_BATS="${TEST_DIR}/dyn-bats/99_hub.bats" +HUBTESTS_BATS="${TEST_DIR}/dyn-bats/hub.bats" cat << EOT > "${HUBTESTS_BATS}" set -u @@ -40,7 +40,7 @@ echo "Generating hub tests..." for testname in $("${CSCLI}" --crowdsec "${CROWDSEC}" --cscli "${CSCLI}" hubtest --hub "${hubdir}" list -o json | grep -v NAME | grep -v -- '-------' | awk '{print $1}'); do cat << EOT >> "${HUBTESTS_BATS}" -@test "\$FILE ${testname}" { +@test "${testname}" { run "\${CSCLI}" --crowdsec "\${CROWDSEC}" --cscli "\${CSCLI}" --hub "${hubdir}" hubtest run "${testname}" --clean # in case of error, need to see what went wrong echo "\$output" diff --git a/tests/lib/config/config-global b/tests/lib/config/config-global index 03b1db25b..dd868b524 100755 --- a/tests/lib/config/config-global +++ b/tests/lib/config/config-global @@ -81,7 +81,7 @@ load_init_data() { dump_backend="$(cat "${LOCAL_INIT_DIR}/.backend")" if [[ "${DB_BACKEND}" != "${dump_backend}" ]]; then - die "Can't run with backend '${DB_BACKEND}' because the test data was build with '${dump_backend}'" + die "Can't run with backend '${DB_BACKEND}' because the test data was built with '${dump_backend}'" fi remove_init_data @@ -97,7 +97,6 @@ load_init_data() { [[ $# -lt 1 ]] && about - case "$1" in make) make_init_data diff --git a/tests/lib/config/config-local b/tests/lib/config/config-local index 9aaf34c30..82dbf5c0d 100755 --- a/tests/lib/config/config-local +++ b/tests/lib/config/config-local @@ -60,6 +60,7 @@ config_generate() { .common.daemonize=true | del(.common.pid_dir) | .common.log_level="info" | + .common.force_color_logs=true | .common.log_dir=strenv(LOG_DIR) | .config_paths.config_dir=strenv(CONFIG_DIR) | .config_paths.data_dir=strenv(DATA_DIR) | @@ -132,7 +133,7 @@ load_init_data() { dump_backend="$(cat "${LOCAL_INIT_DIR}/.backend")" if [[ "${DB_BACKEND}" != "${dump_backend}" ]]; then - die "Can't run with backend '${DB_BACKEND}' because the test data was build with '${dump_backend}'" + die "Can't run with backend '${DB_BACKEND}' because the test data was built with '${dump_backend}'" fi remove_init_data @@ -146,7 +147,6 @@ load_init_data() { [[ $# -lt 1 ]] && about - case "$1" in make) ./assert-crowdsec-not-running diff --git a/tests/lib/setup_file.sh b/tests/lib/setup_file.sh index 281c75f57..6197d7744 100755 --- a/tests/lib/setup_file.sh +++ b/tests/lib/setup_file.sh @@ -24,8 +24,14 @@ cd "${TEST_DIR}" # complain if there's a crowdsec running system-wide or leftover from a previous test ./assert-crowdsec-not-running -# we can use the filename in test descriptions -FILE="$(basename "${BATS_TEST_FILENAME}" .bats):" +# we can prepend the filename to the test descriptions (useful to feed a TAP consumer) +if [[ "${PREFIX_TEST_NAMES_WITH_FILE:-false}" == "true" ]]; then + BATS_TEST_NAME_PREFIX="$(basename "${BATS_TEST_FILENAME}" .bats): " + export BATS_TEST_NAME_PREFIX +fi + +# before bats 1.7, we did that by hand +FILE= export FILE # the variables exported here can be seen in other setup/teardown/test functions diff --git a/tests/run-tests b/tests/run-tests index 4937e8407..b9a92810d 100755 --- a/tests/run-tests +++ b/tests/run-tests @@ -34,15 +34,16 @@ if [[ $# -ge 1 ]]; then echo "test files: $*" "${TEST_DIR}/lib/bats-core/bin/bats" \ --jobs 1 \ - --print-output-on-failure \ --timing \ + --print-output-on-failure \ "$@" else echo "test files: ${TEST_DIR}/bats ${TEST_DIR}/dyn-bats" "${TEST_DIR}/lib/bats-core/bin/bats" \ --jobs 1 \ + --timing \ --print-output-on-failure \ - -T "${TEST_DIR}/bats" "${TEST_DIR}/dyn-bats" + "${TEST_DIR}/bats" "${TEST_DIR}/dyn-bats" fi if [[ -n "${TEST_COVERAGE}" ]]; then