package file // tests for all things to with rule validation import ( "context" "testing" "github.com/stretchr/testify/assert" "edge-infra.dev/pkg/lib/fog" "edge-infra.dev/pkg/sds/emergencyaccess/eaconst" rulesengine "edge-infra.dev/pkg/sds/emergencyaccess/rules" ) const validTestdata = "./testdata/valid" func TestDatasetLoad(t *testing.T) { ds, err := New(fog.New(), validTestdata) assert.NoError(t, err) tests := map[string]struct { bannerID string command string expResult []string }{ "Default with added role": { "a-banner-id", "skubectl", []string{"stage"}, }, "Wrong banner ID": { "no-banner-id", "ls", []string{"basic"}, }, "Addition to basic": { "override", "rm", []string{"admin", "basic"}, }, } for name, tc := range tests { t.Run(name, func(t *testing.T) { command := rulesengine.Command{ Name: tc.command, Type: eaconst.Command, } res, err := ds.EARoles(context.Background(), tc.bannerID, command) assert.NoError(t, err) assert.ElementsMatch(t, tc.expResult, res) }) } } func TestExecutableCommandType(t *testing.T) { ds, err := New(fog.New(), validTestdata) assert.Nil(t, err) command := rulesengine.Command{ Name: "myScript", Type: eaconst.Executable, } _, err = ds.EARoles(context.Background(), "a-banner-id", command) assert.EqualError(t, err, "file based dataset currently does not support non-command types: \"executable\"") } func TestNoDefault(t *testing.T) { dir := "./testdata/nodefault" ds, err := New(fog.New(), dir) assert.Nil(t, err) command := rulesengine.Command{ Name: "somecommand", Type: eaconst.Command, } res, err := ds.EARoles(context.Background(), "a-banner-id", command) assert.Nil(t, err) assert.ElementsMatch(t, []string{"stage"}, res) res, err = ds.EARoles(context.Background(), "another-banner-id", command) assert.Nil(t, err) assert.ElementsMatch(t, []string{}, res) } func TestDuplicateID(t *testing.T) { dir := "./testdata/duplicateid" _, err := New(fog.New(), dir) assert.EqualError(t, err, "cannot overwrite unique id default") } func TestInList(t *testing.T) { tests := map[string]struct { array []string val string expRes bool }{ "Empty array": { []string{}, "any", false, }, "True": { []string{"a", "b", "c"}, "a", true, }, "False": { []string{"a", "b", "c"}, "d", false, }, } for name, tc := range tests { t.Run(name, func(t *testing.T) { assert.Equal(t, tc.expRes, inList(tc.val, tc.array)) }) } }