github.com/peterbale/terraform@v0.9.0-beta2.0.20170315142748-5723acd55547/builtin/providers/heroku/resource_heroku_app_test.go (about) 1 package heroku 2 3 import ( 4 "context" 5 "fmt" 6 "os" 7 "testing" 8 9 "github.com/cyberdelia/heroku-go/v3" 10 "github.com/hashicorp/terraform/helper/acctest" 11 "github.com/hashicorp/terraform/helper/resource" 12 "github.com/hashicorp/terraform/terraform" 13 ) 14 15 func TestAccHerokuApp_Basic(t *testing.T) { 16 var app heroku.AppInfoResult 17 appName := fmt.Sprintf("tftest-%s", acctest.RandString(10)) 18 19 resource.Test(t, resource.TestCase{ 20 PreCheck: func() { testAccPreCheck(t) }, 21 Providers: testAccProviders, 22 CheckDestroy: testAccCheckHerokuAppDestroy, 23 Steps: []resource.TestStep{ 24 { 25 Config: testAccCheckHerokuAppConfig_basic(appName), 26 Check: resource.ComposeTestCheckFunc( 27 testAccCheckHerokuAppExists("heroku_app.foobar", &app), 28 testAccCheckHerokuAppAttributes(&app, appName), 29 resource.TestCheckResourceAttr( 30 "heroku_app.foobar", "name", appName), 31 resource.TestCheckResourceAttr( 32 "heroku_app.foobar", "config_vars.0.FOO", "bar"), 33 ), 34 }, 35 }, 36 }) 37 } 38 39 func TestAccHerokuApp_NameChange(t *testing.T) { 40 var app heroku.AppInfoResult 41 appName := fmt.Sprintf("tftest-%s", acctest.RandString(10)) 42 appName2 := fmt.Sprintf("%s-v2", appName) 43 44 resource.Test(t, resource.TestCase{ 45 PreCheck: func() { testAccPreCheck(t) }, 46 Providers: testAccProviders, 47 CheckDestroy: testAccCheckHerokuAppDestroy, 48 Steps: []resource.TestStep{ 49 { 50 Config: testAccCheckHerokuAppConfig_basic(appName), 51 Check: resource.ComposeTestCheckFunc( 52 testAccCheckHerokuAppExists("heroku_app.foobar", &app), 53 testAccCheckHerokuAppAttributes(&app, appName), 54 resource.TestCheckResourceAttr( 55 "heroku_app.foobar", "name", appName), 56 resource.TestCheckResourceAttr( 57 "heroku_app.foobar", "config_vars.0.FOO", "bar"), 58 ), 59 }, 60 { 61 Config: testAccCheckHerokuAppConfig_updated(appName2), 62 Check: resource.ComposeTestCheckFunc( 63 testAccCheckHerokuAppExists("heroku_app.foobar", &app), 64 testAccCheckHerokuAppAttributesUpdated(&app, appName2), 65 resource.TestCheckResourceAttr( 66 "heroku_app.foobar", "name", appName2), 67 resource.TestCheckResourceAttr( 68 "heroku_app.foobar", "config_vars.0.FOO", "bing"), 69 resource.TestCheckResourceAttr( 70 "heroku_app.foobar", "config_vars.0.BAZ", "bar"), 71 ), 72 }, 73 }, 74 }) 75 } 76 77 func TestAccHerokuApp_NukeVars(t *testing.T) { 78 var app heroku.AppInfoResult 79 appName := fmt.Sprintf("tftest-%s", acctest.RandString(10)) 80 81 resource.Test(t, resource.TestCase{ 82 PreCheck: func() { testAccPreCheck(t) }, 83 Providers: testAccProviders, 84 CheckDestroy: testAccCheckHerokuAppDestroy, 85 Steps: []resource.TestStep{ 86 { 87 Config: testAccCheckHerokuAppConfig_basic(appName), 88 Check: resource.ComposeTestCheckFunc( 89 testAccCheckHerokuAppExists("heroku_app.foobar", &app), 90 testAccCheckHerokuAppAttributes(&app, appName), 91 resource.TestCheckResourceAttr( 92 "heroku_app.foobar", "name", appName), 93 resource.TestCheckResourceAttr( 94 "heroku_app.foobar", "config_vars.0.FOO", "bar"), 95 ), 96 }, 97 { 98 Config: testAccCheckHerokuAppConfig_no_vars(appName), 99 Check: resource.ComposeTestCheckFunc( 100 testAccCheckHerokuAppExists("heroku_app.foobar", &app), 101 testAccCheckHerokuAppAttributesNoVars(&app, appName), 102 resource.TestCheckResourceAttr( 103 "heroku_app.foobar", "name", appName), 104 resource.TestCheckNoResourceAttr( 105 "heroku_app.foobar", "config_vars.0.FOO"), 106 ), 107 }, 108 }, 109 }) 110 } 111 112 func TestAccHerokuApp_Organization(t *testing.T) { 113 var app heroku.OrganizationApp 114 appName := fmt.Sprintf("tftest-%s", acctest.RandString(10)) 115 org := os.Getenv("HEROKU_ORGANIZATION") 116 117 resource.Test(t, resource.TestCase{ 118 PreCheck: func() { 119 testAccPreCheck(t) 120 if org == "" { 121 t.Skip("HEROKU_ORGANIZATION is not set; skipping test.") 122 } 123 }, 124 Providers: testAccProviders, 125 CheckDestroy: testAccCheckHerokuAppDestroy, 126 Steps: []resource.TestStep{ 127 { 128 Config: testAccCheckHerokuAppConfig_organization(appName, org), 129 Check: resource.ComposeTestCheckFunc( 130 testAccCheckHerokuAppExistsOrg("heroku_app.foobar", &app), 131 testAccCheckHerokuAppAttributesOrg(&app, appName, org), 132 ), 133 }, 134 }, 135 }) 136 } 137 138 func testAccCheckHerokuAppDestroy(s *terraform.State) error { 139 client := testAccProvider.Meta().(*heroku.Service) 140 141 for _, rs := range s.RootModule().Resources { 142 if rs.Type != "heroku_app" { 143 continue 144 } 145 146 _, err := client.AppInfo(context.TODO(), rs.Primary.ID) 147 148 if err == nil { 149 return fmt.Errorf("App still exists") 150 } 151 } 152 153 return nil 154 } 155 156 func testAccCheckHerokuAppAttributes(app *heroku.AppInfoResult, appName string) resource.TestCheckFunc { 157 return func(s *terraform.State) error { 158 client := testAccProvider.Meta().(*heroku.Service) 159 160 if app.Region.Name != "us" { 161 return fmt.Errorf("Bad region: %s", app.Region.Name) 162 } 163 164 if app.Stack.Name != "cedar-14" { 165 return fmt.Errorf("Bad stack: %s", app.Stack.Name) 166 } 167 168 if app.Name != appName { 169 return fmt.Errorf("Bad name: %s", app.Name) 170 } 171 172 vars, err := client.ConfigVarInfoForApp(context.TODO(), app.Name) 173 if err != nil { 174 return err 175 } 176 177 if vars["FOO"] == nil || *vars["FOO"] != "bar" { 178 return fmt.Errorf("Bad config vars: %v", vars) 179 } 180 181 return nil 182 } 183 } 184 185 func testAccCheckHerokuAppAttributesUpdated(app *heroku.AppInfoResult, appName string) resource.TestCheckFunc { 186 return func(s *terraform.State) error { 187 client := testAccProvider.Meta().(*heroku.Service) 188 189 if app.Name != appName { 190 return fmt.Errorf("Bad name: %s", app.Name) 191 } 192 193 vars, err := client.ConfigVarInfoForApp(context.TODO(), app.Name) 194 if err != nil { 195 return err 196 } 197 198 // Make sure we kept the old one 199 if vars["FOO"] == nil || *vars["FOO"] != "bing" { 200 return fmt.Errorf("Bad config vars: %v", vars) 201 } 202 203 if vars["BAZ"] == nil || *vars["BAZ"] != "bar" { 204 return fmt.Errorf("Bad config vars: %v", vars) 205 } 206 207 return nil 208 209 } 210 } 211 212 func testAccCheckHerokuAppAttributesNoVars(app *heroku.AppInfoResult, appName string) resource.TestCheckFunc { 213 return func(s *terraform.State) error { 214 client := testAccProvider.Meta().(*heroku.Service) 215 216 if app.Name != appName { 217 return fmt.Errorf("Bad name: %s", app.Name) 218 } 219 220 vars, err := client.ConfigVarInfoForApp(context.TODO(), app.Name) 221 if err != nil { 222 return err 223 } 224 225 if len(vars) != 0 { 226 return fmt.Errorf("vars exist: %v", vars) 227 } 228 229 return nil 230 } 231 } 232 233 func testAccCheckHerokuAppAttributesOrg(app *heroku.OrganizationApp, appName string, org string) resource.TestCheckFunc { 234 return func(s *terraform.State) error { 235 client := testAccProvider.Meta().(*heroku.Service) 236 237 if app.Region.Name != "us" { 238 return fmt.Errorf("Bad region: %s", app.Region.Name) 239 } 240 241 if app.Stack.Name != "cedar-14" { 242 return fmt.Errorf("Bad stack: %s", app.Stack.Name) 243 } 244 245 if app.Name != appName { 246 return fmt.Errorf("Bad name: %s", app.Name) 247 } 248 249 if app.Organization == nil || app.Organization.Name != org { 250 return fmt.Errorf("Bad org: %v", app.Organization) 251 } 252 253 vars, err := client.ConfigVarInfoForApp(context.TODO(), app.Name) 254 if err != nil { 255 return err 256 } 257 258 if vars["FOO"] == nil || *vars["FOO"] != "bar" { 259 return fmt.Errorf("Bad config vars: %v", vars) 260 } 261 262 return nil 263 } 264 } 265 266 func testAccCheckHerokuAppExists(n string, app *heroku.AppInfoResult) resource.TestCheckFunc { 267 return func(s *terraform.State) error { 268 rs, ok := s.RootModule().Resources[n] 269 270 if !ok { 271 return fmt.Errorf("Not found: %s", n) 272 } 273 274 if rs.Primary.ID == "" { 275 return fmt.Errorf("No App Name is set") 276 } 277 278 client := testAccProvider.Meta().(*heroku.Service) 279 280 foundApp, err := client.AppInfo(context.TODO(), rs.Primary.ID) 281 282 if err != nil { 283 return err 284 } 285 286 if foundApp.Name != rs.Primary.ID { 287 return fmt.Errorf("App not found") 288 } 289 290 *app = *foundApp 291 292 return nil 293 } 294 } 295 296 func testAccCheckHerokuAppExistsOrg(n string, app *heroku.OrganizationApp) resource.TestCheckFunc { 297 return func(s *terraform.State) error { 298 rs, ok := s.RootModule().Resources[n] 299 300 if !ok { 301 return fmt.Errorf("Not found: %s", n) 302 } 303 304 if rs.Primary.ID == "" { 305 return fmt.Errorf("No App Name is set") 306 } 307 308 client := testAccProvider.Meta().(*heroku.Service) 309 310 foundApp, err := client.OrganizationAppInfo(context.TODO(), rs.Primary.ID) 311 312 if err != nil { 313 return err 314 } 315 316 if foundApp.Name != rs.Primary.ID { 317 return fmt.Errorf("App not found") 318 } 319 320 *app = *foundApp 321 322 return nil 323 } 324 } 325 326 func testAccCheckHerokuAppConfig_basic(appName string) string { 327 return fmt.Sprintf(` 328 resource "heroku_app" "foobar" { 329 name = "%s" 330 region = "us" 331 332 config_vars { 333 FOO = "bar" 334 } 335 }`, appName) 336 } 337 338 func testAccCheckHerokuAppConfig_updated(appName string) string { 339 return fmt.Sprintf(` 340 resource "heroku_app" "foobar" { 341 name = "%s" 342 region = "us" 343 344 config_vars { 345 FOO = "bing" 346 BAZ = "bar" 347 } 348 }`, appName) 349 } 350 351 func testAccCheckHerokuAppConfig_no_vars(appName string) string { 352 return fmt.Sprintf(` 353 resource "heroku_app" "foobar" { 354 name = "%s" 355 region = "us" 356 }`, appName) 357 } 358 359 func testAccCheckHerokuAppConfig_organization(appName, org string) string { 360 return fmt.Sprintf(` 361 resource "heroku_app" "foobar" { 362 name = "%s" 363 region = "us" 364 365 organization { 366 name = "%s" 367 } 368 369 config_vars { 370 FOO = "bar" 371 } 372 }`, appName, org) 373 }