github.com/danp/terraform@v0.9.5-0.20170426144147-39d740081351/builtin/providers/opc/resource_security_protocol_test.go (about) 1 package opc 2 3 import ( 4 "fmt" 5 "testing" 6 7 "github.com/hashicorp/go-oracle-terraform/compute" 8 "github.com/hashicorp/terraform/helper/acctest" 9 "github.com/hashicorp/terraform/helper/resource" 10 "github.com/hashicorp/terraform/terraform" 11 ) 12 13 func TestAccOPCSecurityProtocol_Basic(t *testing.T) { 14 ri := acctest.RandInt() 15 config := fmt.Sprintf(testAccOPCSecurityProtocolBasic, ri) 16 17 resource.Test(t, resource.TestCase{ 18 PreCheck: func() { testAccPreCheck(t) }, 19 Providers: testAccProviders, 20 CheckDestroy: testAccCheckSecurityProtocolDestroy, 21 Steps: []resource.TestStep{ 22 { 23 Config: config, 24 Check: resource.ComposeTestCheckFunc( 25 testAccCheckSecurityProtocolExists, 26 ), 27 }, 28 }, 29 }) 30 } 31 32 func TestAccOPCSecurityProtocol_Complete(t *testing.T) { 33 protocolResourceName := "opc_compute_security_protocol.test" 34 ri := acctest.RandInt() 35 config := fmt.Sprintf(testAccOPCSecurityProtocolComplete, ri) 36 37 resource.Test(t, resource.TestCase{ 38 PreCheck: func() { testAccPreCheck(t) }, 39 Providers: testAccProviders, 40 CheckDestroy: testAccCheckSecurityProtocolDestroy, 41 Steps: []resource.TestStep{ 42 { 43 Config: config, 44 Check: resource.ComposeTestCheckFunc( 45 testAccCheckSecurityProtocolExists, 46 resource.TestCheckResourceAttr(protocolResourceName, "description", "Terraform Acceptance Test"), 47 resource.TestCheckResourceAttr(protocolResourceName, "dst_ports.0", "2025-2030"), 48 resource.TestCheckResourceAttr(protocolResourceName, "src_ports.0", "3025-3030"), 49 resource.TestCheckResourceAttr(protocolResourceName, "ip_protocol", "tcp"), 50 ), 51 }, 52 }, 53 }) 54 } 55 56 func TestAccOPCSecurityProtocol_Update(t *testing.T) { 57 protocolResourceName := "opc_compute_security_protocol.test" 58 ri := acctest.RandInt() 59 config := fmt.Sprintf(testAccOPCSecurityProtocolComplete, ri) 60 config2 := fmt.Sprintf(testAccOPCSecurityProtocolUpdated, ri) 61 62 resource.Test(t, resource.TestCase{ 63 PreCheck: func() { testAccPreCheck(t) }, 64 Providers: testAccProviders, 65 CheckDestroy: testAccCheckSecurityProtocolDestroy, 66 Steps: []resource.TestStep{ 67 { 68 Config: config, 69 Check: resource.ComposeTestCheckFunc( 70 testAccCheckSecurityProtocolExists, 71 resource.TestCheckResourceAttr(protocolResourceName, "description", "Terraform Acceptance Test"), 72 resource.TestCheckResourceAttr(protocolResourceName, "dst_ports.0", "2025-2030"), 73 resource.TestCheckResourceAttr(protocolResourceName, "src_ports.0", "3025-3030"), 74 resource.TestCheckResourceAttr(protocolResourceName, "ip_protocol", "tcp"), 75 ), 76 }, 77 { 78 Config: config2, 79 Check: resource.ComposeTestCheckFunc( 80 testAccCheckSecurityProtocolExists, 81 resource.TestCheckResourceAttr(protocolResourceName, "description", ""), 82 resource.TestCheckResourceAttr(protocolResourceName, "dst_ports.1", "2040-2050"), 83 resource.TestCheckResourceAttr(protocolResourceName, "src_ports.1", "3040-3050"), 84 resource.TestCheckResourceAttr(protocolResourceName, "ip_protocol", "udp"), 85 ), 86 }, 87 }, 88 }) 89 } 90 91 func testAccCheckSecurityProtocolExists(s *terraform.State) error { 92 client := testAccProvider.Meta().(*compute.Client).SecurityProtocols() 93 94 for _, rs := range s.RootModule().Resources { 95 if rs.Type != "opc_compute_security_protocol" { 96 continue 97 } 98 99 input := compute.GetSecurityProtocolInput{ 100 Name: rs.Primary.Attributes["name"], 101 } 102 if _, err := client.GetSecurityProtocol(&input); err != nil { 103 return fmt.Errorf("Error retrieving state of Security Protocol %s: %s", input.Name, err) 104 } 105 } 106 107 return nil 108 } 109 110 func testAccCheckSecurityProtocolDestroy(s *terraform.State) error { 111 client := testAccProvider.Meta().(*compute.Client).SecurityProtocols() 112 113 for _, rs := range s.RootModule().Resources { 114 if rs.Type != "opc_compute_security_protocol" { 115 continue 116 } 117 118 input := compute.GetSecurityProtocolInput{ 119 Name: rs.Primary.Attributes["name"], 120 } 121 if info, err := client.GetSecurityProtocol(&input); err == nil { 122 return fmt.Errorf("Security Protocol %s still exists: %#v", input.Name, info) 123 } 124 } 125 126 return nil 127 } 128 129 const testAccOPCSecurityProtocolBasic = ` 130 resource "opc_compute_security_protocol" "test" { 131 name = "acc-security-protocol-%d" 132 description = "Terraform Acceptance Test" 133 } 134 ` 135 136 const testAccOPCSecurityProtocolComplete = ` 137 resource "opc_compute_security_protocol" "test" { 138 name = "acc-security-protocol-%d" 139 description = "Terraform Acceptance Test" 140 dst_ports = ["2025-2030"] 141 src_ports = ["3025-3030"] 142 ip_protocol = "tcp" 143 } 144 ` 145 146 const testAccOPCSecurityProtocolUpdated = ` 147 resource "opc_compute_security_protocol" "test" { 148 name = "acc-security-protocol-%d" 149 dst_ports = ["2025-2030", "2040-2050"] 150 src_ports = ["3025-3030", "3040-3050"] 151 ip_protocol = "udp" 152 } 153 `