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