github.com/danp/terraform@v0.9.5-0.20170426144147-39d740081351/builtin/providers/random/resource_pet_test.go (about) 1 package random 2 3 import ( 4 "fmt" 5 "regexp" 6 "strings" 7 "testing" 8 9 "github.com/hashicorp/terraform/helper/resource" 10 "github.com/hashicorp/terraform/terraform" 11 ) 12 13 func TestAccResourcePet_basic(t *testing.T) { 14 resource.Test(t, resource.TestCase{ 15 PreCheck: func() { testAccPreCheck(t) }, 16 Providers: testAccProviders, 17 Steps: []resource.TestStep{ 18 { 19 Config: testAccResourcePet_basic, 20 Check: resource.ComposeTestCheckFunc( 21 testAccResourcePetLength("random_pet.pet_1", "-", 2), 22 ), 23 }, 24 }, 25 }) 26 } 27 28 func TestAccResourcePet_length(t *testing.T) { 29 resource.Test(t, resource.TestCase{ 30 PreCheck: func() { testAccPreCheck(t) }, 31 Providers: testAccProviders, 32 Steps: []resource.TestStep{ 33 { 34 Config: testAccResourcePet_length, 35 Check: resource.ComposeTestCheckFunc( 36 testAccResourcePetLength("random_pet.pet_1", "-", 4), 37 ), 38 }, 39 }, 40 }) 41 } 42 43 func TestAccResourcePet_prefix(t *testing.T) { 44 resource.Test(t, resource.TestCase{ 45 PreCheck: func() { testAccPreCheck(t) }, 46 Providers: testAccProviders, 47 Steps: []resource.TestStep{ 48 { 49 Config: testAccResourcePet_prefix, 50 Check: resource.ComposeTestCheckFunc( 51 testAccResourcePetLength("random_pet.pet_1", "-", 3), 52 resource.TestMatchResourceAttr( 53 "random_pet.pet_1", "id", regexp.MustCompile("^consul-")), 54 ), 55 }, 56 }, 57 }) 58 } 59 60 func TestAccResourcePet_separator(t *testing.T) { 61 resource.Test(t, resource.TestCase{ 62 PreCheck: func() { testAccPreCheck(t) }, 63 Providers: testAccProviders, 64 Steps: []resource.TestStep{ 65 { 66 Config: testAccResourcePet_separator, 67 Check: resource.ComposeTestCheckFunc( 68 testAccResourcePetLength("random_pet.pet_1", "_", 3), 69 ), 70 }, 71 }, 72 }) 73 } 74 75 func testAccResourcePetLength(id string, separator string, length int) resource.TestCheckFunc { 76 return func(s *terraform.State) error { 77 rs, ok := s.RootModule().Resources[id] 78 if !ok { 79 return fmt.Errorf("Not found: %s", id) 80 } 81 if rs.Primary.ID == "" { 82 return fmt.Errorf("No ID is set") 83 } 84 85 petParts := strings.Split(rs.Primary.ID, separator) 86 if len(petParts) != length { 87 return fmt.Errorf("Length does not match") 88 } 89 90 return nil 91 } 92 } 93 94 const testAccResourcePet_basic = ` 95 resource "random_pet" "pet_1" { 96 } 97 ` 98 99 const testAccResourcePet_length = ` 100 resource "random_pet" "pet_1" { 101 length = 4 102 } 103 ` 104 const testAccResourcePet_prefix = ` 105 resource "random_pet" "pet_1" { 106 prefix = "consul" 107 } 108 ` 109 110 const testAccResourcePet_separator = ` 111 resource "random_pet" "pet_1" { 112 length = 3 113 separator = "_" 114 } 115 `