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