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