github.com/koding/terraform@v0.6.4-0.20170608090606-5d7e0339779d/builtin/providers/azurerm/resource_arm_local_network_gateway_test.go (about)

     1  package azurerm
     2  
     3  import (
     4  	"fmt"
     5  	"net/http"
     6  	"testing"
     7  
     8  	"github.com/hashicorp/terraform/helper/acctest"
     9  	"github.com/hashicorp/terraform/helper/resource"
    10  	"github.com/hashicorp/terraform/terraform"
    11  )
    12  
    13  func TestAccAzureRMLocalNetworkGateway_basic(t *testing.T) {
    14  	name := "azurerm_local_network_gateway.test"
    15  
    16  	rInt := acctest.RandInt()
    17  	resource.Test(t, resource.TestCase{
    18  		PreCheck:     func() { testAccPreCheck(t) },
    19  		Providers:    testAccProviders,
    20  		CheckDestroy: testCheckAzureRMLocalNetworkGatewayDestroy,
    21  		Steps: []resource.TestStep{
    22  			{
    23  				Config: testAccAzureRMLocalNetworkGatewayConfig_basic(rInt),
    24  				Check: resource.ComposeTestCheckFunc(
    25  					testCheckAzureRMLocalNetworkGatewayExists(name),
    26  					resource.TestCheckResourceAttr(name, "gateway_address", "127.0.0.1"),
    27  					resource.TestCheckResourceAttr(name, "address_space.0", "127.0.0.0/8"),
    28  				),
    29  			},
    30  		},
    31  	})
    32  }
    33  
    34  func TestAccAzureRMLocalNetworkGateway_disappears(t *testing.T) {
    35  	name := "azurerm_local_network_gateway.test"
    36  	rInt := acctest.RandInt()
    37  
    38  	resource.Test(t, resource.TestCase{
    39  		PreCheck:     func() { testAccPreCheck(t) },
    40  		Providers:    testAccProviders,
    41  		CheckDestroy: testCheckAzureRMLocalNetworkGatewayDestroy,
    42  		Steps: []resource.TestStep{
    43  			{
    44  				Config: testAccAzureRMLocalNetworkGatewayConfig_basic(rInt),
    45  				Check: resource.ComposeTestCheckFunc(
    46  					testCheckAzureRMLocalNetworkGatewayExists(name),
    47  					resource.TestCheckResourceAttr(name, "gateway_address", "127.0.0.1"),
    48  					resource.TestCheckResourceAttr(name, "address_space.0", "127.0.0.0/8"),
    49  					testCheckAzureRMLocalNetworkGatewayDisappears(name),
    50  				),
    51  				ExpectNonEmptyPlan: true,
    52  			},
    53  		},
    54  	})
    55  }
    56  
    57  // testCheckAzureRMLocalNetworkGatewayExists returns the resurce.TestCheckFunc
    58  // which checks whether or not the expected local network gateway exists both
    59  // in the schema, and on Azure.
    60  func testCheckAzureRMLocalNetworkGatewayExists(name string) resource.TestCheckFunc {
    61  	return func(s *terraform.State) error {
    62  		// first check within the schema for the local network gateway:
    63  		res, ok := s.RootModule().Resources[name]
    64  		if !ok {
    65  			return fmt.Errorf("Local network gateway '%s' not found.", name)
    66  		}
    67  
    68  		// then, extract the name and the resource group:
    69  		id, err := parseAzureResourceID(res.Primary.ID)
    70  		if err != nil {
    71  			return err
    72  		}
    73  		localNetName := id.Path["localNetworkGateways"]
    74  		resGrp := id.ResourceGroup
    75  
    76  		// and finally, check that it exists on Azure:
    77  		lnetClient := testAccProvider.Meta().(*ArmClient).localNetConnClient
    78  
    79  		resp, err := lnetClient.Get(resGrp, localNetName)
    80  		if err != nil {
    81  			if resp.StatusCode == http.StatusNotFound {
    82  				return fmt.Errorf("Local network gateway '%s' (resource group '%s') does not exist on Azure.", localNetName, resGrp)
    83  			}
    84  			return fmt.Errorf("Error reading the state of local network gateway '%s'.", localNetName)
    85  		}
    86  
    87  		return nil
    88  	}
    89  }
    90  
    91  func testCheckAzureRMLocalNetworkGatewayDisappears(name string) resource.TestCheckFunc {
    92  	return func(s *terraform.State) error {
    93  		// first check within the schema for the local network gateway:
    94  		res, ok := s.RootModule().Resources[name]
    95  		if !ok {
    96  			return fmt.Errorf("Local network gateway '%s' not found.", name)
    97  		}
    98  
    99  		// then, extract the name and the resource group:
   100  		id, err := parseAzureResourceID(res.Primary.ID)
   101  		if err != nil {
   102  			return err
   103  		}
   104  		localNetName := id.Path["localNetworkGateways"]
   105  		resGrp := id.ResourceGroup
   106  
   107  		// and finally, check that it exists on Azure:
   108  		lnetClient := testAccProvider.Meta().(*ArmClient).localNetConnClient
   109  
   110  		deleteResp, error := lnetClient.Delete(resGrp, localNetName, make(chan struct{}))
   111  		resp := <-deleteResp
   112  		err = <-error
   113  		if err != nil {
   114  			if resp.StatusCode == http.StatusNotFound {
   115  				return fmt.Errorf("Local network gateway '%s' (resource group '%s') does not exist on Azure.", localNetName, resGrp)
   116  			}
   117  			return fmt.Errorf("Error deleting the state of local network gateway '%s'.", localNetName)
   118  		}
   119  
   120  		return nil
   121  	}
   122  }
   123  
   124  func testCheckAzureRMLocalNetworkGatewayDestroy(s *terraform.State) error {
   125  	for _, res := range s.RootModule().Resources {
   126  		if res.Type != "azurerm_local_network_gateway" {
   127  			continue
   128  		}
   129  
   130  		id, err := parseAzureResourceID(res.Primary.ID)
   131  		if err != nil {
   132  			return err
   133  		}
   134  		localNetName := id.Path["localNetworkGateways"]
   135  		resGrp := id.ResourceGroup
   136  
   137  		lnetClient := testAccProvider.Meta().(*ArmClient).localNetConnClient
   138  		resp, err := lnetClient.Get(resGrp, localNetName)
   139  
   140  		if err != nil {
   141  			return nil
   142  		}
   143  
   144  		if resp.StatusCode != http.StatusNotFound {
   145  			return fmt.Errorf("Local network gateway still exists:\n%#v", resp.LocalNetworkGatewayPropertiesFormat)
   146  		}
   147  	}
   148  
   149  	return nil
   150  }
   151  
   152  func testAccAzureRMLocalNetworkGatewayConfig_basic(rInt int) string {
   153  	return fmt.Sprintf(`
   154  resource "azurerm_resource_group" "test" {
   155      name = "acctest-%d"
   156      location = "West US"
   157  }
   158  
   159  resource "azurerm_local_network_gateway" "test" {
   160  	name = "acctestlng-%d"
   161  	location = "${azurerm_resource_group.test.location}"
   162  	resource_group_name = "${azurerm_resource_group.test.name}"
   163  	gateway_address = "127.0.0.1"
   164  	address_space = ["127.0.0.0/8"]
   165  }
   166  `, rInt, rInt)
   167  }