Selaa lähdekoodia

Improve test accuracy for pkg/chrootarchive (part 2)

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>
Yestin Sun 10 vuotta sitten
vanhempi
commit
67df8e4257
1 muutettua tiedostoa jossa 38 lisäystä ja 0 poistoa
  1. 38 0
      pkg/chrootarchive/archive_test.go

+ 38 - 0
pkg/chrootarchive/archive_test.go

@@ -3,6 +3,7 @@ package chrootarchive
 import (
 import (
 	"bytes"
 	"bytes"
 	"fmt"
 	"fmt"
+	"hash/crc32"
 	"io"
 	"io"
 	"io/ioutil"
 	"io/ioutil"
 	"os"
 	"os"
@@ -113,6 +114,16 @@ func prepareSourceDirectory(numberOfFiles int, targetPath string, makeSymLinks b
 	return totalSize, nil
 	return totalSize, nil
 }
 }
 
 
+func getHash(filename string) (uint32, error) {
+	stream, err := ioutil.ReadFile(filename)
+	if err != nil {
+		return 0, err
+	}
+	hash := crc32.NewIEEE()
+	hash.Write(stream)
+	return hash.Sum32(), nil
+}
+
 func compareDirectories(src string, dest string) error {
 func compareDirectories(src string, dest string) error {
 	changes, err := archive.ChangesDirs(dest, src)
 	changes, err := archive.ChangesDirs(dest, src)
 	if err != nil {
 	if err != nil {
@@ -124,6 +135,21 @@ func compareDirectories(src string, dest string) error {
 	return nil
 	return nil
 }
 }
 
 
+func compareFiles(src string, dest string) error {
+	srcHash, err := getHash(src)
+	if err != nil {
+		return err
+	}
+	destHash, err := getHash(dest)
+	if err != nil {
+		return err
+	}
+	if srcHash != destHash {
+		return fmt.Errorf("%s is different from %s", src, dest)
+	}
+	return nil
+}
+
 func TestChrootTarUntarWithSymlink(t *testing.T) {
 func TestChrootTarUntarWithSymlink(t *testing.T) {
 	tmpdir, err := ioutil.TempDir("", "docker-TestChrootTarUntarWithSymlink")
 	tmpdir, err := ioutil.TempDir("", "docker-TestChrootTarUntarWithSymlink")
 	if err != nil {
 	if err != nil {
@@ -176,6 +202,9 @@ func TestChrootCopyWithTar(t *testing.T) {
 	if err := CopyWithTar(srcfile, destfile); err != nil {
 	if err := CopyWithTar(srcfile, destfile); err != nil {
 		t.Fatal(err)
 		t.Fatal(err)
 	}
 	}
+	if err := compareFiles(srcfile, destfile); err != nil {
+		t.Fatal(err)
+	}
 
 
 	// Copy symbolic link
 	// Copy symbolic link
 	srcLinkfile := filepath.Join(src, "file-1-link")
 	srcLinkfile := filepath.Join(src, "file-1-link")
@@ -184,6 +213,9 @@ func TestChrootCopyWithTar(t *testing.T) {
 	if err := CopyWithTar(srcLinkfile, destLinkfile); err != nil {
 	if err := CopyWithTar(srcLinkfile, destLinkfile); err != nil {
 		t.Fatal(err)
 		t.Fatal(err)
 	}
 	}
+	if err := compareFiles(srcLinkfile, destLinkfile); err != nil {
+		t.Fatal(err)
+	}
 }
 }
 
 
 func TestChrootCopyFileWithTar(t *testing.T) {
 func TestChrootCopyFileWithTar(t *testing.T) {
@@ -213,6 +245,9 @@ func TestChrootCopyFileWithTar(t *testing.T) {
 	if err := CopyFileWithTar(srcfile, destfile); err != nil {
 	if err := CopyFileWithTar(srcfile, destfile); err != nil {
 		t.Fatal(err)
 		t.Fatal(err)
 	}
 	}
+	if err := compareFiles(srcfile, destfile); err != nil {
+		t.Fatal(err)
+	}
 
 
 	// Copy symbolic link
 	// Copy symbolic link
 	srcLinkfile := filepath.Join(src, "file-1-link")
 	srcLinkfile := filepath.Join(src, "file-1-link")
@@ -221,6 +256,9 @@ func TestChrootCopyFileWithTar(t *testing.T) {
 	if err := CopyFileWithTar(srcLinkfile, destLinkfile); err != nil {
 	if err := CopyFileWithTar(srcLinkfile, destLinkfile); err != nil {
 		t.Fatal(err)
 		t.Fatal(err)
 	}
 	}
+	if err := compareFiles(srcLinkfile, destLinkfile); err != nil {
+		t.Fatal(err)
+	}
 }
 }
 
 
 func TestChrootUntarPath(t *testing.T) {
 func TestChrootUntarPath(t *testing.T) {