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

     1  package test
     2  
     3  import (
     4  	"fmt"
     5  	"strconv"
     6  
     7  	"github.com/quay/claircore"
     8  	"github.com/quay/claircore/toolkit/types/cpe"
     9  )
    10  
    11  // WFN returns a [cpe.WFN] with "i" as the CPE version.
    12  func WFN(i int) cpe.WFN {
    13  	r := cpe.WFN{}
    14  	for i := 0; i < cpe.NumAttr; i++ {
    15  		r.Attr[i].Kind = cpe.ValueAny
    16  	}
    17  	var err error
    18  	if r.Attr[cpe.Part], err = cpe.NewValue("o"); err != nil {
    19  		panic(err)
    20  	}
    21  	if r.Attr[cpe.Vendor], err = cpe.NewValue("projectquay"); err != nil {
    22  		panic(err)
    23  	}
    24  	if r.Attr[cpe.Product], err = cpe.NewValue(`clair\.test`); err != nil {
    25  		panic(err)
    26  	}
    27  	if r.Attr[cpe.Version], err = cpe.NewValue(strconv.Itoa(i)); err != nil {
    28  		panic(err)
    29  	}
    30  	if err := r.Valid(); err != nil {
    31  		panic(err)
    32  	}
    33  	return r
    34  }
    35  
    36  // GenUniqueDistributions creates an array of unique distributions.
    37  //
    38  // The array is guaranteed not to have any duplicated fields.
    39  func GenUniqueDistributions(n int) []*claircore.Distribution {
    40  	dists := []*claircore.Distribution{}
    41  	for i := 0; i < n; i++ {
    42  		dists = append(dists, &claircore.Distribution{
    43  			ID:              strconv.Itoa(i),
    44  			Name:            fmt.Sprintf("distribution-%d", i),
    45  			Version:         fmt.Sprintf("version-%d", i),
    46  			VersionCodeName: fmt.Sprintf("version-code-name-%d", i),
    47  			DID:             fmt.Sprintf("did-%d", i),
    48  			VersionID:       fmt.Sprintf("version-id-%d", i),
    49  			Arch:            fmt.Sprintf("arch-%d", i),
    50  			CPE:             WFN(i),
    51  			PrettyName:      fmt.Sprintf("pretty-name-%d", i),
    52  		})
    53  	}
    54  	return dists
    55  }