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

     1  package ec2
     2  
     3  import (
     4  	"testing"
     5  
     6  	"github.com/khulnasoft-lab/defsec/pkg/types"
     7  
     8  	"github.com/khulnasoft-lab/defsec/pkg/providers/aws/ec2"
     9  
    10  	"github.com/khulnasoft-lab/defsec/pkg/state"
    11  
    12  	"github.com/khulnasoft-lab/defsec/pkg/scan"
    13  
    14  	"github.com/stretchr/testify/assert"
    15  )
    16  
    17  func TestCheckNoDefaultVpc(t *testing.T) {
    18  	tests := []struct {
    19  		name     string
    20  		input    ec2.EC2
    21  		expected bool
    22  	}{
    23  		{
    24  			name: "default AWS VPC",
    25  			input: ec2.EC2{
    26  				VPCs: []ec2.VPC{
    27  					{
    28  						Metadata:  types.NewTestMetadata(),
    29  						IsDefault: types.Bool(true, types.NewTestMetadata()),
    30  					},
    31  				},
    32  			},
    33  			expected: true,
    34  		},
    35  		{
    36  			name: "vpc but not default AWS VPC",
    37  			input: ec2.EC2{
    38  				VPCs: []ec2.VPC{
    39  					{
    40  						Metadata:  types.NewTestMetadata(),
    41  						IsDefault: types.Bool(false, types.NewTestMetadata()),
    42  					},
    43  				},
    44  			},
    45  			expected: false,
    46  		},
    47  		{
    48  			name:     "no default AWS VPC",
    49  			input:    ec2.EC2{},
    50  			expected: false,
    51  		},
    52  	}
    53  	for _, test := range tests {
    54  		t.Run(test.name, func(t *testing.T) {
    55  			var testState state.State
    56  			testState.AWS.EC2 = test.input
    57  			results := CheckNoDefaultVpc.Evaluate(&testState)
    58  			var found bool
    59  			for _, result := range results {
    60  				if result.Status() == scan.StatusFailed && result.Rule().LongID() == CheckNoDefaultVpc.Rule().LongID() {
    61  					found = true
    62  				}
    63  			}
    64  			if test.expected {
    65  				assert.True(t, found, "Rule should have been found")
    66  			} else {
    67  				assert.False(t, found, "Rule should not have been found")
    68  			}
    69  		})
    70  	}
    71  }