github.com/rentongzhang/docker@v1.8.2-rc1/pkg/tarsum/builder_context_test.go (about)

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