2018-02-05 21:05:59 +00:00
|
|
|
package remotecontext // import "github.com/docker/docker/builder/remotecontext"
|
2017-03-20 22:22:29 +00:00
|
|
|
|
|
|
|
import (
|
|
|
|
"os"
|
|
|
|
"path/filepath"
|
|
|
|
"testing"
|
|
|
|
|
|
|
|
"github.com/docker/docker/builder"
|
|
|
|
"github.com/docker/docker/pkg/archive"
|
|
|
|
"github.com/docker/docker/pkg/reexec"
|
2017-05-15 21:54:27 +00:00
|
|
|
"github.com/pkg/errors"
|
2020-02-07 13:39:24 +00:00
|
|
|
"gotest.tools/v3/skip"
|
2017-03-20 22:22:29 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
const (
|
|
|
|
filename = "test"
|
|
|
|
contents = "contents test"
|
|
|
|
)
|
|
|
|
|
2023-05-09 22:27:40 +00:00
|
|
|
func TestMain(m *testing.M) {
|
|
|
|
if reexec.Init() {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
os.Exit(m.Run())
|
2017-03-20 22:22:29 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestCloseRootDirectory(t *testing.T) {
|
2021-08-24 10:10:50 +00:00
|
|
|
contextDir, err := os.MkdirTemp("", "builder-tarsum-test")
|
2017-05-15 21:54:27 +00:00
|
|
|
defer os.RemoveAll(contextDir)
|
2017-03-20 22:22:29 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("Error with creating temporary directory: %s", err)
|
|
|
|
}
|
|
|
|
|
2017-05-15 21:54:27 +00:00
|
|
|
src := makeTestArchiveContext(t, contextDir)
|
|
|
|
err = src.Close()
|
2017-03-20 22:22:29 +00:00
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("Error while executing Close: %s", err)
|
|
|
|
}
|
|
|
|
|
2022-09-23 18:09:51 +00:00
|
|
|
_, err = os.Stat(src.Root())
|
2017-03-20 22:22:29 +00:00
|
|
|
|
2020-04-17 10:01:01 +00:00
|
|
|
if !errors.Is(err, os.ErrNotExist) {
|
2017-03-20 22:22:29 +00:00
|
|
|
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 09:36:43 +00:00
|
|
|
createTestTempFile(t, contextDir, filename, contents, 0755)
|
2017-03-20 22:22:29 +00:00
|
|
|
|
2017-05-15 21:54:27 +00:00
|
|
|
tarSum := makeTestArchiveContext(t, contextDir)
|
2017-03-20 22:22:29 +00: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")
|
|
|
|
}
|
|
|
|
|
2022-08-30 13:52:32 +00:00
|
|
|
expected := "55dfeb344351ab27f59aa60ebb0ed12025a2f2f4677bf77d26ea7a671274a9ca"
|
2017-03-20 22:22:29 +00:00
|
|
|
|
|
|
|
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 09:36:43 +00:00
|
|
|
err := os.Mkdir(contextSubdir, 0755)
|
2017-03-20 22:22:29 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("Failed to make directory: %s", contextSubdir)
|
|
|
|
}
|
|
|
|
|
2017-05-02 09:36:43 +00:00
|
|
|
testFilename := createTestTempFile(t, contextSubdir, filename, contents, 0755)
|
2017-03-20 22:22:29 +00:00
|
|
|
|
2017-05-15 21:54:27 +00:00
|
|
|
tarSum := makeTestArchiveContext(t, contextDir)
|
2017-03-20 22:22:29 +00: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")
|
|
|
|
}
|
|
|
|
|
2022-08-30 13:52:32 +00:00
|
|
|
expected := "74a3326b8e766ce63a8e5232f22e9dd895be647fb3ca7d337e5e0a9b3da8ef28"
|
2017-03-20 22:22:29 +00:00
|
|
|
|
|
|
|
if actual := sum; expected != actual {
|
|
|
|
t.Fatalf("invalid checksum. expected %s, got %s", expected, actual)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
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 21:54:27 +00:00
|
|
|
src := makeTestArchiveContext(t, contextDir)
|
2017-03-20 22:22:29 +00:00
|
|
|
|
2022-09-23 18:09:51 +00:00
|
|
|
_, err = os.Stat(filepath.Join(src.Root(), relativePath))
|
2017-07-26 23:23:31 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("Statting %s shouldn't fail: %+v", relativePath, err)
|
|
|
|
}
|
2017-03-20 22:22:29 +00:00
|
|
|
|
2017-07-26 23:23:31 +00:00
|
|
|
tarSum := src.(modifiableContext)
|
2017-05-15 21:54:27 +00:00
|
|
|
err = tarSum.Remove(relativePath)
|
2017-03-20 22:22:29 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("Error when executing Remove: %s", err)
|
|
|
|
}
|
|
|
|
|
2022-09-23 18:09:51 +00:00
|
|
|
_, err = os.Stat(filepath.Join(src.Root(), relativePath))
|
2020-04-17 10:01:01 +00:00
|
|
|
if !errors.Is(err, os.ErrNotExist) {
|
2017-07-26 23:23:31 +00:00
|
|
|
t.Fatalf("Directory should not exist at this point: %+v ", err)
|
2017-03-20 22:22:29 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-05-15 21:54:27 +00:00
|
|
|
func makeTestArchiveContext(t *testing.T, dir string) builder.Source {
|
2018-06-11 13:32:11 +00:00
|
|
|
skip.If(t, os.Getuid() != 0, "skipping test that requires root")
|
2017-03-20 22:22:29 +00:00
|
|
|
tarStream, err := archive.Tar(dir, archive.Uncompressed)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("error: %s", err)
|
|
|
|
}
|
|
|
|
defer tarStream.Close()
|
2017-05-15 21:54:27 +00:00
|
|
|
tarSum, err := FromArchive(tarStream)
|
2017-03-20 22:22:29 +00:00
|
|
|
if err != nil {
|
2017-05-15 21:54:27 +00:00
|
|
|
t.Fatalf("Error when executing FromArchive: %s", err)
|
2017-03-20 22:22:29 +00:00
|
|
|
}
|
|
|
|
return tarSum
|
|
|
|
}
|