Просмотр исходного кода

Merge pull request #26511 from AkihiroSuda/fix-pkg-archive-racy-test

[test] fix racy tests in pkg/archive
Alexander Morozov 8 лет назад
Родитель
Сommit
b2263c111f
1 измененных файлов с 21 добавлено и 36 удалено
  1. 21 36
      pkg/archive/archive_test.go

+ 21 - 36
pkg/archive/archive_test.go

@@ -94,60 +94,45 @@ func TestIsArchivePathTar(t *testing.T) {
 	}
 	}
 }
 }
 
 
-func TestDecompressStreamGzip(t *testing.T) {
-	cmd := exec.Command("sh", "-c", "touch /tmp/archive && gzip -f /tmp/archive")
+func testDecompressStream(t *testing.T, ext, compressCommand string) {
+	cmd := exec.Command("sh", "-c",
+		fmt.Sprintf("touch /tmp/archive && %s /tmp/archive", compressCommand))
 	output, err := cmd.CombinedOutput()
 	output, err := cmd.CombinedOutput()
 	if err != nil {
 	if err != nil {
-		t.Fatalf("Fail to create an archive file for test : %s.", output)
+		t.Fatalf("Failed to create an archive file for test : %s.", output)
 	}
 	}
-	archive, err := os.Open(tmp + "archive.gz")
+	filename := "archive." + ext
+	archive, err := os.Open(tmp + filename)
 	if err != nil {
 	if err != nil {
-		t.Fatalf("Fail to open file archive.gz")
+		t.Fatalf("Failed to open file %s: %v", filename, err)
 	}
 	}
 	defer archive.Close()
 	defer archive.Close()
 
 
-	_, err = DecompressStream(archive)
+	r, err := DecompressStream(archive)
 	if err != nil {
 	if err != nil {
-		t.Fatalf("Failed to decompress a gzip file.")
+		t.Fatalf("Failed to decompress %s: %v", filename, err)
 	}
 	}
-}
-
-func TestDecompressStreamBzip2(t *testing.T) {
-	cmd := exec.Command("sh", "-c", "touch /tmp/archive && bzip2 -f /tmp/archive")
-	output, err := cmd.CombinedOutput()
-	if err != nil {
-		t.Fatalf("Fail to create an archive file for test : %s.", output)
+	if _, err = ioutil.ReadAll(r); err != nil {
+		t.Fatalf("Failed to read the decompressed stream: %v ", err)
 	}
 	}
-	archive, err := os.Open(tmp + "archive.bz2")
-	if err != nil {
-		t.Fatalf("Fail to open file archive.bz2")
+	if err = r.Close(); err != nil {
+		t.Fatalf("Failed to close the decompressed stream: %v ", err)
 	}
 	}
-	defer archive.Close()
+}
 
 
-	_, err = DecompressStream(archive)
-	if err != nil {
-		t.Fatalf("Failed to decompress a bzip2 file.")
-	}
+func TestDecompressStreamGzip(t *testing.T) {
+	testDecompressStream(t, "gz", "gzip -f")
+}
+
+func TestDecompressStreamBzip2(t *testing.T) {
+	testDecompressStream(t, "bz2", "bzip2 -f")
 }
 }
 
 
 func TestDecompressStreamXz(t *testing.T) {
 func TestDecompressStreamXz(t *testing.T) {
 	if runtime.GOOS == "windows" {
 	if runtime.GOOS == "windows" {
 		t.Skip("Xz not present in msys2")
 		t.Skip("Xz not present in msys2")
 	}
 	}
-	cmd := exec.Command("sh", "-c", "touch /tmp/archive && xz -f /tmp/archive")
-	output, err := cmd.CombinedOutput()
-	if err != nil {
-		t.Fatalf("Fail to create an archive file for test : %s.", output)
-	}
-	archive, err := os.Open(tmp + "archive.xz")
-	if err != nil {
-		t.Fatalf("Fail to open file archive.xz")
-	}
-	defer archive.Close()
-	_, err = DecompressStream(archive)
-	if err != nil {
-		t.Fatalf("Failed to decompress an xz file.")
-	}
+	testDecompressStream(t, "xz", "xz -f")
 }
 }
 
 
 func TestCompressStreamXzUnsuported(t *testing.T) {
 func TestCompressStreamXzUnsuported(t *testing.T) {