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