PageRenderTime 76ms CodeModel.GetById 23ms RepoModel.GetById 1ms app.codeStats 0ms

/builtin/providers/aws/resource_aws_db_parameter_group_test.go

https://gitlab.com/biopandemic/terraform
Go | 302 lines | 267 code | 33 blank | 2 comment | 29 complexity | 0a567b77dc8b832c120c670a247c280c MD5 | raw file
Possible License(s): MPL-2.0-no-copyleft-exception
  1. package aws
  2. import (
  3. "fmt"
  4. "math/rand"
  5. "testing"
  6. "time"
  7. "github.com/aws/aws-sdk-go/aws"
  8. "github.com/aws/aws-sdk-go/aws/awserr"
  9. "github.com/aws/aws-sdk-go/service/rds"
  10. "github.com/hashicorp/terraform/helper/resource"
  11. "github.com/hashicorp/terraform/terraform"
  12. )
  13. func TestAccAWSDBParameterGroup_basic(t *testing.T) {
  14. var v rds.DBParameterGroup
  15. resource.Test(t, resource.TestCase{
  16. PreCheck: func() { testAccPreCheck(t) },
  17. Providers: testAccProviders,
  18. CheckDestroy: testAccCheckAWSDBParameterGroupDestroy,
  19. Steps: []resource.TestStep{
  20. resource.TestStep{
  21. Config: testAccAWSDBParameterGroupConfig,
  22. Check: resource.ComposeTestCheckFunc(
  23. testAccCheckAWSDBParameterGroupExists("aws_db_parameter_group.bar", &v),
  24. testAccCheckAWSDBParameterGroupAttributes(&v),
  25. resource.TestCheckResourceAttr(
  26. "aws_db_parameter_group.bar", "name", "parameter-group-test-terraform"),
  27. resource.TestCheckResourceAttr(
  28. "aws_db_parameter_group.bar", "family", "mysql5.6"),
  29. resource.TestCheckResourceAttr(
  30. "aws_db_parameter_group.bar", "description", "Test parameter group for terraform"),
  31. resource.TestCheckResourceAttr(
  32. "aws_db_parameter_group.bar", "parameter.1708034931.name", "character_set_results"),
  33. resource.TestCheckResourceAttr(
  34. "aws_db_parameter_group.bar", "parameter.1708034931.value", "utf8"),
  35. resource.TestCheckResourceAttr(
  36. "aws_db_parameter_group.bar", "parameter.2421266705.name", "character_set_server"),
  37. resource.TestCheckResourceAttr(
  38. "aws_db_parameter_group.bar", "parameter.2421266705.value", "utf8"),
  39. resource.TestCheckResourceAttr(
  40. "aws_db_parameter_group.bar", "parameter.2478663599.name", "character_set_client"),
  41. resource.TestCheckResourceAttr(
  42. "aws_db_parameter_group.bar", "parameter.2478663599.value", "utf8"),
  43. ),
  44. },
  45. resource.TestStep{
  46. Config: testAccAWSDBParameterGroupAddParametersConfig,
  47. Check: resource.ComposeTestCheckFunc(
  48. testAccCheckAWSDBParameterGroupExists("aws_db_parameter_group.bar", &v),
  49. testAccCheckAWSDBParameterGroupAttributes(&v),
  50. resource.TestCheckResourceAttr(
  51. "aws_db_parameter_group.bar", "name", "parameter-group-test-terraform"),
  52. resource.TestCheckResourceAttr(
  53. "aws_db_parameter_group.bar", "family", "mysql5.6"),
  54. resource.TestCheckResourceAttr(
  55. "aws_db_parameter_group.bar", "description", "Test parameter group for terraform"),
  56. resource.TestCheckResourceAttr(
  57. "aws_db_parameter_group.bar", "parameter.1706463059.name", "collation_connection"),
  58. resource.TestCheckResourceAttr(
  59. "aws_db_parameter_group.bar", "parameter.1706463059.value", "utf8_unicode_ci"),
  60. resource.TestCheckResourceAttr(
  61. "aws_db_parameter_group.bar", "parameter.1708034931.name", "character_set_results"),
  62. resource.TestCheckResourceAttr(
  63. "aws_db_parameter_group.bar", "parameter.1708034931.value", "utf8"),
  64. resource.TestCheckResourceAttr(
  65. "aws_db_parameter_group.bar", "parameter.2421266705.name", "character_set_server"),
  66. resource.TestCheckResourceAttr(
  67. "aws_db_parameter_group.bar", "parameter.2421266705.value", "utf8"),
  68. resource.TestCheckResourceAttr(
  69. "aws_db_parameter_group.bar", "parameter.2475805061.name", "collation_server"),
  70. resource.TestCheckResourceAttr(
  71. "aws_db_parameter_group.bar", "parameter.2475805061.value", "utf8_unicode_ci"),
  72. resource.TestCheckResourceAttr(
  73. "aws_db_parameter_group.bar", "parameter.2478663599.name", "character_set_client"),
  74. resource.TestCheckResourceAttr(
  75. "aws_db_parameter_group.bar", "parameter.2478663599.value", "utf8"),
  76. ),
  77. },
  78. },
  79. })
  80. }
  81. func TestAccAWSDBParameterGroupOnly(t *testing.T) {
  82. var v rds.DBParameterGroup
  83. resource.Test(t, resource.TestCase{
  84. PreCheck: func() { testAccPreCheck(t) },
  85. Providers: testAccProviders,
  86. CheckDestroy: testAccCheckAWSDBParameterGroupDestroy,
  87. Steps: []resource.TestStep{
  88. resource.TestStep{
  89. Config: testAccAWSDBParameterGroupOnlyConfig,
  90. Check: resource.ComposeTestCheckFunc(
  91. testAccCheckAWSDBParameterGroupExists("aws_db_parameter_group.bar", &v),
  92. testAccCheckAWSDBParameterGroupAttributes(&v),
  93. resource.TestCheckResourceAttr(
  94. "aws_db_parameter_group.bar", "name", "parameter-group-test-terraform"),
  95. resource.TestCheckResourceAttr(
  96. "aws_db_parameter_group.bar", "family", "mysql5.6"),
  97. resource.TestCheckResourceAttr(
  98. "aws_db_parameter_group.bar", "description", "Test parameter group for terraform"),
  99. ),
  100. },
  101. },
  102. })
  103. }
  104. func TestResourceAWSDBParameterGroupName_validation(t *testing.T) {
  105. cases := []struct {
  106. Value string
  107. ErrCount int
  108. }{
  109. {
  110. Value: "tEsting123",
  111. ErrCount: 1,
  112. },
  113. {
  114. Value: "testing123!",
  115. ErrCount: 1,
  116. },
  117. {
  118. Value: "1testing123",
  119. ErrCount: 1,
  120. },
  121. {
  122. Value: "testing--123",
  123. ErrCount: 1,
  124. },
  125. {
  126. Value: "testing123-",
  127. ErrCount: 1,
  128. },
  129. {
  130. Value: randomString(256),
  131. ErrCount: 1,
  132. },
  133. }
  134. for _, tc := range cases {
  135. _, errors := validateDbParamGroupName(tc.Value, "aws_db_parameter_group_name")
  136. if len(errors) != tc.ErrCount {
  137. t.Fatalf("Expected the DB Parameter Group Name to trigger a validation error")
  138. }
  139. }
  140. }
  141. func testAccCheckAWSDBParameterGroupDestroy(s *terraform.State) error {
  142. conn := testAccProvider.Meta().(*AWSClient).rdsconn
  143. for _, rs := range s.RootModule().Resources {
  144. if rs.Type != "aws_db_parameter_group" {
  145. continue
  146. }
  147. // Try to find the Group
  148. resp, err := conn.DescribeDBParameterGroups(
  149. &rds.DescribeDBParameterGroupsInput{
  150. DBParameterGroupName: aws.String(rs.Primary.ID),
  151. })
  152. if err == nil {
  153. if len(resp.DBParameterGroups) != 0 &&
  154. *resp.DBParameterGroups[0].DBParameterGroupName == rs.Primary.ID {
  155. return fmt.Errorf("DB Parameter Group still exists")
  156. }
  157. }
  158. // Verify the error
  159. newerr, ok := err.(awserr.Error)
  160. if !ok {
  161. return err
  162. }
  163. if newerr.Code() != "InvalidDBParameterGroup.NotFound" {
  164. return err
  165. }
  166. }
  167. return nil
  168. }
  169. func testAccCheckAWSDBParameterGroupAttributes(v *rds.DBParameterGroup) resource.TestCheckFunc {
  170. return func(s *terraform.State) error {
  171. if *v.DBParameterGroupName != "parameter-group-test-terraform" {
  172. return fmt.Errorf("bad name: %#v", v.DBParameterGroupName)
  173. }
  174. if *v.DBParameterGroupFamily != "mysql5.6" {
  175. return fmt.Errorf("bad family: %#v", v.DBParameterGroupFamily)
  176. }
  177. if *v.Description != "Test parameter group for terraform" {
  178. return fmt.Errorf("bad description: %#v", v.Description)
  179. }
  180. return nil
  181. }
  182. }
  183. func testAccCheckAWSDBParameterGroupExists(n string, v *rds.DBParameterGroup) resource.TestCheckFunc {
  184. return func(s *terraform.State) error {
  185. rs, ok := s.RootModule().Resources[n]
  186. if !ok {
  187. return fmt.Errorf("Not found: %s", n)
  188. }
  189. if rs.Primary.ID == "" {
  190. return fmt.Errorf("No DB Parameter Group ID is set")
  191. }
  192. conn := testAccProvider.Meta().(*AWSClient).rdsconn
  193. opts := rds.DescribeDBParameterGroupsInput{
  194. DBParameterGroupName: aws.String(rs.Primary.ID),
  195. }
  196. resp, err := conn.DescribeDBParameterGroups(&opts)
  197. if err != nil {
  198. return err
  199. }
  200. if len(resp.DBParameterGroups) != 1 ||
  201. *resp.DBParameterGroups[0].DBParameterGroupName != rs.Primary.ID {
  202. return fmt.Errorf("DB Parameter Group not found")
  203. }
  204. *v = *resp.DBParameterGroups[0]
  205. return nil
  206. }
  207. }
  208. func randomString(strlen int) string {
  209. rand.Seed(time.Now().UTC().UnixNano())
  210. const chars = "abcdefghijklmnopqrstuvwxyz"
  211. result := make([]byte, strlen)
  212. for i := 0; i < strlen; i++ {
  213. result[i] = chars[rand.Intn(len(chars))]
  214. }
  215. return string(result)
  216. }
  217. const testAccAWSDBParameterGroupConfig = `
  218. resource "aws_db_parameter_group" "bar" {
  219. name = "parameter-group-test-terraform"
  220. family = "mysql5.6"
  221. description = "Test parameter group for terraform"
  222. parameter {
  223. name = "character_set_server"
  224. value = "utf8"
  225. }
  226. parameter {
  227. name = "character_set_client"
  228. value = "utf8"
  229. }
  230. parameter{
  231. name = "character_set_results"
  232. value = "utf8"
  233. }
  234. }
  235. `
  236. const testAccAWSDBParameterGroupAddParametersConfig = `
  237. resource "aws_db_parameter_group" "bar" {
  238. name = "parameter-group-test-terraform"
  239. family = "mysql5.6"
  240. description = "Test parameter group for terraform"
  241. parameter {
  242. name = "character_set_server"
  243. value = "utf8"
  244. }
  245. parameter {
  246. name = "character_set_client"
  247. value = "utf8"
  248. }
  249. parameter{
  250. name = "character_set_results"
  251. value = "utf8"
  252. }
  253. parameter {
  254. name = "collation_server"
  255. value = "utf8_unicode_ci"
  256. }
  257. parameter {
  258. name = "collation_connection"
  259. value = "utf8_unicode_ci"
  260. }
  261. }
  262. `
  263. const testAccAWSDBParameterGroupOnlyConfig = `
  264. resource "aws_db_parameter_group" "bar" {
  265. name = "parameter-group-test-terraform"
  266. family = "mysql5.6"
  267. description = "Test parameter group for terraform"
  268. }
  269. `