github.com/danp/terraform@v0.9.5-0.20170426144147-39d740081351/builtin/providers/aws/resource_aws_lightsail_instance_test.go (about) 1 package aws 2 3 import ( 4 "errors" 5 "fmt" 6 "testing" 7 "time" 8 9 "github.com/aws/aws-sdk-go/aws" 10 "github.com/aws/aws-sdk-go/aws/awserr" 11 "github.com/aws/aws-sdk-go/service/lightsail" 12 "github.com/hashicorp/terraform/helper/acctest" 13 "github.com/hashicorp/terraform/helper/resource" 14 "github.com/hashicorp/terraform/terraform" 15 ) 16 17 func TestAccAWSLightsailInstance_basic(t *testing.T) { 18 var conf lightsail.Instance 19 lightsailName := fmt.Sprintf("tf-test-lightsail-%d", acctest.RandInt()) 20 21 resource.Test(t, resource.TestCase{ 22 PreCheck: func() { testAccPreCheck(t) }, 23 IDRefreshName: "aws_lightsail_instance.lightsail_instance_test", 24 Providers: testAccProviders, 25 CheckDestroy: testAccCheckAWSLightsailInstanceDestroy, 26 Steps: []resource.TestStep{ 27 { 28 Config: testAccAWSLightsailInstanceConfig_basic(lightsailName), 29 Check: resource.ComposeAggregateTestCheckFunc( 30 testAccCheckAWSLightsailInstanceExists("aws_lightsail_instance.lightsail_instance_test", &conf), 31 resource.TestCheckResourceAttrSet("aws_lightsail_instance.lightsail_instance_test", "availability_zone"), 32 resource.TestCheckResourceAttrSet("aws_lightsail_instance.lightsail_instance_test", "blueprint_id"), 33 resource.TestCheckResourceAttrSet("aws_lightsail_instance.lightsail_instance_test", "bundle_id"), 34 resource.TestCheckResourceAttrSet("aws_lightsail_instance.lightsail_instance_test", "key_pair_name"), 35 ), 36 }, 37 }, 38 }) 39 } 40 41 func TestAccAWSLightsailInstance_disapear(t *testing.T) { 42 var conf lightsail.Instance 43 lightsailName := fmt.Sprintf("tf-test-lightsail-%d", acctest.RandInt()) 44 45 testDestroy := func(*terraform.State) error { 46 // reach out and DELETE the Instance 47 conn := testAccProvider.Meta().(*AWSClient).lightsailconn 48 _, err := conn.DeleteInstance(&lightsail.DeleteInstanceInput{ 49 InstanceName: aws.String(lightsailName), 50 }) 51 52 if err != nil { 53 return fmt.Errorf("Error deleting Lightsail Instance in disapear test") 54 } 55 56 // sleep 7 seconds to give it time, so we don't have to poll 57 time.Sleep(7 * time.Second) 58 59 return nil 60 } 61 62 resource.Test(t, resource.TestCase{ 63 PreCheck: func() { testAccPreCheck(t) }, 64 Providers: testAccProviders, 65 CheckDestroy: testAccCheckAWSLightsailInstanceDestroy, 66 Steps: []resource.TestStep{ 67 { 68 Config: testAccAWSLightsailInstanceConfig_basic(lightsailName), 69 Check: resource.ComposeTestCheckFunc( 70 testAccCheckAWSLightsailInstanceExists("aws_lightsail_instance.lightsail_instance_test", &conf), 71 testDestroy, 72 ), 73 ExpectNonEmptyPlan: true, 74 }, 75 }, 76 }) 77 } 78 79 func testAccCheckAWSLightsailInstanceExists(n string, res *lightsail.Instance) resource.TestCheckFunc { 80 return func(s *terraform.State) error { 81 rs, ok := s.RootModule().Resources[n] 82 if !ok { 83 return fmt.Errorf("Not found: %s", n) 84 } 85 86 if rs.Primary.ID == "" { 87 return errors.New("No LightsailInstance ID is set") 88 } 89 90 conn := testAccProvider.Meta().(*AWSClient).lightsailconn 91 92 respInstance, err := conn.GetInstance(&lightsail.GetInstanceInput{ 93 InstanceName: aws.String(rs.Primary.Attributes["name"]), 94 }) 95 96 if err != nil { 97 return err 98 } 99 100 if respInstance == nil || respInstance.Instance == nil { 101 return fmt.Errorf("Instance (%s) not found", rs.Primary.Attributes["name"]) 102 } 103 *res = *respInstance.Instance 104 return nil 105 } 106 } 107 108 func testAccCheckAWSLightsailInstanceDestroy(s *terraform.State) error { 109 110 for _, rs := range s.RootModule().Resources { 111 if rs.Type != "aws_lightsail_instance" { 112 continue 113 } 114 115 conn := testAccProvider.Meta().(*AWSClient).lightsailconn 116 117 respInstance, err := conn.GetInstance(&lightsail.GetInstanceInput{ 118 InstanceName: aws.String(rs.Primary.Attributes["name"]), 119 }) 120 121 if err == nil { 122 if respInstance.Instance != nil { 123 return fmt.Errorf("LightsailInstance %q still exists", rs.Primary.ID) 124 } 125 } 126 127 // Verify the error 128 if awsErr, ok := err.(awserr.Error); ok { 129 if awsErr.Code() == "NotFoundException" { 130 return nil 131 } 132 } 133 return err 134 } 135 136 return nil 137 } 138 139 func testAccAWSLightsailInstanceConfig_basic(lightsailName string) string { 140 return fmt.Sprintf(` 141 provider "aws" { 142 region = "us-east-1" 143 } 144 resource "aws_lightsail_instance" "lightsail_instance_test" { 145 name = "%s" 146 availability_zone = "us-east-1b" 147 blueprint_id = "gitlab_8_12_6" 148 bundle_id = "nano_1_0" 149 } 150 `, lightsailName) 151 }