github.com/nathanielks/terraform@v0.6.1-0.20170509030759-13e1a62319dc/builtin/providers/heroku/resource_heroku_pipeline_coupling_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 TestAccHerokuPipelineCoupling_Basic(t *testing.T) {
    15  	var coupling heroku.PipelineCouplingInfoResult
    16  
    17  	appName := fmt.Sprintf("tftest-%s", acctest.RandString(10))
    18  	pipelineName := fmt.Sprintf("tftest-%s", acctest.RandString(10))
    19  	stageName := "development"
    20  
    21  	resource.Test(t, resource.TestCase{
    22  		PreCheck:     func() { testAccPreCheck(t) },
    23  		Providers:    testAccProviders,
    24  		CheckDestroy: testAccCheckHerokuPipelineCouplingDestroy,
    25  		Steps: []resource.TestStep{
    26  			{
    27  				Config: testAccCheckHerokuPipelineCouplingConfig_basic(appName, pipelineName, stageName),
    28  				Check: resource.ComposeTestCheckFunc(
    29  					testAccCheckHerokuPipelineCouplingExists("heroku_pipeline_coupling.default", &coupling),
    30  					testAccCheckHerokuPipelineCouplingAttributes(
    31  						&coupling,
    32  						"heroku_pipeline.default",
    33  						stageName,
    34  					),
    35  				),
    36  			},
    37  		},
    38  	})
    39  }
    40  
    41  func testAccCheckHerokuPipelineCouplingConfig_basic(appName, pipelineName, stageName string) string {
    42  	return fmt.Sprintf(`
    43  resource "heroku_app" "default" {
    44    name   = "%s"
    45    region = "us"
    46  }
    47  
    48  resource "heroku_pipeline" "default" {
    49    name = "%s"
    50  }
    51  
    52  resource "heroku_pipeline_coupling" "default" {
    53    app      = "${heroku_app.default.id}"
    54    pipeline = "${heroku_pipeline.default.id}"
    55    stage    = "%s"
    56  }
    57  `, appName, pipelineName, stageName)
    58  }
    59  
    60  func testAccCheckHerokuPipelineCouplingExists(n string, pipeline *heroku.PipelineCouplingInfoResult) resource.TestCheckFunc {
    61  	return func(s *terraform.State) error {
    62  		rs, ok := s.RootModule().Resources[n]
    63  
    64  		if !ok {
    65  			return fmt.Errorf("Not found: %s", n)
    66  		}
    67  
    68  		if rs.Primary.ID == "" {
    69  			return fmt.Errorf("No coupling ID set")
    70  		}
    71  
    72  		client := testAccProvider.Meta().(*heroku.Service)
    73  
    74  		foundPipelineCoupling, err := client.PipelineCouplingInfo(context.TODO(), rs.Primary.ID)
    75  		if err != nil {
    76  			return err
    77  		}
    78  
    79  		if foundPipelineCoupling.ID != rs.Primary.ID {
    80  			return fmt.Errorf("PipelineCoupling not found: %s != %s", foundPipelineCoupling.ID, rs.Primary.ID)
    81  		}
    82  
    83  		*pipeline = *foundPipelineCoupling
    84  
    85  		return nil
    86  	}
    87  }
    88  
    89  func testAccCheckHerokuPipelineCouplingAttributes(coupling *heroku.PipelineCouplingInfoResult, pipelineResource, stageName string) resource.TestCheckFunc {
    90  	return func(s *terraform.State) error {
    91  		pipeline, ok := s.RootModule().Resources[pipelineResource]
    92  		if !ok {
    93  			return fmt.Errorf("Pipeline not found: %s", pipelineResource)
    94  		}
    95  
    96  		if coupling.Pipeline.ID != pipeline.Primary.ID {
    97  			return fmt.Errorf("Bad pipeline ID: %v != %v", coupling.Pipeline.ID, pipeline.Primary.ID)
    98  		}
    99  		if coupling.Stage != stageName {
   100  			return fmt.Errorf("Bad stage: %s", coupling.Stage)
   101  		}
   102  
   103  		return nil
   104  	}
   105  }
   106  
   107  func testAccCheckHerokuPipelineCouplingDestroy(s *terraform.State) error {
   108  	client := testAccProvider.Meta().(*heroku.Service)
   109  
   110  	for _, rs := range s.RootModule().Resources {
   111  		if rs.Type != "heroku_pipeline_coupling" {
   112  			continue
   113  		}
   114  
   115  		_, err := client.PipelineCouplingInfo(context.TODO(), rs.Primary.ID)
   116  
   117  		if err == nil {
   118  			return fmt.Errorf("PipelineCoupling still exists")
   119  		}
   120  	}
   121  
   122  	return nil
   123  }