github.com/minamijoyo/terraform@v0.7.8-0.20161029001309-18b3736ba44b/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_disappears(t *testing.T) { 34 35 ri := acctest.RandInt() 36 config := fmt.Sprintf(testAccAzureRMRoute_basic, ri, ri, ri) 37 38 resource.Test(t, resource.TestCase{ 39 PreCheck: func() { testAccPreCheck(t) }, 40 Providers: testAccProviders, 41 CheckDestroy: testCheckAzureRMRouteDestroy, 42 Steps: []resource.TestStep{ 43 { 44 Config: config, 45 Check: resource.ComposeTestCheckFunc( 46 testCheckAzureRMRouteExists("azurerm_route.test"), 47 testCheckAzureRMRouteDisappears("azurerm_route.test"), 48 ), 49 ExpectNonEmptyPlan: true, 50 }, 51 }, 52 }) 53 } 54 55 func TestAccAzureRMRoute_multipleRoutes(t *testing.T) { 56 57 ri := acctest.RandInt() 58 preConfig := fmt.Sprintf(testAccAzureRMRoute_basic, ri, ri, ri) 59 postConfig := fmt.Sprintf(testAccAzureRMRoute_multipleRoutes, ri, ri, ri) 60 61 resource.Test(t, resource.TestCase{ 62 PreCheck: func() { testAccPreCheck(t) }, 63 Providers: testAccProviders, 64 CheckDestroy: testCheckAzureRMRouteDestroy, 65 Steps: []resource.TestStep{ 66 { 67 Config: preConfig, 68 Check: resource.ComposeTestCheckFunc( 69 testCheckAzureRMRouteExists("azurerm_route.test"), 70 ), 71 }, 72 73 { 74 Config: postConfig, 75 Check: resource.ComposeTestCheckFunc( 76 testCheckAzureRMRouteExists("azurerm_route.test1"), 77 ), 78 }, 79 }, 80 }) 81 } 82 83 func testCheckAzureRMRouteExists(name string) resource.TestCheckFunc { 84 return func(s *terraform.State) error { 85 86 rs, ok := s.RootModule().Resources[name] 87 if !ok { 88 return fmt.Errorf("Not found: %s", name) 89 } 90 91 name := rs.Primary.Attributes["name"] 92 rtName := rs.Primary.Attributes["route_table_name"] 93 resourceGroup, hasResourceGroup := rs.Primary.Attributes["resource_group_name"] 94 if !hasResourceGroup { 95 return fmt.Errorf("Bad: no resource group found in state for route: %s", name) 96 } 97 98 conn := testAccProvider.Meta().(*ArmClient).routesClient 99 100 resp, err := conn.Get(resourceGroup, rtName, name) 101 if err != nil { 102 return fmt.Errorf("Bad: Get on routesClient: %s", err) 103 } 104 105 if resp.StatusCode == http.StatusNotFound { 106 return fmt.Errorf("Bad: Route %q (resource group: %q) does not exist", name, resourceGroup) 107 } 108 109 return nil 110 } 111 } 112 113 func testCheckAzureRMRouteDisappears(name string) resource.TestCheckFunc { 114 return func(s *terraform.State) error { 115 116 rs, ok := s.RootModule().Resources[name] 117 if !ok { 118 return fmt.Errorf("Not found: %s", name) 119 } 120 121 name := rs.Primary.Attributes["name"] 122 rtName := rs.Primary.Attributes["route_table_name"] 123 resourceGroup, hasResourceGroup := rs.Primary.Attributes["resource_group_name"] 124 if !hasResourceGroup { 125 return fmt.Errorf("Bad: no resource group found in state for route: %s", name) 126 } 127 128 conn := testAccProvider.Meta().(*ArmClient).routesClient 129 130 _, err := conn.Delete(resourceGroup, rtName, name, make(chan struct{})) 131 if err != nil { 132 return fmt.Errorf("Bad: Delete on routesClient: %s", err) 133 } 134 135 return nil 136 } 137 } 138 139 func testCheckAzureRMRouteDestroy(s *terraform.State) error { 140 conn := testAccProvider.Meta().(*ArmClient).routesClient 141 142 for _, rs := range s.RootModule().Resources { 143 if rs.Type != "azurerm_route" { 144 continue 145 } 146 147 name := rs.Primary.Attributes["name"] 148 rtName := rs.Primary.Attributes["route_table_name"] 149 resourceGroup := rs.Primary.Attributes["resource_group_name"] 150 151 resp, err := conn.Get(resourceGroup, rtName, name) 152 153 if err != nil { 154 return nil 155 } 156 157 if resp.StatusCode != http.StatusNotFound { 158 return fmt.Errorf("Route still exists:\n%#v", resp.Properties) 159 } 160 } 161 162 return nil 163 } 164 165 var testAccAzureRMRoute_basic = ` 166 resource "azurerm_resource_group" "test" { 167 name = "acctestRG-%d" 168 location = "West US" 169 } 170 171 resource "azurerm_route_table" "test" { 172 name = "acctestrt%d" 173 location = "West US" 174 resource_group_name = "${azurerm_resource_group.test.name}" 175 } 176 177 resource "azurerm_route" "test" { 178 name = "acctestroute%d" 179 resource_group_name = "${azurerm_resource_group.test.name}" 180 route_table_name = "${azurerm_route_table.test.name}" 181 182 address_prefix = "10.1.0.0/16" 183 next_hop_type = "vnetlocal" 184 } 185 ` 186 187 var testAccAzureRMRoute_multipleRoutes = ` 188 resource "azurerm_resource_group" "test" { 189 name = "acctestRG-%d" 190 location = "West US" 191 } 192 193 resource "azurerm_route_table" "test" { 194 name = "acctestrt%d" 195 location = "West US" 196 resource_group_name = "${azurerm_resource_group.test.name}" 197 } 198 199 resource "azurerm_route" "test1" { 200 name = "acctestroute%d" 201 resource_group_name = "${azurerm_resource_group.test.name}" 202 route_table_name = "${azurerm_route_table.test.name}" 203 204 address_prefix = "10.2.0.0/16" 205 next_hop_type = "none" 206 } 207 `