github.com/joshpmcghee/terragrunt@v0.1.3/test/integration_test.go (about)

     1  package test
     2  
     3  import (
     4  	"fmt"
     5  	"os"
     6  	"os/exec"
     7  	"strings"
     8  	"testing"
     9  
    10  	"github.com/aws/aws-sdk-go/aws"
    11  	"github.com/aws/aws-sdk-go/aws/defaults"
    12  	"github.com/aws/aws-sdk-go/aws/session"
    13  	"github.com/aws/aws-sdk-go/service/s3"
    14  	"github.com/gruntwork-io/terragrunt/cli"
    15  )
    16  
    17  // hard-code this to match the test fixture for now
    18  const (
    19  	TERRAFORM_REMOTE_STATE_S3_REGION      = "us-west-2"
    20  	TERRAFORM_REMOTE_STATE_S3_BUCKET_NAME = "gruntwork-terragrunt-tests"
    21  	TEST_FIXTURE_PATH                     = "fixture/"
    22  )
    23  
    24  func TestTerragruntWorksWithLocalTerraformVersion(t *testing.T) {
    25  	if err := validateTerraformIsInstalled(t); err != nil {
    26  		t.Fatalf("A local instance of Terraform is not in the system PATH. Please install Terraform to continue.\n")
    27  	}
    28  
    29  	if err := validateS3BucketExists(t, TERRAFORM_REMOTE_STATE_S3_REGION, TERRAFORM_REMOTE_STATE_S3_BUCKET_NAME); err != nil {
    30  		t.Fatalf("The S3 Bucket in the .terragrunt file does not exist. %s", err)
    31  	}
    32  
    33  	if err := runTerragruntApply(); err != nil {
    34  		t.Fatalf("Failed to run terragrunt: %s", err)
    35  	}
    36  }
    37  
    38  // Run Terragrunt Apply directory in the test fixture path
    39  func runTerragruntApply() error {
    40  	os.Chdir(TEST_FIXTURE_PATH)
    41  
    42  	app := cli.CreateTerragruntCli("TEST")
    43  
    44  	return app.Run(strings.Split("terragrunt apply", " "))
    45  }
    46  
    47  // Validate that a local instance of Terraform is installed.
    48  func validateTerraformIsInstalled(t *testing.T) error {
    49  	if err := runShellCommandWithNoOutput(t, "terraform", "version"); err != nil {
    50  		return err
    51  	}
    52  
    53  	return nil
    54  }
    55  
    56  // Run the given "command" plus any "args" passed to it. Print the output to stdout.
    57  func runShellCommandWithOutput(t *testing.T, command string, args ...string) error {
    58  	cmd := exec.Command(command, args...)
    59  
    60  	cmd.Stdout = os.Stdout
    61  	cmd.Stderr = os.Stderr
    62  
    63  	return cmd.Run()
    64  }
    65  
    66  // Run the given "command" plus any "args" passed to it. Suppress output.
    67  func runShellCommandWithNoOutput(t *testing.T, command string, args ...string) error {
    68  	cmd := exec.Command(command, args...)
    69  
    70  	cmd.Stdout = nil
    71  	cmd.Stderr = os.Stderr
    72  
    73  	return cmd.Run()
    74  }
    75  
    76  // Check that the S3 Bucket of the given name and region exists by attempting to list objects from it.
    77  func validateS3BucketExists(t *testing.T, awsRegion string, bucketName string) error {
    78  	client, err := createS3Client(awsRegion)
    79  	if err != nil {
    80  		return err
    81  	}
    82  
    83  	params := &s3.ListObjectsInput{
    84  		Bucket: aws.String(bucketName),
    85  	}
    86  
    87  	_, err = client.ListObjects(params)
    88  	if err != nil {
    89  		return fmt.Errorf("S3 Bucket Name = '%s'. S3 Bucket Region = '%s'. Full Error Message = %s", bucketName, awsRegion, err)
    90  	}
    91  
    92  	return nil
    93  }
    94  
    95  // Create an authenticated client for S3
    96  func createS3Client(awsRegion string) (*s3.S3, error) {
    97  	config, err := createAwsConfig(awsRegion)
    98  	if err != nil {
    99  		return nil, err
   100  	}
   101  
   102  	return s3.New(session.New(), config), nil
   103  }
   104  
   105  // Returns an AWS config object for the given region, ensuring that the config has credentials
   106  func createAwsConfig(awsRegion string) (*aws.Config, error) {
   107  	config := defaults.Get().Config.WithRegion(awsRegion)
   108  
   109  	_, err := config.Credentials.Get()
   110  	if err != nil {
   111  		return nil, fmt.Errorf("Error finding AWS credentials (did you set the AWS_ACCESS_KEY_ID and AWS_SECRET_ACCESS_KEY environment variables?): %s", err)
   112  	}
   113  
   114  	return config, nil
   115  }