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

     1  package nodejs
     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  			cfg.Allowlist = []string{"npm"}
    55  			return nil
    56  		},
    57  	}
    58  
    59  	facs := map[string]driver.UpdaterSetFactory{
    60  		"osv": new(osv.Factory),
    61  	}
    62  	mgr, err := updates.NewManager(ctx, store, locks, srv.Client(),
    63  		updates.WithFactories(facs), updates.WithConfigs(cfg))
    64  	if err != nil {
    65  		t.Fatalf("%v", err)
    66  	}
    67  
    68  	// force update
    69  	if err := mgr.Run(ctx); err != nil {
    70  		t.Fatalf("%v", err)
    71  	}
    72  
    73  	path := filepath.Join("testdata", "indexreport-splunk-8.2.6.json")
    74  	f, err := os.Open(path)
    75  	if err != nil {
    76  		t.Fatalf("%v", err)
    77  	}
    78  	defer f.Close()
    79  	var ir claircore.IndexReport
    80  	err = json.NewDecoder(f).Decode(&ir)
    81  	if err != nil {
    82  		t.Fatalf("failed to decode IndexReport: %v", err)
    83  	}
    84  	vr, err := internalMatcher.Match(ctx, &ir, []driver.Matcher{m}, store)
    85  	if err != nil {
    86  		t.Fatalf("expected error to be nil but got %v", err)
    87  	}
    88  
    89  	vulns := vr.Vulnerabilities
    90  	t.Logf("Number of Vulnerabilities found: %d", len(vulns))
    91  
    92  	if len(vulns) < 2 {
    93  		t.Fatal("failed to match vulns")
    94  	}
    95  
    96  	var hasFixed bool
    97  	for _, v := range vulns {
    98  		if v.FixedInVersion != "" {
    99  			hasFixed = true
   100  		}
   101  	}
   102  	if !hasFixed {
   103  		t.Fatalf("failed to find a fixed vuln")
   104  	}
   105  }