github.com/mohanarpit/terraform@v0.6.16-0.20160909104007-291f29853544/builtin/providers/azurerm/resource_arm_route_table_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 TestResourceAzureRMRouteTableNextHopType_validation(t *testing.T) {
    14  	cases := []struct {
    15  		Value    string
    16  		ErrCount int
    17  	}{
    18  		{
    19  			Value:    "Random",
    20  			ErrCount: 1,
    21  		},
    22  		{
    23  			Value:    "VirtualNetworkGateway",
    24  			ErrCount: 0,
    25  		},
    26  		{
    27  			Value:    "VNETLocal",
    28  			ErrCount: 0,
    29  		},
    30  		{
    31  			Value:    "Internet",
    32  			ErrCount: 0,
    33  		},
    34  		{
    35  			Value:    "VirtualAppliance",
    36  			ErrCount: 0,
    37  		},
    38  		{
    39  			Value:    "None",
    40  			ErrCount: 0,
    41  		},
    42  		{
    43  			Value:    "VIRTUALNETWORKGATEWAY",
    44  			ErrCount: 0,
    45  		},
    46  		{
    47  			Value:    "virtualnetworkgateway",
    48  			ErrCount: 0,
    49  		},
    50  	}
    51  
    52  	for _, tc := range cases {
    53  		_, errors := validateRouteTableNextHopType(tc.Value, "azurerm_route_table")
    54  
    55  		if len(errors) != tc.ErrCount {
    56  			t.Fatalf("Expected the Azure RM Route Table nextHopType to trigger a validation error")
    57  		}
    58  	}
    59  }
    60  
    61  func TestAccAzureRMRouteTable_basic(t *testing.T) {
    62  
    63  	ri := acctest.RandInt()
    64  	config := fmt.Sprintf(testAccAzureRMRouteTable_basic, ri, ri)
    65  
    66  	resource.Test(t, resource.TestCase{
    67  		PreCheck:     func() { testAccPreCheck(t) },
    68  		Providers:    testAccProviders,
    69  		CheckDestroy: testCheckAzureRMRouteTableDestroy,
    70  		Steps: []resource.TestStep{
    71  			resource.TestStep{
    72  				Config: config,
    73  				Check: resource.ComposeTestCheckFunc(
    74  					testCheckAzureRMRouteTableExists("azurerm_route_table.test"),
    75  				),
    76  			},
    77  		},
    78  	})
    79  }
    80  
    81  func TestAccAzureRMRouteTable_withTags(t *testing.T) {
    82  
    83  	ri := acctest.RandInt()
    84  	preConfig := fmt.Sprintf(testAccAzureRMRouteTable_withTags, ri, ri)
    85  	postConfig := fmt.Sprintf(testAccAzureRMRouteTable_withTagsUpdate, ri, ri)
    86  
    87  	resource.Test(t, resource.TestCase{
    88  		PreCheck:     func() { testAccPreCheck(t) },
    89  		Providers:    testAccProviders,
    90  		CheckDestroy: testCheckAzureRMRouteTableDestroy,
    91  		Steps: []resource.TestStep{
    92  			resource.TestStep{
    93  				Config: preConfig,
    94  				Check: resource.ComposeTestCheckFunc(
    95  					testCheckAzureRMRouteTableExists("azurerm_route_table.test"),
    96  					resource.TestCheckResourceAttr(
    97  						"azurerm_route_table.test", "tags.%", "2"),
    98  					resource.TestCheckResourceAttr(
    99  						"azurerm_route_table.test", "tags.environment", "Production"),
   100  					resource.TestCheckResourceAttr(
   101  						"azurerm_route_table.test", "tags.cost_center", "MSFT"),
   102  				),
   103  			},
   104  
   105  			resource.TestStep{
   106  				Config: postConfig,
   107  				Check: resource.ComposeTestCheckFunc(
   108  					testCheckAzureRMRouteTableExists("azurerm_route_table.test"),
   109  					resource.TestCheckResourceAttr(
   110  						"azurerm_route_table.test", "tags.%", "1"),
   111  					resource.TestCheckResourceAttr(
   112  						"azurerm_route_table.test", "tags.environment", "staging"),
   113  				),
   114  			},
   115  		},
   116  	})
   117  }
   118  
   119  func TestAccAzureRMRouteTable_multipleRoutes(t *testing.T) {
   120  
   121  	ri := acctest.RandInt()
   122  	preConfig := fmt.Sprintf(testAccAzureRMRouteTable_basic, ri, ri)
   123  	postConfig := fmt.Sprintf(testAccAzureRMRouteTable_multipleRoutes, ri, ri)
   124  
   125  	resource.Test(t, resource.TestCase{
   126  		PreCheck:     func() { testAccPreCheck(t) },
   127  		Providers:    testAccProviders,
   128  		CheckDestroy: testCheckAzureRMRouteTableDestroy,
   129  		Steps: []resource.TestStep{
   130  			resource.TestStep{
   131  				Config: preConfig,
   132  				Check: resource.ComposeTestCheckFunc(
   133  					testCheckAzureRMRouteTableExists("azurerm_route_table.test"),
   134  					resource.TestCheckResourceAttr(
   135  						"azurerm_route_table.test", "route.#", "1"),
   136  				),
   137  			},
   138  
   139  			resource.TestStep{
   140  				Config: postConfig,
   141  				Check: resource.ComposeTestCheckFunc(
   142  					testCheckAzureRMRouteTableExists("azurerm_route_table.test"),
   143  					resource.TestCheckResourceAttr(
   144  						"azurerm_route_table.test", "route.#", "2"),
   145  				),
   146  			},
   147  		},
   148  	})
   149  }
   150  
   151  func testCheckAzureRMRouteTableExists(name string) resource.TestCheckFunc {
   152  	return func(s *terraform.State) error {
   153  
   154  		rs, ok := s.RootModule().Resources[name]
   155  		if !ok {
   156  			return fmt.Errorf("Not found: %s", name)
   157  		}
   158  
   159  		name := rs.Primary.Attributes["name"]
   160  		resourceGroup, hasResourceGroup := rs.Primary.Attributes["resource_group_name"]
   161  		if !hasResourceGroup {
   162  			return fmt.Errorf("Bad: no resource group found in state for route table: %s", name)
   163  		}
   164  
   165  		conn := testAccProvider.Meta().(*ArmClient).routeTablesClient
   166  
   167  		resp, err := conn.Get(resourceGroup, name, "")
   168  		if err != nil {
   169  			return fmt.Errorf("Bad: Get on routeTablesClient: %s", err)
   170  		}
   171  
   172  		if resp.StatusCode == http.StatusNotFound {
   173  			return fmt.Errorf("Bad: Route Table %q (resource group: %q) does not exist", name, resourceGroup)
   174  		}
   175  
   176  		return nil
   177  	}
   178  }
   179  
   180  func testCheckAzureRMRouteTableDestroy(s *terraform.State) error {
   181  	conn := testAccProvider.Meta().(*ArmClient).routeTablesClient
   182  
   183  	for _, rs := range s.RootModule().Resources {
   184  		if rs.Type != "azurerm_route_table" {
   185  			continue
   186  		}
   187  
   188  		name := rs.Primary.Attributes["name"]
   189  		resourceGroup := rs.Primary.Attributes["resource_group_name"]
   190  
   191  		resp, err := conn.Get(resourceGroup, name, "")
   192  
   193  		if err != nil {
   194  			return nil
   195  		}
   196  
   197  		if resp.StatusCode != http.StatusNotFound {
   198  			return fmt.Errorf("Route Table still exists:\n%#v", resp.Properties)
   199  		}
   200  	}
   201  
   202  	return nil
   203  }
   204  
   205  var testAccAzureRMRouteTable_basic = `
   206  resource "azurerm_resource_group" "test" {
   207      name = "acctestrg-%d"
   208      location = "West US"
   209  }
   210  
   211  resource "azurerm_route_table" "test" {
   212      name = "acctestrt%d"
   213      location = "West US"
   214      resource_group_name = "${azurerm_resource_group.test.name}"
   215  
   216      route {
   217      	name = "route1"
   218      	address_prefix = "*"
   219      	next_hop_type = "internet"
   220      }
   221  }
   222  `
   223  
   224  var testAccAzureRMRouteTable_multipleRoutes = `
   225  resource "azurerm_resource_group" "test" {
   226      name = "acctestrg-%d"
   227      location = "West US"
   228  }
   229  
   230  resource "azurerm_route_table" "test" {
   231      name = "acctestrt%d"
   232      location = "West US"
   233      resource_group_name = "${azurerm_resource_group.test.name}"
   234  
   235      route {
   236      	name = "route1"
   237      	address_prefix = "*"
   238      	next_hop_type = "internet"
   239      }
   240  
   241      route {
   242      	name = "route2"
   243      	address_prefix = "*"
   244      	next_hop_type = "virtualappliance"
   245      }
   246  }
   247  `
   248  
   249  var testAccAzureRMRouteTable_withTags = `
   250  resource "azurerm_resource_group" "test" {
   251      name = "acctestrg-%d"
   252      location = "West US"
   253  }
   254  
   255  resource "azurerm_route_table" "test" {
   256      name = "acctestrt%d"
   257      location = "West US"
   258      resource_group_name = "${azurerm_resource_group.test.name}"
   259  
   260      route {
   261      	name = "route1"
   262      	address_prefix = "*"
   263      	next_hop_type = "internet"
   264      }
   265  
   266      tags {
   267  	environment = "Production"
   268  	cost_center = "MSFT"
   269      }
   270  }
   271  `
   272  
   273  var testAccAzureRMRouteTable_withTagsUpdate = `
   274  resource "azurerm_resource_group" "test" {
   275      name = "acctestrg-%d"
   276      location = "West US"
   277  }
   278  
   279  resource "azurerm_route_table" "test" {
   280      name = "acctestrt%d"
   281      location = "West US"
   282      resource_group_name = "${azurerm_resource_group.test.name}"
   283  
   284      route {
   285      	name = "route1"
   286      	address_prefix = "*"
   287      	next_hop_type = "internet"
   288      }
   289  
   290      tags {
   291  	environment = "staging"
   292      }
   293  }
   294  `