github.com/vtorhonen/terraform@v0.9.0-beta2.0.20170307220345-5d894e4ffda7/builtin/providers/azurerm/resource_arm_subnet_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 TestAccAzureRMSubnet_basic(t *testing.T) {
    14  
    15  	ri := acctest.RandInt()
    16  	config := fmt.Sprintf(testAccAzureRMSubnet_basic, ri, ri, ri)
    17  
    18  	resource.Test(t, resource.TestCase{
    19  		PreCheck:     func() { testAccPreCheck(t) },
    20  		Providers:    testAccProviders,
    21  		CheckDestroy: testCheckAzureRMSubnetDestroy,
    22  		Steps: []resource.TestStep{
    23  			resource.TestStep{
    24  				Config: config,
    25  				Check: resource.ComposeTestCheckFunc(
    26  					testCheckAzureRMSubnetExists("azurerm_subnet.test"),
    27  				),
    28  			},
    29  		},
    30  	})
    31  }
    32  
    33  func TestAccAzureRMSubnet_disappears(t *testing.T) {
    34  
    35  	ri := acctest.RandInt()
    36  	config := fmt.Sprintf(testAccAzureRMSubnet_basic, ri, ri, ri)
    37  
    38  	resource.Test(t, resource.TestCase{
    39  		PreCheck:     func() { testAccPreCheck(t) },
    40  		Providers:    testAccProviders,
    41  		CheckDestroy: testCheckAzureRMSubnetDestroy,
    42  		Steps: []resource.TestStep{
    43  			resource.TestStep{
    44  				Config: config,
    45  				Check: resource.ComposeTestCheckFunc(
    46  					testCheckAzureRMSubnetExists("azurerm_subnet.test"),
    47  					testCheckAzureRMSubnetDisappears("azurerm_subnet.test"),
    48  				),
    49  				ExpectNonEmptyPlan: true,
    50  			},
    51  		},
    52  	})
    53  }
    54  
    55  func testCheckAzureRMSubnetExists(name string) resource.TestCheckFunc {
    56  	return func(s *terraform.State) error {
    57  		// Ensure we have enough information in state to look up in API
    58  		rs, ok := s.RootModule().Resources[name]
    59  		if !ok {
    60  			return fmt.Errorf("Not found: %s", name)
    61  		}
    62  
    63  		name := rs.Primary.Attributes["name"]
    64  		vnetName := rs.Primary.Attributes["virtual_network_name"]
    65  		resourceGroup, hasResourceGroup := rs.Primary.Attributes["resource_group_name"]
    66  		if !hasResourceGroup {
    67  			return fmt.Errorf("Bad: no resource group found in state for subnet: %s", name)
    68  		}
    69  
    70  		conn := testAccProvider.Meta().(*ArmClient).subnetClient
    71  
    72  		resp, err := conn.Get(resourceGroup, vnetName, name, "")
    73  		if err != nil {
    74  			return fmt.Errorf("Bad: Get on subnetClient: %s", err)
    75  		}
    76  
    77  		if resp.StatusCode == http.StatusNotFound {
    78  			return fmt.Errorf("Bad: Subnet %q (resource group: %q) does not exist", name, resourceGroup)
    79  		}
    80  
    81  		return nil
    82  	}
    83  }
    84  
    85  func testCheckAzureRMSubnetDisappears(name string) resource.TestCheckFunc {
    86  	return func(s *terraform.State) error {
    87  		// Ensure we have enough information in state to look up in API
    88  		rs, ok := s.RootModule().Resources[name]
    89  		if !ok {
    90  			return fmt.Errorf("Not found: %s", name)
    91  		}
    92  
    93  		name := rs.Primary.Attributes["name"]
    94  		vnetName := rs.Primary.Attributes["virtual_network_name"]
    95  		resourceGroup, hasResourceGroup := rs.Primary.Attributes["resource_group_name"]
    96  		if !hasResourceGroup {
    97  			return fmt.Errorf("Bad: no resource group found in state for subnet: %s", name)
    98  		}
    99  
   100  		conn := testAccProvider.Meta().(*ArmClient).subnetClient
   101  
   102  		_, err := conn.Delete(resourceGroup, vnetName, name, make(chan struct{}))
   103  		if err != nil {
   104  			return fmt.Errorf("Bad: Delete on subnetClient: %s", err)
   105  		}
   106  
   107  		return nil
   108  	}
   109  }
   110  
   111  func testCheckAzureRMSubnetDestroy(s *terraform.State) error {
   112  	conn := testAccProvider.Meta().(*ArmClient).subnetClient
   113  
   114  	for _, rs := range s.RootModule().Resources {
   115  		if rs.Type != "azurerm_subnet" {
   116  			continue
   117  		}
   118  
   119  		name := rs.Primary.Attributes["name"]
   120  		vnetName := rs.Primary.Attributes["virtual_network_name"]
   121  		resourceGroup := rs.Primary.Attributes["resource_group_name"]
   122  
   123  		resp, err := conn.Get(resourceGroup, vnetName, name, "")
   124  
   125  		if err != nil {
   126  			return nil
   127  		}
   128  
   129  		if resp.StatusCode != http.StatusNotFound {
   130  			return fmt.Errorf("Subnet still exists:\n%#v", resp.SubnetPropertiesFormat)
   131  		}
   132  	}
   133  
   134  	return nil
   135  }
   136  
   137  var testAccAzureRMSubnet_basic = `
   138  resource "azurerm_resource_group" "test" {
   139      name = "acctestRG-%d"
   140      location = "West US"
   141  }
   142  
   143  resource "azurerm_virtual_network" "test" {
   144      name = "acctestvirtnet%d"
   145      address_space = ["10.0.0.0/16"]
   146      location = "West US"
   147      resource_group_name = "${azurerm_resource_group.test.name}"
   148  }
   149  
   150  resource "azurerm_subnet" "test" {
   151      name = "acctestsubnet%d"
   152      resource_group_name = "${azurerm_resource_group.test.name}"
   153      virtual_network_name = "${azurerm_virtual_network.test.name}"
   154      address_prefix = "10.0.2.0/24"
   155  }
   156  `