Skip to content

Commit 8883eb6

Browse files
committed
Add test for archive extraction code
This checks that the value returned from Uncompress matches what we expect, and also checks the disk content. This tests .zip and .tar.gz files, both with and without filters.
1 parent bf72e90 commit 8883eb6

File tree

3 files changed

+150
-0
lines changed

3 files changed

+150
-0
lines changed

pkg/extract/extract_test.go

Lines changed: 150 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,150 @@
1+
package extract
2+
3+
import (
4+
"fmt"
5+
"io/ioutil"
6+
"os"
7+
"path/filepath"
8+
"strings"
9+
"testing"
10+
11+
"github.com/code-ready/crc/pkg/crc/logging"
12+
)
13+
14+
type FileMap map[string]string
15+
16+
func copyFileMap(orig FileMap) FileMap {
17+
copy := FileMap{}
18+
for key, value := range orig {
19+
copy[key] = value
20+
}
21+
22+
return copy
23+
}
24+
25+
// This checks that the list of files returned by Uncompress matches what we expect
26+
func checkFileList(destDir string, extractedFiles []string, expectedFiles FileMap) error {
27+
// We are going to remove elements from the map, but we don't want to modify the map used by the caller
28+
expectedFiles = copyFileMap(expectedFiles)
29+
30+
for _, file := range extractedFiles {
31+
logging.Debugf("Checking %s", file)
32+
file = strings.TrimPrefix(file, destDir)
33+
_, found := expectedFiles[file]
34+
if !found {
35+
return fmt.Errorf("Unexpected file '%s' in file list", file)
36+
}
37+
delete(expectedFiles, file)
38+
}
39+
40+
if len(expectedFiles) != 0 {
41+
return fmt.Errorf("Some expected files were not in file list: %v", expectedFiles)
42+
}
43+
44+
return nil
45+
}
46+
47+
// This checks that the files in the destination directory matches what we expect
48+
func checkFiles(destDir string, files FileMap) error {
49+
// We are going to remove elements from the map, but we don't want to modify the map used by the caller
50+
files = copyFileMap(files)
51+
52+
err := filepath.Walk(destDir, func(path string, info os.FileInfo, err error) error {
53+
logging.Debugf("Walking %s", path)
54+
if err != nil {
55+
return err
56+
}
57+
if info.IsDir() {
58+
logging.Debugf("Skipping directory %s", path)
59+
return nil
60+
}
61+
logging.Debugf("Checking file %s", path)
62+
archivePath := strings.TrimPrefix(path, destDir)
63+
expectedContent, found := files[archivePath]
64+
if !found {
65+
return fmt.Errorf("Unexpected extracted file '%s'", path)
66+
}
67+
delete(files, archivePath)
68+
69+
data, err := ioutil.ReadFile(path) // #nosec G304
70+
if err != nil {
71+
return err
72+
}
73+
if string(data) != expectedContent {
74+
return fmt.Errorf("Unexpected content for '%s': expected [%s], got [%s]", path, expectedContent, string(data))
75+
}
76+
logging.Debugf("'%s' successfully checked", path)
77+
return nil
78+
})
79+
if err != nil {
80+
return err
81+
}
82+
if len(files) != 0 {
83+
return fmt.Errorf("Some expected files were not extracted: %v", files)
84+
}
85+
86+
return nil
87+
}
88+
89+
func testUncompress(archiveName string, fileFilter func(string) bool, files FileMap) error {
90+
destDir, err := ioutil.TempDir("", "crc-extract-test")
91+
if err != nil {
92+
return err
93+
}
94+
err = os.MkdirAll(destDir, 0700)
95+
if err != nil {
96+
return err
97+
}
98+
defer os.RemoveAll(destDir)
99+
100+
var fileList []string
101+
if fileFilter != nil {
102+
fileList, err = UncompressWithFilter(archiveName, destDir, fileFilter)
103+
} else {
104+
fileList, err = Uncompress(archiveName, destDir)
105+
}
106+
if err != nil {
107+
return err
108+
}
109+
110+
err = checkFileList(destDir, fileList, files)
111+
if err != nil {
112+
return err
113+
}
114+
115+
return checkFiles(destDir, files)
116+
}
117+
118+
func fileFilter(filename string) bool {
119+
return filepath.Base(filename) == "c.txt"
120+
}
121+
122+
func TestUncompress(t *testing.T) {
123+
var files FileMap = map[string]string{
124+
"/a/b/c.txt": "ccc",
125+
"/a/d/e.txt": "eee",
126+
}
127+
var filteredFiles FileMap = map[string]string{
128+
"/a/b/c.txt": "ccc",
129+
}
130+
131+
err := testUncompress("testdata/test.tar.gz", nil, files)
132+
if err != nil {
133+
t.Errorf("Failed to uncompress test.tar.gz: %v", err)
134+
}
135+
136+
err = testUncompress("testdata/test.zip", nil, files)
137+
if err != nil {
138+
t.Errorf("Failed to uncompress test.zip: %v", err)
139+
}
140+
141+
err = testUncompress("testdata/test.tar.gz", fileFilter, filteredFiles)
142+
if err != nil {
143+
t.Errorf("Failed to uncompress c.txt from test.tar.gz: %v", err)
144+
}
145+
146+
err = testUncompress("testdata/test.zip", fileFilter, filteredFiles)
147+
if err != nil {
148+
t.Errorf("Failed to uncompress c.txt from test.zip: %v", err)
149+
}
150+
}

pkg/extract/testdata/test.tar.gz

216 Bytes
Binary file not shown.

pkg/extract/testdata/test.zip

860 Bytes
Binary file not shown.

0 commit comments

Comments
 (0)