github.com/rawahars/moby@v24.0.4+incompatible/pkg/tarsum/builder_context_test.go (about)

     1  package tarsum // import "github.com/docker/docker/pkg/tarsum"
     2  
     3  import (
     4  	"io"
     5  	"os"
     6  	"testing"
     7  )
     8  
     9  // Try to remove tarsum (in the BuilderContext) that do not exists, won't change a thing
    10  func TestTarSumRemoveNonExistent(t *testing.T) {
    11  	filename := "testdata/46af0962ab5afeb5ce6740d4d91652e69206fc991fd5328c1a94d364ad00e457/layer.tar"
    12  	reader, err := os.Open(filename)
    13  	if err != nil {
    14  		t.Fatal(err)
    15  	}
    16  	defer reader.Close()
    17  
    18  	ts, err := NewTarSum(reader, false, Version0)
    19  	if err != nil {
    20  		t.Fatal(err)
    21  	}
    22  
    23  	// Read and discard bytes so that it populates sums
    24  	_, err = io.Copy(io.Discard, ts)
    25  	if err != nil {
    26  		t.Errorf("failed to read from %s: %s", filename, err)
    27  	}
    28  
    29  	expected := len(ts.GetSums())
    30  
    31  	ts.(BuilderContext).Remove("")
    32  	ts.(BuilderContext).Remove("Anything")
    33  
    34  	if len(ts.GetSums()) != expected {
    35  		t.Fatalf("Expected %v sums, go %v.", expected, ts.GetSums())
    36  	}
    37  }
    38  
    39  // Remove a tarsum (in the BuilderContext)
    40  func TestTarSumRemove(t *testing.T) {
    41  	filename := "testdata/46af0962ab5afeb5ce6740d4d91652e69206fc991fd5328c1a94d364ad00e457/layer.tar"
    42  	reader, err := os.Open(filename)
    43  	if err != nil {
    44  		t.Fatal(err)
    45  	}
    46  	defer reader.Close()
    47  
    48  	ts, err := NewTarSum(reader, false, Version0)
    49  	if err != nil {
    50  		t.Fatal(err)
    51  	}
    52  
    53  	// Read and discard bytes so that it populates sums
    54  	_, err = io.Copy(io.Discard, ts)
    55  	if err != nil {
    56  		t.Errorf("failed to read from %s: %s", filename, err)
    57  	}
    58  
    59  	expected := len(ts.GetSums()) - 1
    60  
    61  	ts.(BuilderContext).Remove("etc/sudoers")
    62  
    63  	if len(ts.GetSums()) != expected {
    64  		t.Fatalf("Expected %v sums, go %v.", expected, len(ts.GetSums()))
    65  	}
    66  }