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