github.com/minamijoyo/terraform@v0.7.8-0.20161029001309-18b3736ba44b/builtin/providers/openstack/resource_openstack_fw_firewall_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/firewalls" 10 "github.com/hashicorp/terraform/helper/resource" 11 "github.com/hashicorp/terraform/terraform" 12 ) 13 14 func TestAccFWFirewallV1_basic(t *testing.T) { 15 16 var policyID *string 17 18 resource.Test(t, resource.TestCase{ 19 PreCheck: func() { testAccPreCheck(t) }, 20 Providers: testAccProviders, 21 CheckDestroy: testAccCheckFWFirewallV1Destroy, 22 Steps: []resource.TestStep{ 23 resource.TestStep{ 24 Config: testFirewallConfig, 25 Check: resource.ComposeTestCheckFunc( 26 testAccCheckFWFirewallV1Exists("openstack_fw_firewall_v1.accept_test", "", "", policyID), 27 ), 28 }, 29 resource.TestStep{ 30 Config: testFirewallConfigUpdated, 31 Check: resource.ComposeTestCheckFunc( 32 testAccCheckFWFirewallV1Exists("openstack_fw_firewall_v1.accept_test", "accept_test", "terraform acceptance test", policyID), 33 ), 34 }, 35 }, 36 }) 37 } 38 39 func testAccCheckFWFirewallV1Destroy(s *terraform.State) error { 40 41 config := testAccProvider.Meta().(*Config) 42 networkingClient, err := config.networkingV2Client(OS_REGION_NAME) 43 if err != nil { 44 return fmt.Errorf("(testAccCheckOpenstackFirewallDestroy) Error creating OpenStack networking client: %s", err) 45 } 46 for _, rs := range s.RootModule().Resources { 47 if rs.Type != "openstack_firewall" { 48 continue 49 } 50 51 _, err = firewalls.Get(networkingClient, rs.Primary.ID).Extract() 52 if err == nil { 53 return fmt.Errorf("Firewall (%s) still exists.", rs.Primary.ID) 54 } 55 if _, ok := err.(gophercloud.ErrDefault404); !ok { 56 return err 57 } 58 } 59 return nil 60 } 61 62 func testAccCheckFWFirewallV1Exists(n, expectedName, expectedDescription string, policyID *string) resource.TestCheckFunc { 63 64 return func(s *terraform.State) error { 65 66 rs, ok := s.RootModule().Resources[n] 67 if !ok { 68 return fmt.Errorf("Not found: %s", n) 69 } 70 71 if rs.Primary.ID == "" { 72 return fmt.Errorf("No ID is set") 73 } 74 75 config := testAccProvider.Meta().(*Config) 76 networkingClient, err := config.networkingV2Client(OS_REGION_NAME) 77 if err != nil { 78 return fmt.Errorf("(testAccCheckFirewallExists) Error creating OpenStack networking client: %s", err) 79 } 80 81 var found *firewalls.Firewall 82 for i := 0; i < 5; i++ { 83 // Firewall creation is asynchronous. Retry some times 84 // if we get a 404 error. Fail on any other error. 85 found, err = firewalls.Get(networkingClient, rs.Primary.ID).Extract() 86 if err != nil { 87 if _, ok := err.(gophercloud.ErrDefault404); ok { 88 time.Sleep(time.Second) 89 continue 90 } 91 return err 92 } 93 break 94 } 95 96 if found.Name != expectedName { 97 return fmt.Errorf("Expected Name to be <%s> but found <%s>", expectedName, found.Name) 98 } 99 if found.Description != expectedDescription { 100 return fmt.Errorf("Expected Description to be <%s> but found <%s>", expectedDescription, found.Description) 101 } 102 if found.PolicyID == "" { 103 return fmt.Errorf("Policy should not be empty") 104 } 105 if policyID != nil && found.PolicyID == *policyID { 106 return fmt.Errorf("Policy had not been correctly updated. Went from <%s> to <%s>", expectedName, found.Name) 107 } 108 109 policyID = &found.PolicyID 110 111 return nil 112 } 113 } 114 115 const testFirewallConfig = ` 116 resource "openstack_fw_firewall_v1" "accept_test" { 117 policy_id = "${openstack_fw_policy_v1.accept_test_policy_1.id}" 118 } 119 120 resource "openstack_fw_policy_v1" "accept_test_policy_1" { 121 name = "policy-1" 122 } 123 ` 124 125 const testFirewallConfigUpdated = ` 126 resource "openstack_fw_firewall_v1" "accept_test" { 127 name = "accept_test" 128 description = "terraform acceptance test" 129 policy_id = "${openstack_fw_policy_v1.accept_test_policy_2.id}" 130 admin_state_up = true 131 } 132 133 resource "openstack_fw_policy_v1" "accept_test_policy_2" { 134 name = "policy-2" 135 } 136 `