-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
4 changed files
with
256 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
}) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
}) | ||
} | ||
} |