/vendor/github.com/terraform-providers/terraform-provider-aws/aws/resource_aws_redshift_security_group_test.go

https://github.com/kris-nova/terraformctl · Go · 440 lines · 400 code · 38 blank · 2 comment · 22 complexity · c4c30cd7ce43ab610d478545bd71cdda MD5 · raw file

  1. package aws
  2. import (
  3. "fmt"
  4. "testing"
  5. "github.com/aws/aws-sdk-go/aws"
  6. "github.com/aws/aws-sdk-go/aws/awserr"
  7. "github.com/aws/aws-sdk-go/service/redshift"
  8. "github.com/hashicorp/terraform/helper/acctest"
  9. "github.com/hashicorp/terraform/helper/resource"
  10. "github.com/hashicorp/terraform/terraform"
  11. )
  12. func TestAccAWSRedshiftSecurityGroup_ingressCidr(t *testing.T) {
  13. var v redshift.ClusterSecurityGroup
  14. rInt := acctest.RandInt()
  15. resource.Test(t, resource.TestCase{
  16. PreCheck: func() { testAccPreCheck(t) },
  17. Providers: testAccProviders,
  18. CheckDestroy: testAccCheckAWSRedshiftSecurityGroupDestroy,
  19. Steps: []resource.TestStep{
  20. {
  21. Config: testAccAWSRedshiftSecurityGroupConfig_ingressCidr(rInt),
  22. Check: resource.ComposeTestCheckFunc(
  23. testAccCheckAWSRedshiftSecurityGroupExists("aws_redshift_security_group.bar", &v),
  24. resource.TestCheckResourceAttr(
  25. "aws_redshift_security_group.bar", "name", fmt.Sprintf("redshift-sg-terraform-%d", rInt)),
  26. resource.TestCheckResourceAttr(
  27. "aws_redshift_security_group.bar", "description", "Managed by Terraform"),
  28. resource.TestCheckResourceAttr(
  29. "aws_redshift_security_group.bar", "ingress.2735652665.cidr", "10.0.0.1/24"),
  30. resource.TestCheckResourceAttr(
  31. "aws_redshift_security_group.bar", "ingress.#", "1"),
  32. ),
  33. },
  34. },
  35. })
  36. }
  37. func TestAccAWSRedshiftSecurityGroup_updateIngressCidr(t *testing.T) {
  38. var v redshift.ClusterSecurityGroup
  39. rInt := acctest.RandInt()
  40. resource.Test(t, resource.TestCase{
  41. PreCheck: func() { testAccPreCheck(t) },
  42. Providers: testAccProviders,
  43. CheckDestroy: testAccCheckAWSRedshiftSecurityGroupDestroy,
  44. Steps: []resource.TestStep{
  45. {
  46. Config: testAccAWSRedshiftSecurityGroupConfig_ingressCidr(rInt),
  47. Check: resource.ComposeTestCheckFunc(
  48. testAccCheckAWSRedshiftSecurityGroupExists("aws_redshift_security_group.bar", &v),
  49. resource.TestCheckResourceAttr(
  50. "aws_redshift_security_group.bar", "ingress.#", "1"),
  51. ),
  52. },
  53. {
  54. Config: testAccAWSRedshiftSecurityGroupConfig_ingressCidrAdd(rInt),
  55. Check: resource.ComposeTestCheckFunc(
  56. testAccCheckAWSRedshiftSecurityGroupExists("aws_redshift_security_group.bar", &v),
  57. resource.TestCheckResourceAttr(
  58. "aws_redshift_security_group.bar", "ingress.#", "3"),
  59. ),
  60. },
  61. {
  62. Config: testAccAWSRedshiftSecurityGroupConfig_ingressCidrReduce(rInt),
  63. Check: resource.ComposeTestCheckFunc(
  64. testAccCheckAWSRedshiftSecurityGroupExists("aws_redshift_security_group.bar", &v),
  65. resource.TestCheckResourceAttr(
  66. "aws_redshift_security_group.bar", "ingress.#", "2"),
  67. ),
  68. },
  69. },
  70. })
  71. }
  72. func TestAccAWSRedshiftSecurityGroup_ingressSecurityGroup(t *testing.T) {
  73. var v redshift.ClusterSecurityGroup
  74. rInt := acctest.RandInt()
  75. resource.Test(t, resource.TestCase{
  76. PreCheck: func() { testAccPreCheck(t) },
  77. Providers: testAccProviders,
  78. CheckDestroy: testAccCheckAWSRedshiftSecurityGroupDestroy,
  79. Steps: []resource.TestStep{
  80. {
  81. Config: testAccAWSRedshiftSecurityGroupConfig_ingressSgId(rInt),
  82. Check: resource.ComposeTestCheckFunc(
  83. testAccCheckAWSRedshiftSecurityGroupExists("aws_redshift_security_group.bar", &v),
  84. resource.TestCheckResourceAttr(
  85. "aws_redshift_security_group.bar", "name", fmt.Sprintf("redshift-sg-terraform-%d", rInt)),
  86. resource.TestCheckResourceAttr(
  87. "aws_redshift_security_group.bar", "description", "this is a description"),
  88. resource.TestCheckResourceAttr(
  89. "aws_redshift_security_group.bar", "ingress.#", "1"),
  90. ),
  91. },
  92. },
  93. })
  94. }
  95. func TestAccAWSRedshiftSecurityGroup_updateIngressSecurityGroup(t *testing.T) {
  96. var v redshift.ClusterSecurityGroup
  97. rInt := acctest.RandInt()
  98. resource.Test(t, resource.TestCase{
  99. PreCheck: func() { testAccPreCheck(t) },
  100. Providers: testAccProviders,
  101. CheckDestroy: testAccCheckAWSRedshiftSecurityGroupDestroy,
  102. Steps: []resource.TestStep{
  103. {
  104. Config: testAccAWSRedshiftSecurityGroupConfig_ingressSgId(rInt),
  105. Check: resource.ComposeTestCheckFunc(
  106. testAccCheckAWSRedshiftSecurityGroupExists("aws_redshift_security_group.bar", &v),
  107. resource.TestCheckResourceAttr(
  108. "aws_redshift_security_group.bar", "ingress.#", "1"),
  109. ),
  110. },
  111. {
  112. Config: testAccAWSRedshiftSecurityGroupConfig_ingressSgIdAdd(rInt),
  113. Check: resource.ComposeTestCheckFunc(
  114. testAccCheckAWSRedshiftSecurityGroupExists("aws_redshift_security_group.bar", &v),
  115. resource.TestCheckResourceAttr(
  116. "aws_redshift_security_group.bar", "ingress.#", "3"),
  117. ),
  118. },
  119. {
  120. Config: testAccAWSRedshiftSecurityGroupConfig_ingressSgIdReduce(rInt),
  121. Check: resource.ComposeTestCheckFunc(
  122. testAccCheckAWSRedshiftSecurityGroupExists("aws_redshift_security_group.bar", &v),
  123. resource.TestCheckResourceAttr(
  124. "aws_redshift_security_group.bar", "ingress.#", "2"),
  125. ),
  126. },
  127. },
  128. })
  129. }
  130. func testAccCheckAWSRedshiftSecurityGroupExists(n string, v *redshift.ClusterSecurityGroup) resource.TestCheckFunc {
  131. return func(s *terraform.State) error {
  132. rs, ok := s.RootModule().Resources[n]
  133. if !ok {
  134. return fmt.Errorf("Not found: %s", n)
  135. }
  136. if rs.Primary.ID == "" {
  137. return fmt.Errorf("No Redshift Security Group ID is set")
  138. }
  139. conn := testAccProvider.Meta().(*AWSClient).redshiftconn
  140. opts := redshift.DescribeClusterSecurityGroupsInput{
  141. ClusterSecurityGroupName: aws.String(rs.Primary.ID),
  142. }
  143. resp, err := conn.DescribeClusterSecurityGroups(&opts)
  144. if err != nil {
  145. return err
  146. }
  147. if len(resp.ClusterSecurityGroups) != 1 ||
  148. *resp.ClusterSecurityGroups[0].ClusterSecurityGroupName != rs.Primary.ID {
  149. return fmt.Errorf("Redshift Security Group not found")
  150. }
  151. *v = *resp.ClusterSecurityGroups[0]
  152. return nil
  153. }
  154. }
  155. func testAccCheckAWSRedshiftSecurityGroupDestroy(s *terraform.State) error {
  156. conn := testAccProvider.Meta().(*AWSClient).redshiftconn
  157. for _, rs := range s.RootModule().Resources {
  158. if rs.Type != "aws_redshift_security_group" {
  159. continue
  160. }
  161. // Try to find the Group
  162. resp, err := conn.DescribeClusterSecurityGroups(
  163. &redshift.DescribeClusterSecurityGroupsInput{
  164. ClusterSecurityGroupName: aws.String(rs.Primary.ID),
  165. })
  166. if err == nil {
  167. if len(resp.ClusterSecurityGroups) != 0 &&
  168. *resp.ClusterSecurityGroups[0].ClusterSecurityGroupName == rs.Primary.ID {
  169. return fmt.Errorf("Redshift Security Group still exists")
  170. }
  171. }
  172. // Verify the error
  173. newerr, ok := err.(awserr.Error)
  174. if !ok {
  175. return err
  176. }
  177. if newerr.Code() != "ClusterSecurityGroupNotFound" {
  178. return err
  179. }
  180. }
  181. return nil
  182. }
  183. func TestResourceAWSRedshiftSecurityGroupNameValidation(t *testing.T) {
  184. cases := []struct {
  185. Value string
  186. ErrCount int
  187. }{
  188. {
  189. Value: "default",
  190. ErrCount: 1,
  191. },
  192. {
  193. Value: "testing123%%",
  194. ErrCount: 1,
  195. },
  196. {
  197. Value: "TestingSG",
  198. ErrCount: 1,
  199. },
  200. {
  201. Value: randomString(256),
  202. ErrCount: 1,
  203. },
  204. }
  205. for _, tc := range cases {
  206. _, errors := validateRedshiftSecurityGroupName(tc.Value, "aws_redshift_security_group_name")
  207. if len(errors) != tc.ErrCount {
  208. t.Fatalf("Expected the Redshift Security Group Name to trigger a validation error")
  209. }
  210. }
  211. }
  212. func testAccAWSRedshiftSecurityGroupConfig_ingressCidr(rInt int) string {
  213. return fmt.Sprintf(`
  214. provider "aws" {
  215. region = "us-east-1"
  216. }
  217. resource "aws_redshift_security_group" "bar" {
  218. name = "redshift-sg-terraform-%d"
  219. ingress {
  220. cidr = "10.0.0.1/24"
  221. }
  222. }`, rInt)
  223. }
  224. func testAccAWSRedshiftSecurityGroupConfig_ingressCidrAdd(rInt int) string {
  225. return fmt.Sprintf(`
  226. provider "aws" {
  227. region = "us-east-1"
  228. }
  229. resource "aws_redshift_security_group" "bar" {
  230. name = "redshift-sg-terraform-%d"
  231. description = "this is a description"
  232. ingress {
  233. cidr = "10.0.0.1/24"
  234. }
  235. ingress {
  236. cidr = "10.0.10.1/24"
  237. }
  238. ingress {
  239. cidr = "10.0.20.1/24"
  240. }
  241. }`, rInt)
  242. }
  243. func testAccAWSRedshiftSecurityGroupConfig_ingressCidrReduce(rInt int) string {
  244. return fmt.Sprintf(`
  245. provider "aws" {
  246. region = "us-east-1"
  247. }
  248. resource "aws_redshift_security_group" "bar" {
  249. name = "redshift-sg-terraform-%d"
  250. description = "this is a description"
  251. ingress {
  252. cidr = "10.0.0.1/24"
  253. }
  254. ingress {
  255. cidr = "10.0.10.1/24"
  256. }
  257. }`, rInt)
  258. }
  259. func testAccAWSRedshiftSecurityGroupConfig_ingressSgId(rInt int) string {
  260. return fmt.Sprintf(`
  261. provider "aws" {
  262. region = "us-east-1"
  263. }
  264. resource "aws_security_group" "redshift" {
  265. name = "terraform_redshift_test_%d"
  266. description = "Used in the redshift acceptance tests"
  267. ingress {
  268. protocol = "tcp"
  269. from_port = 22
  270. to_port = 22
  271. cidr_blocks = ["10.0.0.0/8"]
  272. }
  273. }
  274. resource "aws_redshift_security_group" "bar" {
  275. name = "redshift-sg-terraform-%d"
  276. description = "this is a description"
  277. ingress {
  278. security_group_name = "${aws_security_group.redshift.name}"
  279. security_group_owner_id = "${aws_security_group.redshift.owner_id}"
  280. }
  281. }`, rInt, rInt)
  282. }
  283. func testAccAWSRedshiftSecurityGroupConfig_ingressSgIdAdd(rInt int) string {
  284. return fmt.Sprintf(`
  285. provider "aws" {
  286. region = "us-east-1"
  287. }
  288. resource "aws_security_group" "redshift" {
  289. name = "terraform_redshift_test_%d"
  290. description = "Used in the redshift acceptance tests"
  291. ingress {
  292. protocol = "tcp"
  293. from_port = 22
  294. to_port = 22
  295. cidr_blocks = ["10.0.0.0/16"]
  296. }
  297. }
  298. resource "aws_security_group" "redshift2" {
  299. name = "terraform_redshift_test_2_%d"
  300. description = "Used in the redshift acceptance tests #2"
  301. ingress {
  302. protocol = "tcp"
  303. from_port = 22
  304. to_port = 22
  305. cidr_blocks = ["10.1.0.0/16"]
  306. }
  307. }
  308. resource "aws_security_group" "redshift3" {
  309. name = "terraform_redshift_test_3_%d"
  310. description = "Used in the redshift acceptance tests #3"
  311. ingress {
  312. protocol = "tcp"
  313. from_port = 22
  314. to_port = 22
  315. cidr_blocks = ["10.2.0.0/16"]
  316. }
  317. }
  318. resource "aws_redshift_security_group" "bar" {
  319. name = "redshift-sg-terraform-%d"
  320. description = "this is a description"
  321. ingress {
  322. security_group_name = "${aws_security_group.redshift.name}"
  323. security_group_owner_id = "${aws_security_group.redshift.owner_id}"
  324. }
  325. ingress {
  326. security_group_name = "${aws_security_group.redshift2.name}"
  327. security_group_owner_id = "${aws_security_group.redshift.owner_id}"
  328. }
  329. ingress {
  330. security_group_name = "${aws_security_group.redshift3.name}"
  331. security_group_owner_id = "${aws_security_group.redshift.owner_id}"
  332. }
  333. }`, rInt, rInt, rInt, rInt)
  334. }
  335. func testAccAWSRedshiftSecurityGroupConfig_ingressSgIdReduce(rInt int) string {
  336. return fmt.Sprintf(`
  337. provider "aws" {
  338. region = "us-east-1"
  339. }
  340. resource "aws_security_group" "redshift" {
  341. name = "terraform_redshift_test_%d"
  342. description = "Used in the redshift acceptance tests"
  343. ingress {
  344. protocol = "tcp"
  345. from_port = 22
  346. to_port = 22
  347. cidr_blocks = ["10.0.0.0/16"]
  348. }
  349. }
  350. resource "aws_security_group" "redshift2" {
  351. name = "terraform_redshift_test_2_%d"
  352. description = "Used in the redshift acceptance tests #2"
  353. ingress {
  354. protocol = "tcp"
  355. from_port = 22
  356. to_port = 22
  357. cidr_blocks = ["10.1.0.0/16"]
  358. }
  359. }
  360. resource "aws_redshift_security_group" "bar" {
  361. name = "redshift-sg-terraform-%d"
  362. description = "this is a description"
  363. ingress {
  364. security_group_name = "${aws_security_group.redshift.name}"
  365. security_group_owner_id = "${aws_security_group.redshift.owner_id}"
  366. }
  367. ingress {
  368. security_group_name = "${aws_security_group.redshift2.name}"
  369. security_group_owner_id = "${aws_security_group.redshift.owner_id}"
  370. }
  371. }`, rInt, rInt, rInt)
  372. }