github.com/danp/terraform@v0.9.5-0.20170426144147-39d740081351/builtin/providers/openstack/resource_openstack_fw_policy_v1_test.go (about) 1 package openstack 2 3 import ( 4 "fmt" 5 "testing" 6 "time" 7 8 "github.com/gophercloud/gophercloud" 9 "github.com/gophercloud/gophercloud/openstack/networking/v2/extensions/fwaas/policies" 10 "github.com/hashicorp/terraform/helper/resource" 11 "github.com/hashicorp/terraform/terraform" 12 ) 13 14 func TestAccFWPolicyV1_basic(t *testing.T) { 15 resource.Test(t, resource.TestCase{ 16 PreCheck: func() { testAccPreCheck(t) }, 17 Providers: testAccProviders, 18 CheckDestroy: testAccCheckFWPolicyV1Destroy, 19 Steps: []resource.TestStep{ 20 resource.TestStep{ 21 Config: testAccFWPolicyV1_basic, 22 Check: resource.ComposeTestCheckFunc( 23 testAccCheckFWPolicyV1Exists( 24 "openstack_fw_policy_v1.policy_1", "", "", 0), 25 ), 26 }, 27 }, 28 }) 29 } 30 31 func TestAccFWPolicyV1_addRules(t *testing.T) { 32 resource.Test(t, resource.TestCase{ 33 PreCheck: func() { testAccPreCheck(t) }, 34 Providers: testAccProviders, 35 CheckDestroy: testAccCheckFWPolicyV1Destroy, 36 Steps: []resource.TestStep{ 37 resource.TestStep{ 38 Config: testAccFWPolicyV1_addRules, 39 Check: resource.ComposeTestCheckFunc( 40 testAccCheckFWPolicyV1Exists( 41 "openstack_fw_policy_v1.policy_1", "policy_1", "terraform acceptance test", 2), 42 ), 43 }, 44 }, 45 }) 46 } 47 48 func TestAccFWPolicyV1_deleteRules(t *testing.T) { 49 resource.Test(t, resource.TestCase{ 50 PreCheck: func() { testAccPreCheck(t) }, 51 Providers: testAccProviders, 52 CheckDestroy: testAccCheckFWPolicyV1Destroy, 53 Steps: []resource.TestStep{ 54 resource.TestStep{ 55 Config: testAccFWPolicyV1_deleteRules, 56 Check: resource.ComposeTestCheckFunc( 57 testAccCheckFWPolicyV1Exists( 58 "openstack_fw_policy_v1.policy_1", "policy_1", "terraform acceptance test", 1), 59 ), 60 }, 61 }, 62 }) 63 } 64 65 func TestAccFWPolicyV1_timeout(t *testing.T) { 66 resource.Test(t, resource.TestCase{ 67 PreCheck: func() { testAccPreCheck(t) }, 68 Providers: testAccProviders, 69 CheckDestroy: testAccCheckFWPolicyV1Destroy, 70 Steps: []resource.TestStep{ 71 resource.TestStep{ 72 Config: testAccFWPolicyV1_timeout, 73 Check: resource.ComposeTestCheckFunc( 74 testAccCheckFWPolicyV1Exists( 75 "openstack_fw_policy_v1.policy_1", "", "", 0), 76 ), 77 }, 78 }, 79 }) 80 } 81 82 func testAccCheckFWPolicyV1Destroy(s *terraform.State) error { 83 config := testAccProvider.Meta().(*Config) 84 networkingClient, err := config.networkingV2Client(OS_REGION_NAME) 85 if err != nil { 86 return fmt.Errorf("Error creating OpenStack networking client: %s", err) 87 } 88 for _, rs := range s.RootModule().Resources { 89 if rs.Type != "openstack_fw_policy_v1" { 90 continue 91 } 92 _, err = policies.Get(networkingClient, rs.Primary.ID).Extract() 93 if err == nil { 94 return fmt.Errorf("Firewall policy (%s) still exists.", rs.Primary.ID) 95 } 96 if _, ok := err.(gophercloud.ErrDefault404); !ok { 97 return err 98 } 99 } 100 return nil 101 } 102 103 func testAccCheckFWPolicyV1Exists(n, name, description string, ruleCount int) resource.TestCheckFunc { 104 return func(s *terraform.State) error { 105 rs, ok := s.RootModule().Resources[n] 106 if !ok { 107 return fmt.Errorf("Not found: %s", n) 108 } 109 110 if rs.Primary.ID == "" { 111 return fmt.Errorf("No ID is set") 112 } 113 114 config := testAccProvider.Meta().(*Config) 115 networkingClient, err := config.networkingV2Client(OS_REGION_NAME) 116 if err != nil { 117 return fmt.Errorf("Error creating OpenStack networking client: %s", err) 118 } 119 120 var found *policies.Policy 121 for i := 0; i < 5; i++ { 122 // Firewall policy creation is asynchronous. Retry some times 123 // if we get a 404 error. Fail on any other error. 124 found, err = policies.Get(networkingClient, rs.Primary.ID).Extract() 125 if err != nil { 126 if _, ok := err.(gophercloud.ErrDefault404); ok { 127 time.Sleep(time.Second) 128 continue 129 } 130 return err 131 } 132 break 133 } 134 135 switch { 136 case name != found.Name: 137 err = fmt.Errorf("Expected name <%s>, but found <%s>", name, found.Name) 138 case description != found.Description: 139 err = fmt.Errorf("Expected description <%s>, but found <%s>", description, found.Description) 140 case ruleCount != len(found.Rules): 141 err = fmt.Errorf("Expected rule count <%d>, but found <%d>", ruleCount, len(found.Rules)) 142 } 143 144 if err != nil { 145 return err 146 } 147 148 return nil 149 } 150 } 151 152 const testAccFWPolicyV1_basic = ` 153 resource "openstack_fw_policy_v1" "policy_1" { 154 } 155 ` 156 157 const testAccFWPolicyV1_addRules = ` 158 resource "openstack_fw_policy_v1" "policy_1" { 159 name = "policy_1" 160 description = "terraform acceptance test" 161 rules = [ 162 "${openstack_fw_rule_v1.udp_deny.id}", 163 "${openstack_fw_rule_v1.tcp_allow.id}" 164 ] 165 } 166 167 resource "openstack_fw_rule_v1" "tcp_allow" { 168 protocol = "tcp" 169 action = "allow" 170 } 171 172 resource "openstack_fw_rule_v1" "udp_deny" { 173 protocol = "udp" 174 action = "deny" 175 } 176 ` 177 178 const testAccFWPolicyV1_deleteRules = ` 179 resource "openstack_fw_policy_v1" "policy_1" { 180 name = "policy_1" 181 description = "terraform acceptance test" 182 rules = [ 183 "${openstack_fw_rule_v1.udp_deny.id}" 184 ] 185 } 186 187 resource "openstack_fw_rule_v1" "udp_deny" { 188 protocol = "udp" 189 action = "deny" 190 } 191 ` 192 193 const testAccFWPolicyV1_timeout = ` 194 resource "openstack_fw_policy_v1" "policy_1" { 195 timeouts { 196 create = "5m" 197 } 198 } 199 `