github.com/vtorhonen/terraform@v0.9.0-beta2.0.20170307220345-5d894e4ffda7/builtin/providers/aws/resource_aws_sfn_activity_test.go (about)

     1  package aws
     2  
     3  import (
     4  	"fmt"
     5  	"testing"
     6  
     7  	"github.com/aws/aws-sdk-go/aws"
     8  	"github.com/aws/aws-sdk-go/aws/awserr"
     9  	"github.com/aws/aws-sdk-go/service/sfn"
    10  	"github.com/hashicorp/terraform/helper/acctest"
    11  	"github.com/hashicorp/terraform/helper/resource"
    12  	"github.com/hashicorp/terraform/terraform"
    13  	"time"
    14  )
    15  
    16  func TestAccAWSSfnActivity_basic(t *testing.T) {
    17  	name := acctest.RandString(10)
    18  
    19  	resource.Test(t, resource.TestCase{
    20  		PreCheck:     func() { testAccPreCheck(t) },
    21  		Providers:    testAccProviders,
    22  		CheckDestroy: testAccCheckAWSSfnActivityDestroy,
    23  		Steps: []resource.TestStep{
    24  			{
    25  				Config: testAccAWSSfnActivityBasicConfig(name),
    26  				Check: resource.ComposeTestCheckFunc(
    27  					testAccCheckAWSSfnActivityExists("aws_sfn_activity.foo"),
    28  					resource.TestCheckResourceAttr("aws_sfn_activity.foo", "name", name),
    29  					resource.TestCheckResourceAttrSet("aws_sfn_activity.foo", "creation_date"),
    30  				),
    31  			},
    32  		},
    33  	})
    34  }
    35  
    36  func testAccCheckAWSSfnActivityExists(n string) resource.TestCheckFunc {
    37  	return func(s *terraform.State) error {
    38  		rs, ok := s.RootModule().Resources[n]
    39  		if !ok {
    40  			return fmt.Errorf("Not found: %s", n)
    41  		}
    42  
    43  		if rs.Primary.ID == "" {
    44  			return fmt.Errorf("No Step Function ID set")
    45  		}
    46  
    47  		conn := testAccProvider.Meta().(*AWSClient).sfnconn
    48  
    49  		_, err := conn.DescribeActivity(&sfn.DescribeActivityInput{
    50  			ActivityArn: aws.String(rs.Primary.ID),
    51  		})
    52  
    53  		if err != nil {
    54  			return err
    55  		}
    56  
    57  		return nil
    58  	}
    59  }
    60  
    61  func testAccCheckAWSSfnActivityDestroy(s *terraform.State) error {
    62  	conn := testAccProvider.Meta().(*AWSClient).sfnconn
    63  
    64  	for _, rs := range s.RootModule().Resources {
    65  		if rs.Type != "aws_sfn_activity" {
    66  			continue
    67  		}
    68  
    69  		// Retrying as Read after Delete is not always consistent
    70  		retryErr := resource.Retry(1*time.Minute, func() *resource.RetryError {
    71  			var err error
    72  
    73  			_, err = conn.DescribeActivity(&sfn.DescribeActivityInput{
    74  				ActivityArn: aws.String(rs.Primary.ID),
    75  			})
    76  
    77  			if err != nil {
    78  				if awsErr, ok := err.(awserr.Error); ok && awsErr.Code() == "ActivityDoesNotExist" {
    79  					return nil
    80  				}
    81  
    82  				return resource.NonRetryableError(err)
    83  			}
    84  
    85  			// If there are no errors, the removal failed
    86  			// and the object is not yet removed.
    87  			return resource.RetryableError(fmt.Errorf("Expected AWS Step Function Activity to be destroyed, but was still found, retrying"))
    88  		})
    89  
    90  		if retryErr != nil {
    91  			return retryErr
    92  		}
    93  
    94  		return nil
    95  	}
    96  
    97  	return fmt.Errorf("Default error in Step Function Test")
    98  }
    99  
   100  func testAccAWSSfnActivityBasicConfig(rName string) string {
   101  	return fmt.Sprintf(`
   102  resource "aws_sfn_activity" "foo" {
   103    name = "%s"
   104  }
   105  `, rName)
   106  }