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

     1  package python
     2  
     3  import (
     4  	"context"
     5  	"encoding/json"
     6  	"net/http"
     7  	"net/http/httptest"
     8  	"os"
     9  	"path/filepath"
    10  	"testing"
    11  
    12  	"github.com/quay/zlog"
    13  
    14  	"github.com/quay/claircore"
    15  	"github.com/quay/claircore/datastore/postgres"
    16  	internalMatcher "github.com/quay/claircore/internal/matcher"
    17  	"github.com/quay/claircore/libvuln/driver"
    18  	"github.com/quay/claircore/libvuln/updates"
    19  	"github.com/quay/claircore/pkg/ctxlock"
    20  	"github.com/quay/claircore/test/integration"
    21  	pgtest "github.com/quay/claircore/test/postgres"
    22  	"github.com/quay/claircore/updater/osv"
    23  )
    24  
    25  func TestMain(m *testing.M) {
    26  	var c int
    27  	defer func() { os.Exit(c) }()
    28  	defer integration.DBSetup()()
    29  	c = m.Run()
    30  }
    31  
    32  func TestMatcherIntegration(t *testing.T) {
    33  	integration.NeedDB(t)
    34  	ctx := zlog.Test(context.Background(), t)
    35  	pool := pgtest.TestMatcherDB(ctx, t)
    36  	store := postgres.NewMatcherStore(pool)
    37  
    38  	srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
    39  		w.WriteHeader(http.StatusTeapot)
    40  	}))
    41  	defer srv.Close()
    42  
    43  	m := &Matcher{}
    44  	locks, err := ctxlock.New(ctx, pool)
    45  	if err != nil {
    46  		t.Fatalf("%v", err)
    47  	}
    48  	defer locks.Close(ctx)
    49  
    50  	cfg := map[string]driver.ConfigUnmarshaler{
    51  		"osv": func(v interface{}) error {
    52  			cfg := v.(*osv.FactoryConfig)
    53  			cfg.URL = osv.DefaultURL
    54  			return nil
    55  		},
    56  	}
    57  
    58  	facs := map[string]driver.UpdaterSetFactory{
    59  		"osv": new(osv.Factory),
    60  	}
    61  	mgr, err := updates.NewManager(ctx, store, locks, srv.Client(),
    62  		updates.WithFactories(facs), updates.WithConfigs(cfg))
    63  	if err != nil {
    64  		t.Fatalf("%v", err)
    65  	}
    66  
    67  	// force update
    68  	if err := mgr.Run(ctx); err != nil {
    69  		t.Fatalf("%v", err)
    70  	}
    71  
    72  	path := filepath.Join("testdata", "indexreport-rhel8-data.json")
    73  	f, err := os.Open(path)
    74  	if err != nil {
    75  		t.Fatalf("%v", err)
    76  	}
    77  	defer f.Close()
    78  	var ir claircore.IndexReport
    79  	err = json.NewDecoder(f).Decode(&ir)
    80  	if err != nil {
    81  		t.Fatalf("failed to decode IndexReport: %v", err)
    82  	}
    83  	vr, err := internalMatcher.Match(ctx, &ir, []driver.Matcher{m}, store)
    84  	if err != nil {
    85  		t.Fatalf("expected error to be nil but got %v", err)
    86  	}
    87  
    88  	vulns := vr.Vulnerabilities
    89  	t.Logf("Number of Vulnerabilities found: %d", len(vulns))
    90  
    91  	if len(vulns) < 1 {
    92  		t.Fatalf("failed to match vulns: %v", err)
    93  	}
    94  }