github.com/quay/claircore@v1.5.28/osrelease/scanner_test.go (about)

     1  package osrelease
     2  
     3  import (
     4  	"bytes"
     5  	"context"
     6  	"encoding/json"
     7  	"path/filepath"
     8  	"strings"
     9  	"testing"
    10  
    11  	"github.com/google/go-cmp/cmp"
    12  	"github.com/quay/zlog"
    13  	"golang.org/x/tools/txtar"
    14  
    15  	"github.com/quay/claircore"
    16  	"github.com/quay/claircore/test"
    17  )
    18  
    19  func TestParse(t *testing.T) {
    20  	t.Parallel()
    21  	ctx := context.Background()
    22  
    23  	ms, _ := filepath.Glob("testdata/*.txtar")
    24  	for i := range ms {
    25  		m := ms[i]
    26  		name := strings.TrimSuffix(filepath.Base(m), ".txtar")
    27  		t.Run(name, func(t *testing.T) {
    28  			ctx := zlog.Test(ctx, t)
    29  			ar, err := txtar.ParseFile(m)
    30  			if err != nil {
    31  				t.Fatal(err)
    32  			}
    33  
    34  			// Find the correct archive members.
    35  			var inFile, parsedFile, distFile *txtar.File
    36  			for i, f := range ar.Files {
    37  				switch f.Name {
    38  				case "os-release":
    39  					inFile = &ar.Files[i]
    40  				case "Parsed":
    41  					parsedFile = &ar.Files[i]
    42  				case "Distribution":
    43  					distFile = &ar.Files[i]
    44  				default:
    45  					t.Logf("unknown file: %s", f.Name)
    46  				}
    47  			}
    48  			if inFile == nil {
    49  				t.Error("missing os-release")
    50  			}
    51  			if parsedFile == nil {
    52  				t.Error("missing Parsed")
    53  			}
    54  			if distFile == nil {
    55  				t.Error("missing Distribution")
    56  			}
    57  			if t.Failed() {
    58  				t.FailNow()
    59  			}
    60  
    61  			// Compare the output of [Parse].
    62  			gotKVs, err := Parse(ctx, bytes.NewReader(inFile.Data))
    63  			if err != nil {
    64  				t.Errorf("parse error: %v", err)
    65  			}
    66  			wantKVs := make(map[string]string)
    67  			if err := json.Unmarshal(parsedFile.Data, &wantKVs); err != nil {
    68  				t.Errorf("unmarshal error: %v", err)
    69  			}
    70  			if got, want := gotKVs, wantKVs; !cmp.Equal(got, want) {
    71  				t.Error(cmp.Diff(got, want))
    72  			}
    73  
    74  			// Compare the [claircore.Distribution] output.
    75  			gotDist, err := toDist(ctx, bytes.NewReader(inFile.Data))
    76  			if err != nil {
    77  				t.Errorf("toDist error: %v", err)
    78  			}
    79  			wantDist := &claircore.Distribution{}
    80  			if err := json.Unmarshal(distFile.Data, &wantDist); err != nil {
    81  				t.Errorf("unmarshal error: %v", err)
    82  			}
    83  			if got, want := gotDist, wantDist; !cmp.Equal(got, want) {
    84  				t.Error(cmp.Diff(got, want))
    85  			}
    86  		})
    87  	}
    88  }
    89  
    90  type layercase struct {
    91  	Name  string
    92  	Layer test.LayerRef
    93  	Want  []*claircore.Distribution
    94  }
    95  type layerspec struct {
    96  	From, Repo string
    97  	Blob       claircore.Digest
    98  }
    99  
   100  func (lc layercase) Test(t *testing.T) {
   101  	t.Parallel()
   102  	ctx := zlog.Test(context.Background(), t)
   103  	l := test.RealizeLayer(ctx, t, lc.Layer)
   104  	var s Scanner
   105  
   106  	ds, err := s.Scan(ctx, l)
   107  	if err != nil {
   108  		t.Error(err)
   109  	}
   110  	if got, want := ds, lc.Want; !cmp.Equal(got, want) {
   111  		t.Fatal(cmp.Diff(got, want))
   112  	}
   113  }
   114  
   115  func TestLayer(t *testing.T) {
   116  	t.Parallel()
   117  	t.Run("Ubuntu", func(t *testing.T) {
   118  		tt := []layercase{
   119  			{
   120  				Name: "18.04",
   121  				Layer: test.LayerRef{
   122  					Registry: "docker.io",
   123  					Name:     "library/ubuntu",
   124  					Digest:   `sha256:35c102085707f703de2d9eaad8752d6fe1b8f02b5d2149f1d8357c9cc7fb7d0a`,
   125  				},
   126  				Want: []*claircore.Distribution{
   127  					{
   128  						DID:             "ubuntu",
   129  						Name:            "Ubuntu",
   130  						Version:         "18.04.3 LTS (Bionic Beaver)",
   131  						VersionCodeName: "bionic",
   132  						VersionID:       "18.04",
   133  						PrettyName:      "Ubuntu 18.04.3 LTS",
   134  					},
   135  				},
   136  			},
   137  		}
   138  
   139  		for _, tc := range tt {
   140  			t.Run(tc.Name, tc.Test)
   141  		}
   142  	})
   143  }