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

     1  package rpm
     2  
     3  import (
     4  	"context"
     5  	"encoding/json"
     6  	"errors"
     7  	"io"
     8  	"os"
     9  	"path/filepath"
    10  	"strings"
    11  	"testing"
    12  
    13  	"github.com/google/go-cmp/cmp"
    14  	"github.com/quay/zlog"
    15  	"golang.org/x/tools/txtar"
    16  
    17  	"github.com/quay/claircore/rpm/bdb"
    18  	"github.com/quay/claircore/rpm/internal/rpm"
    19  	"github.com/quay/claircore/rpm/ndb"
    20  	"github.com/quay/claircore/rpm/sqlite"
    21  )
    22  
    23  func TestInfo(t *testing.T) {
    24  	t.Run("Files", func(t *testing.T) {
    25  		ms, err := filepath.Glob("testdata/Info.Files.*.txtar")
    26  		if err != nil {
    27  			t.Fatal(err)
    28  		}
    29  		for _, m := range ms {
    30  			ar, err := txtar.ParseFile(m)
    31  			if err != nil {
    32  				t.Fatal(err)
    33  			}
    34  			name := strings.TrimPrefix(strings.TrimSuffix(filepath.Base(m), ".txtar"), "Info.Files.")
    35  			t.Run(name, func(t *testing.T) {
    36  				t.Parallel()
    37  				ctx := zlog.Test(context.Background(), t)
    38  				filename := strings.TrimSpace(string(ar.Comment))
    39  				t.Logf("opening %q", filename)
    40  
    41  				var want map[string][]string
    42  				for _, f := range ar.Files {
    43  					if f.Name == "want.json" {
    44  						want = make(map[string][]string)
    45  						if err := json.Unmarshal(f.Data, &want); err != nil {
    46  							t.Fatal(err)
    47  						}
    48  						break
    49  					}
    50  				}
    51  				if want == nil {
    52  					t.Fatal(`"want.json" not found`)
    53  				}
    54  
    55  				pre, _, ok := strings.Cut(filename, `/testdata/`)
    56  				if !ok {
    57  					t.Fatal("input file not in a testdata directory")
    58  				}
    59  
    60  				var nat nativeDB
    61  				switch pre {
    62  				case `bdb`:
    63  					f, err := os.Open(filename)
    64  					if err != nil {
    65  						t.Fatal(err)
    66  					} else {
    67  						t.Cleanup(func() { f.Close() })
    68  					}
    69  					var db bdb.PackageDB
    70  					if err := db.Parse(f); err != nil {
    71  						t.Fatal(err)
    72  					}
    73  					nat = &db
    74  				case `ndb`:
    75  					f, err := os.Open(filename)
    76  					if err != nil {
    77  						t.Fatal(err)
    78  					} else {
    79  						t.Cleanup(func() { f.Close() })
    80  					}
    81  					var db ndb.PackageDB
    82  					if err := db.Parse(f); err != nil {
    83  						t.Fatal(err)
    84  					}
    85  					nat = &db
    86  				case `sqlite`:
    87  					f, err := os.Create(filepath.Join(t.TempDir(), "db"))
    88  					if err != nil {
    89  						t.Fatal(err)
    90  					}
    91  					src, err := os.Open(filename)
    92  					if err != nil {
    93  						t.Fatal(err)
    94  					}
    95  					if _, err := io.Copy(f, src); err != nil {
    96  						t.Fatal(err)
    97  					}
    98  					if err := errors.Join(src.Close(), f.Close()); err != nil {
    99  						t.Fatal(err)
   100  					}
   101  					filename = f.Name()
   102  					t.Logf("copied sqlite database to: %s", filename)
   103  
   104  					db, err := sqlite.Open(filename)
   105  					if err != nil {
   106  						t.Fatal(err)
   107  					} else {
   108  						t.Cleanup(func() { db.Close() })
   109  					}
   110  					nat = db
   111  				}
   112  
   113  				rds, err := nat.AllHeaders(ctx)
   114  				if err != nil {
   115  					t.Fatal(err)
   116  				}
   117  
   118  				got := make(map[string][]string, len(want))
   119  				for _, rd := range rds {
   120  					var h rpm.Header
   121  					if err := h.Parse(ctx, rd); err != nil {
   122  						t.Error(err)
   123  						continue
   124  					}
   125  					var info Info
   126  					if err := info.Load(ctx, &h); err != nil {
   127  						t.Error(err)
   128  						continue
   129  					}
   130  					if info.Name == "gpg-pubkey" {
   131  						// This is *not* an rpm package. It is just a public key stored in the rpm database.
   132  						// Ignore this "package".
   133  						continue
   134  					}
   135  					got[info.Name] = info.Filenames
   136  				}
   137  
   138  				if !cmp.Equal(got, want) {
   139  					t.Error(cmp.Diff(got, want))
   140  				}
   141  			})
   142  		}
   143  	})
   144  }