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

     1  package nodejs
     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  	repo := []*claircore.Repository{&Repository}
    21  	layerArtifacts := []*indexer.LayerArtifacts{
    22  		{
    23  			Hash: test.RandomSHA256Digest(t),
    24  			Pkgs: pkgs[:1],
    25  		},
    26  		{
    27  			Hash: test.RandomSHA256Digest(t),
    28  			Pkgs: pkgs[:2],
    29  		},
    30  		{
    31  			Hash:  test.RandomSHA256Digest(t),
    32  			Pkgs:  pkgs[:3],
    33  			Repos: repo,
    34  		},
    35  		{
    36  			Hash: test.RandomSHA256Digest(t),
    37  			Pkgs: pkgs[:4],
    38  		},
    39  		{
    40  			Hash:  test.RandomSHA256Digest(t),
    41  			Pkgs:  pkgs[:5],
    42  			Repos: repo,
    43  		},
    44  		{
    45  			Hash: test.RandomSHA256Digest(t),
    46  			Pkgs: pkgs,
    47  		},
    48  	}
    49  	ir, err := coalescer.Coalesce(ctx, layerArtifacts)
    50  	if err != nil {
    51  		t.Fatalf("received error from coalesce method: %v", err)
    52  	}
    53  	// Expect 0-5 to have gotten associated with the repository.
    54  	for i := range pkgs {
    55  		es, ok := ir.Environments[strconv.Itoa(i)]
    56  		if !ok && i == 5 {
    57  			// Left out the last package.
    58  			continue
    59  		}
    60  		e := es[0]
    61  		if len(e.RepositoryIDs) == 0 {
    62  			t.Error("expected some repositories")
    63  		}
    64  		for _, id := range e.RepositoryIDs {
    65  			r := ir.Repositories[id]
    66  			if got, want := r.Name, Repository.Name; got != want {
    67  				t.Errorf("got: %q, want: %q", got, want)
    68  			}
    69  		}
    70  	}
    71  }