forked from buildpacks/pack
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtar_test.go
160 lines (140 loc) · 4.24 KB
/
tar_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
package fs_test
import (
"archive/tar"
"io/ioutil"
"math/rand"
"os"
"path/filepath"
"runtime"
"testing"
"time"
"github.com/fatih/color"
"github.com/sclevine/spec"
"github.com/sclevine/spec/report"
"github.com/buildpack/pack/fs"
)
func TestFS(t *testing.T) {
color.NoColor = true
rand.Seed(time.Now().UTC().UnixNano())
spec.Run(t, "fs", testFS, spec.Report(report.Terminal{}))
}
func testFS(t *testing.T, when spec.G, it spec.S) {
var (
tmpDir, src string
fs fs.FS
)
it.Before(func() {
var err error
tmpDir, err = ioutil.TempDir("", "create-tar-test")
if err != nil {
t.Fatalf("failed to create tmp dir %s: %s", tmpDir, err)
}
src = filepath.Join("testdata", "dir-to-tar")
})
it.After(func() {
if err := os.RemoveAll(tmpDir); err != nil {
t.Fatalf("failed to clean up tmp dir %s: %s", tmpDir, err)
}
})
it("writes a tar to the dest dir", func() {
tarFile := filepath.Join(tmpDir, "some.tar")
err := fs.CreateTarFile(tarFile, src, "/nested/dir/dir-in-archive", 1234, 2345)
if err != nil {
t.Fatalf("CreateTarFile failed: %s", err)
}
file, err := os.Open(tarFile)
if err != nil {
t.Fatalf("could not open tar file %s: %s", tarFile, err)
}
defer file.Close()
tr := tar.NewReader(file)
verify := tarVerifier{t, tr, 1234, 2345}
verify.nextDirectory("/nested", 0755)
verify.nextDirectory("/nested/dir", 0755)
verify.nextDirectory("/nested/dir/dir-in-archive", 0755)
verify.nextFile("/nested/dir/dir-in-archive/some-file.txt", "some-content")
verify.nextDirectory("/nested/dir/dir-in-archive/sub-dir", fileMode(t, filepath.Join(src, "sub-dir")))
if runtime.GOOS != "windows" {
verify.nextSymLink("/nested/dir/dir-in-archive/sub-dir/link-file", "../some-file.txt")
}
})
}
func fileMode(t *testing.T, path string) int64 {
info, err := os.Stat(path)
if err != nil {
t.Fatalf("failed to stat %s", path)
}
mode := int64(info.Mode() & os.ModePerm)
return mode
}
type tarVerifier struct {
t *testing.T
tr *tar.Reader
uid int
gid int
}
func (v *tarVerifier) nextDirectory(name string, mode int64) {
header, err := v.tr.Next()
if err != nil {
v.t.Fatalf("Failed to get next file: %s", err)
}
if header.Name != name {
v.t.Fatalf(`expected dir with name %s, got %s`, name, header.Name)
}
if header.Typeflag != tar.TypeDir {
v.t.Fatalf(`expected %s to be a Directory`, header.Name)
}
if header.Uid != v.uid {
v.t.Fatalf(`expected %s to have uid %d but, got: %d`, header.Name, v.uid, header.Uid)
}
if header.Gid != v.gid {
v.t.Fatalf(`expected %s to have gid %d but, got: %d`, header.Name, v.gid, header.Gid)
}
if header.Mode != mode {
v.t.Fatalf(`expected %s to have mode %o but, got: %o`, header.Name, mode, header.Mode)
}
}
func (v *tarVerifier) nextFile(name, expectedFileContents string) {
header, err := v.tr.Next()
if err != nil {
v.t.Fatalf("Failed to get next file: %s", err)
}
if header.Name != name {
v.t.Fatalf(`expected dir with name %s, got %s`, name, header.Name)
}
if header.Typeflag != tar.TypeReg {
v.t.Fatalf(`expected %s to be a file`, header.Name)
}
if header.Uid != v.uid {
v.t.Fatalf(`expected %s to have uid %d but, got: %d`, header.Name, v.uid, header.Uid)
}
if header.Gid != v.gid {
v.t.Fatalf(`expected %s to have gid %d but, got: %d`, header.Name, v.gid, header.Gid)
}
fileContents := make([]byte, header.Size, header.Size)
v.tr.Read(fileContents)
if string(fileContents) != expectedFileContents {
v.t.Fatalf(`expected to some-file.txt to have %s got %s`, expectedFileContents, string(fileContents))
}
}
func (v *tarVerifier) nextSymLink(name, link string) {
header, err := v.tr.Next()
if err != nil {
v.t.Fatalf("Failed to get next file: %s", err)
}
if header.Name != name {
v.t.Fatalf(`expected dir with name %s, got %s`, name, header.Name)
}
if header.Typeflag != tar.TypeSymlink {
v.t.Fatalf(`expected %s to be a link got %s`, header.Name, string(header.Typeflag))
}
if header.Uid != v.uid {
v.t.Fatalf(`expected %s to have uid %d but, got: %d`, header.Name, v.uid, header.Uid)
}
if header.Gid != v.gid {
v.t.Fatalf(`expected %s to have gid %d but, got: %d`, header.Name, v.gid, header.Gid)
}
if header.Linkname != "../some-file.txt" {
v.t.Fatalf(`expected to link-file to have target %s got: %s`, link, header.Linkname)
}
}