-
Notifications
You must be signed in to change notification settings - Fork 278
/
tar_assertions.go
180 lines (152 loc) · 4.81 KB
/
tar_assertions.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
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
package testhelpers
import (
"archive/tar"
"bytes"
"compress/gzip"
"encoding/json"
"io"
"os"
"path/filepath"
"testing"
"time"
"github.com/pkg/errors"
"github.com/buildpacks/pack/pkg/archive"
)
var gzipMagicHeader = []byte{'\x1f', '\x8b'}
type TarEntryAssertion func(t *testing.T, header *tar.Header, data []byte)
func AssertOnTarEntry(t *testing.T, tarPath, entryPath string, assertFns ...TarEntryAssertion) {
t.Helper()
tarFile, err := os.Open(filepath.Clean(tarPath))
AssertNil(t, err)
defer tarFile.Close()
header, data, err := readTarFileEntry(tarFile, entryPath)
AssertNil(t, err)
for _, fn := range assertFns {
fn(t, header, data)
}
}
func AssertOnNestedTar(nestedEntryPath string, assertions ...TarEntryAssertion) TarEntryAssertion {
return func(t *testing.T, _ *tar.Header, data []byte) {
t.Helper()
header, data, err := readTarFileEntry(bytes.NewReader(data), nestedEntryPath)
AssertNil(t, err)
for _, assertion := range assertions {
assertion(t, header, data)
}
}
}
func readTarFileEntry(reader io.Reader, entryPath string) (*tar.Header, []byte, error) {
var (
gzipReader *gzip.Reader
err error
)
headerBytes, isGzipped, err := isGzipped(reader)
if err != nil {
return nil, nil, errors.Wrap(err, "checking if reader")
}
reader = io.MultiReader(bytes.NewReader(headerBytes), reader)
if isGzipped {
gzipReader, err = gzip.NewReader(reader)
if err != nil {
return nil, nil, errors.Wrap(err, "failed to create gzip reader")
}
reader = gzipReader
defer gzipReader.Close()
}
return archive.ReadTarEntry(reader, entryPath)
}
func isGzipped(reader io.Reader) (headerBytes []byte, isGzipped bool, err error) {
magicHeader := make([]byte, 2)
n, err := reader.Read(magicHeader)
if n == 0 && err == io.EOF {
return magicHeader, false, nil
}
if err != nil {
return magicHeader, false, err
}
// This assertion is based on https://stackoverflow.com/a/28332019. It checks whether the two header bytes of
// the file match the expected headers for a gzip file; the first one is 0x1f and the second is 0x8b
return magicHeader, bytes.Equal(magicHeader, gzipMagicHeader), nil
}
func ContentContains(expected string) TarEntryAssertion {
return func(t *testing.T, header *tar.Header, contents []byte) {
t.Helper()
AssertContains(t, string(contents), expected)
}
}
func ContentEquals(expected string) TarEntryAssertion {
return func(t *testing.T, header *tar.Header, contents []byte) {
t.Helper()
AssertEq(t, string(contents), expected)
}
}
func SymlinksTo(expectedTarget string) TarEntryAssertion {
return func(t *testing.T, header *tar.Header, _ []byte) {
t.Helper()
if header.Typeflag != tar.TypeSymlink {
t.Fatalf("path '%s' is not a symlink, type flag is '%c'", header.Name, header.Typeflag)
}
if header.Linkname != expectedTarget {
t.Fatalf("symlink '%s' does not point to '%s', instead it points to '%s'", header.Name, expectedTarget, header.Linkname)
}
}
}
func HasOwnerAndGroup(expectedUID int, expectedGID int) TarEntryAssertion {
return func(t *testing.T, header *tar.Header, _ []byte) {
t.Helper()
if header.Uid != expectedUID {
t.Fatalf("expected '%s' to have uid '%d', but got '%d'", header.Name, expectedUID, header.Uid)
}
if header.Gid != expectedGID {
t.Fatalf("expected '%s' to have gid '%d', but got '%d'", header.Name, expectedGID, header.Gid)
}
}
}
func IsJSON() TarEntryAssertion {
return func(t *testing.T, header *tar.Header, data []byte) {
if !json.Valid(data) {
t.Fatal("not valid JSON")
}
}
}
func IsGzipped() TarEntryAssertion {
return func(t *testing.T, header *tar.Header, data []byte) {
_, isGzipped, err := isGzipped(bytes.NewReader(data))
AssertNil(t, err)
if !isGzipped {
t.Fatal("is not gzipped")
}
}
}
func HasFileMode(expectedMode int64) TarEntryAssertion {
return func(t *testing.T, header *tar.Header, _ []byte) {
t.Helper()
if header.Mode != expectedMode {
t.Fatalf("expected '%s' to have mode '%o', but got '%o'", header.Name, expectedMode, header.Mode)
}
}
}
func HasModTime(expectedTime time.Time) TarEntryAssertion {
return func(t *testing.T, header *tar.Header, _ []byte) {
t.Helper()
if header.ModTime.UnixNano() != expectedTime.UnixNano() {
t.Fatalf("expected '%s' to have mod time '%s', but got '%s'", header.Name, expectedTime, header.ModTime)
}
}
}
func DoesNotHaveModTime(expectedTime time.Time) TarEntryAssertion {
return func(t *testing.T, header *tar.Header, _ []byte) {
t.Helper()
if header.ModTime.UnixNano() == expectedTime.UnixNano() {
t.Fatalf("expected '%s' to not have mod time '%s'", header.Name, expectedTime)
}
}
}
func IsDirectory() TarEntryAssertion {
return func(t *testing.T, header *tar.Header, _ []byte) {
t.Helper()
if header.Typeflag != tar.TypeDir {
t.Fatalf("expected '%s' to be a directory but was '%d'", header.Name, header.Typeflag)
}
}
}