github.com/mohanarpit/terraform@v0.6.16-0.20160909104007-291f29853544/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  			resource.TestStep{
    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  		hexStr := rs.Primary.Attributes["hex"]
    38  		decStr := rs.Primary.Attributes["dec"]
    39  
    40  		if got, want := len(b64Str), 6; got != want {
    41  			return fmt.Errorf("base64 string length is %d; want %d", got, want)
    42  		}
    43  		if got, want := len(hexStr), 8; got != want {
    44  			return fmt.Errorf("hex string length is %d; want %d", got, want)
    45  		}
    46  		if len(decStr) < 1 {
    47  			return fmt.Errorf("decimal string is empty; want at least one digit")
    48  		}
    49  
    50  		return nil
    51  	}
    52  }
    53  
    54  const testAccResourceIDConfig = `
    55  resource "random_id" "foo" {
    56      byte_length = 4
    57  }
    58  `