1
- package coderd_test
1
+ package archive_test
2
2
3
3
import (
4
4
"archive/tar"
5
5
"archive/zip"
6
6
"bytes"
7
- "io"
8
7
"io/fs"
9
8
"os"
10
9
"os/exec"
11
10
"path/filepath"
12
11
"runtime"
13
12
"strings"
14
13
"testing"
15
- "time"
16
14
17
15
"github.com/stretchr/testify/assert"
18
16
"github.com/stretchr/testify/require"
19
- "golang.org/x/xerrors"
20
17
21
- "github.com/coder/coder/v2/coderd"
18
+ "github.com/coder/coder/v2/archive"
19
+ "github.com/coder/coder/v2/archive/archivetest"
22
20
"github.com/coder/coder/v2/testutil"
23
21
)
24
22
@@ -30,18 +28,17 @@ func TestCreateTarFromZip(t *testing.T) {
30
28
31
29
// Read a zip file we prepared earlier
32
30
ctx := testutil .Context (t , testutil .WaitShort )
33
- zipBytes , err := os .ReadFile (filepath .Join ("testdata" , "test.zip" ))
34
- require .NoError (t , err , "failed to read sample zip file" )
31
+ zipBytes := archivetest .TestZipFileBytes ()
35
32
// Assert invariant
36
- assertSampleZipFile (t , zipBytes )
33
+ archivetest . AssertSampleZipFile (t , zipBytes )
37
34
38
35
zr , err := zip .NewReader (bytes .NewReader (zipBytes ), int64 (len (zipBytes )))
39
36
require .NoError (t , err , "failed to parse sample zip file" )
40
37
41
- tarBytes , err := coderd .CreateTarFromZip (zr )
38
+ tarBytes , err := archive .CreateTarFromZip (zr , int64 ( len ( zipBytes )) )
42
39
require .NoError (t , err , "failed to convert zip to tar" )
43
40
44
- assertSampleTarFile (t , tarBytes )
41
+ archivetest . AssertSampleTarFile (t , tarBytes )
45
42
46
43
tempDir := t .TempDir ()
47
44
tempFilePath := filepath .Join (tempDir , "test.tar" )
@@ -60,14 +57,13 @@ func TestCreateZipFromTar(t *testing.T) {
60
57
}
61
58
t .Run ("OK" , func (t * testing.T ) {
62
59
t .Parallel ()
63
- tarBytes , err := os .ReadFile (filepath .Join ("." , "testdata" , "test.tar" ))
64
- require .NoError (t , err , "failed to read sample tar file" )
60
+ tarBytes := archivetest .TestTarFileBytes ()
65
61
66
62
tr := tar .NewReader (bytes .NewReader (tarBytes ))
67
- zipBytes , err := coderd .CreateZipFromTar (tr )
63
+ zipBytes , err := archive .CreateZipFromTar (tr , int64 ( len ( tarBytes )) )
68
64
require .NoError (t , err )
69
65
70
- assertSampleZipFile (t , zipBytes )
66
+ archivetest . AssertSampleZipFile (t , zipBytes )
71
67
72
68
tempDir := t .TempDir ()
73
69
tempFilePath := filepath .Join (tempDir , "test.zip" )
@@ -99,7 +95,7 @@ func TestCreateZipFromTar(t *testing.T) {
99
95
100
96
// When: we convert this to a zip
101
97
tr := tar .NewReader (& tarBytes )
102
- zipBytes , err := coderd .CreateZipFromTar (tr )
98
+ zipBytes , err := archive .CreateZipFromTar (tr , int64 ( tarBytes . Len ()) )
103
99
require .NoError (t , err )
104
100
105
101
// Then: the resulting zip should contain a corresponding directory
@@ -133,7 +129,7 @@ func assertExtractedFiles(t *testing.T, dir string, checkModePerm bool) {
133
129
if checkModePerm {
134
130
assert .Equal (t , fs .ModePerm & 0o755 , stat .Mode ().Perm (), "expected mode 0755 on directory" )
135
131
}
136
- assert .Equal (t , archiveRefTime (t ).UTC (), stat .ModTime ().UTC (), "unexpected modtime of %q" , path )
132
+ assert .Equal (t , archivetest . ArchiveRefTime (t ).UTC (), stat .ModTime ().UTC (), "unexpected modtime of %q" , path )
137
133
case "/test/hello.txt" :
138
134
stat , err := os .Stat (path )
139
135
assert .NoError (t , err , "failed to stat path %q" , path )
@@ -168,84 +164,3 @@ func assertExtractedFiles(t *testing.T, dir string, checkModePerm bool) {
168
164
return nil
169
165
})
170
166
}
171
-
172
- func assertSampleTarFile (t * testing.T , tarBytes []byte ) {
173
- t .Helper ()
174
-
175
- tr := tar .NewReader (bytes .NewReader (tarBytes ))
176
- for {
177
- hdr , err := tr .Next ()
178
- if err != nil {
179
- if err == io .EOF {
180
- return
181
- }
182
- require .NoError (t , err )
183
- }
184
-
185
- // Note: ignoring timezones here.
186
- require .Equal (t , archiveRefTime (t ).UTC (), hdr .ModTime .UTC ())
187
-
188
- switch hdr .Name {
189
- case "test/" :
190
- require .Equal (t , hdr .Typeflag , byte (tar .TypeDir ))
191
- case "test/hello.txt" :
192
- require .Equal (t , hdr .Typeflag , byte (tar .TypeReg ))
193
- bs , err := io .ReadAll (tr )
194
- if err != nil && ! xerrors .Is (err , io .EOF ) {
195
- require .NoError (t , err )
196
- }
197
- require .Equal (t , "hello" , string (bs ))
198
- case "test/dir/" :
199
- require .Equal (t , hdr .Typeflag , byte (tar .TypeDir ))
200
- case "test/dir/world.txt" :
201
- require .Equal (t , hdr .Typeflag , byte (tar .TypeReg ))
202
- bs , err := io .ReadAll (tr )
203
- if err != nil && ! xerrors .Is (err , io .EOF ) {
204
- require .NoError (t , err )
205
- }
206
- require .Equal (t , "world" , string (bs ))
207
- default :
208
- require .Failf (t , "unexpected file in tar" , hdr .Name )
209
- }
210
- }
211
- }
212
-
213
- func assertSampleZipFile (t * testing.T , zipBytes []byte ) {
214
- t .Helper ()
215
-
216
- zr , err := zip .NewReader (bytes .NewReader (zipBytes ), int64 (len (zipBytes )))
217
- require .NoError (t , err )
218
-
219
- for _ , f := range zr .File {
220
- // Note: ignoring timezones here.
221
- require .Equal (t , archiveRefTime (t ).UTC (), f .Modified .UTC ())
222
- switch f .Name {
223
- case "test/" , "test/dir/" :
224
- // directory
225
- case "test/hello.txt" :
226
- rc , err := f .Open ()
227
- require .NoError (t , err )
228
- bs , err := io .ReadAll (rc )
229
- _ = rc .Close ()
230
- require .NoError (t , err )
231
- require .Equal (t , "hello" , string (bs ))
232
- case "test/dir/world.txt" :
233
- rc , err := f .Open ()
234
- require .NoError (t , err )
235
- bs , err := io .ReadAll (rc )
236
- _ = rc .Close ()
237
- require .NoError (t , err )
238
- require .Equal (t , "world" , string (bs ))
239
- default :
240
- require .Failf (t , "unexpected file in zip" , f .Name )
241
- }
242
- }
243
- }
244
-
245
- // archiveRefTime is the Go reference time. The contents of the sample tar and zip files
246
- // in testdata/ all have their modtimes set to the below in some timezone.
247
- func archiveRefTime (t * testing.T ) time.Time {
248
- locMST , err := time .LoadLocation ("MST" )
249
- require .NoError (t , err , "failed to load MST timezone" )
250
- return time .Date (2006 , 1 , 2 , 3 , 4 , 5 , 0 , locMST )
251
- }
0 commit comments