github.com/demonoid81/moby@v0.0.0-20200517203328-62dd8e17c460/builder/remotecontext/tarsum_test.go (about) 1 package remotecontext // import "github.com/demonoid81/moby/builder/remotecontext" 2 3 import ( 4 "io/ioutil" 5 "os" 6 "path/filepath" 7 "testing" 8 9 "github.com/demonoid81/moby/builder" 10 "github.com/demonoid81/moby/pkg/archive" 11 "github.com/demonoid81/moby/pkg/reexec" 12 "github.com/pkg/errors" 13 "gotest.tools/v3/skip" 14 ) 15 16 const ( 17 filename = "test" 18 contents = "contents test" 19 ) 20 21 func init() { 22 reexec.Init() 23 } 24 25 func TestCloseRootDirectory(t *testing.T) { 26 contextDir, err := ioutil.TempDir("", "builder-tarsum-test") 27 defer os.RemoveAll(contextDir) 28 if err != nil { 29 t.Fatalf("Error with creating temporary directory: %s", err) 30 } 31 32 src := makeTestArchiveContext(t, contextDir) 33 err = src.Close() 34 35 if err != nil { 36 t.Fatalf("Error while executing Close: %s", err) 37 } 38 39 _, err = os.Stat(src.Root().Path()) 40 41 if !errors.Is(err, os.ErrNotExist) { 42 t.Fatal("Directory should not exist at this point") 43 } 44 } 45 46 func TestHashFile(t *testing.T) { 47 contextDir, cleanup := createTestTempDir(t, "", "builder-tarsum-test") 48 defer cleanup() 49 50 createTestTempFile(t, contextDir, filename, contents, 0755) 51 52 tarSum := makeTestArchiveContext(t, contextDir) 53 54 sum, err := tarSum.Hash(filename) 55 56 if err != nil { 57 t.Fatalf("Error when executing Stat: %s", err) 58 } 59 60 if len(sum) == 0 { 61 t.Fatalf("Hash returned empty sum") 62 } 63 64 expected := "1149ab94af7be6cc1da1335e398f24ee1cf4926b720044d229969dfc248ae7ec" 65 66 if actual := sum; expected != actual { 67 t.Fatalf("invalid checksum. expected %s, got %s", expected, actual) 68 } 69 } 70 71 func TestHashSubdir(t *testing.T) { 72 contextDir, cleanup := createTestTempDir(t, "", "builder-tarsum-test") 73 defer cleanup() 74 75 contextSubdir := filepath.Join(contextDir, "builder-tarsum-test-subdir") 76 err := os.Mkdir(contextSubdir, 0755) 77 if err != nil { 78 t.Fatalf("Failed to make directory: %s", contextSubdir) 79 } 80 81 testFilename := createTestTempFile(t, contextSubdir, filename, contents, 0755) 82 83 tarSum := makeTestArchiveContext(t, contextDir) 84 85 relativePath, err := filepath.Rel(contextDir, testFilename) 86 87 if err != nil { 88 t.Fatalf("Error when getting relative path: %s", err) 89 } 90 91 sum, err := tarSum.Hash(relativePath) 92 93 if err != nil { 94 t.Fatalf("Error when executing Stat: %s", err) 95 } 96 97 if len(sum) == 0 { 98 t.Fatalf("Hash returned empty sum") 99 } 100 101 expected := "d7f8d6353dee4816f9134f4156bf6a9d470fdadfb5d89213721f7e86744a4e69" 102 103 if actual := sum; expected != actual { 104 t.Fatalf("invalid checksum. expected %s, got %s", expected, actual) 105 } 106 } 107 108 func TestRemoveDirectory(t *testing.T) { 109 contextDir, cleanup := createTestTempDir(t, "", "builder-tarsum-test") 110 defer cleanup() 111 112 contextSubdir := createTestTempSubdir(t, contextDir, "builder-tarsum-test-subdir") 113 114 relativePath, err := filepath.Rel(contextDir, contextSubdir) 115 116 if err != nil { 117 t.Fatalf("Error when getting relative path: %s", err) 118 } 119 120 src := makeTestArchiveContext(t, contextDir) 121 122 _, err = src.Root().Stat(src.Root().Join(src.Root().Path(), relativePath)) 123 if err != nil { 124 t.Fatalf("Statting %s shouldn't fail: %+v", relativePath, err) 125 } 126 127 tarSum := src.(modifiableContext) 128 err = tarSum.Remove(relativePath) 129 if err != nil { 130 t.Fatalf("Error when executing Remove: %s", err) 131 } 132 133 _, err = src.Root().Stat(src.Root().Join(src.Root().Path(), relativePath)) 134 if !errors.Is(err, os.ErrNotExist) { 135 t.Fatalf("Directory should not exist at this point: %+v ", err) 136 } 137 } 138 139 func makeTestArchiveContext(t *testing.T, dir string) builder.Source { 140 skip.If(t, os.Getuid() != 0, "skipping test that requires root") 141 tarStream, err := archive.Tar(dir, archive.Uncompressed) 142 if err != nil { 143 t.Fatalf("error: %s", err) 144 } 145 defer tarStream.Close() 146 tarSum, err := FromArchive(tarStream) 147 if err != nil { 148 t.Fatalf("Error when executing FromArchive: %s", err) 149 } 150 return tarSum 151 }