github.com/andrewrynhard/terraform@v0.9.5-0.20170502003928-8d286b83eae4/builtin/providers/heroku/resource_heroku_app_feature_test.go (about)

     1  package heroku
     2  
     3  import (
     4  	"context"
     5  	"fmt"
     6  	"testing"
     7  
     8  	heroku "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 TestAccHerokuAppFeature(t *testing.T) {
    15  	var feature heroku.AppFeatureInfoResult
    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: testAccCheckHerokuFeatureDestroy,
    22  		Steps: []resource.TestStep{
    23  			{
    24  				Config: testAccCheckHerokuFeature_basic(appName),
    25  				Check: resource.ComposeTestCheckFunc(
    26  					testAccCheckHerokuFeatureExists("heroku_app_feature.runtime_metrics", &feature),
    27  					testAccCheckHerokuFeatureEnabled(&feature, true),
    28  					resource.TestCheckResourceAttr(
    29  						"heroku_app_feature.runtime_metrics", "enabled", "true",
    30  					),
    31  				),
    32  			},
    33  			{
    34  				Config: testAccCheckHerokuFeature_disabled(appName),
    35  				Check: resource.ComposeTestCheckFunc(
    36  					testAccCheckHerokuFeatureExists("heroku_app_feature.runtime_metrics", &feature),
    37  					testAccCheckHerokuFeatureEnabled(&feature, false),
    38  					resource.TestCheckResourceAttr(
    39  						"heroku_app_feature.runtime_metrics", "enabled", "false",
    40  					),
    41  				),
    42  			},
    43  		},
    44  	})
    45  }
    46  
    47  func testAccCheckHerokuFeatureDestroy(s *terraform.State) error {
    48  	client := testAccProvider.Meta().(*heroku.Service)
    49  
    50  	for _, rs := range s.RootModule().Resources {
    51  		if rs.Type != "heroku_app_feature" {
    52  			continue
    53  		}
    54  
    55  		_, err := client.AppFeatureInfo(context.TODO(), rs.Primary.Attributes["app"], rs.Primary.ID)
    56  
    57  		if err == nil {
    58  			return fmt.Errorf("Feature still exists")
    59  		}
    60  	}
    61  
    62  	return nil
    63  }
    64  
    65  func testAccCheckHerokuFeatureExists(n string, feature *heroku.AppFeatureInfoResult) resource.TestCheckFunc {
    66  	return func(s *terraform.State) error {
    67  		rs, ok := s.RootModule().Resources[n]
    68  
    69  		if !ok {
    70  			return fmt.Errorf("Not found: %s", n)
    71  		}
    72  
    73  		if rs.Primary.ID == "" {
    74  			return fmt.Errorf("No feature ID is set")
    75  		}
    76  
    77  		app, id := parseCompositeID(rs.Primary.ID)
    78  		if app != rs.Primary.Attributes["app"] {
    79  			return fmt.Errorf("Bad app: %s", app)
    80  		}
    81  
    82  		client := testAccProvider.Meta().(*heroku.Service)
    83  
    84  		foundFeature, err := client.AppFeatureInfo(context.TODO(), app, id)
    85  		if err != nil {
    86  			return err
    87  		}
    88  
    89  		if foundFeature.ID != id {
    90  			return fmt.Errorf("Feature not found")
    91  		}
    92  
    93  		*feature = *foundFeature
    94  		return nil
    95  	}
    96  }
    97  
    98  func testAccCheckHerokuFeatureEnabled(feature *heroku.AppFeatureInfoResult, enabled bool) resource.TestCheckFunc {
    99  	return func(s *terraform.State) error {
   100  		if feature.Enabled != enabled {
   101  			return fmt.Errorf("Bad enabled: %v", feature.Enabled)
   102  		}
   103  
   104  		return nil
   105  	}
   106  }
   107  
   108  func testAccCheckHerokuFeature_basic(appName string) string {
   109  	return fmt.Sprintf(`
   110  resource "heroku_app" "example" {
   111  	name = "%s"
   112  	region = "us"
   113  }
   114  
   115  resource "heroku_app_feature" "runtime_metrics" {
   116  	app = "${heroku_app.example.name}"
   117  	name = "log-runtime-metrics"
   118  }
   119  `, appName)
   120  }
   121  
   122  func testAccCheckHerokuFeature_disabled(appName string) string {
   123  	return fmt.Sprintf(`
   124  resource "heroku_app" "example" {
   125  	name = "%s"
   126  	region = "us"
   127  }
   128  
   129  resource "heroku_app_feature" "runtime_metrics" {
   130  	app = "${heroku_app.example.name}"
   131  	name = "log-runtime-metrics"
   132  	enabled = false
   133  }
   134  `, appName)
   135  }