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

     1  package claircore_test
     2  
     3  import (
     4  	"bytes"
     5  	"context"
     6  	"io"
     7  	"os"
     8  	"strings"
     9  	"testing"
    10  	"time"
    11  
    12  	"github.com/quay/claircore"
    13  	"github.com/quay/claircore/test"
    14  )
    15  
    16  func TestLayer(t *testing.T) {
    17  	ctx := context.Background()
    18  
    19  	// Tarish is a tar-ish file.
    20  	tarish := test.GenerateFixture(t, "tarish", time.Time{}, func(t testing.TB, f *os.File) {
    21  		if _, err := f.Write(make([]byte, 1024)); err != nil {
    22  			t.Fatal(err)
    23  		}
    24  	})
    25  	// GoodLayer returns a layer that looks like a tar.
    26  	//
    27  	// This helper does not arrange for the Close method to be called.
    28  	goodLayer := func(t *testing.T) *claircore.Layer {
    29  		t.Helper()
    30  		var l claircore.Layer
    31  		desc := claircore.LayerDescription{
    32  			Digest:    "sha256:" + strings.Repeat("00c0ffee", 8),
    33  			MediaType: `application/vnd.oci.image.layer.v1.tar`,
    34  		}
    35  		f, err := os.Open(tarish)
    36  		if err != nil {
    37  			t.Fatal(err)
    38  		}
    39  		t.Cleanup(func() {
    40  			if err := f.Close(); err != nil {
    41  				t.Error(err)
    42  			}
    43  		})
    44  
    45  		if err := l.Init(ctx, &desc, f); err != nil {
    46  			t.Fatalf("unexpected error: %v", err)
    47  		}
    48  		return &l
    49  	}
    50  
    51  	t.Run("Init", func(t *testing.T) {
    52  		t.Run("Checksum", func(t *testing.T) {
    53  			var l claircore.Layer
    54  			desc := claircore.LayerDescription{
    55  				Digest:    "sha256:" + strings.Repeat("00c0ffee", 9),
    56  				MediaType: `application/octet-stream`,
    57  			}
    58  
    59  			err := l.Init(ctx, &desc, bytes.NewReader(nil))
    60  			t.Logf("error: %v", err)
    61  			if err == nil {
    62  				t.Error("unexpected success")
    63  			}
    64  		})
    65  		t.Run("MediaType", func(t *testing.T) {
    66  			var l claircore.Layer
    67  			desc := claircore.LayerDescription{
    68  				Digest:    "sha256:" + strings.Repeat("00c0ffee", 8),
    69  				MediaType: `application/octet-stream`,
    70  			}
    71  
    72  			err := l.Init(ctx, &desc, bytes.NewReader(nil))
    73  			t.Logf("error: %v", err)
    74  			if err == nil {
    75  				t.Error("unexpected success")
    76  			}
    77  		})
    78  		t.Run("Success", func(t *testing.T) {
    79  			l := goodLayer(t)
    80  			if err := l.Close(); err != nil {
    81  				t.Errorf("close error: %v", err)
    82  			}
    83  		})
    84  		t.Run("DoubleInit", func(t *testing.T) {
    85  			l := goodLayer(t)
    86  			t.Cleanup(func() {
    87  				if err := l.Close(); err != nil {
    88  					t.Errorf("close error: %v", err)
    89  				}
    90  			})
    91  			err := l.Init(ctx, nil, nil)
    92  			t.Logf("error: %v", err)
    93  			if err == nil {
    94  				t.Error("unexpected success")
    95  			}
    96  		})
    97  	})
    98  	t.Run("Close", func(t *testing.T) {
    99  		t.Run("Success", func(t *testing.T) {
   100  			l := goodLayer(t)
   101  			if err := l.Close(); err != nil {
   102  				t.Errorf("close error: %v", err)
   103  			}
   104  		})
   105  		t.Run("DoubleClose", func(t *testing.T) {
   106  			l := goodLayer(t)
   107  			if err := l.Close(); err != nil {
   108  				t.Errorf("close error: %v", err)
   109  			}
   110  			defer func() {
   111  				if r := recover(); r != nil {
   112  					switch v := r.(type) {
   113  					case string:
   114  						if strings.Contains(v, "Layer closed twice") {
   115  							return
   116  						}
   117  						panic(r)
   118  					default:
   119  						panic(r)
   120  					}
   121  				}
   122  			}()
   123  			err := l.Close()
   124  			t.Errorf("returned from second Close: %v", err)
   125  		})
   126  	})
   127  	t.Run("SetLocal", func(t *testing.T) {
   128  		var l claircore.Layer
   129  		err := l.SetLocal("")
   130  		t.Logf("error: %v", err)
   131  		if err == nil {
   132  			t.Error("unexpected success")
   133  		}
   134  	})
   135  	t.Run("Fetched", func(t *testing.T) {
   136  		t.Run("True", func(t *testing.T) {
   137  			l := goodLayer(t)
   138  			t.Cleanup(func() {
   139  				if err := l.Close(); err != nil {
   140  					t.Errorf("close error: %v", err)
   141  				}
   142  			})
   143  			f := l.Fetched()
   144  			t.Logf("fetched: %v", f)
   145  			if !f {
   146  				t.Error("unexpected failure")
   147  			}
   148  		})
   149  		t.Run("False", func(t *testing.T) {
   150  			var l claircore.Layer
   151  			f := l.Fetched()
   152  			t.Logf("fetched: %v", f)
   153  			if f {
   154  				t.Error("unexpected success")
   155  			}
   156  		})
   157  	})
   158  	t.Run("FS", func(t *testing.T) {
   159  		t.Run("Fail", func(t *testing.T) {
   160  			var l claircore.Layer
   161  			_, err := l.FS()
   162  			t.Logf("error: %v", err)
   163  			if err == nil {
   164  				t.Error("unexpected success")
   165  			}
   166  		})
   167  		t.Run("Success", func(t *testing.T) {
   168  			l := goodLayer(t)
   169  			t.Cleanup(func() {
   170  				if err := l.Close(); err != nil {
   171  					t.Errorf("close error: %v", err)
   172  				}
   173  			})
   174  			_, err := l.FS()
   175  			t.Logf("error: %v", err)
   176  			if err != nil {
   177  				t.Error("unexpected error")
   178  			}
   179  		})
   180  	})
   181  	t.Run("Reader", func(t *testing.T) {
   182  		t.Run("Fail", func(t *testing.T) {
   183  			var l claircore.Layer
   184  			_, err := l.Reader()
   185  			t.Logf("error: %v", err)
   186  			if err == nil {
   187  				t.Error("unexpected success")
   188  			}
   189  		})
   190  		t.Run("Success", func(t *testing.T) {
   191  			l := goodLayer(t)
   192  			t.Cleanup(func() {
   193  				if err := l.Close(); err != nil {
   194  					t.Errorf("close error: %v", err)
   195  				}
   196  			})
   197  			rac, err := l.Reader()
   198  			if err != nil {
   199  				t.Fatalf("unexpected error: %v", err)
   200  			}
   201  			t.Cleanup(func() {
   202  				if err := rac.Close(); err != nil {
   203  					t.Errorf("reader close error: %v", err)
   204  				}
   205  			})
   206  			n, err := io.Copy(io.Discard, rac)
   207  			if n != 1024 || err != nil {
   208  				t.Errorf("unexpected error: read %d bytes, got error: %v", n, err)
   209  			}
   210  		})
   211  	})
   212  }