github.com/quay/claircore@v1.5.28/rpm/ndb/package_test.go (about)

     1  package ndb
     2  
     3  import (
     4  	"bytes"
     5  	"context"
     6  	"io/fs"
     7  	"os"
     8  	"testing"
     9  
    10  	"github.com/quay/zlog"
    11  
    12  	"github.com/quay/claircore/rpm/internal/rpm"
    13  )
    14  
    15  func TestLoadPackage(t *testing.T) {
    16  	ctx := context.Background()
    17  
    18  	dir := os.DirFS("testdata")
    19  	ms, err := fs.Glob(dir, "Packages*.db")
    20  	if err != nil || len(ms) == 0 {
    21  		t.Fatalf("error or not enough matches: %v/%d", err, len(ms))
    22  	}
    23  	for _, n := range ms {
    24  		t.Run(n, func(t *testing.T) {
    25  			ctx := zlog.Test(ctx, t)
    26  			b, err := fs.ReadFile(dir, n)
    27  			if err != nil {
    28  				t.Fatal(err)
    29  			}
    30  			pkgf := bytes.NewReader(b)
    31  
    32  			var pkg PackageDB
    33  			if err := pkg.Parse(pkgf); err != nil {
    34  				t.Fatal("error parsing Packages file", err)
    35  			}
    36  			rds, err := pkg.AllHeaders(ctx)
    37  			if err != nil {
    38  				t.Fatal("error getting AllHeaders", err)
    39  			}
    40  			for _, rd := range rds {
    41  				var h rpm.Header
    42  				if err := h.Parse(ctx, rd); err != nil {
    43  					t.Fatal(err)
    44  				}
    45  				var found bool
    46  				for i := range h.Infos {
    47  					if h.Infos[i].Tag == rpm.TagName {
    48  						v, err := h.ReadData(ctx, &h.Infos[i])
    49  						if err != nil {
    50  							t.Error(err)
    51  							continue
    52  						}
    53  						t.Logf("package: %q", v)
    54  						found = true
    55  						break
    56  					}
    57  				}
    58  				if !found {
    59  					t.Error(`missing "name" tag`)
    60  				}
    61  			}
    62  		})
    63  	}
    64  }