github.com/nathanielks/terraform@v0.6.1-0.20170509030759-13e1a62319dc/builtin/providers/heroku/resource_heroku_pipeline_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 TestAccHerokuPipeline_Basic(t *testing.T) {
    15  	var pipeline heroku.PipelineInfoResult
    16  	pipelineName := fmt.Sprintf("tftest-%s", acctest.RandString(10))
    17  	pipelineName2 := fmt.Sprintf("%s-2", pipelineName)
    18  
    19  	resource.Test(t, resource.TestCase{
    20  		PreCheck:     func() { testAccPreCheck(t) },
    21  		Providers:    testAccProviders,
    22  		CheckDestroy: testAccCheckHerokuPipelineDestroy,
    23  		Steps: []resource.TestStep{
    24  			{
    25  				Config: testAccCheckHerokuPipelineConfig_basic(pipelineName),
    26  				Check: resource.ComposeTestCheckFunc(
    27  					testAccCheckHerokuPipelineExists("heroku_pipeline.foobar", &pipeline),
    28  					resource.TestCheckResourceAttr(
    29  						"heroku_pipeline.foobar", "name", pipelineName),
    30  				),
    31  			},
    32  			{
    33  				Config: testAccCheckHerokuPipelineConfig_basic(pipelineName2),
    34  				Check: resource.ComposeTestCheckFunc(
    35  					resource.TestCheckResourceAttr(
    36  						"heroku_pipeline.foobar", "name", pipelineName2),
    37  				),
    38  			},
    39  		},
    40  	})
    41  }
    42  
    43  func testAccCheckHerokuPipelineConfig_basic(pipelineName string) string {
    44  	return fmt.Sprintf(`
    45  resource "heroku_pipeline" "foobar" {
    46    name = "%s"
    47  }
    48  `, pipelineName)
    49  }
    50  
    51  func testAccCheckHerokuPipelineExists(n string, pipeline *heroku.PipelineInfoResult) resource.TestCheckFunc {
    52  	return func(s *terraform.State) error {
    53  		rs, ok := s.RootModule().Resources[n]
    54  
    55  		if !ok {
    56  			return fmt.Errorf("Not found: %s", n)
    57  		}
    58  
    59  		if rs.Primary.ID == "" {
    60  			return fmt.Errorf("No pipeline name set")
    61  		}
    62  
    63  		client := testAccProvider.Meta().(*heroku.Service)
    64  
    65  		foundPipeline, err := client.PipelineInfo(context.TODO(), rs.Primary.ID)
    66  		if err != nil {
    67  			return err
    68  		}
    69  
    70  		if foundPipeline.ID != rs.Primary.ID {
    71  			return fmt.Errorf("Pipeline not found")
    72  		}
    73  
    74  		*pipeline = *foundPipeline
    75  
    76  		return nil
    77  	}
    78  }
    79  
    80  func testAccCheckHerokuPipelineDestroy(s *terraform.State) error {
    81  	client := testAccProvider.Meta().(*heroku.Service)
    82  
    83  	for _, rs := range s.RootModule().Resources {
    84  		if rs.Type != "heroku_pipeline" {
    85  			continue
    86  		}
    87  
    88  		_, err := client.PipelineInfo(context.TODO(), rs.Primary.ID)
    89  
    90  		if err == nil {
    91  			return fmt.Errorf("Pipeline still exists")
    92  		}
    93  	}
    94  
    95  	return nil
    96  }