github.com/khulnasoft-lab/defsec@v1.0.5-0.20230827010352-5e9f46893d95/rules/cloud/policies/aws/redshift/no_classic_resources_test.go (about)

     1  package redshift
     2  
     3  import (
     4  	"testing"
     5  
     6  	"github.com/khulnasoft-lab/defsec/pkg/types"
     7  
     8  	"github.com/khulnasoft-lab/defsec/pkg/state"
     9  
    10  	"github.com/khulnasoft-lab/defsec/pkg/providers/aws/redshift"
    11  	"github.com/khulnasoft-lab/defsec/pkg/scan"
    12  
    13  	"github.com/stretchr/testify/assert"
    14  )
    15  
    16  func TestCheckNoClassicResources(t *testing.T) {
    17  	tests := []struct {
    18  		name     string
    19  		input    redshift.Redshift
    20  		expected bool
    21  	}{
    22  		{
    23  			name: "security groups present",
    24  			input: redshift.Redshift{
    25  				SecurityGroups: []redshift.SecurityGroup{
    26  					{
    27  						Metadata: types.NewTestMetadata(),
    28  					},
    29  				},
    30  			},
    31  			expected: true,
    32  		},
    33  		{
    34  			name:     "no security groups",
    35  			input:    redshift.Redshift{},
    36  			expected: false,
    37  		},
    38  	}
    39  	for _, test := range tests {
    40  		t.Run(test.name, func(t *testing.T) {
    41  			var testState state.State
    42  			testState.AWS.Redshift = test.input
    43  			results := CheckNoClassicResources.Evaluate(&testState)
    44  			var found bool
    45  			for _, result := range results {
    46  				if result.Status() == scan.StatusFailed && result.Rule().LongID() == CheckNoClassicResources.Rule().LongID() {
    47  					found = true
    48  				}
    49  			}
    50  			if test.expected {
    51  				assert.True(t, found, "Rule should have been found")
    52  			} else {
    53  				assert.False(t, found, "Rule should not have been found")
    54  			}
    55  		})
    56  	}
    57  }