github.com/quay/claircore@v1.5.28/debian/matcher_integration_test.go (about)

     1  package debian
     2  
     3  import (
     4  	"context"
     5  	"encoding/json"
     6  	"fmt"
     7  	"net/http"
     8  	"net/http/httptest"
     9  	"net/url"
    10  	"os"
    11  	"path"
    12  	"path/filepath"
    13  	"testing"
    14  
    15  	"github.com/quay/zlog"
    16  
    17  	"github.com/quay/claircore"
    18  	"github.com/quay/claircore/datastore/postgres"
    19  	"github.com/quay/claircore/internal/matcher"
    20  	"github.com/quay/claircore/libvuln/driver"
    21  	"github.com/quay/claircore/libvuln/updates"
    22  	"github.com/quay/claircore/pkg/ctxlock"
    23  	"github.com/quay/claircore/test/integration"
    24  	pgtest "github.com/quay/claircore/test/postgres"
    25  )
    26  
    27  func TestMain(m *testing.M) {
    28  	var c int
    29  	defer func() { os.Exit(c) }()
    30  	defer integration.DBSetup()()
    31  	c = m.Run()
    32  }
    33  
    34  // TestMatcherIntegration confirms packages are matched
    35  // with vulnerabilities correctly. the returned
    36  // store from postgres.NewTestStore must have Debian
    37  // CVE data
    38  func TestMatcherIntegration(t *testing.T) {
    39  	integration.NeedDB(t)
    40  	ctx := zlog.Test(context.Background(), t)
    41  	pool := pgtest.TestMatcherDB(ctx, t)
    42  	store := postgres.NewMatcherStore(pool)
    43  	srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
    44  		switch r.URL.Path {
    45  		case `/debian/dists/`:
    46  			fmt.Fprintln(w, `href="buster/"`)
    47  		case `/debian/dists/buster/Release`:
    48  			fmt.Fprintln(w, `Origin: Debian`)
    49  			fmt.Fprintln(w, `Label: Debian`)
    50  			fmt.Fprintln(w, `Suite: oldstable`)
    51  			fmt.Fprintln(w, `Version: 10.12`)
    52  			fmt.Fprintln(w, `Codename: buster`)
    53  		case `/`:
    54  			tgt, err := url.Parse(defaultJSON)
    55  			if err != nil {
    56  				t.Error(err)
    57  				w.WriteHeader(http.StatusInternalServerError)
    58  				return
    59  			}
    60  			w.Header().Set("Location", tgt.String())
    61  			w.WriteHeader(http.StatusMovedPermanently)
    62  		case `/debian/dists/buster/main/source/Sources.gz`,
    63  			`/debian/dists/buster/contrib/source/Sources.gz`,
    64  			`/debian/dists/buster/non-free/source/Sources.gz`:
    65  			tgt, err := url.Parse(defaultMirror)
    66  			if err != nil {
    67  				t.Error(err)
    68  				w.WriteHeader(http.StatusInternalServerError)
    69  				return
    70  			}
    71  			loc, err := tgt.Parse(path.Join(tgt.Path, r.URL.Path))
    72  			if err != nil {
    73  				t.Error(err)
    74  				w.WriteHeader(http.StatusInternalServerError)
    75  				return
    76  			}
    77  			w.Header().Set("Location", loc.String())
    78  			w.WriteHeader(http.StatusMovedPermanently)
    79  		default:
    80  			t.Logf("requested: %q", r.URL.Path)
    81  			w.WriteHeader(http.StatusTeapot)
    82  		}
    83  	}))
    84  	defer srv.Close()
    85  
    86  	m := &Matcher{}
    87  
    88  	locks, err := ctxlock.New(ctx, pool)
    89  	if err != nil {
    90  		t.Error(err)
    91  	}
    92  	defer locks.Close(ctx)
    93  	fac, err := NewFactory(ctx)
    94  	if err != nil {
    95  		t.Fatal(err)
    96  	}
    97  	facs := map[string]driver.UpdaterSetFactory{
    98  		"debian": fac,
    99  	}
   100  	cfg := map[string]driver.ConfigUnmarshaler{
   101  		"debian": func(v interface{}) error {
   102  			cfg := v.(*FactoryConfig)
   103  			cfg.MirrorURL = srv.URL
   104  			cfg.JSONURL = srv.URL
   105  			return nil
   106  		},
   107  	}
   108  	mgr, err := updates.NewManager(ctx, store, locks, srv.Client(),
   109  		updates.WithFactories(facs), updates.WithConfigs(cfg))
   110  	if err != nil {
   111  		t.Error(err)
   112  	}
   113  	// force update
   114  	if err := mgr.Run(ctx); err != nil {
   115  		t.Error(err)
   116  	}
   117  
   118  	path := filepath.Join("testdata", "indexreport-buster-jackson-databind.json")
   119  	f, err := os.Open(path)
   120  	if err != nil {
   121  		t.Fatalf("%v", err)
   122  	}
   123  
   124  	var ir claircore.IndexReport
   125  	err = json.NewDecoder(f).Decode(&ir)
   126  	if err != nil {
   127  		t.Fatalf("failed to decode IndexReport: %v", err)
   128  	}
   129  	vr, err := matcher.Match(ctx, &ir, []driver.Matcher{m}, store)
   130  	if err != nil {
   131  		t.Fatalf("expected nil error but got %v", err)
   132  	}
   133  	_, err = json.Marshal(&vr)
   134  	if err != nil {
   135  		t.Fatalf("failed to marshal VR: %v", err)
   136  	}
   137  }