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