/azurerm/internal/acceptance/data.go

https://github.com/terraform-providers/terraform-provider-azurerm · Go · 127 lines · 78 code · 26 blank · 23 comment · 12 complexity · fc49cf0b8a07533cf78eff2e916dfa9b MD5 · raw file

  1. package acceptance
  2. import (
  3. "fmt"
  4. "math"
  5. "os"
  6. "strconv"
  7. "testing"
  8. "github.com/Azure/go-autorest/autorest/azure"
  9. "github.com/hashicorp/terraform-plugin-sdk/helper/acctest"
  10. "github.com/terraform-providers/terraform-provider-azuread/azuread/helpers/tf"
  11. "github.com/terraform-providers/terraform-provider-azurerm/azurerm/internal/features"
  12. )
  13. func init() {
  14. // unit testing
  15. if os.Getenv("TF_ACC") == "" {
  16. return
  17. }
  18. EnsureProvidersAreInitialised()
  19. }
  20. type TestData struct {
  21. // Locations is a set of Azure Regions which should be used for this Test
  22. Locations Regions
  23. // RandomInteger is a random integer which is unique to this test case
  24. RandomInteger int
  25. // RandomString is a random 5 character string is unique to this test case
  26. RandomString string
  27. // ResourceName is the fully qualified resource name, comprising of the
  28. // resource type and then the resource label
  29. // e.g. `azurerm_resource_group.test`
  30. ResourceName string
  31. // ResourceType is the Terraform Resource Type - `azurerm_resource_group`
  32. ResourceType string
  33. // Environment is a struct containing Details about the Azure Environment
  34. // that we're running against
  35. Environment azure.Environment
  36. // EnvironmentName is the name of the Azure Environment where we're running
  37. EnvironmentName string
  38. // MetadataURL is the url of the endpoint where the environment is obtained
  39. MetadataURL string
  40. // resourceLabel is the local used for the resource - generally "test""
  41. resourceLabel string
  42. }
  43. // BuildTestData generates some test data for the given resource
  44. func BuildTestData(t *testing.T, resourceType string, resourceLabel string) TestData {
  45. EnsureProvidersAreInitialised()
  46. env, err := Environment()
  47. if err != nil {
  48. t.Fatalf("Error retrieving Environment: %+v", err)
  49. }
  50. testData := TestData{
  51. RandomInteger: tf.AccRandTimeInt(),
  52. RandomString: acctest.RandString(5),
  53. ResourceName: fmt.Sprintf("%s.%s", resourceType, resourceLabel),
  54. Environment: *env,
  55. EnvironmentName: EnvironmentName(),
  56. MetadataURL: os.Getenv("ARM_METADATA_HOST"),
  57. ResourceType: resourceType,
  58. resourceLabel: resourceLabel,
  59. }
  60. if features.UseDynamicTestLocations() {
  61. testData.Locations = availableLocations()
  62. } else {
  63. testData.Locations = Regions{
  64. Primary: os.Getenv("ARM_TEST_LOCATION"),
  65. Secondary: os.Getenv("ARM_TEST_LOCATION_ALT"),
  66. Ternary: os.Getenv("ARM_TEST_LOCATION_ALT2"),
  67. }
  68. }
  69. return testData
  70. }
  71. // RandomIntOfLength is a random 8 to 18 digit integer which is unique to this test case
  72. func (td *TestData) RandomIntOfLength(len int) int {
  73. // len should not be
  74. // - greater then 18, longest a int can represent
  75. // - less then 8, as that gives us YYMMDDRR
  76. if 8 > len || len > 18 {
  77. panic("Invalid Test: RandomIntOfLength: len is not between 8 or 18 inclusive")
  78. }
  79. // 18 - just return the int
  80. if len >= 18 {
  81. return td.RandomInteger
  82. }
  83. // 16-17 just strip off the last 1-2 digits
  84. if len >= 16 {
  85. return td.RandomInteger / int(math.Pow10(18-len))
  86. }
  87. // 8-15 keep len - 2 digits and add 2 characters of randomness on
  88. s := strconv.Itoa(td.RandomInteger)
  89. r := s[16:18]
  90. v := s[0 : len-2]
  91. i, _ := strconv.Atoi(v + r)
  92. return i
  93. }
  94. // RandomStringOfLength is a random 1 to 1024 character string which is unique to this test case
  95. func (td *TestData) RandomStringOfLength(len int) string {
  96. // len should not be less then 1 or greater than 1024
  97. if 1 > len || len > 1024 {
  98. panic("Invalid Test: RandomStringOfLength: length argument must be between 1 and 1024 characters")
  99. }
  100. return acctest.RandString(len)
  101. }