Check test correctness of untar by comparing destination with
source. For part 2, it checkes hashes of source and destination
files or the target files of symbolic links.
This is a supplement to the #11601 fix.
Signed-off-by: Yestin Sun <sunyi0804@gmail.com>
| ... | ... |
@@ -3,6 +3,7 @@ package chrootarchive |
| 3 | 3 |
import ( |
| 4 | 4 |
"bytes" |
| 5 | 5 |
"fmt" |
| 6 |
+ "hash/crc32" |
|
| 6 | 7 |
"io" |
| 7 | 8 |
"io/ioutil" |
| 8 | 9 |
"os" |
| ... | ... |
@@ -113,6 +114,16 @@ func prepareSourceDirectory(numberOfFiles int, targetPath string, makeSymLinks b |
| 113 | 113 |
return totalSize, nil |
| 114 | 114 |
} |
| 115 | 115 |
|
| 116 |
+func getHash(filename string) (uint32, error) {
|
|
| 117 |
+ stream, err := ioutil.ReadFile(filename) |
|
| 118 |
+ if err != nil {
|
|
| 119 |
+ return 0, err |
|
| 120 |
+ } |
|
| 121 |
+ hash := crc32.NewIEEE() |
|
| 122 |
+ hash.Write(stream) |
|
| 123 |
+ return hash.Sum32(), nil |
|
| 124 |
+} |
|
| 125 |
+ |
|
| 116 | 126 |
func compareDirectories(src string, dest string) error {
|
| 117 | 127 |
changes, err := archive.ChangesDirs(dest, src) |
| 118 | 128 |
if err != nil {
|
| ... | ... |
@@ -124,6 +135,21 @@ func compareDirectories(src string, dest string) error {
|
| 124 | 124 |
return nil |
| 125 | 125 |
} |
| 126 | 126 |
|
| 127 |
+func compareFiles(src string, dest string) error {
|
|
| 128 |
+ srcHash, err := getHash(src) |
|
| 129 |
+ if err != nil {
|
|
| 130 |
+ return err |
|
| 131 |
+ } |
|
| 132 |
+ destHash, err := getHash(dest) |
|
| 133 |
+ if err != nil {
|
|
| 134 |
+ return err |
|
| 135 |
+ } |
|
| 136 |
+ if srcHash != destHash {
|
|
| 137 |
+ return fmt.Errorf("%s is different from %s", src, dest)
|
|
| 138 |
+ } |
|
| 139 |
+ return nil |
|
| 140 |
+} |
|
| 141 |
+ |
|
| 127 | 142 |
func TestChrootTarUntarWithSymlink(t *testing.T) {
|
| 128 | 143 |
tmpdir, err := ioutil.TempDir("", "docker-TestChrootTarUntarWithSymlink")
|
| 129 | 144 |
if err != nil {
|
| ... | ... |
@@ -176,6 +202,9 @@ func TestChrootCopyWithTar(t *testing.T) {
|
| 176 | 176 |
if err := CopyWithTar(srcfile, destfile); err != nil {
|
| 177 | 177 |
t.Fatal(err) |
| 178 | 178 |
} |
| 179 |
+ if err := compareFiles(srcfile, destfile); err != nil {
|
|
| 180 |
+ t.Fatal(err) |
|
| 181 |
+ } |
|
| 179 | 182 |
|
| 180 | 183 |
// Copy symbolic link |
| 181 | 184 |
srcLinkfile := filepath.Join(src, "file-1-link") |
| ... | ... |
@@ -184,6 +213,9 @@ func TestChrootCopyWithTar(t *testing.T) {
|
| 184 | 184 |
if err := CopyWithTar(srcLinkfile, destLinkfile); err != nil {
|
| 185 | 185 |
t.Fatal(err) |
| 186 | 186 |
} |
| 187 |
+ if err := compareFiles(srcLinkfile, destLinkfile); err != nil {
|
|
| 188 |
+ t.Fatal(err) |
|
| 189 |
+ } |
|
| 187 | 190 |
} |
| 188 | 191 |
|
| 189 | 192 |
func TestChrootCopyFileWithTar(t *testing.T) {
|
| ... | ... |
@@ -213,6 +245,9 @@ func TestChrootCopyFileWithTar(t *testing.T) {
|
| 213 | 213 |
if err := CopyFileWithTar(srcfile, destfile); err != nil {
|
| 214 | 214 |
t.Fatal(err) |
| 215 | 215 |
} |
| 216 |
+ if err := compareFiles(srcfile, destfile); err != nil {
|
|
| 217 |
+ t.Fatal(err) |
|
| 218 |
+ } |
|
| 216 | 219 |
|
| 217 | 220 |
// Copy symbolic link |
| 218 | 221 |
srcLinkfile := filepath.Join(src, "file-1-link") |
| ... | ... |
@@ -221,6 +256,9 @@ func TestChrootCopyFileWithTar(t *testing.T) {
|
| 221 | 221 |
if err := CopyFileWithTar(srcLinkfile, destLinkfile); err != nil {
|
| 222 | 222 |
t.Fatal(err) |
| 223 | 223 |
} |
| 224 |
+ if err := compareFiles(srcLinkfile, destLinkfile); err != nil {
|
|
| 225 |
+ t.Fatal(err) |
|
| 226 |
+ } |
|
| 224 | 227 |
} |
| 225 | 228 |
|
| 226 | 229 |
func TestChrootUntarPath(t *testing.T) {
|