Skip to content

Commit

Permalink
tests: added units for analysers
Browse files Browse the repository at this point in the history
  • Loading branch information
yusufhm committed Oct 20, 2024
1 parent 5d5ed5a commit 60c024a
Show file tree
Hide file tree
Showing 4 changed files with 256 additions and 5 deletions.
8 changes: 4 additions & 4 deletions pkg/analyse/allowedlist_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,20 +4,20 @@ import (
"io"
"testing"

"github.com/sirupsen/logrus"
"github.com/stretchr/testify/assert"

. "github.com/salsadigitalauorg/shipshape/pkg/analyse"
"github.com/salsadigitalauorg/shipshape/pkg/breach"
"github.com/salsadigitalauorg/shipshape/pkg/data"
"github.com/salsadigitalauorg/shipshape/pkg/fact"
"github.com/salsadigitalauorg/shipshape/pkg/fact/testdata"
"github.com/sirupsen/logrus"
"github.com/stretchr/testify/assert"
)

func TestAllowedListInit(t *testing.T) {

assert := assert.New(t)

// Test that the yaml:key plugin is registered.
// Test that the plugin is registered.
plugin := Registry["allowed:list"]("testAllowedList")
assert.NotNil(plugin)
analyser, ok := plugin.(*AllowedList)
Expand Down
3 changes: 2 additions & 1 deletion pkg/analyse/analyse_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,12 +53,13 @@ func TestParseConfig(t *testing.T) {

t.Run(tc.name, func(t *testing.T) {
assert.Len(Analysers, 0)
registryBackup := Registry
if tc.registry != nil {
Registry = tc.registry
}
ParseConfig(tc.config)
defer func() {
Registry = map[string]func(string) Analyser{}
Registry = registryBackup
Analysers = map[string]Analyser{}
}()
assert.Len(Analysers, tc.expectAnalyserCount)
Expand Down
96 changes: 96 additions & 0 deletions pkg/analyse/notempty_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,96 @@
package analyse_test

import (
"io"
"testing"

"github.com/sirupsen/logrus"
"github.com/stretchr/testify/assert"

. "github.com/salsadigitalauorg/shipshape/pkg/analyse"
"github.com/salsadigitalauorg/shipshape/pkg/breach"
"github.com/salsadigitalauorg/shipshape/pkg/data"
"github.com/salsadigitalauorg/shipshape/pkg/fact"
"github.com/salsadigitalauorg/shipshape/pkg/fact/testdata"
)

func TestNotEmptyInit(t *testing.T) {
assert := assert.New(t)

// Test that the plugin is registered.
plugin := Registry["not:empty"]("testNotEmpty")
assert.NotNil(plugin)
analyser, ok := plugin.(*NotEmpty)
assert.True(ok)
assert.Equal("testNotEmpty", analyser.Id)
}

func TestNotEmptyPluginName(t *testing.T) {
instance := NotEmpty{Id: "testNotEmpty"}
assert.Equal(t, "not:empty", instance.PluginName())
}

func TestNotEmptyAnalyse(t *testing.T) {
tt := []struct {
name string
input fact.Facter
expectedBreaches []breach.Breach
}{
{
name: "mapNestedStringNil",
input: &testdata.TestFacter{
Name: "testFacter",
TestInputDataFormat: data.FormatMapNestedString,
TestInputData: map[string]map[string]string(nil),
},
expectedBreaches: []breach.Breach{},
},
{
name: "mapNestedStringEmpty",
input: &testdata.TestFacter{
Name: "testFacter",
TestInputDataFormat: data.FormatMapNestedString,
TestInputData: map[string]map[string]string{},
},
expectedBreaches: []breach.Breach{},
},
{
name: "mapNestedStringNotEmpty",
input: &testdata.TestFacter{
Name: "testFacter",
TestInputDataFormat: data.FormatMapNestedString,
TestInputData: map[string]map[string]string{
"key1": {"subKey1": "value1"},
},
},
expectedBreaches: []breach.Breach{
&breach.KeyValueBreach{
BreachType: "key-value",
CheckName: "mapNestedStringNotEmpty",
Key: "key1",
ValueLabel: "subKey1",
Value: "value1",
},
},
},
}

for _, tc := range tt {
assert := assert.New(t)

currLogOut := logrus.StandardLogger().Out
defer logrus.SetOutput(currLogOut)
logrus.SetOutput(io.Discard)

t.Run(tc.name, func(t *testing.T) {
analyser := NotEmpty{Id: tc.name}

tc.input.Collect()
analyser.SetInput(tc.input)
analyser.Analyse()

assert.Len(analyser.Result.Breaches, len(tc.expectedBreaches))
assert.ElementsMatch(tc.expectedBreaches, analyser.Result.Breaches)
})
}
}
154 changes: 154 additions & 0 deletions pkg/analyse/regexmatch_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,154 @@
package analyse_test

import (
"io"
"testing"

"github.com/sirupsen/logrus"
"github.com/stretchr/testify/assert"

. "github.com/salsadigitalauorg/shipshape/pkg/analyse"
"github.com/salsadigitalauorg/shipshape/pkg/breach"
"github.com/salsadigitalauorg/shipshape/pkg/data"
"github.com/salsadigitalauorg/shipshape/pkg/fact"
"github.com/salsadigitalauorg/shipshape/pkg/fact/testdata"
)

func TestRegexMatchInit(t *testing.T) {
assert := assert.New(t)

// Test that the plugin is registered.
plugin := Registry["regex:match"]("testRegexMatch")
assert.NotNil(plugin)
analyser, ok := plugin.(*RegexMatch)
assert.True(ok)
assert.Equal("testRegexMatch", analyser.Id)
}

func TestRegexMatchPluginName(t *testing.T) {
instance := RegexMatch{Id: "testRegexMatch"}
assert.Equal(t, "regex:match", instance.PluginName())
}

func TestRegexMatchAnalyse(t *testing.T) {
tt := []struct {
name string
input fact.Facter
pattern string
ignore string
expectedBreaches []breach.Breach
}{
{
name: "mapNestedStringEmpty",
input: &testdata.TestFacter{
Name: "testFacter",
TestInputDataFormat: data.FormatMapNestedString,
TestInputData: map[string]map[string]string{},
},
},
{
name: "mapNestedStringNoMatch",
input: &testdata.TestFacter{
Name: "testFacter",
TestInputDataFormat: data.FormatMapNestedString,
TestInputData: map[string]map[string]string{
"key1": {
"subkey1": "value1",
"subkey2": "value2",
},
},
},
pattern: ".*value3.*",
expectedBreaches: []breach.Breach{},
},
{
name: "mapNestedString1Match",
input: &testdata.TestFacter{
Name: "testFacter",
TestInputDataFormat: data.FormatMapNestedString,
TestInputData: map[string]map[string]string{
"key1": {
"subkey1": "value1",
"subkey2": "value2",
},
},
},
pattern: ".*value2.*",
expectedBreaches: []breach.Breach{
&breach.KeyValueBreach{
BreachType: "key-value",
CheckName: "mapNestedString1Match",
Key: "key1",
ValueLabel: "subkey2",
Value: "value2",
},
},
},
{
name: "mapNestedStringMultipleMatches",
input: &testdata.TestFacter{
Name: "testFacter",
TestInputDataFormat: data.FormatMapNestedString,
TestInputData: map[string]map[string]string{
"key1": {
"subkey1": "value1",
"subkey2": "value2",
"subkey4": "value4",
"subkey5": "value5",
},
"key2": {
"subkey2": "value2",
"subkey3": "value3",
},
},
},
pattern: ".*value(1|3|5).*",
expectedBreaches: []breach.Breach{
&breach.KeyValueBreach{
BreachType: "key-value",
CheckName: "mapNestedStringMultipleMatches",
Key: "key1",
ValueLabel: "subkey1",
Value: "value1",
},
&breach.KeyValueBreach{
BreachType: "key-value",
CheckName: "mapNestedStringMultipleMatches",
Key: "key1",
ValueLabel: "subkey5",
Value: "value5",
},
&breach.KeyValueBreach{
BreachType: "key-value",
CheckName: "mapNestedStringMultipleMatches",
Key: "key2",
ValueLabel: "subkey3",
Value: "value3",
},
},
},
}

for _, tc := range tt {
assert := assert.New(t)

currLogOut := logrus.StandardLogger().Out
defer logrus.SetOutput(currLogOut)
logrus.SetOutput(io.Discard)

t.Run(tc.name, func(t *testing.T) {
analyser := RegexMatch{
Id: tc.name,
Pattern: tc.pattern,
Ignore: tc.ignore,
}

tc.input.Collect()
analyser.SetInput(tc.input)
analyser.Analyse()

assert.Len(analyser.Result.Breaches, len(tc.expectedBreaches))
assert.ElementsMatch(tc.expectedBreaches, analyser.Result.Breaches)
})
}
}

0 comments on commit 60c024a

Please sign in to comment.