github.com/quay/claircore@v1.5.28/libvuln/jsonblob/jsonblob_test.go (about)

     1  package jsonblob
     2  
     3  import (
     4  	"bytes"
     5  	"context"
     6  	"io"
     7  	"testing"
     8  
     9  	"github.com/google/go-cmp/cmp"
    10  	"golang.org/x/sync/errgroup"
    11  
    12  	"github.com/quay/claircore"
    13  	"github.com/quay/claircore/libvuln/driver"
    14  	"github.com/quay/claircore/test"
    15  )
    16  
    17  func TestStore(t *testing.T) {
    18  	s, err := New()
    19  	if err != nil {
    20  		t.Fatal(err)
    21  	}
    22  	ctx := context.Background()
    23  
    24  	vs := test.GenUniqueVulnerabilities(10, "test")
    25  	ref, err := s.UpdateVulnerabilities(ctx, "test", "", vs)
    26  	if err != nil {
    27  		t.Error(err)
    28  	}
    29  	t.Logf("ref: %v", ref)
    30  
    31  	var buf bytes.Buffer
    32  	if err := s.Store(&buf); err != nil {
    33  		t.Error(err)
    34  	}
    35  	t.Logf("wrote:\n%s", buf.String())
    36  }
    37  
    38  func TestRoundtrip(t *testing.T) {
    39  	ctx := context.Background()
    40  	a, err := New()
    41  	if err != nil {
    42  		t.Fatal(err)
    43  	}
    44  
    45  	var want, got struct {
    46  		V []*claircore.Vulnerability
    47  		E []driver.EnrichmentRecord
    48  	}
    49  
    50  	want.V = test.GenUniqueVulnerabilities(10, "test")
    51  	ref, err := a.UpdateVulnerabilities(ctx, "test", "", want.V)
    52  	if err != nil {
    53  		t.Error(err)
    54  	}
    55  	t.Logf("ref: %v", ref)
    56  
    57  	want.E = test.GenEnrichments(15)
    58  	ref, err = a.UpdateEnrichments(ctx, "test", "", want.E)
    59  	if err != nil {
    60  		t.Error(err)
    61  	}
    62  	t.Logf("ref: %v", ref)
    63  
    64  	var buf bytes.Buffer
    65  	defer func() {
    66  		t.Logf("wrote:\n%s", buf.String())
    67  	}()
    68  	r, w := io.Pipe()
    69  	eg, ctx := errgroup.WithContext(ctx)
    70  	eg.Go(func() error { defer w.Close(); return a.Store(w) })
    71  	eg.Go(func() error {
    72  		l, err := Load(ctx, io.TeeReader(r, &buf))
    73  		if err != nil {
    74  			return err
    75  		}
    76  		for l.Next() {
    77  			e := l.Entry()
    78  			if e.Vuln != nil && e.Enrichment != nil {
    79  				t.Error("expecting entry to have either vulnerability or enrichment, got both")
    80  			}
    81  			if e.Vuln != nil {
    82  				got.V = append(got.V, l.Entry().Vuln...)
    83  			}
    84  			if e.Enrichment != nil {
    85  				got.E = append(got.E, l.Entry().Enrichment...)
    86  			}
    87  		}
    88  		if err := l.Err(); err != nil {
    89  			return err
    90  		}
    91  		return nil
    92  	})
    93  	if err := eg.Wait(); err != nil {
    94  		t.Error(err)
    95  	}
    96  	if !cmp.Equal(got, want) {
    97  		t.Error(cmp.Diff(got, want))
    98  	}
    99  }
   100  
   101  func TestEnrichments(t *testing.T) {
   102  	s, err := New()
   103  	if err != nil {
   104  		t.Fatal(err)
   105  	}
   106  	ctx := context.Background()
   107  
   108  	en := test.GenEnrichments(5)
   109  	ref, err := s.UpdateEnrichments(ctx, "test", "", en)
   110  	if err != nil {
   111  		t.Error(err)
   112  	}
   113  	t.Logf("ref: %v", ref)
   114  
   115  	var buf bytes.Buffer
   116  	if err := s.Store(&buf); err != nil {
   117  		t.Error(err)
   118  	}
   119  	t.Logf("wrote:\n%s", buf.String())
   120  }