...

Source file src/edge-infra.dev/pkg/sds/emergencyaccess/rules/storage/file/validation_test.go

Documentation: edge-infra.dev/pkg/sds/emergencyaccess/rules/storage/file

     1  package file
     2  
     3  // tests for all things to with rule validation
     4  import (
     5  	"context"
     6  	"testing"
     7  
     8  	"github.com/stretchr/testify/assert"
     9  
    10  	"edge-infra.dev/pkg/lib/fog"
    11  	"edge-infra.dev/pkg/sds/emergencyaccess/eaconst"
    12  	rulesengine "edge-infra.dev/pkg/sds/emergencyaccess/rules"
    13  )
    14  
    15  const validTestdata = "./testdata/valid"
    16  
    17  func TestDatasetLoad(t *testing.T) {
    18  	ds, err := New(fog.New(), validTestdata)
    19  	assert.NoError(t, err)
    20  	tests := map[string]struct {
    21  		bannerID  string
    22  		command   string
    23  		expResult []string
    24  	}{
    25  		"Default with added role": {
    26  			"a-banner-id",
    27  			"skubectl",
    28  			[]string{"stage"},
    29  		},
    30  		"Wrong banner ID": {
    31  			"no-banner-id",
    32  			"ls",
    33  			[]string{"basic"},
    34  		},
    35  		"Addition to basic": {
    36  			"override",
    37  			"rm",
    38  			[]string{"admin", "basic"},
    39  		},
    40  	}
    41  	for name, tc := range tests {
    42  		t.Run(name, func(t *testing.T) {
    43  			command := rulesengine.Command{
    44  				Name: tc.command,
    45  				Type: eaconst.Command,
    46  			}
    47  
    48  			res, err := ds.EARoles(context.Background(), tc.bannerID, command)
    49  			assert.NoError(t, err)
    50  			assert.ElementsMatch(t, tc.expResult, res)
    51  		})
    52  	}
    53  }
    54  
    55  func TestExecutableCommandType(t *testing.T) {
    56  	ds, err := New(fog.New(), validTestdata)
    57  	assert.Nil(t, err)
    58  
    59  	command := rulesengine.Command{
    60  		Name: "myScript",
    61  		Type: eaconst.Executable,
    62  	}
    63  
    64  	_, err = ds.EARoles(context.Background(), "a-banner-id", command)
    65  	assert.EqualError(t, err, "file based dataset currently does not support non-command types: \"executable\"")
    66  }
    67  
    68  func TestNoDefault(t *testing.T) {
    69  	dir := "./testdata/nodefault"
    70  	ds, err := New(fog.New(), dir)
    71  	assert.Nil(t, err)
    72  
    73  	command := rulesengine.Command{
    74  		Name: "somecommand",
    75  		Type: eaconst.Command,
    76  	}
    77  
    78  	res, err := ds.EARoles(context.Background(), "a-banner-id", command)
    79  	assert.Nil(t, err)
    80  	assert.ElementsMatch(t, []string{"stage"}, res)
    81  	res, err = ds.EARoles(context.Background(), "another-banner-id", command)
    82  	assert.Nil(t, err)
    83  	assert.ElementsMatch(t, []string{}, res)
    84  }
    85  
    86  func TestDuplicateID(t *testing.T) {
    87  	dir := "./testdata/duplicateid"
    88  	_, err := New(fog.New(), dir)
    89  	assert.EqualError(t, err, "cannot overwrite unique id default")
    90  }
    91  
    92  func TestInList(t *testing.T) {
    93  	tests := map[string]struct {
    94  		array  []string
    95  		val    string
    96  		expRes bool
    97  	}{
    98  		"Empty array": {
    99  			[]string{},
   100  			"any",
   101  			false,
   102  		},
   103  		"True": {
   104  			[]string{"a", "b", "c"},
   105  			"a",
   106  			true,
   107  		},
   108  		"False": {
   109  			[]string{"a", "b", "c"},
   110  			"d",
   111  			false,
   112  		},
   113  	}
   114  	for name, tc := range tests {
   115  		t.Run(name, func(t *testing.T) {
   116  			assert.Equal(t, tc.expRes, inList(tc.val, tc.array))
   117  		})
   118  	}
   119  }
   120  

View as plain text