github.com/quay/claircore@v1.5.28/affectedmanifests_test.go (about)

     1  package claircore_test
     2  
     3  import (
     4  	"testing"
     5  
     6  	"github.com/quay/claircore"
     7  	"github.com/quay/claircore/test"
     8  )
     9  
    10  // TestAffectedManifestsAddAndSort confirms adding to and sorting
    11  // the AffectedManifests struct works correctly.
    12  func TestAffectedManifestsAddAndSort(t *testing.T) {
    13  	vulns := test.GenUniqueVulnerabilities(2, "test-updater")
    14  	manifest := claircore.MustParseDigest(`sha256:5f70bf18a086007016e948b04aed3b82103a36bea41755b6cddfaf10ace3c6ef`)
    15  	affected := claircore.NewAffectedManifests()
    16  
    17  	// make vuln 1 higher severity, to test sorting
    18  	vulns[1].NormalizedSeverity = claircore.High
    19  
    20  	affected.Add(vulns[0], manifest)
    21  	affected.Add(vulns[1], manifest)
    22  
    23  	if len(affected.Vulnerabilities) != 2 {
    24  		t.Fatalf("got: %d, want: %d", len(affected.Vulnerabilities), 2)
    25  	}
    26  
    27  	if _, ok := affected.VulnerableManifests[manifest.String()]; !ok {
    28  		t.Fatalf("got: %v, want: %v", ok, true)
    29  	}
    30  
    31  	affected.Sort()
    32  
    33  	ids := affected.VulnerableManifests[manifest.String()]
    34  	if len(ids) != 2 {
    35  		t.Fatalf("got: %v, want: %v", len(ids), 2)
    36  	}
    37  
    38  	v1 := affected.Vulnerabilities[ids[0]]
    39  	v2 := affected.Vulnerabilities[ids[1]]
    40  
    41  	if v1.NormalizedSeverity != claircore.High {
    42  		t.Fatalf("got: %v, want: %v", v1.NormalizedSeverity, claircore.High)
    43  	}
    44  
    45  	if v2.NormalizedSeverity != claircore.Unknown {
    46  		t.Fatalf("got: %v, want: %v", v1.NormalizedSeverity, claircore.Unknown)
    47  	}
    48  }