2016-05-14 23:00:58 +02:00
|
|
|
package random
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"testing"
|
|
|
|
|
|
|
|
"github.com/hashicorp/terraform/helper/resource"
|
|
|
|
"github.com/hashicorp/terraform/terraform"
|
|
|
|
)
|
|
|
|
|
2017-02-16 17:08:17 +01:00
|
|
|
type idLens struct {
|
|
|
|
b64Len int
|
|
|
|
b64UrlLen int
|
|
|
|
b64StdLen int
|
|
|
|
hexLen int
|
|
|
|
}
|
|
|
|
|
2016-05-14 23:00:58 +02:00
|
|
|
func TestAccResourceID(t *testing.T) {
|
|
|
|
resource.Test(t, resource.TestCase{
|
|
|
|
PreCheck: func() { testAccPreCheck(t) },
|
|
|
|
Providers: testAccProviders,
|
|
|
|
Steps: []resource.TestStep{
|
2016-11-06 23:57:15 +01:00
|
|
|
{
|
2016-05-14 23:00:58 +02:00
|
|
|
Config: testAccResourceIDConfig,
|
|
|
|
Check: resource.ComposeTestCheckFunc(
|
2017-02-16 17:08:17 +01:00
|
|
|
testAccResourceIDCheck("random_id.foo", &idLens{
|
|
|
|
b64Len: 6,
|
|
|
|
b64UrlLen: 6,
|
|
|
|
b64StdLen: 8,
|
|
|
|
hexLen: 8,
|
|
|
|
}),
|
|
|
|
testAccResourceIDCheck("random_id.bar", &idLens{
|
|
|
|
b64Len: 12,
|
|
|
|
b64UrlLen: 12,
|
|
|
|
b64StdLen: 14,
|
|
|
|
hexLen: 14,
|
|
|
|
}),
|
2016-05-14 23:00:58 +02:00
|
|
|
),
|
|
|
|
},
|
|
|
|
},
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2017-02-16 17:08:17 +01:00
|
|
|
func testAccResourceIDCheck(id string, want *idLens) resource.TestCheckFunc {
|
2016-05-14 23:00:58 +02:00
|
|
|
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")
|
|
|
|
}
|
|
|
|
|
|
|
|
b64Str := rs.Primary.Attributes["b64"]
|
2016-11-06 23:57:15 +01:00
|
|
|
b64UrlStr := rs.Primary.Attributes["b64_url"]
|
|
|
|
b64StdStr := rs.Primary.Attributes["b64_std"]
|
2016-05-14 23:00:58 +02:00
|
|
|
hexStr := rs.Primary.Attributes["hex"]
|
|
|
|
decStr := rs.Primary.Attributes["dec"]
|
|
|
|
|
2017-02-16 17:08:17 +01:00
|
|
|
if got, want := len(b64Str), want.b64Len; got != want {
|
2016-05-14 23:00:58 +02:00
|
|
|
return fmt.Errorf("base64 string length is %d; want %d", got, want)
|
|
|
|
}
|
2017-02-16 17:08:17 +01:00
|
|
|
if got, want := len(b64UrlStr), want.b64UrlLen; got != want {
|
2016-11-06 23:57:15 +01:00
|
|
|
return fmt.Errorf("base64 URL string length is %d; want %d", got, want)
|
|
|
|
}
|
2017-02-16 17:08:17 +01:00
|
|
|
if got, want := len(b64StdStr), want.b64StdLen; got != want {
|
2016-11-06 23:57:15 +01:00
|
|
|
return fmt.Errorf("base64 STD string length is %d; want %d", got, want)
|
|
|
|
}
|
2017-02-16 17:08:17 +01:00
|
|
|
if got, want := len(hexStr), want.hexLen; got != want {
|
2016-05-14 23:00:58 +02:00
|
|
|
return fmt.Errorf("hex string length is %d; want %d", got, want)
|
|
|
|
}
|
|
|
|
if len(decStr) < 1 {
|
|
|
|
return fmt.Errorf("decimal string is empty; want at least one digit")
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-02-16 17:08:17 +01:00
|
|
|
const (
|
|
|
|
testAccResourceIDConfig = `
|
2016-05-14 23:00:58 +02:00
|
|
|
resource "random_id" "foo" {
|
2017-02-16 17:08:17 +01:00
|
|
|
byte_length = 4
|
|
|
|
}
|
|
|
|
|
|
|
|
resource "random_id" "bar" {
|
|
|
|
byte_length = 4
|
|
|
|
prefix = "cloud-"
|
2016-05-14 23:00:58 +02:00
|
|
|
}
|
|
|
|
`
|
2017-02-16 17:08:17 +01:00
|
|
|
)
|