Skip to content

Commit

Permalink
tests
Browse files Browse the repository at this point in the history
  • Loading branch information
mmetc committed Jan 6, 2025
1 parent 7fb6dfa commit ce056d1
Show file tree
Hide file tree
Showing 7 changed files with 142 additions and 84 deletions.
3 changes: 2 additions & 1 deletion pkg/cwhub/download_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,8 @@ func TestFetchIndex(t *testing.T) {

downloader.URLTemplate = "http://x/%s/%s"
downloaded, err = downloader.FetchIndex(ctx, destPath, !withContent, discard)
cstest.AssertErrorContains(t, err, `Get "http://x/main/.index.json": dial tcp: lookup x: no such host`)
// can be no such host, server misbehaving, etc
cstest.AssertErrorContains(t, err, `Get "http://x/main/.index.json": dial tcp: lookup x`)
assert.False(t, downloaded)

}
Expand Down
2 changes: 1 addition & 1 deletion pkg/cwhub/hub_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ import (
// testHubCfg creates an empty hub structure in a temporary directory
// and returns its configuration object.
//
// This allow the reuse of the temporary directory / hub content for multiple instances
// This allow the reuse of the hub content for multiple instances
// of the Hub object.
func testHubCfg(t *testing.T) *csconfig.LocalHubCfg {
tempDir := t.TempDir()
Expand Down
58 changes: 0 additions & 58 deletions pkg/cwhub/item.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,6 @@ import (

"github.com/Masterminds/semver/v3"
yaml "gopkg.in/yaml.v3"

"github.com/crowdsecurity/crowdsec/pkg/emoji"
)

const (
Expand Down Expand Up @@ -46,62 +44,6 @@ type ItemVersion struct {
Deprecated bool `json:"deprecated,omitempty" yaml:"deprecated,omitempty"`
}

// ItemState is used to keep the local state (i.e. at runtime) of an item.
// This data is not stored in the index, but is displayed with "cscli ... inspect".
type ItemState struct {
LocalPath string `json:"local_path,omitempty" yaml:"local_path,omitempty"`
LocalVersion string `json:"local_version,omitempty" yaml:"local_version,omitempty"`
LocalHash string `json:"local_hash,omitempty" yaml:"local_hash,omitempty"`
Installed bool `json:"installed"`
Downloaded bool `json:"downloaded"`
UpToDate bool `json:"up_to_date"`
Tainted bool `json:"tainted"`
TaintedBy []string `json:"tainted_by,omitempty" yaml:"tainted_by,omitempty"`
BelongsToCollections []string `json:"belongs_to_collections,omitempty" yaml:"belongs_to_collections,omitempty"`
}

// IsLocal returns true if the item has been create by a user (not downloaded from the hub).
func (s *ItemState) IsLocal() bool {
return s.Installed && !s.Downloaded
}

// Text returns the status of the item as a string (eg. "enabled,update-available").
func (s *ItemState) Text() string {
ret := "disabled"

if s.Installed {
ret = "enabled"
}

if s.IsLocal() {
ret += ",local"
}

if s.Tainted {
ret += ",tainted"
} else if !s.UpToDate && !s.IsLocal() {
ret += ",update-available"
}

return ret
}

// Emoji returns the status of the item as an emoji (eg. emoji.Warning).
func (s *ItemState) Emoji() string {
switch {
case s.IsLocal():
return emoji.House
case !s.Installed:
return emoji.Prohibited
case s.Tainted || (!s.UpToDate && !s.IsLocal()):
return emoji.Warning
case s.Installed:
return emoji.CheckMark
default:
return emoji.QuestionMark
}
}

type Dependencies struct {
Parsers []string `json:"parsers,omitempty" yaml:"parsers,omitempty"`
PostOverflows []string `json:"postoverflows,omitempty" yaml:"postoverflows,omitempty"`
Expand Down
25 changes: 1 addition & 24 deletions pkg/cwhub/item_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,39 +6,16 @@ import (
"github.com/stretchr/testify/require"
)

func TestItemStatus(t *testing.T) {
func TestItemStats(t *testing.T) {
hub := envSetup(t)

// get existing map
x := hub.GetItemMap(COLLECTIONS)
require.NotEmpty(t, x)

// Get item: good and bad
for k := range x {
item := hub.GetItem(COLLECTIONS, k)
require.NotNil(t, item)

item.State.Installed = true
item.State.UpToDate = false
item.State.Tainted = false
item.State.Downloaded = true

txt := item.State.Text()
require.Equal(t, "enabled,update-available", txt)

item.State.Installed = true
item.State.UpToDate = false
item.State.Tainted = false
item.State.Downloaded = false

txt = item.State.Text()
require.Equal(t, "enabled,local", txt)
}

stats := hub.ItemStats()
require.Equal(t, []string{
"Loaded: 2 parsers, 1 scenarios, 3 collections",
"Unmanaged items: 3 local, 0 tainted",
}, stats)
}

Expand Down
61 changes: 61 additions & 0 deletions pkg/cwhub/state.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
package cwhub

import (
"github.com/crowdsecurity/crowdsec/pkg/emoji"
)

// ItemState is used to keep the local state (i.e. at runtime) of an item.
// This data is not stored in the index, but is displayed with "cscli ... inspect".
type ItemState struct {
LocalPath string `json:"local_path,omitempty" yaml:"local_path,omitempty"`
LocalVersion string `json:"local_version,omitempty" yaml:"local_version,omitempty"`
LocalHash string `json:"local_hash,omitempty" yaml:"local_hash,omitempty"`
Installed bool `json:"installed"`
Downloaded bool `json:"downloaded"`
UpToDate bool `json:"up_to_date"`
Tainted bool `json:"tainted"`
TaintedBy []string `json:"tainted_by,omitempty" yaml:"tainted_by,omitempty"`
BelongsToCollections []string `json:"belongs_to_collections,omitempty" yaml:"belongs_to_collections,omitempty"`
}

// IsLocal returns true if the item has been create by a user (not downloaded from the hub).
func (s *ItemState) IsLocal() bool {
return s.Installed && !s.Downloaded
}

// Text returns the status of the item as a string (eg. "enabled,update-available").
func (s *ItemState) Text() string {
ret := "disabled"

if s.Installed {
ret = "enabled"
}

if s.IsLocal() {
ret += ",local"
}

if s.Tainted {
ret += ",tainted"
} else if !s.UpToDate && !s.IsLocal() {
ret += ",update-available"
}

return ret
}

// Emoji returns the status of the item as an emoji (eg. emoji.Warning).
func (s *ItemState) Emoji() string {
switch {
case s.IsLocal():
return emoji.House
case !s.Installed:
return emoji.Prohibited
case s.Tainted || (!s.UpToDate && !s.IsLocal()):
return emoji.Warning
case s.Installed:
return emoji.CheckMark
default:
return emoji.QuestionMark

Check warning on line 59 in pkg/cwhub/state.go

View check run for this annotation

Codecov / codecov/patch

pkg/cwhub/state.go#L58-L59

Added lines #L58 - L59 were not covered by tests
}
}
76 changes: 76 additions & 0 deletions pkg/cwhub/state_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
package cwhub

import (
"strconv"
"testing"

"github.com/stretchr/testify/assert"

"github.com/crowdsecurity/crowdsec/pkg/emoji"
)

func TestItemStateText(t *testing.T) {
// Test the text representation of an item state.
type test struct {
state ItemState
want string
wantIcon string
}

tests := []test{
{
ItemState{
Installed: true,
UpToDate: false,
Tainted: false,
Downloaded: true,
},
"enabled,update-available",
emoji.Warning,
}, {
ItemState{
Installed: true,
UpToDate: true,
Tainted: false,
Downloaded: true,
},
"enabled",
emoji.CheckMark,
}, {
ItemState{
Installed: true,
UpToDate: false,
Tainted: false,
Downloaded: false,
},
"enabled,local",
emoji.House,
}, {
ItemState{
Installed: false,
UpToDate: false,
Tainted: false,
Downloaded: true,
},
"disabled,update-available",
emoji.Prohibited,
}, {
ItemState{
Installed: true,
UpToDate: false,
Tainted: true,
Downloaded: true,
},
"enabled,tainted",
emoji.Warning,
},
}

for idx, tc := range tests {
t.Run("Test "+strconv.Itoa(idx), func(t *testing.T) {
got := tc.state.Text()
assert.Equal(t, tc.want, got)
assert.Equal(t, tc.wantIcon, tc.state.Emoji())
})
}
}
1 change: 1 addition & 0 deletions pkg/cwhub/sync.go
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,7 @@ func (h *Hub) getItemFileInfo(path string, logger *logrus.Logger) (*itemFileInfo
if len(subsHub) < 4 {
return nil, fmt.Errorf("path is too short: %s (%d)", path, len(subsHub))
}

stage = subsHub[1]
fauthor = subsHub[2]
fname = subsHub[3]
Expand Down

0 comments on commit ce056d1

Please sign in to comment.