2018-02-05 16:05:59 -05:00
|
|
|
package tarsum // import "github.com/docker/docker/pkg/tarsum"
|
2015-06-02 03:08:01 -04:00
|
|
|
|
|
|
|
import (
|
|
|
|
"io"
|
|
|
|
"os"
|
|
|
|
"testing"
|
|
|
|
)
|
|
|
|
|
|
|
|
// Try to remove tarsum (in the BuilderContext) that do not exists, won't change a thing
|
|
|
|
func TestTarSumRemoveNonExistent(t *testing.T) {
|
|
|
|
filename := "testdata/46af0962ab5afeb5ce6740d4d91652e69206fc991fd5328c1a94d364ad00e457/layer.tar"
|
|
|
|
reader, err := os.Open(filename)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
2016-06-24 23:57:21 -04:00
|
|
|
defer reader.Close()
|
|
|
|
|
2015-06-02 03:08:01 -04:00
|
|
|
ts, err := NewTarSum(reader, false, Version0)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Read and discard bytes so that it populates sums
|
2021-08-24 06:10:50 -04:00
|
|
|
_, err = io.Copy(io.Discard, ts)
|
2015-06-02 03:08:01 -04:00
|
|
|
if err != nil {
|
|
|
|
t.Errorf("failed to read from %s: %s", filename, err)
|
|
|
|
}
|
|
|
|
|
|
|
|
expected := len(ts.GetSums())
|
|
|
|
|
|
|
|
ts.(BuilderContext).Remove("")
|
|
|
|
ts.(BuilderContext).Remove("Anything")
|
|
|
|
|
|
|
|
if len(ts.GetSums()) != expected {
|
|
|
|
t.Fatalf("Expected %v sums, go %v.", expected, ts.GetSums())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Remove a tarsum (in the BuilderContext)
|
|
|
|
func TestTarSumRemove(t *testing.T) {
|
|
|
|
filename := "testdata/46af0962ab5afeb5ce6740d4d91652e69206fc991fd5328c1a94d364ad00e457/layer.tar"
|
|
|
|
reader, err := os.Open(filename)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
2016-06-24 23:57:21 -04:00
|
|
|
defer reader.Close()
|
|
|
|
|
2015-06-02 03:08:01 -04:00
|
|
|
ts, err := NewTarSum(reader, false, Version0)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Read and discard bytes so that it populates sums
|
2021-08-24 06:10:50 -04:00
|
|
|
_, err = io.Copy(io.Discard, ts)
|
2015-06-02 03:08:01 -04:00
|
|
|
if err != nil {
|
|
|
|
t.Errorf("failed to read from %s: %s", filename, err)
|
|
|
|
}
|
|
|
|
|
|
|
|
expected := len(ts.GetSums()) - 1
|
|
|
|
|
|
|
|
ts.(BuilderContext).Remove("etc/sudoers")
|
|
|
|
|
|
|
|
if len(ts.GetSums()) != expected {
|
|
|
|
t.Fatalf("Expected %v sums, go %v.", expected, len(ts.GetSums()))
|
|
|
|
}
|
|
|
|
}
|