github.com/quay/claircore@v1.5.28/rhel/fetch_test.go (about)

     1  package rhel
     2  
     3  import (
     4  	"context"
     5  	"io"
     6  	"net/http"
     7  	"net/http/httptest"
     8  	"testing"
     9  
    10  	"github.com/quay/zlog"
    11  
    12  	"github.com/quay/claircore/libvuln/driver"
    13  )
    14  
    15  func TestFetch(t *testing.T) {
    16  	t.Parallel()
    17  	ctx := context.Background()
    18  	srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
    19  		http.ServeFile(w, r, "testdata/Red_Hat_Enterprise_Linux_3.xml")
    20  	}))
    21  	defer srv.Close()
    22  
    23  	t.Run("FetchContext", func(t *testing.T) {
    24  		ctx := zlog.Test(ctx, t)
    25  		u, err := NewUpdater(`rhel-3-updater`, 3, srv.URL, false)
    26  		if err != nil {
    27  			t.Fatal(err)
    28  		}
    29  		if err := u.Configure(ctx, func(_ interface{}) error { return nil }, srv.Client()); err != nil {
    30  			t.Fatal(err)
    31  		}
    32  		rd, hint, err := u.Fetch(ctx, driver.Fingerprint(""))
    33  		if err != nil {
    34  			t.Fatal(err)
    35  		}
    36  		t.Logf("got fingerprint: %+v", hint)
    37  		defer rd.Close()
    38  		n, err := io.Copy(io.Discard, rd)
    39  		switch {
    40  		case err != nil:
    41  			t.Fatalf("unable to read returned data: %v", err)
    42  		case n == 0:
    43  			t.Fatalf("expected more data than %d bytes", n)
    44  		}
    45  
    46  		rd, got, err := u.Fetch(ctx, hint)
    47  		t.Logf("got fingerprint: %+v", got)
    48  		t.Logf("returned expected error: %v", err)
    49  		if err != driver.Unchanged {
    50  			rd.Close()
    51  			t.Log("resource changed unexpectedly")
    52  			t.Fatalf("%x != %x", got, hint)
    53  		}
    54  	})
    55  
    56  	t.Run("Fetch", func(t *testing.T) {
    57  		ctx := zlog.Test(ctx, t)
    58  		u, err := NewUpdater(`rhel-3-updater`, 3, srv.URL, false)
    59  		if err != nil {
    60  			t.Fatal(err)
    61  		}
    62  		if err := u.Configure(ctx, func(_ interface{}) error { return nil }, srv.Client()); err != nil {
    63  			t.Fatal(err)
    64  		}
    65  		rd, hint, err := u.Fetch(ctx, "")
    66  		if err != nil {
    67  			t.Fatal(err)
    68  		}
    69  		t.Logf("got fingerprint: %+v", hint)
    70  		defer rd.Close()
    71  		n, err := io.Copy(io.Discard, rd)
    72  		switch {
    73  		case err != nil:
    74  			t.Fatalf("unable to read returned data: %v", err)
    75  		case n == 0:
    76  			t.Fatalf("expected more data than %d bytes", n)
    77  		}
    78  
    79  		rd, got, err := u.Fetch(ctx, "")
    80  		t.Logf("got fingerprint: %+v", got)
    81  		if err != nil {
    82  			t.Fatal(err)
    83  		}
    84  		defer rd.Close()
    85  		if hint != got {
    86  			t.Log("resource changed unexpectedly")
    87  			t.Fatalf("%x != %x", got, hint)
    88  		}
    89  	})
    90  }