github.com/danp/terraform@v0.9.5-0.20170426144147-39d740081351/builtin/providers/heroku/resource_heroku_addon_test.go (about) 1 package heroku 2 3 import ( 4 "context" 5 "fmt" 6 "testing" 7 8 "github.com/cyberdelia/heroku-go/v3" 9 "github.com/hashicorp/terraform/helper/acctest" 10 "github.com/hashicorp/terraform/helper/resource" 11 "github.com/hashicorp/terraform/terraform" 12 ) 13 14 func TestAccHerokuAddon_Basic(t *testing.T) { 15 var addon heroku.AddOnInfoResult 16 appName := fmt.Sprintf("tftest-%s", acctest.RandString(10)) 17 18 resource.Test(t, resource.TestCase{ 19 PreCheck: func() { testAccPreCheck(t) }, 20 Providers: testAccProviders, 21 CheckDestroy: testAccCheckHerokuAddonDestroy, 22 Steps: []resource.TestStep{ 23 { 24 Config: testAccCheckHerokuAddonConfig_basic(appName), 25 Check: resource.ComposeTestCheckFunc( 26 testAccCheckHerokuAddonExists("heroku_addon.foobar", &addon), 27 testAccCheckHerokuAddonAttributes(&addon, "deployhooks:http"), 28 resource.TestCheckResourceAttr( 29 "heroku_addon.foobar", "config.0.url", "http://google.com"), 30 resource.TestCheckResourceAttr( 31 "heroku_addon.foobar", "app", appName), 32 resource.TestCheckResourceAttr( 33 "heroku_addon.foobar", "plan", "deployhooks:http"), 34 ), 35 }, 36 }, 37 }) 38 } 39 40 // GH-198 41 func TestAccHerokuAddon_noPlan(t *testing.T) { 42 var addon heroku.AddOnInfoResult 43 appName := fmt.Sprintf("tftest-%s", acctest.RandString(10)) 44 45 resource.Test(t, resource.TestCase{ 46 PreCheck: func() { testAccPreCheck(t) }, 47 Providers: testAccProviders, 48 CheckDestroy: testAccCheckHerokuAddonDestroy, 49 Steps: []resource.TestStep{ 50 { 51 Config: testAccCheckHerokuAddonConfig_no_plan(appName), 52 Check: resource.ComposeTestCheckFunc( 53 testAccCheckHerokuAddonExists("heroku_addon.foobar", &addon), 54 testAccCheckHerokuAddonAttributes(&addon, "memcachier:dev"), 55 resource.TestCheckResourceAttr( 56 "heroku_addon.foobar", "app", appName), 57 resource.TestCheckResourceAttr( 58 "heroku_addon.foobar", "plan", "memcachier"), 59 ), 60 }, 61 { 62 Config: testAccCheckHerokuAddonConfig_no_plan(appName), 63 Check: resource.ComposeTestCheckFunc( 64 testAccCheckHerokuAddonExists("heroku_addon.foobar", &addon), 65 testAccCheckHerokuAddonAttributes(&addon, "memcachier:dev"), 66 resource.TestCheckResourceAttr( 67 "heroku_addon.foobar", "app", appName), 68 resource.TestCheckResourceAttr( 69 "heroku_addon.foobar", "plan", "memcachier"), 70 ), 71 }, 72 }, 73 }) 74 } 75 76 func testAccCheckHerokuAddonDestroy(s *terraform.State) error { 77 client := testAccProvider.Meta().(*heroku.Service) 78 79 for _, rs := range s.RootModule().Resources { 80 if rs.Type != "heroku_addon" { 81 continue 82 } 83 84 _, err := client.AddOnInfo(context.TODO(), rs.Primary.Attributes["app"], rs.Primary.ID) 85 86 if err == nil { 87 return fmt.Errorf("Addon still exists") 88 } 89 } 90 91 return nil 92 } 93 94 func testAccCheckHerokuAddonAttributes(addon *heroku.AddOnInfoResult, n string) resource.TestCheckFunc { 95 return func(s *terraform.State) error { 96 97 if addon.Plan.Name != n { 98 return fmt.Errorf("Bad plan: %s", addon.Plan.Name) 99 } 100 101 return nil 102 } 103 } 104 105 func testAccCheckHerokuAddonExists(n string, addon *heroku.AddOnInfoResult) resource.TestCheckFunc { 106 return func(s *terraform.State) error { 107 rs, ok := s.RootModule().Resources[n] 108 109 if !ok { 110 return fmt.Errorf("Not found: %s", n) 111 } 112 113 if rs.Primary.ID == "" { 114 return fmt.Errorf("No Addon ID is set") 115 } 116 117 client := testAccProvider.Meta().(*heroku.Service) 118 119 foundAddon, err := client.AddOnInfo(context.TODO(), rs.Primary.Attributes["app"], rs.Primary.ID) 120 121 if err != nil { 122 return err 123 } 124 125 if foundAddon.ID != rs.Primary.ID { 126 return fmt.Errorf("Addon not found") 127 } 128 129 *addon = *foundAddon 130 131 return nil 132 } 133 } 134 135 func testAccCheckHerokuAddonConfig_basic(appName string) string { 136 return fmt.Sprintf(` 137 resource "heroku_app" "foobar" { 138 name = "%s" 139 region = "us" 140 } 141 142 resource "heroku_addon" "foobar" { 143 app = "${heroku_app.foobar.name}" 144 plan = "deployhooks:http" 145 config { 146 url = "http://google.com" 147 } 148 }`, appName) 149 } 150 151 func testAccCheckHerokuAddonConfig_no_plan(appName string) string { 152 return fmt.Sprintf(` 153 resource "heroku_app" "foobar" { 154 name = "%s" 155 region = "us" 156 } 157 158 resource "heroku_addon" "foobar" { 159 app = "${heroku_app.foobar.name}" 160 plan = "memcachier" 161 }`, appName) 162 }