github.com/minamijoyo/terraform@v0.7.8-0.20161029001309-18b3736ba44b/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: 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  		if _, ok := err.(gophercloud.ErrDefault404); !ok {
    84  			return err
    85  		}
    86  	}
    87  	return nil
    88  }
    89  
    90  func testAccCheckFWPolicyV1Exists(n, name, description string, ruleCount int) resource.TestCheckFunc {
    91  
    92  	return func(s *terraform.State) error {
    93  
    94  		rs, ok := s.RootModule().Resources[n]
    95  		if !ok {
    96  			return fmt.Errorf("Not found: %s", n)
    97  		}
    98  
    99  		if rs.Primary.ID == "" {
   100  			return fmt.Errorf("No ID is set")
   101  		}
   102  
   103  		config := testAccProvider.Meta().(*Config)
   104  		networkingClient, err := config.networkingV2Client(OS_REGION_NAME)
   105  		if err != nil {
   106  			return fmt.Errorf("(testAccCheckFirewallPolicyExists) Error creating OpenStack networking client: %s", err)
   107  		}
   108  
   109  		var found *policies.Policy
   110  		for i := 0; i < 5; i++ {
   111  			// Firewall policy creation is asynchronous. Retry some times
   112  			// if we get a 404 error. Fail on any other error.
   113  			found, err = policies.Get(networkingClient, rs.Primary.ID).Extract()
   114  			if err != nil {
   115  				if _, ok := err.(gophercloud.ErrDefault404); ok {
   116  					time.Sleep(time.Second)
   117  					continue
   118  				}
   119  				return err
   120  			}
   121  			break
   122  		}
   123  
   124  		if name != found.Name {
   125  			return fmt.Errorf("Expected name <%s>, but found <%s>", name, found.Name)
   126  		}
   127  
   128  		if description != found.Description {
   129  			return fmt.Errorf("Expected description <%s>, but found <%s>", description, found.Description)
   130  		}
   131  
   132  		if ruleCount != len(found.Rules) {
   133  			return fmt.Errorf("Expected rule count <%d>, but found <%d>", ruleCount, len(found.Rules))
   134  		}
   135  
   136  		return nil
   137  	}
   138  }
   139  
   140  const testFirewallPolicyConfig = `
   141  resource "openstack_fw_policy_v1" "accept_test" {
   142  
   143  }
   144  `
   145  
   146  const testFirewallPolicyConfigAddRules = `
   147  resource "openstack_fw_policy_v1" "accept_test" {
   148  	name = "accept_test"
   149  	description =  "terraform acceptance test"
   150  	rules = [
   151  		"${openstack_fw_rule_v1.accept_test_udp_deny.id}",
   152  		"${openstack_fw_rule_v1.accept_test_tcp_allow.id}"
   153  	]
   154  }
   155  
   156  resource "openstack_fw_rule_v1" "accept_test_tcp_allow" {
   157  	protocol = "tcp"
   158  	action = "allow"
   159  }
   160  
   161  resource "openstack_fw_rule_v1" "accept_test_udp_deny" {
   162  	protocol = "udp"
   163  	action = "deny"
   164  }
   165  `
   166  
   167  const testFirewallPolicyUpdateDeleteRule = `
   168  resource "openstack_fw_policy_v1" "accept_test" {
   169  	name = "accept_test"
   170  	description =  "terraform acceptance test"
   171  	rules = [
   172  		"${openstack_fw_rule_v1.accept_test_udp_deny.id}"
   173  	]
   174  }
   175  
   176  resource "openstack_fw_rule_v1" "accept_test_udp_deny" {
   177  	protocol = "udp"
   178  	action = "deny"
   179  }
   180  `