2017-03-20 18:22:29 -04:00
|
|
|
package remotecontext
|
|
|
|
|
|
|
|
import (
|
|
|
|
"io/ioutil"
|
|
|
|
"os"
|
|
|
|
"path/filepath"
|
|
|
|
"testing"
|
|
|
|
|
|
|
|
"github.com/docker/docker/builder"
|
|
|
|
"github.com/docker/docker/pkg/archive"
|
|
|
|
"github.com/docker/docker/pkg/reexec"
|
2017-05-15 17:54:27 -04:00
|
|
|
"github.com/pkg/errors"
|
2017-03-20 18:22:29 -04:00
|
|
|
)
|
|
|
|
|
|
|
|
const (
|
|
|
|
filename = "test"
|
|
|
|
contents = "contents test"
|
|
|
|
)
|
|
|
|
|
|
|
|
func init() {
|
|
|
|
reexec.Init()
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestCloseRootDirectory(t *testing.T) {
|
|
|
|
contextDir, err := ioutil.TempDir("", "builder-tarsum-test")
|
2017-05-15 17:54:27 -04:00
|
|
|
defer os.RemoveAll(contextDir)
|
2017-03-20 18:22:29 -04:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("Error with creating temporary directory: %s", err)
|
|
|
|
}
|
|
|
|
|
2017-05-15 17:54:27 -04:00
|
|
|
src := makeTestArchiveContext(t, contextDir)
|
|
|
|
err = src.Close()
|
2017-03-20 18:22:29 -04:00
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("Error while executing Close: %s", err)
|
|
|
|
}
|
|
|
|
|
2017-05-15 17:54:27 -04:00
|
|
|
_, err = os.Stat(src.Root())
|
2017-03-20 18:22:29 -04:00
|
|
|
|
|
|
|
if !os.IsNotExist(err) {
|
|
|
|
t.Fatal("Directory should not exist at this point")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestHashFile(t *testing.T) {
|
|
|
|
contextDir, cleanup := createTestTempDir(t, "", "builder-tarsum-test")
|
|
|
|
defer cleanup()
|
|
|
|
|
2017-05-02 05:36:43 -04:00
|
|
|
createTestTempFile(t, contextDir, filename, contents, 0755)
|
2017-03-20 18:22:29 -04:00
|
|
|
|
2017-05-15 17:54:27 -04:00
|
|
|
tarSum := makeTestArchiveContext(t, contextDir)
|
2017-03-20 18:22:29 -04:00
|
|
|
|
|
|
|
sum, err := tarSum.Hash(filename)
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("Error when executing Stat: %s", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if len(sum) == 0 {
|
|
|
|
t.Fatalf("Hash returned empty sum")
|
|
|
|
}
|
|
|
|
|
|
|
|
expected := "1149ab94af7be6cc1da1335e398f24ee1cf4926b720044d229969dfc248ae7ec"
|
|
|
|
|
|
|
|
if actual := sum; expected != actual {
|
|
|
|
t.Fatalf("invalid checksum. expected %s, got %s", expected, actual)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestHashSubdir(t *testing.T) {
|
|
|
|
contextDir, cleanup := createTestTempDir(t, "", "builder-tarsum-test")
|
|
|
|
defer cleanup()
|
|
|
|
|
|
|
|
contextSubdir := filepath.Join(contextDir, "builder-tarsum-test-subdir")
|
2017-05-02 05:36:43 -04:00
|
|
|
err := os.Mkdir(contextSubdir, 0755)
|
2017-03-20 18:22:29 -04:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("Failed to make directory: %s", contextSubdir)
|
|
|
|
}
|
|
|
|
|
2017-05-02 05:36:43 -04:00
|
|
|
testFilename := createTestTempFile(t, contextSubdir, filename, contents, 0755)
|
2017-03-20 18:22:29 -04:00
|
|
|
|
2017-05-15 17:54:27 -04:00
|
|
|
tarSum := makeTestArchiveContext(t, contextDir)
|
2017-03-20 18:22:29 -04:00
|
|
|
|
|
|
|
relativePath, err := filepath.Rel(contextDir, testFilename)
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("Error when getting relative path: %s", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
sum, err := tarSum.Hash(relativePath)
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("Error when executing Stat: %s", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if len(sum) == 0 {
|
|
|
|
t.Fatalf("Hash returned empty sum")
|
|
|
|
}
|
|
|
|
|
|
|
|
expected := "d7f8d6353dee4816f9134f4156bf6a9d470fdadfb5d89213721f7e86744a4e69"
|
|
|
|
|
|
|
|
if actual := sum; expected != actual {
|
|
|
|
t.Fatalf("invalid checksum. expected %s, got %s", expected, actual)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestStatNotExisting(t *testing.T) {
|
|
|
|
contextDir, cleanup := createTestTempDir(t, "", "builder-tarsum-test")
|
|
|
|
defer cleanup()
|
|
|
|
|
2017-05-15 17:54:27 -04:00
|
|
|
src := makeTestArchiveContext(t, contextDir)
|
|
|
|
_, err := src.Hash("not-existing")
|
|
|
|
if !os.IsNotExist(errors.Cause(err)) {
|
2017-03-20 18:22:29 -04:00
|
|
|
t.Fatalf("This file should not exist: %s", err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestRemoveDirectory(t *testing.T) {
|
|
|
|
contextDir, cleanup := createTestTempDir(t, "", "builder-tarsum-test")
|
|
|
|
defer cleanup()
|
|
|
|
|
|
|
|
contextSubdir := createTestTempSubdir(t, contextDir, "builder-tarsum-test-subdir")
|
|
|
|
|
|
|
|
relativePath, err := filepath.Rel(contextDir, contextSubdir)
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("Error when getting relative path: %s", err)
|
|
|
|
}
|
|
|
|
|
2017-05-15 17:54:27 -04:00
|
|
|
src := makeTestArchiveContext(t, contextDir)
|
2017-03-20 18:22:29 -04:00
|
|
|
|
2017-05-15 17:54:27 -04:00
|
|
|
tarSum := src.(modifiableContext)
|
2017-03-20 18:22:29 -04:00
|
|
|
|
2017-05-15 17:54:27 -04:00
|
|
|
err = tarSum.Remove(relativePath)
|
2017-03-20 18:22:29 -04:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("Error when executing Remove: %s", err)
|
|
|
|
}
|
|
|
|
|
2017-05-15 17:54:27 -04:00
|
|
|
_, err = src.Hash(contextSubdir)
|
2017-03-20 18:22:29 -04:00
|
|
|
|
2017-05-15 17:54:27 -04:00
|
|
|
if !os.IsNotExist(errors.Cause(err)) {
|
2017-03-20 18:22:29 -04:00
|
|
|
t.Fatal("Directory should not exist at this point")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-05-15 17:54:27 -04:00
|
|
|
func makeTestArchiveContext(t *testing.T, dir string) builder.Source {
|
2017-03-20 18:22:29 -04:00
|
|
|
tarStream, err := archive.Tar(dir, archive.Uncompressed)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("error: %s", err)
|
|
|
|
}
|
|
|
|
defer tarStream.Close()
|
2017-05-15 17:54:27 -04:00
|
|
|
tarSum, err := FromArchive(tarStream)
|
2017-03-20 18:22:29 -04:00
|
|
|
if err != nil {
|
2017-05-15 17:54:27 -04:00
|
|
|
t.Fatalf("Error when executing FromArchive: %s", err)
|
2017-03-20 18:22:29 -04:00
|
|
|
}
|
|
|
|
return tarSum
|
|
|
|
}
|