github.com/nathanielks/terraform@v0.6.1-0.20170509030759-13e1a62319dc/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  	var policyID *string
    16  
    17  	resource.Test(t, resource.TestCase{
    18  		PreCheck:     func() { testAccPreCheck(t) },
    19  		Providers:    testAccProviders,
    20  		CheckDestroy: testAccCheckFWFirewallV1Destroy,
    21  		Steps: []resource.TestStep{
    22  			resource.TestStep{
    23  				Config: testAccFWFirewallV1_basic_1,
    24  				Check: resource.ComposeTestCheckFunc(
    25  					testAccCheckFWFirewallV1Exists("openstack_fw_firewall_v1.fw_1", "", "", policyID),
    26  				),
    27  			},
    28  			resource.TestStep{
    29  				Config: testAccFWFirewallV1_basic_2,
    30  				Check: resource.ComposeTestCheckFunc(
    31  					testAccCheckFWFirewallV1Exists(
    32  						"openstack_fw_firewall_v1.fw_1", "fw_1", "terraform acceptance test", policyID),
    33  				),
    34  			},
    35  		},
    36  	})
    37  }
    38  
    39  func TestAccFWFirewallV1_timeout(t *testing.T) {
    40  	var policyID *string
    41  
    42  	resource.Test(t, resource.TestCase{
    43  		PreCheck:     func() { testAccPreCheck(t) },
    44  		Providers:    testAccProviders,
    45  		CheckDestroy: testAccCheckFWFirewallV1Destroy,
    46  		Steps: []resource.TestStep{
    47  			resource.TestStep{
    48  				Config: testAccFWFirewallV1_timeout,
    49  				Check: resource.ComposeTestCheckFunc(
    50  					testAccCheckFWFirewallV1Exists("openstack_fw_firewall_v1.fw_1", "", "", policyID),
    51  				),
    52  			},
    53  		},
    54  	})
    55  }
    56  
    57  func testAccCheckFWFirewallV1Destroy(s *terraform.State) error {
    58  	config := testAccProvider.Meta().(*Config)
    59  	networkingClient, err := config.networkingV2Client(OS_REGION_NAME)
    60  	if err != nil {
    61  		return fmt.Errorf("Error creating OpenStack networking client: %s", err)
    62  	}
    63  	for _, rs := range s.RootModule().Resources {
    64  		if rs.Type != "openstack_firewall" {
    65  			continue
    66  		}
    67  
    68  		_, err = firewalls.Get(networkingClient, rs.Primary.ID).Extract()
    69  		if err == nil {
    70  			return fmt.Errorf("Firewall (%s) still exists.", rs.Primary.ID)
    71  		}
    72  		if _, ok := err.(gophercloud.ErrDefault404); !ok {
    73  			return err
    74  		}
    75  	}
    76  	return nil
    77  }
    78  
    79  func testAccCheckFWFirewallV1Exists(n, expectedName, expectedDescription string, policyID *string) resource.TestCheckFunc {
    80  	return func(s *terraform.State) error {
    81  		rs, ok := s.RootModule().Resources[n]
    82  		if !ok {
    83  			return fmt.Errorf("Not found: %s", n)
    84  		}
    85  
    86  		if rs.Primary.ID == "" {
    87  			return fmt.Errorf("No ID is set")
    88  		}
    89  
    90  		config := testAccProvider.Meta().(*Config)
    91  		networkingClient, err := config.networkingV2Client(OS_REGION_NAME)
    92  		if err != nil {
    93  			return fmt.Errorf("Exists) Error creating OpenStack networking client: %s", err)
    94  		}
    95  
    96  		var found *firewalls.Firewall
    97  		for i := 0; i < 5; i++ {
    98  			// Firewall creation is asynchronous. Retry some times
    99  			// if we get a 404 error. Fail on any other error.
   100  			found, err = firewalls.Get(networkingClient, rs.Primary.ID).Extract()
   101  			if err != nil {
   102  				if _, ok := err.(gophercloud.ErrDefault404); ok {
   103  					time.Sleep(time.Second)
   104  					continue
   105  				}
   106  				return err
   107  			}
   108  			break
   109  		}
   110  
   111  		switch {
   112  		case found.Name != expectedName:
   113  			err = fmt.Errorf("Expected Name to be <%s> but found <%s>", expectedName, found.Name)
   114  		case found.Description != expectedDescription:
   115  			err = fmt.Errorf("Expected Description to be <%s> but found <%s>",
   116  				expectedDescription, found.Description)
   117  		case found.PolicyID == "":
   118  			err = fmt.Errorf("Policy should not be empty")
   119  		case policyID != nil && found.PolicyID == *policyID:
   120  			err = fmt.Errorf("Policy had not been correctly updated. Went from <%s> to <%s>",
   121  				expectedName, found.Name)
   122  		}
   123  
   124  		if err != nil {
   125  			return err
   126  		}
   127  
   128  		policyID = &found.PolicyID
   129  
   130  		return nil
   131  	}
   132  }
   133  
   134  const testAccFWFirewallV1_basic_1 = `
   135  resource "openstack_fw_firewall_v1" "fw_1" {
   136    policy_id = "${openstack_fw_policy_v1.policy_1.id}"
   137  }
   138  
   139  resource "openstack_fw_policy_v1" "policy_1" {
   140    name = "policy_1"
   141  }
   142  `
   143  
   144  const testAccFWFirewallV1_basic_2 = `
   145  resource "openstack_fw_firewall_v1" "fw_1" {
   146    name = "fw_1"
   147    description = "terraform acceptance test"
   148    policy_id = "${openstack_fw_policy_v1.policy_2.id}"
   149    admin_state_up = true
   150  }
   151  
   152  resource "openstack_fw_policy_v1" "policy_2" {
   153    name = "policy_2"
   154  }
   155  `
   156  
   157  const testAccFWFirewallV1_timeout = `
   158  resource "openstack_fw_firewall_v1" "fw_1" {
   159    policy_id = "${openstack_fw_policy_v1.policy_1.id}"
   160  
   161    timeouts {
   162      create = "5m"
   163      update = "5m"
   164      delete = "5m"
   165    }
   166  }
   167  
   168  resource "openstack_fw_policy_v1" "policy_1" {
   169    name = "policy_1"
   170  }
   171  `