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

     1  package rds
     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/rds"
    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    rds.RDS
    20  		expected bool
    21  	}{
    22  		{
    23  			name: "Classic resources present",
    24  			input: rds.RDS{
    25  				Classic: rds.Classic{
    26  					DBSecurityGroups: []rds.DBSecurityGroup{
    27  						{
    28  							Metadata: types.NewTestMetadata(),
    29  						},
    30  					},
    31  				},
    32  			},
    33  			expected: true,
    34  		},
    35  		{
    36  			name:     "no Classic resources present",
    37  			input:    rds.RDS{},
    38  			expected: false,
    39  		},
    40  	}
    41  	for _, test := range tests {
    42  		t.Run(test.name, func(t *testing.T) {
    43  			var testState state.State
    44  			testState.AWS.RDS = test.input
    45  			results := CheckNoClassicResources.Evaluate(&testState)
    46  			var found bool
    47  			for _, result := range results {
    48  				if result.Status() == scan.StatusFailed && result.Rule().LongID() == CheckNoClassicResources.Rule().LongID() {
    49  					found = true
    50  				}
    51  			}
    52  			if test.expected {
    53  				assert.True(t, found, "Rule should have been found")
    54  			} else {
    55  				assert.False(t, found, "Rule should not have been found")
    56  			}
    57  		})
    58  	}
    59  }