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

     1  package report_test
     2  
     3  import (
     4  	"testing"
     5  
     6  	"github.com/stretchr/testify/assert"
     7  
     8  	"github.com/devseccon/trivy/pkg/types"
     9  )
    10  
    11  func TestResults_Failed(t *testing.T) {
    12  	tests := []struct {
    13  		name    string
    14  		results types.Results
    15  		want    bool
    16  	}{
    17  		{
    18  			name: "no vulnerabilities and misconfigurations",
    19  			results: types.Results{
    20  				{
    21  					Target: "test",
    22  					Type:   "test",
    23  				},
    24  			},
    25  			want: false,
    26  		},
    27  		{
    28  			name: "vulnerabilities found",
    29  			results: types.Results{
    30  				{
    31  					Target: "test",
    32  					Type:   "test",
    33  					Vulnerabilities: []types.DetectedVulnerability{
    34  						{
    35  							VulnerabilityID: "CVE-2021-0001",
    36  							PkgName:         "test",
    37  						},
    38  					},
    39  				},
    40  			},
    41  			want: true,
    42  		},
    43  		{
    44  			name: "failed misconfigurations",
    45  			results: types.Results{
    46  				{
    47  					Target: "test",
    48  					Type:   "test",
    49  					Misconfigurations: []types.DetectedMisconfiguration{
    50  						{
    51  							Type:   "Docker Security Check",
    52  							ID:     "ID-001",
    53  							Status: types.StatusFailure,
    54  						},
    55  					},
    56  				},
    57  			},
    58  			want: true,
    59  		},
    60  		{
    61  			name: "passed misconfigurations",
    62  			results: types.Results{
    63  				{
    64  					Target: "test",
    65  					Type:   "test",
    66  					Misconfigurations: []types.DetectedMisconfiguration{
    67  						{
    68  							Type:   "Docker Security Check",
    69  							ID:     "ID-001",
    70  							Status: types.StatusPassed,
    71  						},
    72  					},
    73  				},
    74  			},
    75  			want: false,
    76  		},
    77  	}
    78  	for _, tt := range tests {
    79  		t.Run(tt.name, func(t *testing.T) {
    80  			got := tt.results.Failed()
    81  			assert.Equal(t, tt.want, got)
    82  		})
    83  	}
    84  }