mirror of
https://github.com/opentofu/opentofu.git
synced 2025-01-09 07:33:58 -06:00
eec6c88fd2
random_shuffle takes a list of strings and returns a new list with the same items in a random permutation. Optionally allows the result list to be a different length than the input list. A shorter result than input results in some items being excluded. A longer result than input results in some items being repeated, but never more often than the number of input items.
92 lines
2.4 KiB
Go
92 lines
2.4 KiB
Go
package random
|
|
|
|
import (
|
|
"fmt"
|
|
"strconv"
|
|
"testing"
|
|
|
|
"github.com/hashicorp/terraform/helper/resource"
|
|
"github.com/hashicorp/terraform/terraform"
|
|
)
|
|
|
|
func TestAccResourceShuffle(t *testing.T) {
|
|
resource.Test(t, resource.TestCase{
|
|
PreCheck: func() { testAccPreCheck(t) },
|
|
Providers: testAccProviders,
|
|
Steps: []resource.TestStep{
|
|
resource.TestStep{
|
|
Config: testAccResourceShuffleConfig,
|
|
Check: resource.ComposeTestCheckFunc(
|
|
// These results are current as of Go 1.6. The Go
|
|
// "rand" package does not guarantee that the random
|
|
// number generator will generate the same results
|
|
// forever, but the maintainers endeavor not to change
|
|
// it gratuitously.
|
|
// These tests allow us to detect such changes and
|
|
// document them when they arise, but the docs for this
|
|
// resource specifically warn that results are not
|
|
// guaranteed consistent across Terraform releases.
|
|
testAccResourceShuffleCheck(
|
|
"random_shuffle.default_length",
|
|
[]string{"a", "c", "b", "e", "d"},
|
|
),
|
|
testAccResourceShuffleCheck(
|
|
"random_shuffle.shorter_length",
|
|
[]string{"a", "c", "b"},
|
|
),
|
|
testAccResourceShuffleCheck(
|
|
"random_shuffle.longer_length",
|
|
[]string{"a", "c", "b", "e", "d", "a", "e", "d", "c", "b", "a", "b"},
|
|
),
|
|
),
|
|
},
|
|
},
|
|
})
|
|
}
|
|
|
|
func testAccResourceShuffleCheck(id string, wants []string) resource.TestCheckFunc {
|
|
return func(s *terraform.State) error {
|
|
rs, ok := s.RootModule().Resources[id]
|
|
if !ok {
|
|
return fmt.Errorf("Not found: %s", id)
|
|
}
|
|
if rs.Primary.ID == "" {
|
|
return fmt.Errorf("No ID is set")
|
|
}
|
|
|
|
attrs := rs.Primary.Attributes
|
|
|
|
gotLen := attrs["result.#"]
|
|
wantLen := strconv.Itoa(len(wants))
|
|
if gotLen != wantLen {
|
|
return fmt.Errorf("got %s result items; want %s", gotLen, wantLen)
|
|
}
|
|
|
|
for i, want := range wants {
|
|
key := fmt.Sprintf("result.%d", i)
|
|
if got := attrs[key]; got != want {
|
|
return fmt.Errorf("index %d is %q; want %q", i, got, want)
|
|
}
|
|
}
|
|
|
|
return nil
|
|
}
|
|
}
|
|
|
|
const testAccResourceShuffleConfig = `
|
|
resource "random_shuffle" "default_length" {
|
|
input = ["a", "b", "c", "d", "e"]
|
|
seed = "-"
|
|
}
|
|
resource "random_shuffle" "shorter_length" {
|
|
input = ["a", "b", "c", "d", "e"]
|
|
seed = "-"
|
|
result_count = 3
|
|
}
|
|
resource "random_shuffle" "longer_length" {
|
|
input = ["a", "b", "c", "d", "e"]
|
|
seed = "-"
|
|
result_count = 12
|
|
}
|
|
`
|