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

     1  package sqlite
     2  
     3  import (
     4  	"context"
     5  	"encoding/json"
     6  	"hash/crc64"
     7  	"io"
     8  	"os"
     9  	"path/filepath"
    10  	"testing"
    11  
    12  	"github.com/google/go-cmp/cmp"
    13  	"github.com/quay/zlog"
    14  )
    15  
    16  func TestPackages(t *testing.T) {
    17  	ctx := zlog.Test(context.Background(), t)
    18  	h := crc64.New(crc64.MakeTable(crc64.ISO))
    19  
    20  	// Copying to a tempdir is needed if the tests are being run from a prepared
    21  	// module. In that case, the module's layout is read-only and attempting any
    22  	// queries tries to create wal files alongside the database file.
    23  	dbfile := filepath.Join(t.TempDir(), `rpmdb.sqlite`)
    24  	dst, err := os.Create(dbfile)
    25  	if err != nil {
    26  		t.Fatal(err)
    27  	}
    28  	defer dst.Close()
    29  	src, err := os.Open(`testdata/rpmdb.sqlite`)
    30  	if err != nil {
    31  		t.Fatal(err)
    32  	}
    33  	defer src.Close()
    34  	if _, err := io.Copy(dst, src); err != nil {
    35  		t.Fatal(err)
    36  	}
    37  	if err := dst.Sync(); err != nil {
    38  		t.Fatal(err)
    39  	}
    40  
    41  	db, err := Open(dbfile)
    42  	if err != nil {
    43  		t.Fatal(err)
    44  	}
    45  	defer func() {
    46  		if err := db.Close(); err != nil {
    47  			t.Error(err)
    48  		}
    49  	}()
    50  	var want []uint64
    51  	f, err := os.Open(`testdata/rpmdb.sqlite.checksums`)
    52  	if err != nil {
    53  		t.Fatal(err)
    54  	}
    55  	defer func() {
    56  		if err := f.Close(); err != nil {
    57  			t.Error(err)
    58  		}
    59  	}()
    60  	err = db.Validate(ctx)
    61  	if err != nil {
    62  		t.Error("error validating sqlite DB", err)
    63  	}
    64  
    65  	if err := json.NewDecoder(f).Decode(&want); err != nil {
    66  		t.Error(err)
    67  	}
    68  
    69  	hdrs, err := db.AllHeaders(ctx)
    70  	if err != nil {
    71  		t.Error(err)
    72  	}
    73  	var got []uint64
    74  	for _, rd := range hdrs {
    75  		h.Reset()
    76  		if _, err := io.Copy(h, rd.(io.Reader)); err != nil {
    77  			t.Error(err)
    78  			continue
    79  		}
    80  		got = append(got, h.Sum64())
    81  	}
    82  
    83  	if !cmp.Equal(got, want) {
    84  		t.Error(cmp.Diff(got, want))
    85  	}
    86  }
    87  
    88  func TestValidate(t *testing.T) {
    89  	ctx := zlog.Test(context.Background(), t)
    90  
    91  	dbfile := filepath.Join(t.TempDir(), `no_packages.sqlite`)
    92  	dst, err := os.Create(dbfile)
    93  	if err != nil {
    94  		t.Fatal(err)
    95  	}
    96  	defer dst.Close()
    97  	src, err := os.Open(`testdata/no_packages.sqlite`)
    98  	if err != nil {
    99  		t.Fatal(err)
   100  	}
   101  	defer src.Close()
   102  	if _, err := io.Copy(dst, src); err != nil {
   103  		t.Fatal(err)
   104  	}
   105  	if err := dst.Sync(); err != nil {
   106  		t.Fatal(err)
   107  	}
   108  
   109  	db, err := Open(dbfile)
   110  	if err != nil {
   111  		t.Fatal(err)
   112  	}
   113  	defer func() {
   114  		if err := db.Close(); err != nil {
   115  			t.Error(err)
   116  		}
   117  	}()
   118  	err = db.Validate(ctx)
   119  	if err == nil {
   120  		t.Error("expecting error from Validate() for empty DB")
   121  	}
   122  }