Merge pull request #792 from pulumi/fail-test

Keep test temp directories around on failure
This commit is contained in:
Matthew Riley 2018-01-12 12:04:58 -08:00 committed by GitHub
commit 3620cdb243
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23

View file

@ -304,17 +304,18 @@ func testLifeCycleInitAndDestroy(
return errors.Wrap(err, "Couldn't copy test to temporary directory") return errors.Wrap(err, "Couldn't copy test to temporary directory")
} }
// Keep the temporary test directory around for debugging unless
// the test completes successfully.
keepTestDir := true
defer func() { defer func() {
if t.Failed() { if keepTestDir {
// Test failed -- keep temporary files around for debugging. // Maybe copy to "failed tests" directory.
// Maybe also copy to "failed tests" directory.
failedTestsDir := os.Getenv("PULUMI_FAILED_TESTS_DIR") failedTestsDir := os.Getenv("PULUMI_FAILED_TESTS_DIR")
if failedTestsDir != "" { if failedTestsDir != "" {
dest := filepath.Join(failedTestsDir, t.Name()+uniqueSuffix()) dest := filepath.Join(failedTestsDir, t.Name()+uniqueSuffix())
contract.IgnoreError(fsutil.CopyFile(dest, dir, nil)) contract.IgnoreError(fsutil.CopyFile(dest, dir, nil))
} }
} else { } else {
// Test passed -- delete temporary files.
contract.IgnoreError(os.RemoveAll(dir)) contract.IgnoreError(os.RemoveAll(dir))
} }
}() }()
@ -332,7 +333,13 @@ func testLifeCycleInitAndDestroy(
} }
}() }()
return between(t, opts, dir) err = between(t, opts, dir)
if err != nil {
return err
}
keepTestDir = false
return nil
} }
func testLifeCycleInitialize(t *testing.T, opts *ProgramTestOptions, dir string) error { func testLifeCycleInitialize(t *testing.T, opts *ProgramTestOptions, dir string) error {
@ -644,11 +651,11 @@ func performExtraRuntimeValidation(
} }
// CopyTestToTemporaryDirectory creates a temporary directory to run the test in and copies the test to it. // CopyTestToTemporaryDirectory creates a temporary directory to run the test in and copies the test to it.
func CopyTestToTemporaryDirectory(t *testing.T, opts *ProgramTestOptions) (dir string, err error) { func CopyTestToTemporaryDirectory(t *testing.T, opts *ProgramTestOptions) (string, error) {
// Ensure the required programs are present. // Ensure the required programs are present.
if opts.Bin == "" { if opts.Bin == "" {
var pulumi string var pulumi string
pulumi, err = exec.LookPath("pulumi") pulumi, err := exec.LookPath("pulumi")
if err != nil { if err != nil {
return "", errors.Wrapf(err, "Expected to find `pulumi` binary on $PATH") return "", errors.Wrapf(err, "Expected to find `pulumi` binary on $PATH")
} }
@ -656,7 +663,7 @@ func CopyTestToTemporaryDirectory(t *testing.T, opts *ProgramTestOptions) (dir s
} }
if opts.YarnBin == "" { if opts.YarnBin == "" {
var yarn string var yarn string
yarn, err = exec.LookPath("yarn") yarn, err := exec.LookPath("yarn")
if err != nil { if err != nil {
return "", errors.Wrapf(err, "Expected to find `yarn` binary on $PATH") return "", errors.Wrapf(err, "Expected to find `yarn` binary on $PATH")
} }
@ -683,7 +690,7 @@ func CopyTestToTemporaryDirectory(t *testing.T, opts *ProgramTestOptions) (dir s
opts.Stderr = stderr opts.Stderr = stderr
} }
pioutil.MustFprintf(opts.Stdout, "sample: %v\n", dir) pioutil.MustFprintf(opts.Stdout, "sample: %v\n", sourceDir)
pioutil.MustFprintf(opts.Stdout, "pulumi: %v\n", opts.Bin) pioutil.MustFprintf(opts.Stdout, "pulumi: %v\n", opts.Bin)
pioutil.MustFprintf(opts.Stdout, "yarn: %v\n", opts.YarnBin) pioutil.MustFprintf(opts.Stdout, "yarn: %v\n", opts.YarnBin)
@ -694,23 +701,25 @@ func CopyTestToTemporaryDirectory(t *testing.T, opts *ProgramTestOptions) (dir s
} }
// Clean up the temporary directory on failure // Clean up the temporary directory on failure
deleteTargetDir := true
defer func() { defer func() {
if err != nil { if deleteTargetDir {
contract.IgnoreError(os.RemoveAll(targetDir)) contract.IgnoreError(os.RemoveAll(targetDir))
} }
}() }()
// Copy the source project // Copy the source project
if copyerr := fsutil.CopyFile(targetDir, sourceDir, nil); copyerr != nil { if err = fsutil.CopyFile(targetDir, sourceDir, nil); err != nil {
return "", copyerr return "", err
} }
err = prepareProject(t, opts, targetDir) err = prepareProject(t, opts, targetDir)
if err != nil { if err != nil {
return "", errors.Wrapf(err, "Failed to prepare %v", dir) return "", errors.Wrapf(err, "Failed to prepare %v", targetDir)
} }
pioutil.MustFprintf(stdout, "projdir: %v\n", targetDir) pioutil.MustFprintf(stdout, "projdir: %v\n", targetDir)
deleteTargetDir = false
return targetDir, nil return targetDir, nil
} }