github.com/mohanarpit/terraform@v0.6.16-0.20160909104007-291f29853544/builtin/providers/azurerm/resource_arm_route_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 TestAccAzureRMRoute_basic(t *testing.T) {
    14  
    15  	ri := acctest.RandInt()
    16  	config := fmt.Sprintf(testAccAzureRMRoute_basic, ri, ri, ri)
    17  
    18  	resource.Test(t, resource.TestCase{
    19  		PreCheck:     func() { testAccPreCheck(t) },
    20  		Providers:    testAccProviders,
    21  		CheckDestroy: testCheckAzureRMRouteDestroy,
    22  		Steps: []resource.TestStep{
    23  			{
    24  				Config: config,
    25  				Check: resource.ComposeTestCheckFunc(
    26  					testCheckAzureRMRouteExists("azurerm_route.test"),
    27  				),
    28  			},
    29  		},
    30  	})
    31  }
    32  
    33  func TestAccAzureRMRoute_multipleRoutes(t *testing.T) {
    34  
    35  	ri := acctest.RandInt()
    36  	preConfig := fmt.Sprintf(testAccAzureRMRoute_basic, ri, ri, ri)
    37  	postConfig := fmt.Sprintf(testAccAzureRMRoute_multipleRoutes, ri, ri, ri)
    38  
    39  	resource.Test(t, resource.TestCase{
    40  		PreCheck:     func() { testAccPreCheck(t) },
    41  		Providers:    testAccProviders,
    42  		CheckDestroy: testCheckAzureRMRouteDestroy,
    43  		Steps: []resource.TestStep{
    44  			{
    45  				Config: preConfig,
    46  				Check: resource.ComposeTestCheckFunc(
    47  					testCheckAzureRMRouteExists("azurerm_route.test"),
    48  				),
    49  			},
    50  
    51  			{
    52  				Config: postConfig,
    53  				Check: resource.ComposeTestCheckFunc(
    54  					testCheckAzureRMRouteExists("azurerm_route.test1"),
    55  				),
    56  			},
    57  		},
    58  	})
    59  }
    60  
    61  func testCheckAzureRMRouteExists(name string) resource.TestCheckFunc {
    62  	return func(s *terraform.State) error {
    63  
    64  		rs, ok := s.RootModule().Resources[name]
    65  		if !ok {
    66  			return fmt.Errorf("Not found: %s", name)
    67  		}
    68  
    69  		name := rs.Primary.Attributes["name"]
    70  		rtName := rs.Primary.Attributes["route_table_name"]
    71  		resourceGroup, hasResourceGroup := rs.Primary.Attributes["resource_group_name"]
    72  		if !hasResourceGroup {
    73  			return fmt.Errorf("Bad: no resource group found in state for route: %s", name)
    74  		}
    75  
    76  		conn := testAccProvider.Meta().(*ArmClient).routesClient
    77  
    78  		resp, err := conn.Get(resourceGroup, rtName, name)
    79  		if err != nil {
    80  			return fmt.Errorf("Bad: Get on routesClient: %s", err)
    81  		}
    82  
    83  		if resp.StatusCode == http.StatusNotFound {
    84  			return fmt.Errorf("Bad: Route %q (resource group: %q) does not exist", name, resourceGroup)
    85  		}
    86  
    87  		return nil
    88  	}
    89  }
    90  
    91  func testCheckAzureRMRouteDestroy(s *terraform.State) error {
    92  	conn := testAccProvider.Meta().(*ArmClient).routesClient
    93  
    94  	for _, rs := range s.RootModule().Resources {
    95  		if rs.Type != "azurerm_route" {
    96  			continue
    97  		}
    98  
    99  		name := rs.Primary.Attributes["name"]
   100  		rtName := rs.Primary.Attributes["route_table_name"]
   101  		resourceGroup := rs.Primary.Attributes["resource_group_name"]
   102  
   103  		resp, err := conn.Get(resourceGroup, rtName, name)
   104  
   105  		if err != nil {
   106  			return nil
   107  		}
   108  
   109  		if resp.StatusCode != http.StatusNotFound {
   110  			return fmt.Errorf("Route still exists:\n%#v", resp.Properties)
   111  		}
   112  	}
   113  
   114  	return nil
   115  }
   116  
   117  var testAccAzureRMRoute_basic = `
   118  resource "azurerm_resource_group" "test" {
   119      name = "acctestrg-%d"
   120      location = "West US"
   121  }
   122  
   123  resource "azurerm_route_table" "test" {
   124      name = "acctestrt%d"
   125      location = "West US"
   126      resource_group_name = "${azurerm_resource_group.test.name}"
   127  }
   128  
   129  resource "azurerm_route" "test" {
   130      name = "acctestroute%d"
   131      resource_group_name = "${azurerm_resource_group.test.name}"
   132      route_table_name = "${azurerm_route_table.test.name}"
   133  
   134      address_prefix = "10.1.0.0/16"
   135      next_hop_type = "vnetlocal"
   136  }
   137  `
   138  
   139  var testAccAzureRMRoute_multipleRoutes = `
   140  resource "azurerm_resource_group" "test" {
   141      name = "acctestrg-%d"
   142      location = "West US"
   143  }
   144  
   145  resource "azurerm_route_table" "test" {
   146      name = "acctestrt%d"
   147      location = "West US"
   148      resource_group_name = "${azurerm_resource_group.test.name}"
   149  }
   150  
   151  resource "azurerm_route" "test1" {
   152      name = "acctestroute%d"
   153      resource_group_name = "${azurerm_resource_group.test.name}"
   154      route_table_name = "${azurerm_route_table.test.name}"
   155  
   156      address_prefix = "10.2.0.0/16"
   157      next_hop_type = "none"
   158  }
   159  `