github.com/devseccon/trivy@v0.47.1-0.20231123133102-bd902a0bd996/pkg/cloud/report/resource_test.go (about)

     1  package report
     2  
     3  import (
     4  	"bytes"
     5  	"testing"
     6  
     7  	"github.com/stretchr/testify/assert"
     8  	"github.com/stretchr/testify/require"
     9  
    10  	"github.com/aquasecurity/trivy-db/pkg/types"
    11  	"github.com/devseccon/trivy/pkg/flag"
    12  )
    13  
    14  func Test_ResourceReport(t *testing.T) {
    15  	tests := []struct {
    16  		name      string
    17  		options   flag.Options
    18  		fromCache bool
    19  		expected  string
    20  	}{
    21  		{
    22  			name: "simple table output",
    23  			options: flag.Options{
    24  				ReportOptions: flag.ReportOptions{
    25  					Format: tableFormat,
    26  					Severities: []types.Severity{
    27  						types.SeverityLow,
    28  						types.SeverityMedium,
    29  						types.SeverityHigh,
    30  						types.SeverityCritical,
    31  					},
    32  				},
    33  				AWSOptions: flag.AWSOptions{
    34  					Services: []string{"s3"},
    35  				},
    36  			},
    37  			fromCache: false,
    38  			expected: `
    39  Resource Summary for Service 's3' (AWS Account )
    40  ┌─────────────────────────────────────────┬──────────────────────────────────────────┐
    41  │                                         │            Misconfigurations             │
    42  │                                         ├──────────┬──────┬────────┬─────┬─────────┤
    43  │ Resource                                │ Critical │ High │ Medium │ Low │ Unknown │
    44  ├─────────────────────────────────────────┼──────────┼──────┼────────┼─────┼─────────┤
    45  │ arn:aws:s3:us-east-1:1234567890:bucket1 │        0 │    1 │      0 │   0 │       0 │
    46  │ arn:aws:s3:us-east-1:1234567890:bucket2 │        0 │    2 │      0 │   0 │       0 │
    47  └─────────────────────────────────────────┴──────────┴──────┴────────┴─────┴─────────┘
    48  `,
    49  		},
    50  		{
    51  			name: "results from cache",
    52  			options: flag.Options{
    53  				ReportOptions: flag.ReportOptions{
    54  					Format: tableFormat,
    55  					Severities: []types.Severity{
    56  						types.SeverityLow,
    57  						types.SeverityMedium,
    58  						types.SeverityHigh,
    59  						types.SeverityCritical,
    60  					},
    61  				},
    62  				AWSOptions: flag.AWSOptions{
    63  					Services: []string{"s3"},
    64  				},
    65  			},
    66  			fromCache: true,
    67  			expected: `
    68  Resource Summary for Service 's3' (AWS Account )
    69  ┌─────────────────────────────────────────┬──────────────────────────────────────────┐
    70  │                                         │            Misconfigurations             │
    71  │                                         ├──────────┬──────┬────────┬─────┬─────────┤
    72  │ Resource                                │ Critical │ High │ Medium │ Low │ Unknown │
    73  ├─────────────────────────────────────────┼──────────┼──────┼────────┼─────┼─────────┤
    74  │ arn:aws:s3:us-east-1:1234567890:bucket1 │        0 │    1 │      0 │   0 │       0 │
    75  │ arn:aws:s3:us-east-1:1234567890:bucket2 │        0 │    2 │      0 │   0 │       0 │
    76  └─────────────────────────────────────────┴──────────┴──────┴────────┴─────┴─────────┘
    77  
    78  This scan report was loaded from cached results. If you'd like to run a fresh scan, use --update-cache.
    79  `,
    80  		},
    81  		{
    82  			name: "no problems",
    83  			options: flag.Options{
    84  				ReportOptions: flag.ReportOptions{
    85  					Format: tableFormat,
    86  					Severities: []types.Severity{
    87  						types.SeverityLow,
    88  					},
    89  				},
    90  				AWSOptions: flag.AWSOptions{
    91  					Services: []string{"s3"},
    92  				},
    93  			},
    94  			fromCache: false,
    95  			expected: `
    96  Resource Summary for Service 's3' (AWS Account )
    97  
    98  No problems detected.
    99  `,
   100  		},
   101  	}
   102  	for _, tt := range tests {
   103  		t.Run(tt.name, func(t *testing.T) {
   104  			report := New(
   105  				"AWS",
   106  				tt.options.AWSOptions.Account,
   107  				tt.options.AWSOptions.Region,
   108  				createTestResults(),
   109  				tt.options.AWSOptions.Services,
   110  			)
   111  
   112  			output := bytes.NewBuffer(nil)
   113  			tt.options.SetOutputWriter(output)
   114  			require.NoError(t, Write(report, tt.options, tt.fromCache))
   115  
   116  			assert.Equal(t, "AWS", report.Provider)
   117  			assert.Equal(t, tt.options.AWSOptions.Account, report.AccountID)
   118  			assert.Equal(t, tt.options.AWSOptions.Region, report.Region)
   119  			assert.ElementsMatch(t, tt.options.AWSOptions.Services, report.ServicesInScope)
   120  			assert.Equal(t, tt.expected, output.String())
   121  		})
   122  	}
   123  }