github.com/quay/claircore@v1.5.28/test/periodic/updater_test.go (about)

     1  package periodic
     2  
     3  import (
     4  	"context"
     5  	"io"
     6  	"testing"
     7  	"time"
     8  
     9  	"github.com/quay/zlog"
    10  
    11  	"github.com/quay/claircore/alpine"
    12  	"github.com/quay/claircore/aws"
    13  	"github.com/quay/claircore/debian"
    14  	"github.com/quay/claircore/libvuln/driver"
    15  	"github.com/quay/claircore/oracle"
    16  	"github.com/quay/claircore/photon"
    17  	"github.com/quay/claircore/rhel"
    18  	"github.com/quay/claircore/suse"
    19  	"github.com/quay/claircore/ubuntu"
    20  	"github.com/quay/claircore/updater/osv"
    21  )
    22  
    23  // Helper for keeping the default configuration.
    24  func noopConfigure(_ interface{}) error {
    25  	return nil
    26  }
    27  
    28  func TestAWS(t *testing.T) {
    29  	ctx := zlog.Test(context.Background(), t)
    30  	set, err := aws.UpdaterSet(ctx)
    31  	if err != nil {
    32  		t.Fatal()
    33  	}
    34  	runUpdaterSet(ctx, t, set)
    35  }
    36  
    37  func TestAlpine(t *testing.T) {
    38  	ctx := zlog.Test(context.Background(), t)
    39  	fac, err := alpine.NewFactory(ctx)
    40  	if err != nil {
    41  		t.Fatal()
    42  	}
    43  	err = fac.Configure(ctx, noopConfigure, pkgClient)
    44  	if err != nil {
    45  		t.Fatal(err)
    46  	}
    47  	set, err := fac.UpdaterSet(ctx)
    48  	if err != nil {
    49  		t.Fatal(err)
    50  	}
    51  	runUpdaterSet(ctx, t, set)
    52  }
    53  
    54  func TestDebian(t *testing.T) {
    55  	ctx := zlog.Test(context.Background(), t)
    56  	fac, err := debian.NewFactory(ctx)
    57  	if err != nil {
    58  		t.Fatal()
    59  	}
    60  	err = fac.Configure(ctx, noopConfigure, pkgClient)
    61  	if err != nil {
    62  		t.Fatal(err)
    63  	}
    64  	set, err := fac.UpdaterSet(ctx)
    65  	if err != nil {
    66  		t.Fatal(err)
    67  	}
    68  	runUpdaterSet(ctx, t, set)
    69  }
    70  
    71  func TestOracle(t *testing.T) {
    72  	ctx := zlog.Test(context.Background(), t)
    73  	set, err := oracle.UpdaterSet(ctx)
    74  	if err != nil {
    75  		t.Fatal(err)
    76  	}
    77  	runUpdaterSet(ctx, t, set)
    78  }
    79  
    80  func TestPhoton(t *testing.T) {
    81  	ctx := zlog.Test(context.Background(), t)
    82  	set, err := photon.UpdaterSet(ctx)
    83  	if err != nil {
    84  		t.Fatal(err)
    85  	}
    86  	runUpdaterSet(ctx, t, set)
    87  }
    88  
    89  func TestRHEL(t *testing.T) {
    90  	ctx := zlog.Test(context.Background(), t)
    91  	fac, err := rhel.NewFactory(ctx, rhel.DefaultManifest)
    92  	if err != nil {
    93  		t.Fatal()
    94  	}
    95  	err = fac.Configure(ctx, noopConfigure, pkgClient)
    96  	if err != nil {
    97  		t.Fatal(err)
    98  	}
    99  	set, err := fac.UpdaterSet(ctx)
   100  	if err != nil {
   101  		t.Fatal(err)
   102  	}
   103  	runUpdaterSet(ctx, t, set)
   104  }
   105  
   106  func TestSUSE(t *testing.T) {
   107  	ctx := zlog.Test(context.Background(), t)
   108  	set, err := suse.UpdaterSet(ctx)
   109  	if err != nil {
   110  		t.Fatal()
   111  	}
   112  	runUpdaterSet(ctx, t, set)
   113  }
   114  
   115  func TestUbuntu(t *testing.T) {
   116  	ctx := zlog.Test(context.Background(), t)
   117  	fac, err := ubuntu.NewFactory(ctx)
   118  	if err != nil {
   119  		t.Fatal(err)
   120  	}
   121  	err = fac.Configure(ctx, noopConfigure, pkgClient)
   122  	if err != nil {
   123  		t.Fatal(err)
   124  	}
   125  	set, err := fac.UpdaterSet(ctx)
   126  	if err != nil {
   127  		t.Fatal(err)
   128  	}
   129  	runUpdaterSet(ctx, t, set)
   130  }
   131  
   132  func TestOSV(t *testing.T) {
   133  	ctx := zlog.Test(context.Background(), t)
   134  	fac := &osv.Factory{}
   135  	if err := fac.Configure(ctx, noopConfigure, pkgClient); err != nil {
   136  		t.Fatal(err)
   137  	}
   138  	us, err := fac.UpdaterSet(ctx)
   139  	if err != nil {
   140  		t.Fatal(err)
   141  	}
   142  	runUpdaterSet(ctx, t, us)
   143  }
   144  
   145  func runUpdaterSet(ctx context.Context, t *testing.T, set driver.UpdaterSet) {
   146  	t.Helper()
   147  	for _, u := range set.Updaters() {
   148  		t.Run(u.Name(), func(t *testing.T) {
   149  			ctx := zlog.Test(ctx, t)
   150  			if cfg, ok := u.(driver.Configurable); ok {
   151  				err := cfg.Configure(ctx, noopConfigure, pkgClient)
   152  				if err != nil {
   153  					t.Fatal(err)
   154  				}
   155  			}
   156  			runUpdater(ctx, t, u)
   157  		})
   158  	}
   159  }
   160  
   161  func runUpdater(ctx context.Context, t *testing.T, u driver.Updater) {
   162  	var rc io.ReadCloser
   163  	var nfp driver.Fingerprint
   164  	var err error
   165  	// Debounce any network hiccups.
   166  	for i := 0; i < 5; i++ {
   167  		rc, nfp, err = u.Fetch(ctx, fp)
   168  		if err == nil {
   169  			break
   170  		}
   171  		select {
   172  		case <-ctx.Done():
   173  			t.Fatal(ctx.Err())
   174  		case <-time.After((2 << i) * time.Second):
   175  		}
   176  	}
   177  	if err != nil {
   178  		t.Fatal(err)
   179  	}
   180  	t.Log(nfp)
   181  	defer func() {
   182  		if err := rc.Close(); err != nil {
   183  			t.Log(err)
   184  		}
   185  	}()
   186  
   187  	vs, err := u.Parse(ctx, rc)
   188  	if err != nil {
   189  		t.Error(err)
   190  	}
   191  	t.Logf("reported %d vulnerabilites", len(vs))
   192  }