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

     1  package claircore_test
     2  
     3  import (
     4  	"bytes"
     5  	"context"
     6  	"testing"
     7  
     8  	"github.com/quay/claircore/test"
     9  	"github.com/quay/claircore/test/integration"
    10  )
    11  
    12  // TODO(hank) These tests should be OK to remove, as the tarfs code now
    13  // encapsulates a better way to do this and exercises that in its tests.
    14  
    15  var goldenLayers = []test.LayerRef{
    16  	{
    17  		Registry: "docker.io",
    18  		Name:     "library/ubuntu",
    19  		Digest:   "sha256:35c102085707f703de2d9eaad8752d6fe1b8f02b5d2149f1d8357c9cc7fb7d0a",
    20  	},
    21  }
    22  
    23  type filesTestcase struct {
    24  	name   string
    25  	layers []test.LayerRef
    26  	// A list of paths we know exist in the retrieved layer(s).
    27  	// We will test to make sure their associated buffer is full.
    28  	paths []string
    29  }
    30  
    31  func TestLayerFilesMiss(t *testing.T) {
    32  	integration.Skip(t)
    33  	ctx := context.Background()
    34  	var tt = []filesTestcase{
    35  		{
    36  			name:   "ubuntu:18.04 fake path, leading slash",
    37  			layers: goldenLayers,
    38  			paths:  []string{"/path/to/nowhere"},
    39  		},
    40  		{
    41  			name:   "ubuntu:18.04 fake path, no leading slash",
    42  			layers: goldenLayers,
    43  			paths:  []string{"path/to/nowhere"},
    44  		},
    45  	}
    46  
    47  	for _, table := range tt {
    48  		t.Run(table.name, func(t *testing.T) {
    49  			// fetch the layer
    50  			layers := test.RealizeLayers(ctx, t, table.layers...)
    51  
    52  			// attempt to get files
    53  			_, err := layers[0].Files(table.paths...)
    54  			if err == nil {
    55  				t.Error("expected error")
    56  			}
    57  		})
    58  	}
    59  }
    60  
    61  func TestLayerFilesHit(t *testing.T) {
    62  	integration.Skip(t)
    63  	ctx := context.Background()
    64  	var tt = []filesTestcase{
    65  		{
    66  			name:   "ubuntu:18.04 os-release (linked file), leading slash, inmem fetch",
    67  			layers: goldenLayers,
    68  			paths:  []string{"/etc/os-release"},
    69  		},
    70  		{
    71  			name:   "ubuntu:18.04 os-release (linked file), no leading slash, inmem fetch",
    72  			layers: goldenLayers,
    73  			paths:  []string{"etc/os-release"},
    74  		},
    75  	}
    76  
    77  	for _, table := range tt {
    78  		t.Run(table.name, func(t *testing.T) {
    79  			// fetch the layer
    80  			layers := test.RealizeLayers(ctx, t, table.layers...)
    81  
    82  			// attempt to get files
    83  			files, err := layers[0].Files(table.paths...)
    84  			if err != nil {
    85  				t.Fatal(err)
    86  			}
    87  
    88  			var b *bytes.Buffer
    89  			var ok bool
    90  			for _, path := range table.paths {
    91  				if b, ok = files[path]; !ok {
    92  					t.Fatalf("test path %v was not found in resulting file map", path)
    93  				}
    94  				if l := b.Len(); l <= 0 {
    95  					t.Fatalf("returned buffer for path %v has len %v", path, l)
    96  				}
    97  
    98  				t.Logf("contents: %+q", b.String())
    99  			}
   100  		})
   101  	}
   102  }