github.com/aquasecurity/trivy-iac@v0.8.1-0.20240127024015-3d8e412cf0ab/test/rules_test.go (about)

     1  package test
     2  
     3  import (
     4  	"fmt"
     5  	"os"
     6  	"path/filepath"
     7  	"strings"
     8  	"testing"
     9  
    10  	"github.com/aquasecurity/defsec/pkg/framework"
    11  	"github.com/aquasecurity/defsec/pkg/rules"
    12  	"github.com/stretchr/testify/require"
    13  )
    14  
    15  func TestAVDIDs(t *testing.T) {
    16  	existing := make(map[string]struct{})
    17  	for _, rule := range rules.GetRegistered(framework.ALL) {
    18  		t.Run(rule.LongID(), func(t *testing.T) {
    19  			if rule.GetRule().AVDID == "" {
    20  				t.Errorf("Rule has no AVD ID: %#v", rule)
    21  				return
    22  			}
    23  			if _, ok := existing[rule.GetRule().AVDID]; ok {
    24  				t.Errorf("Rule detected with duplicate AVD ID: %s", rule.GetRule().AVDID)
    25  			}
    26  		})
    27  		existing[rule.GetRule().AVDID] = struct{}{}
    28  	}
    29  }
    30  
    31  func TestRulesAgainstExampleCode(t *testing.T) {
    32  	for _, rule := range rules.GetRegistered(framework.ALL) {
    33  		testName := fmt.Sprintf("%s/%s", rule.GetRule().AVDID, rule.LongID())
    34  		t.Run(testName, func(t *testing.T) {
    35  			rule := rule
    36  			t.Parallel()
    37  
    38  			t.Run("avd docs", func(t *testing.T) {
    39  				provider := strings.ToLower(rule.GetRule().Provider.ConstName())
    40  				service := strings.ToLower(strings.ReplaceAll(rule.GetRule().Service, "-", ""))
    41  				_, err := os.Stat(filepath.Join("..", "avd_docs", provider, service, rule.GetRule().AVDID, "docs.md"))
    42  				require.NoError(t, err)
    43  			})
    44  		})
    45  	}
    46  }