github.com/mohanarpit/terraform@v0.6.16-0.20160909104007-291f29853544/builtin/providers/heroku/resource_heroku_addon_test.go (about)

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