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