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

     1  package elasticache
     2  
     3  import (
     4  	"testing"
     5  
     6  	defsecTypes "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/elasticache"
    11  	"github.com/khulnasoft-lab/defsec/pkg/scan"
    12  
    13  	"github.com/stretchr/testify/assert"
    14  )
    15  
    16  func TestCheckEnableBackupRetention(t *testing.T) {
    17  	tests := []struct {
    18  		name     string
    19  		input    elasticache.ElastiCache
    20  		expected bool
    21  	}{
    22  		{
    23  			name: "Cluster snapshot retention days set to 0",
    24  			input: elasticache.ElastiCache{
    25  				Clusters: []elasticache.Cluster{
    26  					{
    27  						Metadata:               defsecTypes.NewTestMetadata(),
    28  						Engine:                 defsecTypes.String("redis", defsecTypes.NewTestMetadata()),
    29  						NodeType:               defsecTypes.String("cache.m4.large", defsecTypes.NewTestMetadata()),
    30  						SnapshotRetentionLimit: defsecTypes.Int(0, defsecTypes.NewTestMetadata()),
    31  					},
    32  				},
    33  			},
    34  			expected: true,
    35  		},
    36  		{
    37  			name: "Cluster snapshot retention days set to 5",
    38  			input: elasticache.ElastiCache{
    39  				Clusters: []elasticache.Cluster{
    40  					{
    41  						Metadata:               defsecTypes.NewTestMetadata(),
    42  						Engine:                 defsecTypes.String("redis", defsecTypes.NewTestMetadata()),
    43  						NodeType:               defsecTypes.String("cache.m4.large", defsecTypes.NewTestMetadata()),
    44  						SnapshotRetentionLimit: defsecTypes.Int(5, defsecTypes.NewTestMetadata()),
    45  					},
    46  				},
    47  			},
    48  			expected: false,
    49  		},
    50  	}
    51  	for _, test := range tests {
    52  		t.Run(test.name, func(t *testing.T) {
    53  			var testState state.State
    54  			testState.AWS.ElastiCache = test.input
    55  			results := CheckEnableBackupRetention.Evaluate(&testState)
    56  			var found bool
    57  			for _, result := range results {
    58  				if result.Status() == scan.StatusFailed && result.Rule().LongID() == CheckEnableBackupRetention.Rule().LongID() {
    59  					found = true
    60  				}
    61  			}
    62  			if test.expected {
    63  				assert.True(t, found, "Rule should have been found")
    64  			} else {
    65  				assert.False(t, found, "Rule should not have been found")
    66  			}
    67  		})
    68  	}
    69  }