github.com/quay/claircore@v1.5.28/rhel/rhcc/coalescer_test.go (about)

     1  package rhcc
     2  
     3  import (
     4  	"context"
     5  	"strconv"
     6  	"testing"
     7  
     8  	"github.com/quay/zlog"
     9  
    10  	"github.com/quay/claircore"
    11  	"github.com/quay/claircore/indexer"
    12  	"github.com/quay/claircore/test"
    13  )
    14  
    15  func TestCoalescer(t *testing.T) {
    16  	t.Parallel()
    17  	ctx := zlog.Test(context.Background(), t)
    18  	coalescer := &coalescer{}
    19  	pkgs := test.GenUniquePackages(6)
    20  	for _, p := range pkgs {
    21  		// Mark them as if they came from this package's package scanner
    22  		p.RepositoryHint = `rhcc`
    23  	}
    24  	repo := []*claircore.Repository{&goldRepo}
    25  	layerArtifacts := []*indexer.LayerArtifacts{
    26  		{
    27  			Hash: test.RandomSHA256Digest(t),
    28  			Pkgs: pkgs[:1],
    29  		},
    30  		{
    31  			Hash: test.RandomSHA256Digest(t),
    32  			Pkgs: pkgs[:2],
    33  		},
    34  		{
    35  			Hash:  test.RandomSHA256Digest(t),
    36  			Pkgs:  pkgs[:3],
    37  			Repos: repo,
    38  		},
    39  		{
    40  			Hash: test.RandomSHA256Digest(t),
    41  			Pkgs: pkgs[:4],
    42  		},
    43  		{
    44  			Hash:  test.RandomSHA256Digest(t),
    45  			Pkgs:  pkgs[:5],
    46  			Repos: repo,
    47  		},
    48  		{
    49  			Hash: test.RandomSHA256Digest(t),
    50  			Pkgs: pkgs,
    51  		},
    52  	}
    53  	ir, err := coalescer.Coalesce(ctx, layerArtifacts)
    54  	if err != nil {
    55  		t.Fatalf("received error from coalesce method: %v", err)
    56  	}
    57  	// Expect 0-5 to have gotten associated with the repository.
    58  	for i := range pkgs {
    59  		es, ok := ir.Environments[strconv.Itoa(i)]
    60  		if !ok && i == 5 {
    61  			// Left out the last package.
    62  			continue
    63  		}
    64  		e := es[0]
    65  		if len(e.RepositoryIDs) == 0 {
    66  			t.Error("expected some repositories")
    67  		}
    68  		for _, id := range e.RepositoryIDs {
    69  			r := ir.Repositories[id]
    70  			if got, want := r.Name, goldRepo.Name; got != want {
    71  				t.Errorf("got: %q, want: %q", got, want)
    72  			}
    73  		}
    74  	}
    75  }