github.com/recobe182/terraform@v0.8.5-0.20170117231232-49ab22a935b7/builtin/providers/random/resource_id_test.go (about)

     1  package random
     2  
     3  import (
     4  	"fmt"
     5  	"testing"
     6  
     7  	"github.com/hashicorp/terraform/helper/resource"
     8  	"github.com/hashicorp/terraform/terraform"
     9  )
    10  
    11  func TestAccResourceID(t *testing.T) {
    12  	resource.Test(t, resource.TestCase{
    13  		PreCheck:  func() { testAccPreCheck(t) },
    14  		Providers: testAccProviders,
    15  		Steps: []resource.TestStep{
    16  			{
    17  				Config: testAccResourceIDConfig,
    18  				Check: resource.ComposeTestCheckFunc(
    19  					testAccResourceIDCheck("random_id.foo"),
    20  				),
    21  			},
    22  		},
    23  	})
    24  }
    25  
    26  func testAccResourceIDCheck(id string) resource.TestCheckFunc {
    27  	return func(s *terraform.State) error {
    28  		rs, ok := s.RootModule().Resources[id]
    29  		if !ok {
    30  			return fmt.Errorf("Not found: %s", id)
    31  		}
    32  		if rs.Primary.ID == "" {
    33  			return fmt.Errorf("No ID is set")
    34  		}
    35  
    36  		b64Str := rs.Primary.Attributes["b64"]
    37  		b64UrlStr := rs.Primary.Attributes["b64_url"]
    38  		b64StdStr := rs.Primary.Attributes["b64_std"]
    39  		hexStr := rs.Primary.Attributes["hex"]
    40  		decStr := rs.Primary.Attributes["dec"]
    41  
    42  		if got, want := len(b64Str), 6; got != want {
    43  			return fmt.Errorf("base64 string length is %d; want %d", got, want)
    44  		}
    45  		if got, want := len(b64UrlStr), 6; got != want {
    46  			return fmt.Errorf("base64 URL string length is %d; want %d", got, want)
    47  		}
    48  		if got, want := len(b64StdStr), 8; got != want {
    49  			return fmt.Errorf("base64 STD string length is %d; want %d", got, want)
    50  		}
    51  		if got, want := len(hexStr), 8; got != want {
    52  			return fmt.Errorf("hex string length is %d; want %d", got, want)
    53  		}
    54  		if len(decStr) < 1 {
    55  			return fmt.Errorf("decimal string is empty; want at least one digit")
    56  		}
    57  
    58  		return nil
    59  	}
    60  }
    61  
    62  const testAccResourceIDConfig = `
    63  resource "random_id" "foo" {
    64      byte_length = 4
    65  }
    66  `