From e907fbfb1022cf1690f50cee9f031551f01ad44e Mon Sep 17 00:00:00 2001 From: marco Date: Wed, 3 Apr 2024 17:10:37 +0200 Subject: [PATCH] fix tests; lint --- .golangci.yml | 4 ++++ pkg/cwhub/hub_test.go | 2 +- pkg/cwhub/iteminstall.go | 8 ++++---- pkg/cwhub/itemupgrade.go | 19 ++++++++----------- test/bats/20_hub.bats | 8 +++++++- test/bats/20_hub_collections.bats | 1 - test/bats/20_hub_parsers.bats | 1 - test/bats/20_hub_postoverflows.bats | 1 - test/bats/20_hub_scenarios.bats | 1 - 9 files changed, 24 insertions(+), 21 deletions(-) diff --git a/.golangci.yml b/.golangci.yml index ff46ef1c0..ceb366933 100644 --- a/.golangci.yml +++ b/.golangci.yml @@ -387,3 +387,7 @@ issues: - linters: - nonamedreturns text: "named return .* with type .* found" + + - linters: + - staticcheck + text: "csdaemon.NotifySystemd is deprecated: Use Notify instead." diff --git a/pkg/cwhub/hub_test.go b/pkg/cwhub/hub_test.go index 86569cde3..1f0d3ac89 100644 --- a/pkg/cwhub/hub_test.go +++ b/pkg/cwhub/hub_test.go @@ -72,5 +72,5 @@ func TestUpdateIndex(t *testing.T) { hub.local.HubIndexFile = "/does/not/exist/index.json" err = hub.updateIndex() - cstest.RequireErrorContains(t, err, "failed to write hub index: open /does/not/exist/index.json:") + cstest.RequireErrorContains(t, err, "failed to create temporary download file for /does/not/exist/index.json:") } diff --git a/pkg/cwhub/iteminstall.go b/pkg/cwhub/iteminstall.go index cf331c05f..274e7128a 100644 --- a/pkg/cwhub/iteminstall.go +++ b/pkg/cwhub/iteminstall.go @@ -58,15 +58,15 @@ func (i *Item) Install(force bool, downloadOnly bool) error { return nil } + if err := i.enable(); err != nil { + return fmt.Errorf("while enabling %s: %w", i.Name, err) + } + // a check on stdout is used while scripting to know if the hub has been upgraded // and a configuration reload is required // TODO: use a better way to communicate this fmt.Printf("installed %s\n", i.Name) - if err := i.enable(); err != nil { - return fmt.Errorf("while enabling %s: %w", i.Name, err) - } - i.hub.logger.Infof("Enabled %s", i.Name) return nil diff --git a/pkg/cwhub/itemupgrade.go b/pkg/cwhub/itemupgrade.go index 29b681be1..f02145027 100644 --- a/pkg/cwhub/itemupgrade.go +++ b/pkg/cwhub/itemupgrade.go @@ -17,8 +17,6 @@ import ( // Upgrade downloads and applies the last version of the item from the hub. func (i *Item) Upgrade(force bool) (bool, error) { - updated := false - if i.State.IsLocal() { i.hub.logger.Infof("not upgrading %s: local item", i.Name) return false, nil @@ -53,17 +51,16 @@ func (i *Item) Upgrade(force bool) (bool, error) { if i.State.Tainted { i.hub.logger.Warningf("%v %s is tainted, --force to overwrite", emoji.Warning, i.Name) } - } else { - // a check on stdout is used while scripting to know if the hub has been upgraded - // and a configuration reload is required - // TODO: use a better way to communicate this - fmt.Printf("updated %s\n", i.Name) - i.hub.logger.Infof("%v %s: updated", emoji.Package, i.Name) - - updated = true + return false, nil } - return updated, nil + // a check on stdout is used while scripting to know if the hub has been upgraded + // and a configuration reload is required + // TODO: use a better way to communicate this + fmt.Printf("updated %s\n", i.Name) + i.hub.logger.Infof("%v %s: updated", emoji.Package, i.Name) + + return true, nil } // downloadLatest downloads the latest version of the item to the hub directory. diff --git a/test/bats/20_hub.bats b/test/bats/20_hub.bats index 18e3770bc..0d9f29b24 100644 --- a/test/bats/20_hub.bats +++ b/test/bats/20_hub.bats @@ -125,13 +125,19 @@ teardown() { assert_stderr --partial "Upgraded 0 contexts" assert_stderr --partial "Upgrading collections" assert_stderr --partial "Upgraded 0 collections" + assert_stderr --partial "Upgrading appsec-configs" + assert_stderr --partial "Upgraded 0 appsec-configs" + assert_stderr --partial "Upgrading appsec-rules" + assert_stderr --partial "Upgraded 0 appsec-rules" + assert_stderr --partial "Upgrading collections" + assert_stderr --partial "Upgraded 0 collections" rune -0 cscli parsers install crowdsecurity/syslog-logs rune -0 cscli hub upgrade assert_stderr --partial "crowdsecurity/syslog-logs: up-to-date" rune -0 cscli hub upgrade --force - assert_stderr --partial "crowdsecurity/syslog-logs: overwrite" + assert_stderr --partial "crowdsecurity/syslog-logs: up-to-date" assert_stderr --partial "crowdsecurity/syslog-logs: updated" assert_stderr --partial "Upgraded 1 parsers" # this is used by the cron script to know if the hub was updated diff --git a/test/bats/20_hub_collections.bats b/test/bats/20_hub_collections.bats index 5e5b43a9e..1381fe8e5 100644 --- a/test/bats/20_hub_collections.bats +++ b/test/bats/20_hub_collections.bats @@ -180,7 +180,6 @@ teardown() { assert_stderr --partial "error while installing 'crowdsecurity/sshd': while enabling crowdsecurity/sshd: crowdsecurity/sshd is tainted, won't enable unless --force" rune -0 cscli collections install crowdsecurity/sshd --force - assert_stderr --partial "crowdsecurity/sshd: overwrite" assert_stderr --partial "Enabled crowdsecurity/sshd" } diff --git a/test/bats/20_hub_parsers.bats b/test/bats/20_hub_parsers.bats index 71a1f933a..214463f9c 100644 --- a/test/bats/20_hub_parsers.bats +++ b/test/bats/20_hub_parsers.bats @@ -180,7 +180,6 @@ teardown() { assert_stderr --partial "error while installing 'crowdsecurity/whitelists': while enabling crowdsecurity/whitelists: crowdsecurity/whitelists is tainted, won't enable unless --force" rune -0 cscli parsers install crowdsecurity/whitelists --force - assert_stderr --partial "crowdsecurity/whitelists: overwrite" assert_stderr --partial "Enabled crowdsecurity/whitelists" } diff --git a/test/bats/20_hub_postoverflows.bats b/test/bats/20_hub_postoverflows.bats index de4b1e8a5..5123966a4 100644 --- a/test/bats/20_hub_postoverflows.bats +++ b/test/bats/20_hub_postoverflows.bats @@ -180,7 +180,6 @@ teardown() { assert_stderr --partial "error while installing 'crowdsecurity/rdns': while enabling crowdsecurity/rdns: crowdsecurity/rdns is tainted, won't enable unless --force" rune -0 cscli postoverflows install crowdsecurity/rdns --force - assert_stderr --partial "crowdsecurity/rdns: overwrite" assert_stderr --partial "Enabled crowdsecurity/rdns" } diff --git a/test/bats/20_hub_scenarios.bats b/test/bats/20_hub_scenarios.bats index 9c441057a..3089e244c 100644 --- a/test/bats/20_hub_scenarios.bats +++ b/test/bats/20_hub_scenarios.bats @@ -181,7 +181,6 @@ teardown() { assert_stderr --partial "error while installing 'crowdsecurity/ssh-bf': while enabling crowdsecurity/ssh-bf: crowdsecurity/ssh-bf is tainted, won't enable unless --force" rune -0 cscli scenarios install crowdsecurity/ssh-bf --force - assert_stderr --partial "crowdsecurity/ssh-bf: overwrite" assert_stderr --partial "Enabled crowdsecurity/ssh-bf" }