diff --git a/go.mod b/go.mod index 51a72f337..5d978987a 100644 --- a/go.mod +++ b/go.mod @@ -21,6 +21,7 @@ require ( github.com/google/go-containerregistry v0.0.0-20200110202235-f4fb41bf00a3 github.com/google/uuid v1.1.1 github.com/gruntwork-io/gruntwork-cli v0.7.0 + github.com/hashicorp/go-multierror v1.1.0 github.com/imdario/mergo v0.3.7 // indirect github.com/jinzhu/copier v0.0.0-20190924061706-b57f9002281a github.com/jstemmer/go-junit-report v0.9.1 diff --git a/go.sum b/go.sum index b0381907b..9494f729a 100644 --- a/go.sum +++ b/go.sum @@ -243,6 +243,10 @@ github.com/grpc-ecosystem/go-grpc-prometheus v1.2.0/go.mod h1:8NvIoxWQoOIhqOTXgf github.com/grpc-ecosystem/grpc-gateway v1.9.5/go.mod h1:vNeuVxBJEsws4ogUvrchl83t/GYV9WGTSLVdBhOQFDY= github.com/gruntwork-io/gruntwork-cli v0.7.0 h1:YgSAmfCj9c61H+zuvHwKfYUwlMhu5arnQQLM4RH+CYs= github.com/gruntwork-io/gruntwork-cli v0.7.0/go.mod h1:jp6Z7NcLF2avpY8v71fBx6hds9eOFPELSuD/VPv7w00= +github.com/hashicorp/errwrap v1.0.0 h1:hLrqtEDnRye3+sgx6z4qVLNuviH3MR5aQ0ykNJa/UYA= +github.com/hashicorp/errwrap v1.0.0/go.mod h1:YH+1FKiLXxHSkmPseP+kNlulaMuP3n2brvKWEqk/Jc4= +github.com/hashicorp/go-multierror v1.1.0 h1:B9UzwGQJehnUY1yNrnwREHc3fGbC2xefo8g4TbElacI= +github.com/hashicorp/go-multierror v1.1.0/go.mod h1:spPvp8C1qA32ftKqdAHm4hHTbPw+vmowP0z+KUhOZdA= github.com/hashicorp/golang-lru v0.5.0/go.mod h1:/m3WP610KZHVQ1SGc6re/UDhFvYD7pJ4Ao+sR/qLZy8= github.com/hashicorp/golang-lru v0.5.1/go.mod h1:/m3WP610KZHVQ1SGc6re/UDhFvYD7pJ4Ao+sR/qLZy8= github.com/hashicorp/golang-lru v0.5.3/go.mod h1:iADmTwqILo4mZ8BN3D2Q6+9jd8WM5uGBxy+E8yxSoD4= diff --git a/modules/aws/ec2-files.go b/modules/aws/ec2-files.go index fef8edf84..f6810b117 100644 --- a/modules/aws/ec2-files.go +++ b/modules/aws/ec2-files.go @@ -4,10 +4,10 @@ import ( "os" "path/filepath" - "github.com/gruntwork-io/terratest/modules/customerrors" "github.com/gruntwork-io/terratest/modules/files" "github.com/gruntwork-io/terratest/modules/ssh" "github.com/gruntwork-io/terratest/modules/testing" + "github.com/hashicorp/go-multierror" ) // RemoteFileSpecification describes which files you want to copy from your instances @@ -212,24 +212,24 @@ func FetchFilesFromAsgs(t testing.TestingT, awsRegion string, spec RemoteFileSpe // remoteDirectory (using sudo if useSudo is true), and stores the files locally at // localDirectory// func FetchFilesFromAsgsE(t testing.TestingT, awsRegion string, spec RemoteFileSpecification) error { - errorsOccurred := []error{} + var errorsOccurred = new(multierror.Error) for _, curAsg := range spec.AsgNames { for curRemoteDir, fileFilters := range spec.RemotePathToFileFilter { instanceIDs, err := GetInstanceIdsForAsgE(t, curAsg, awsRegion) if err != nil { - errorsOccurred = append(errorsOccurred, err) + errorsOccurred = multierror.Append(errorsOccurred, err) } else { for _, instanceID := range instanceIDs { err = FetchFilesFromInstanceE(t, awsRegion, spec.SshUser, spec.KeyPair, instanceID, spec.UseSudo, curRemoteDir, spec.LocalDestinationDir, fileFilters) if err != nil { - errorsOccurred = append(errorsOccurred, err) + errorsOccurred = multierror.Append(errorsOccurred, err) } } } } } - return customerrors.NewMultiError(errorsOccurred...) + return errorsOccurred.ErrorOrNil() } diff --git a/modules/customerrors/multierror.go b/modules/customerrors/multierror.go deleted file mode 100644 index 1b9691cf0..000000000 --- a/modules/customerrors/multierror.go +++ /dev/null @@ -1,36 +0,0 @@ -package customerrors - -import ( - "fmt" - "strings" -) - -type MultiError struct { - Errors []error -} - -func NewMultiError(errs ...error) error { - nilsRemoved := make([]error, 0, len(errs)) - for _, item := range errs { - if item != nil { - nilsRemoved = append(nilsRemoved, item) - } - } - - if len(nilsRemoved) == 0 { - return nil - } - - return MultiError{Errors: nilsRemoved} -} - -func (errs MultiError) Error() string { - errorMessages := []string{} - for _, err := range errs.Errors { - if err != nil { - errorMessages = append(errorMessages, err.Error()) - } - } - - return fmt.Sprintf("Hit multiple errors:\n%s", strings.Join(errorMessages, "\n")) -} diff --git a/modules/packer/packer.go b/modules/packer/packer.go index f360ba969..879878d8d 100644 --- a/modules/packer/packer.go +++ b/modules/packer/packer.go @@ -9,8 +9,8 @@ import ( "time" "github.com/gruntwork-io/terratest/modules/retry" + "github.com/hashicorp/go-multierror" - "github.com/gruntwork-io/terratest/modules/customerrors" "github.com/gruntwork-io/terratest/modules/logger" "github.com/gruntwork-io/terratest/modules/shell" "github.com/gruntwork-io/terratest/modules/testing" @@ -55,7 +55,7 @@ func BuildArtifactsE(t testing.TestingT, artifactNameToOptions map[string]*Optio waitForArtifacts.Add(len(artifactNameToOptions)) var artifactNameToArtifactId = map[string]string{} - errorsOccurred := []error{} + var errorsOccurred = new(multierror.Error) for artifactName, curOptions := range artifactNameToOptions { // The following is necessary to make sure artifactName and curOptions don't @@ -67,7 +67,7 @@ func BuildArtifactsE(t testing.TestingT, artifactNameToOptions map[string]*Optio artifactId, err := BuildArtifactE(t, curOptions) if err != nil { - errorsOccurred = append(errorsOccurred, err) + errorsOccurred = multierror.Append(errorsOccurred, err) } else { artifactNameToArtifactId[artifactName] = artifactId } @@ -76,7 +76,7 @@ func BuildArtifactsE(t testing.TestingT, artifactNameToOptions map[string]*Optio waitForArtifacts.Wait() - return artifactNameToArtifactId, customerrors.NewMultiError(errorsOccurred...) + return artifactNameToArtifactId, errorsOccurred.ErrorOrNil() } // BuildArtifact builds the given Packer template and return the generated Artifact ID. diff --git a/modules/ssh/ssh.go b/modules/ssh/ssh.go index 9d37b284c..dfbeafed4 100644 --- a/modules/ssh/ssh.go +++ b/modules/ssh/ssh.go @@ -12,10 +12,10 @@ import ( "strings" "time" - "github.com/gruntwork-io/terratest/modules/customerrors" "github.com/gruntwork-io/terratest/modules/files" "github.com/gruntwork-io/terratest/modules/logger" "github.com/gruntwork-io/terratest/modules/testing" + "github.com/hashicorp/go-multierror" "golang.org/x/crypto/ssh" "golang.org/x/crypto/ssh/agent" ) @@ -164,7 +164,7 @@ func ScpDirFromE(t testing.TestingT, options ScpDownloadOptions, useSudo bool) e } } - errorsOccurred := []error{} + var errorsOccurred = new(multierror.Error) for _, fullRemoteFilePath := range filesInDir { fileName := filepath.Base(fullRemoteFilePath) @@ -179,10 +179,10 @@ func ScpDirFromE(t testing.TestingT, options ScpDownloadOptions, useSudo bool) e logger.Logf(t, "Copying remote file: %s to local path %s", fullRemoteFilePath, localFilePath) err = copyFileFromRemote(t, sshSession, localFile, fullRemoteFilePath, useSudo) - errorsOccurred = append(errorsOccurred, err) + errorsOccurred = multierror.Append(errorsOccurred, err) } - return customerrors.NewMultiError(errorsOccurred...) + return errorsOccurred.ErrorOrNil() } // CheckSshConnection checks that you can connect via SSH to the given host and fail the test if the connection fails.