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

     1  package report_test
     2  
     3  import (
     4  	"testing"
     5  
     6  	"github.com/samber/lo"
     7  	"github.com/stretchr/testify/assert"
     8  
     9  	"github.com/devseccon/trivy/pkg/compliance/report"
    10  	"github.com/devseccon/trivy/pkg/types"
    11  )
    12  
    13  func TestBuildSummary(t *testing.T) {
    14  	tests := []struct {
    15  		name       string
    16  		reportType string
    17  		input      *report.ComplianceReport
    18  		want       *report.SummaryReport
    19  	}{
    20  		{
    21  			name:       "build report summary config only",
    22  			reportType: "summary",
    23  			input: &report.ComplianceReport{
    24  				ID:               "1234",
    25  				Title:            "NSA",
    26  				RelatedResources: []string{"https://example.com"},
    27  				Results: []*report.ControlCheckResult{
    28  					{
    29  						ID:       "1.0",
    30  						Name:     "Non-root containers",
    31  						Severity: "MEDIUM",
    32  						Results: types.Results{
    33  							{
    34  								Misconfigurations: []types.DetectedMisconfiguration{
    35  									{AVDID: "AVD-KSV012", Status: types.StatusFailure},
    36  								},
    37  							},
    38  						},
    39  					},
    40  					{
    41  						ID:       "1.1",
    42  						Name:     "Immutable container file systems",
    43  						Severity: "LOW",
    44  						Results: types.Results{
    45  							{
    46  								Misconfigurations: []types.DetectedMisconfiguration{
    47  									{AVDID: "AVD-KSV013", Status: types.StatusFailure},
    48  								},
    49  							},
    50  						},
    51  					},
    52  				},
    53  			},
    54  			want: &report.SummaryReport{
    55  				SchemaVersion: 0,
    56  				ID:            "1234",
    57  				Title:         "NSA",
    58  				SummaryControls: []report.ControlCheckSummary{
    59  					{
    60  						ID:        "1.0",
    61  						Name:      "Non-root containers",
    62  						Severity:  "MEDIUM",
    63  						TotalFail: lo.ToPtr(1),
    64  					},
    65  					{
    66  						ID:        "1.1",
    67  						Name:      "Immutable container file systems",
    68  						Severity:  "LOW",
    69  						TotalFail: lo.ToPtr(1),
    70  					},
    71  				},
    72  			},
    73  		},
    74  		{
    75  			name:       "build full json output report",
    76  			reportType: "all",
    77  			input: &report.ComplianceReport{
    78  				ID:               "1234",
    79  				Title:            "NSA",
    80  				RelatedResources: []string{"https://example.com"},
    81  				Results: []*report.ControlCheckResult{
    82  					{
    83  						ID:       "1.0",
    84  						Name:     "Non-root containers",
    85  						Severity: "MEDIUM",
    86  						Results: types.Results{
    87  							{
    88  								Misconfigurations: []types.DetectedMisconfiguration{
    89  									{AVDID: "AVD-KSV012", Status: types.StatusFailure},
    90  								},
    91  							},
    92  						},
    93  					},
    94  					{
    95  						ID:       "1.1",
    96  						Name:     "Immutable container file systems",
    97  						Severity: "LOW",
    98  						Results: types.Results{
    99  							{
   100  								Misconfigurations: []types.DetectedMisconfiguration{
   101  									{AVDID: "AVD-KSV013", Status: types.StatusFailure},
   102  								},
   103  							},
   104  						},
   105  					},
   106  					{
   107  						ID:       "1.2",
   108  						Name:     "tzdata - new upstream version",
   109  						Severity: "LOW",
   110  						Results: types.Results{
   111  							{
   112  								Vulnerabilities: []types.DetectedVulnerability{
   113  									{VulnerabilityID: "CVE-9999-0001"},
   114  									{VulnerabilityID: "CVE-9999-0002"},
   115  								},
   116  							},
   117  						},
   118  					},
   119  				},
   120  			},
   121  			want: &report.SummaryReport{
   122  				SchemaVersion: 0,
   123  				ID:            "1234",
   124  				Title:         "NSA",
   125  				SummaryControls: []report.ControlCheckSummary{
   126  					{
   127  						ID:        "1.0",
   128  						Name:      "Non-root containers",
   129  						Severity:  "MEDIUM",
   130  						TotalFail: lo.ToPtr(1),
   131  					},
   132  					{
   133  						ID:        "1.1",
   134  						Name:      "Immutable container file systems",
   135  						Severity:  "LOW",
   136  						TotalFail: lo.ToPtr(1),
   137  					},
   138  					{
   139  						ID:        "1.2",
   140  						Name:      "tzdata - new upstream version",
   141  						Severity:  "LOW",
   142  						TotalFail: lo.ToPtr(1),
   143  					},
   144  				},
   145  			},
   146  		},
   147  	}
   148  
   149  	for _, tt := range tests {
   150  		t.Run(tt.name, func(t *testing.T) {
   151  			got := report.BuildSummary(tt.input)
   152  			assert.Equal(t, tt.want, got)
   153  		})
   154  	}
   155  }