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