github.com/khulnasoft-lab/defsec@v1.0.5-0.20230827010352-5e9f46893d95/rules/cloud/policies/aws/sam/enable_function_tracing_test.go (about) 1 package sam 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/sam" 11 "github.com/khulnasoft-lab/defsec/pkg/scan" 12 13 "github.com/stretchr/testify/assert" 14 ) 15 16 func TestCheckEnableFunctionTracing(t *testing.T) { 17 tests := []struct { 18 name string 19 input sam.SAM 20 expected bool 21 }{ 22 { 23 name: "SAM pass-through tracing mode", 24 input: sam.SAM{ 25 Functions: []sam.Function{ 26 { 27 Metadata: defsecTypes.NewTestMetadata(), 28 Tracing: defsecTypes.String(sam.TracingModePassThrough, defsecTypes.NewTestMetadata()), 29 }, 30 }, 31 }, 32 expected: true, 33 }, 34 { 35 name: "SAM active tracing mode", 36 input: sam.SAM{ 37 Functions: []sam.Function{ 38 { 39 Metadata: defsecTypes.NewTestMetadata(), 40 Tracing: defsecTypes.String(sam.TracingModeActive, defsecTypes.NewTestMetadata()), 41 }, 42 }, 43 }, 44 expected: false, 45 }, 46 } 47 for _, test := range tests { 48 t.Run(test.name, func(t *testing.T) { 49 var testState state.State 50 testState.AWS.SAM = test.input 51 results := CheckEnableFunctionTracing.Evaluate(&testState) 52 var found bool 53 for _, result := range results { 54 if result.Status() == scan.StatusFailed && result.Rule().LongID() == CheckEnableFunctionTracing.Rule().LongID() { 55 found = true 56 } 57 } 58 if test.expected { 59 assert.True(t, found, "Rule should have been found") 60 } else { 61 assert.False(t, found, "Rule should not have been found") 62 } 63 }) 64 } 65 }