github.com/danp/terraform@v0.9.5-0.20170426144147-39d740081351/builtin/providers/heroku/resource_heroku_drain_test.go (about)

     1  package heroku
     2  
     3  import (
     4  	"context"
     5  	"fmt"
     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 TestAccHerokuDrain_Basic(t *testing.T) {
    15  	var drain heroku.LogDrainInfoResult
    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: testAccCheckHerokuDrainDestroy,
    22  		Steps: []resource.TestStep{
    23  			{
    24  				Config: testAccCheckHerokuDrainConfig_basic(appName),
    25  				Check: resource.ComposeTestCheckFunc(
    26  					testAccCheckHerokuDrainExists("heroku_drain.foobar", &drain),
    27  					testAccCheckHerokuDrainAttributes(&drain),
    28  					resource.TestCheckResourceAttr(
    29  						"heroku_drain.foobar", "url", "syslog://terraform.example.com:1234"),
    30  					resource.TestCheckResourceAttr(
    31  						"heroku_drain.foobar", "app", appName),
    32  				),
    33  			},
    34  		},
    35  	})
    36  }
    37  
    38  func testAccCheckHerokuDrainDestroy(s *terraform.State) error {
    39  	client := testAccProvider.Meta().(*heroku.Service)
    40  
    41  	for _, rs := range s.RootModule().Resources {
    42  		if rs.Type != "heroku_drain" {
    43  			continue
    44  		}
    45  
    46  		_, err := client.LogDrainInfo(context.TODO(), rs.Primary.Attributes["app"], rs.Primary.ID)
    47  
    48  		if err == nil {
    49  			return fmt.Errorf("Drain still exists")
    50  		}
    51  	}
    52  
    53  	return nil
    54  }
    55  
    56  func testAccCheckHerokuDrainAttributes(Drain *heroku.LogDrainInfoResult) resource.TestCheckFunc {
    57  	return func(s *terraform.State) error {
    58  
    59  		if Drain.URL != "syslog://terraform.example.com:1234" {
    60  			return fmt.Errorf("Bad URL: %s", Drain.URL)
    61  		}
    62  
    63  		if Drain.Token == "" {
    64  			return fmt.Errorf("No token: %#v", Drain)
    65  		}
    66  
    67  		return nil
    68  	}
    69  }
    70  
    71  func testAccCheckHerokuDrainExists(n string, Drain *heroku.LogDrainInfoResult) resource.TestCheckFunc {
    72  	return func(s *terraform.State) error {
    73  		rs, ok := s.RootModule().Resources[n]
    74  
    75  		if !ok {
    76  			return fmt.Errorf("Not found: %s", n)
    77  		}
    78  
    79  		if rs.Primary.ID == "" {
    80  			return fmt.Errorf("No Drain ID is set")
    81  		}
    82  
    83  		client := testAccProvider.Meta().(*heroku.Service)
    84  
    85  		foundDrain, err := client.LogDrainInfo(context.TODO(), rs.Primary.Attributes["app"], rs.Primary.ID)
    86  
    87  		if err != nil {
    88  			return err
    89  		}
    90  
    91  		if foundDrain.ID != rs.Primary.ID {
    92  			return fmt.Errorf("Drain not found")
    93  		}
    94  
    95  		*Drain = *foundDrain
    96  
    97  		return nil
    98  	}
    99  }
   100  
   101  func testAccCheckHerokuDrainConfig_basic(appName string) string {
   102  	return fmt.Sprintf(`
   103  resource "heroku_app" "foobar" {
   104      name = "%s"
   105      region = "us"
   106  }
   107  
   108  resource "heroku_drain" "foobar" {
   109      app = "${heroku_app.foobar.name}"
   110      url = "syslog://terraform.example.com:1234"
   111  }`, appName)
   112  }