github.com/aquasecurity/trivy-iac@v0.8.1-0.20240127024015-3d8e412cf0ab/cmd/avd_generator/main_test.go (about)

     1  package main
     2  
     3  import (
     4  	"fmt"
     5  	"os"
     6  	"path"
     7  	"path/filepath"
     8  	"runtime"
     9  	"testing"
    10  
    11  	"github.com/aquasecurity/defsec/pkg/framework"
    12  	registered "github.com/aquasecurity/defsec/pkg/rules"
    13  	"github.com/stretchr/testify/assert"
    14  	"github.com/stretchr/testify/require"
    15  )
    16  
    17  func init() { // change the pwd for the test to top level defesc dir
    18  	_, filename, _, _ := runtime.Caller(0)
    19  	dir := path.Join(path.Dir(filename), "../..")
    20  	err := os.Chdir(dir)
    21  	if err != nil {
    22  		panic(err)
    23  	}
    24  }
    25  
    26  func Test_AVDPageGeneration(t *testing.T) {
    27  	tmpDir := t.TempDir()
    28  	defer func() {
    29  		os.RemoveAll(tmpDir)
    30  	}()
    31  
    32  	var generateCount int
    33  	for _, metadata := range registered.GetRegistered(framework.ALL) {
    34  		writeDocsFile(metadata, tmpDir)
    35  		generateCount++
    36  	}
    37  	fmt.Printf("\nGenerated %d files in avd_docs\n", generateCount)
    38  
    39  	// check golang policies
    40  	b, err := os.ReadFile(filepath.Join(tmpDir, "aws/rds/AVD-AWS-0077", "Terraform.md"))
    41  	require.NoError(t, err)
    42  	assert.Contains(t, string(b), `hcl
    43   resource "aws_rds_cluster" "good_example" {
    44   	cluster_identifier      = "aurora-cluster-demo"
    45   	engine                  = "aurora-mysql"
    46   	engine_version          = "5.7.mysql_aurora.2.03.2"
    47   	availability_zones      = ["us-west-2a", "us-west-2b", "us-west-2c"]
    48   	database_name           = "mydb"
    49   	master_username         = "foo"
    50   	master_password         = "bar"
    51   	backup_retention_period = 5
    52   	preferred_backup_window = "07:00-09:00"
    53     }`)
    54  
    55  	b, err = os.ReadFile(filepath.Join(tmpDir, "aws/rds/AVD-AWS-0077", "CloudFormation.md"))
    56  	require.NoError(t, err)
    57  	assert.Contains(t, string(b), `yaml---
    58  AWSTemplateFormatVersion: 2010-09-09
    59  Description: Good example
    60  Resources:
    61    Queue:
    62      Type: AWS::RDS::DBInstance
    63      Properties:
    64        BackupRetentionPeriod: 30
    65  `)
    66  
    67  	// check rego policies
    68  	b, err = os.ReadFile(filepath.Join(tmpDir, "aws/rds/AVD-AWS-0180", "Terraform.md"))
    69  	require.NoError(t, err)
    70  	assert.Contains(t, string(b), `hcl
    71   resource "aws_db_instance" "good_example" {
    72   	publicly_accessible = false
    73   }`)
    74  
    75  	b, err = os.ReadFile(filepath.Join(tmpDir, "aws/rds/AVD-AWS-0180", "CloudFormation.md"))
    76  	require.NoError(t, err)
    77  	assert.Contains(t, string(b), `yaml---
    78  AWSTemplateFormatVersion: 2010-09-09
    79  Description: Good example
    80  Resources:
    81    Queue:
    82      Type: AWS::RDS::DBInstance
    83      Properties:
    84        PubliclyAccessible: false`)
    85  }