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

     1  package linux
     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  	ctx := zlog.Test(context.Background(), t)
    17  	coalescer := &Coalescer{
    18  		ir: &claircore.IndexReport{
    19  			Environments:  map[string][]*claircore.Environment{},
    20  			Packages:      map[string]*claircore.Package{},
    21  			Distributions: map[string]*claircore.Distribution{},
    22  			Repositories:  map[string]*claircore.Repository{},
    23  		},
    24  	}
    25  	// we will test
    26  	// 1) packages before a distribution was discovered are tagged with
    27  	//    the first distribution found
    28  	// 2) all packages found after a subsequent distribution is located
    29  	//    are tagged wih this distribution
    30  	pkgs := test.GenUniquePackages(6)
    31  	dists := test.GenUniqueDistributions(3) // we will discard dist 0 due to zero value ambiguity
    32  	layerArtifacts := []*indexer.LayerArtifacts{
    33  		{
    34  			Hash:  test.RandomSHA256Digest(t),
    35  			Pkgs:  pkgs[0:1],
    36  			Dist:  nil,
    37  			Repos: nil,
    38  		},
    39  		{
    40  			Hash:  test.RandomSHA256Digest(t),
    41  			Pkgs:  pkgs[1:2],
    42  			Dist:  nil,
    43  			Repos: nil,
    44  		},
    45  		{
    46  			Hash:  test.RandomSHA256Digest(t),
    47  			Pkgs:  pkgs[2:3],
    48  			Dist:  dists[1:2],
    49  			Repos: nil,
    50  		},
    51  		{
    52  			Hash:  test.RandomSHA256Digest(t),
    53  			Pkgs:  pkgs[3:4],
    54  			Dist:  nil,
    55  			Repos: nil,
    56  		},
    57  		{
    58  			Hash:  test.RandomSHA256Digest(t),
    59  			Pkgs:  pkgs[4:5],
    60  			Dist:  dists[2:],
    61  			Repos: nil,
    62  		},
    63  		{
    64  			Hash:  test.RandomSHA256Digest(t),
    65  			Pkgs:  pkgs[5:],
    66  			Dist:  nil,
    67  			Repos: nil,
    68  		},
    69  	}
    70  	ir, err := coalescer.Coalesce(ctx, layerArtifacts)
    71  	if err != nil {
    72  		t.Fatalf("received error from coalesce method: %v", err)
    73  	}
    74  	// we expect packages 1-4 to be tagged with dist id 1
    75  	// and packages 5-6 to be tagged with dist id 2
    76  	for i := 0; i < 4; i++ {
    77  		environment := ir.Environments[strconv.Itoa(i)][0]
    78  		if environment.DistributionID != "1" {
    79  			t.Fatalf("expected distribution id %d but got %s", 1, environment.DistributionID)
    80  		}
    81  	}
    82  	for i := 4; i < 6; i++ {
    83  		environment := ir.Environments[strconv.Itoa(i)][0]
    84  		if environment.DistributionID != "2" {
    85  			t.Fatalf("expected distribution id %d but got %s", 2, environment.DistributionID)
    86  		}
    87  	}
    88  }