github.com/mohanarpit/terraform@v0.6.16-0.20160909104007-291f29853544/builtin/providers/random/resource_shuffle_test.go (about) 1 package random 2 3 import ( 4 "fmt" 5 "strconv" 6 "testing" 7 8 "github.com/hashicorp/terraform/helper/resource" 9 "github.com/hashicorp/terraform/terraform" 10 ) 11 12 func TestAccResourceShuffle(t *testing.T) { 13 resource.Test(t, resource.TestCase{ 14 PreCheck: func() { testAccPreCheck(t) }, 15 Providers: testAccProviders, 16 Steps: []resource.TestStep{ 17 resource.TestStep{ 18 Config: testAccResourceShuffleConfig, 19 Check: resource.ComposeTestCheckFunc( 20 // These results are current as of Go 1.6. The Go 21 // "rand" package does not guarantee that the random 22 // number generator will generate the same results 23 // forever, but the maintainers endeavor not to change 24 // it gratuitously. 25 // These tests allow us to detect such changes and 26 // document them when they arise, but the docs for this 27 // resource specifically warn that results are not 28 // guaranteed consistent across Terraform releases. 29 testAccResourceShuffleCheck( 30 "random_shuffle.default_length", 31 []string{"a", "c", "b", "e", "d"}, 32 ), 33 testAccResourceShuffleCheck( 34 "random_shuffle.shorter_length", 35 []string{"a", "c", "b"}, 36 ), 37 testAccResourceShuffleCheck( 38 "random_shuffle.longer_length", 39 []string{"a", "c", "b", "e", "d", "a", "e", "d", "c", "b", "a", "b"}, 40 ), 41 ), 42 }, 43 }, 44 }) 45 } 46 47 func testAccResourceShuffleCheck(id string, wants []string) resource.TestCheckFunc { 48 return func(s *terraform.State) error { 49 rs, ok := s.RootModule().Resources[id] 50 if !ok { 51 return fmt.Errorf("Not found: %s", id) 52 } 53 if rs.Primary.ID == "" { 54 return fmt.Errorf("No ID is set") 55 } 56 57 attrs := rs.Primary.Attributes 58 59 gotLen := attrs["result.#"] 60 wantLen := strconv.Itoa(len(wants)) 61 if gotLen != wantLen { 62 return fmt.Errorf("got %s result items; want %s", gotLen, wantLen) 63 } 64 65 for i, want := range wants { 66 key := fmt.Sprintf("result.%d", i) 67 if got := attrs[key]; got != want { 68 return fmt.Errorf("index %d is %q; want %q", i, got, want) 69 } 70 } 71 72 return nil 73 } 74 } 75 76 const testAccResourceShuffleConfig = ` 77 resource "random_shuffle" "default_length" { 78 input = ["a", "b", "c", "d", "e"] 79 seed = "-" 80 } 81 resource "random_shuffle" "shorter_length" { 82 input = ["a", "b", "c", "d", "e"] 83 seed = "-" 84 result_count = 3 85 } 86 resource "random_shuffle" "longer_length" { 87 input = ["a", "b", "c", "d", "e"] 88 seed = "-" 89 result_count = 12 90 } 91 `