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

     1  package nodejs_test
     2  
     3  import (
     4  	"context"
     5  	"fmt"
     6  	"os"
     7  	"testing"
     8  
     9  	"github.com/quay/claircore"
    10  	"github.com/quay/claircore/nodejs"
    11  
    12  	"github.com/google/go-cmp/cmp"
    13  	"github.com/quay/zlog"
    14  )
    15  
    16  func TestScanLocal(t *testing.T) {
    17  	ctx, done := context.WithCancel(context.Background())
    18  	defer done()
    19  
    20  	table := []struct {
    21  		name      string
    22  		want      []*claircore.Package
    23  		layerPath string
    24  	}{
    25  		{
    26  			name: "sample NodeJS app",
    27  			want: []*claircore.Package{
    28  				{
    29  					Name:    "accepts",
    30  					Version: "1.3.8",
    31  				},
    32  				{
    33  					Name:    "array-flatten",
    34  					Version: "1.1.1",
    35  				},
    36  				{
    37  					Name:    "express",
    38  					Version: "4.18.2",
    39  				},
    40  				{
    41  					Name:    "ipaddr.js",
    42  					Version: "1.9.1",
    43  				},
    44  			},
    45  			layerPath: "testdata/sample-nodejs-app.tar",
    46  		},
    47  	}
    48  	for _, tt := range table {
    49  		t.Run(tt.name, func(t *testing.T) {
    50  			file, err := os.Open(tt.layerPath)
    51  			if err != nil {
    52  				t.Fatal(err)
    53  			}
    54  			defer file.Close()
    55  
    56  			ctx := zlog.Test(ctx, t)
    57  			scanner := &nodejs.Scanner{}
    58  			var l claircore.Layer
    59  			err = l.Init(ctx, &claircore.LayerDescription{
    60  				Digest:    "sha256:1e1bb6832aca0391eefafc58fd9a6b77d728eab3195c536562a86f15b06aed92",
    61  				MediaType: `application/vnd.oci.image.layer.v1.tar`,
    62  			}, file)
    63  			if err != nil {
    64  				t.Fatal(err)
    65  			}
    66  			defer l.Close()
    67  
    68  			got, err := scanner.Scan(ctx, &l)
    69  			if err != nil {
    70  				t.Error(err)
    71  			}
    72  			if !cmp.Equal(len(got), 57) {
    73  				t.Error(cmp.Diff(len(got), 57))
    74  			}
    75  
    76  			gotMap := make(map[string]*claircore.Package, len(got))
    77  			for _, g := range got {
    78  				gotMap[g.Name] = g
    79  			}
    80  
    81  			// Test a select few packages.
    82  			for _, w := range tt.want {
    83  				g, exists := gotMap[w.Name]
    84  				if !exists {
    85  					t.Error(fmt.Sprintf("%s was not found", w.Name))
    86  					continue
    87  				}
    88  
    89  				// Only compare name and version at this time.
    90  				p := &claircore.Package{
    91  					Name:    g.Name,
    92  					Version: g.Version,
    93  				}
    94  				if !cmp.Equal(p, w) {
    95  					t.Error(cmp.Diff(p, w))
    96  				}
    97  			}
    98  		})
    99  	}
   100  }