github.com/chalford/terraform@v0.3.7-0.20150113080010-a78c69a8c81f/builtin/providers/heroku/resource_heroku_app_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 TestAccHerokuApp_Basic(t *testing.T) {
    13  	var app heroku.App
    14  
    15  	resource.Test(t, resource.TestCase{
    16  		PreCheck:     func() { testAccPreCheck(t) },
    17  		Providers:    testAccProviders,
    18  		CheckDestroy: testAccCheckHerokuAppDestroy,
    19  		Steps: []resource.TestStep{
    20  			resource.TestStep{
    21  				Config: testAccCheckHerokuAppConfig_basic,
    22  				Check: resource.ComposeTestCheckFunc(
    23  					testAccCheckHerokuAppExists("heroku_app.foobar", &app),
    24  					testAccCheckHerokuAppAttributes(&app),
    25  					resource.TestCheckResourceAttr(
    26  						"heroku_app.foobar", "name", "terraform-test-app"),
    27  					resource.TestCheckResourceAttr(
    28  						"heroku_app.foobar", "config_vars.0.FOO", "bar"),
    29  				),
    30  			},
    31  		},
    32  	})
    33  }
    34  
    35  func TestAccHerokuApp_NameChange(t *testing.T) {
    36  	var app heroku.App
    37  
    38  	resource.Test(t, resource.TestCase{
    39  		PreCheck:     func() { testAccPreCheck(t) },
    40  		Providers:    testAccProviders,
    41  		CheckDestroy: testAccCheckHerokuAppDestroy,
    42  		Steps: []resource.TestStep{
    43  			resource.TestStep{
    44  				Config: testAccCheckHerokuAppConfig_basic,
    45  				Check: resource.ComposeTestCheckFunc(
    46  					testAccCheckHerokuAppExists("heroku_app.foobar", &app),
    47  					testAccCheckHerokuAppAttributes(&app),
    48  					resource.TestCheckResourceAttr(
    49  						"heroku_app.foobar", "name", "terraform-test-app"),
    50  					resource.TestCheckResourceAttr(
    51  						"heroku_app.foobar", "config_vars.0.FOO", "bar"),
    52  				),
    53  			},
    54  			resource.TestStep{
    55  				Config: testAccCheckHerokuAppConfig_updated,
    56  				Check: resource.ComposeTestCheckFunc(
    57  					testAccCheckHerokuAppExists("heroku_app.foobar", &app),
    58  					testAccCheckHerokuAppAttributesUpdated(&app),
    59  					resource.TestCheckResourceAttr(
    60  						"heroku_app.foobar", "name", "terraform-test-renamed"),
    61  					resource.TestCheckResourceAttr(
    62  						"heroku_app.foobar", "config_vars.0.FOO", "bing"),
    63  					resource.TestCheckResourceAttr(
    64  						"heroku_app.foobar", "config_vars.0.BAZ", "bar"),
    65  				),
    66  			},
    67  		},
    68  	})
    69  }
    70  
    71  func TestAccHerokuApp_NukeVars(t *testing.T) {
    72  	var app heroku.App
    73  
    74  	resource.Test(t, resource.TestCase{
    75  		PreCheck:     func() { testAccPreCheck(t) },
    76  		Providers:    testAccProviders,
    77  		CheckDestroy: testAccCheckHerokuAppDestroy,
    78  		Steps: []resource.TestStep{
    79  			resource.TestStep{
    80  				Config: testAccCheckHerokuAppConfig_basic,
    81  				Check: resource.ComposeTestCheckFunc(
    82  					testAccCheckHerokuAppExists("heroku_app.foobar", &app),
    83  					testAccCheckHerokuAppAttributes(&app),
    84  					resource.TestCheckResourceAttr(
    85  						"heroku_app.foobar", "name", "terraform-test-app"),
    86  					resource.TestCheckResourceAttr(
    87  						"heroku_app.foobar", "config_vars.0.FOO", "bar"),
    88  				),
    89  			},
    90  			resource.TestStep{
    91  				Config: testAccCheckHerokuAppConfig_no_vars,
    92  				Check: resource.ComposeTestCheckFunc(
    93  					testAccCheckHerokuAppExists("heroku_app.foobar", &app),
    94  					testAccCheckHerokuAppAttributesNoVars(&app),
    95  					resource.TestCheckResourceAttr(
    96  						"heroku_app.foobar", "name", "terraform-test-app"),
    97  					resource.TestCheckResourceAttr(
    98  						"heroku_app.foobar", "config_vars.0.FOO", ""),
    99  				),
   100  			},
   101  		},
   102  	})
   103  }
   104  
   105  func testAccCheckHerokuAppDestroy(s *terraform.State) error {
   106  	client := testAccProvider.Meta().(*heroku.Service)
   107  
   108  	for _, rs := range s.RootModule().Resources {
   109  		if rs.Type != "heroku_app" {
   110  			continue
   111  		}
   112  
   113  		_, err := client.AppInfo(rs.Primary.ID)
   114  
   115  		if err == nil {
   116  			return fmt.Errorf("App still exists")
   117  		}
   118  	}
   119  
   120  	return nil
   121  }
   122  
   123  func testAccCheckHerokuAppAttributes(app *heroku.App) resource.TestCheckFunc {
   124  	return func(s *terraform.State) error {
   125  		client := testAccProvider.Meta().(*heroku.Service)
   126  
   127  		if app.Region.Name != "us" {
   128  			return fmt.Errorf("Bad region: %s", app.Region.Name)
   129  		}
   130  
   131  		if app.Stack.Name != "cedar-14" {
   132  			return fmt.Errorf("Bad stack: %s", app.Stack.Name)
   133  		}
   134  
   135  		if app.Name != "terraform-test-app" {
   136  			return fmt.Errorf("Bad name: %s", app.Name)
   137  		}
   138  
   139  		vars, err := client.ConfigVarInfo(app.Name)
   140  		if err != nil {
   141  			return err
   142  		}
   143  
   144  		if vars["FOO"] != "bar" {
   145  			return fmt.Errorf("Bad config vars: %v", vars)
   146  		}
   147  
   148  		return nil
   149  	}
   150  }
   151  
   152  func testAccCheckHerokuAppAttributesUpdated(app *heroku.App) resource.TestCheckFunc {
   153  	return func(s *terraform.State) error {
   154  		client := testAccProvider.Meta().(*heroku.Service)
   155  
   156  		if app.Name != "terraform-test-renamed" {
   157  			return fmt.Errorf("Bad name: %s", app.Name)
   158  		}
   159  
   160  		vars, err := client.ConfigVarInfo(app.Name)
   161  		if err != nil {
   162  			return err
   163  		}
   164  
   165  		// Make sure we kept the old one
   166  		if vars["FOO"] != "bing" {
   167  			return fmt.Errorf("Bad config vars: %v", vars)
   168  		}
   169  
   170  		if vars["BAZ"] != "bar" {
   171  			return fmt.Errorf("Bad config vars: %v", vars)
   172  		}
   173  
   174  		return nil
   175  
   176  	}
   177  }
   178  
   179  func testAccCheckHerokuAppAttributesNoVars(app *heroku.App) resource.TestCheckFunc {
   180  	return func(s *terraform.State) error {
   181  		client := testAccProvider.Meta().(*heroku.Service)
   182  
   183  		if app.Name != "terraform-test-app" {
   184  			return fmt.Errorf("Bad name: %s", app.Name)
   185  		}
   186  
   187  		vars, err := client.ConfigVarInfo(app.Name)
   188  		if err != nil {
   189  			return err
   190  		}
   191  
   192  		if len(vars) != 0 {
   193  			return fmt.Errorf("vars exist: %v", vars)
   194  		}
   195  
   196  		return nil
   197  	}
   198  }
   199  
   200  func testAccCheckHerokuAppExists(n string, app *heroku.App) resource.TestCheckFunc {
   201  	return func(s *terraform.State) error {
   202  		rs, ok := s.RootModule().Resources[n]
   203  
   204  		if !ok {
   205  			return fmt.Errorf("Not found: %s", n)
   206  		}
   207  
   208  		if rs.Primary.ID == "" {
   209  			return fmt.Errorf("No App Name is set")
   210  		}
   211  
   212  		client := testAccProvider.Meta().(*heroku.Service)
   213  
   214  		foundApp, err := client.AppInfo(rs.Primary.ID)
   215  
   216  		if err != nil {
   217  			return err
   218  		}
   219  
   220  		if foundApp.Name != rs.Primary.ID {
   221  			return fmt.Errorf("App not found")
   222  		}
   223  
   224  		*app = *foundApp
   225  
   226  		return nil
   227  	}
   228  }
   229  
   230  const testAccCheckHerokuAppConfig_basic = `
   231  resource "heroku_app" "foobar" {
   232  	name = "terraform-test-app"
   233  	region = "us"
   234  
   235  	config_vars {
   236  		FOO = "bar"
   237  	}
   238  }`
   239  
   240  const testAccCheckHerokuAppConfig_updated = `
   241  resource "heroku_app" "foobar" {
   242  	name = "terraform-test-renamed"
   243  	region = "us"
   244  
   245  	config_vars {
   246  		FOO = "bing"
   247  		BAZ = "bar"
   248  	}
   249  }`
   250  
   251  const testAccCheckHerokuAppConfig_no_vars = `
   252  resource "heroku_app" "foobar" {
   253  	name = "terraform-test-app"
   254  	region = "us"
   255  }`