github.com/khulnasoft-lab/defsec@v1.0.5-0.20230827010352-5e9f46893d95/internal/adapters/terraform/aws/config/adapt_test.go (about)

     1  package config
     2  
     3  import (
     4  	"testing"
     5  
     6  	defsecTypes "github.com/khulnasoft-lab/defsec/pkg/types"
     7  
     8  	"github.com/khulnasoft-lab/defsec/pkg/providers/aws/config"
     9  
    10  	"github.com/khulnasoft-lab/defsec/internal/adapters/terraform/tftestutil"
    11  
    12  	"github.com/khulnasoft-lab/defsec/test/testutil"
    13  	"github.com/stretchr/testify/assert"
    14  )
    15  
    16  func Test_adaptConfigurationAggregrator(t *testing.T) {
    17  	tests := []struct {
    18  		name      string
    19  		terraform string
    20  		expected  config.ConfigurationAggregrator
    21  	}{
    22  		{
    23  			name: "configured",
    24  			terraform: `
    25  			resource "aws_config_configuration_aggregator" "example" {
    26  				name = "example"
    27  				  
    28  				account_aggregation_source {
    29  				  account_ids = ["123456789012"]
    30  				  all_regions = true
    31  				}
    32  			}
    33  `,
    34  			expected: config.ConfigurationAggregrator{
    35  				Metadata:         defsecTypes.NewTestMetadata(),
    36  				SourceAllRegions: defsecTypes.Bool(true, defsecTypes.NewTestMetadata()),
    37  			},
    38  		},
    39  		{
    40  			name: "defaults",
    41  			terraform: `
    42  			resource "aws_config_configuration_aggregator" "example" {
    43  			}
    44  `,
    45  			expected: config.ConfigurationAggregrator{
    46  				Metadata:         defsecTypes.NewTestMetadata(),
    47  				SourceAllRegions: defsecTypes.Bool(false, defsecTypes.NewTestMetadata()),
    48  			},
    49  		},
    50  	}
    51  
    52  	for _, test := range tests {
    53  		t.Run(test.name, func(t *testing.T) {
    54  			modules := tftestutil.CreateModulesFromSource(t, test.terraform, ".tf")
    55  			adapted := adaptConfigurationAggregrator(modules)
    56  			testutil.AssertDefsecEqual(t, test.expected, adapted)
    57  		})
    58  	}
    59  }
    60  
    61  func TestLines(t *testing.T) {
    62  	src := `
    63  	resource "aws_config_configuration_aggregator" "example" {
    64  		name = "example"
    65  		  
    66  		account_aggregation_source {
    67  		  account_ids = ["123456789012"]
    68  		  all_regions = true
    69  		}
    70  	}`
    71  
    72  	modules := tftestutil.CreateModulesFromSource(t, src, ".tf")
    73  	adapted := Adapt(modules)
    74  	aggregator := adapted.ConfigurationAggregrator
    75  
    76  	assert.Equal(t, 2, aggregator.Metadata.Range().GetStartLine())
    77  	assert.Equal(t, 9, aggregator.Metadata.Range().GetEndLine())
    78  
    79  	assert.Equal(t, 7, aggregator.SourceAllRegions.GetMetadata().Range().GetStartLine())
    80  	assert.Equal(t, 7, aggregator.SourceAllRegions.GetMetadata().Range().GetEndLine())
    81  }