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

     1  package circonus
     2  
     3  import (
     4  	"fmt"
     5  	"strings"
     6  	"testing"
     7  
     8  	"github.com/circonus-labs/circonus-gometrics/api"
     9  	"github.com/hashicorp/terraform/helper/acctest"
    10  	"github.com/hashicorp/terraform/helper/resource"
    11  	"github.com/hashicorp/terraform/terraform"
    12  )
    13  
    14  func TestAccCirconusMetricCluster_basic(t *testing.T) {
    15  	metricClusterName := fmt.Sprintf("job1-stream-agg - %s", acctest.RandString(5))
    16  
    17  	resource.Test(t, resource.TestCase{
    18  		PreCheck:     func() { testAccPreCheck(t) },
    19  		Providers:    testAccProviders,
    20  		CheckDestroy: testAccCheckDestroyCirconusMetricCluster,
    21  		Steps: []resource.TestStep{
    22  			{
    23  				Config: fmt.Sprintf(testAccCirconusMetricClusterConfigFmt, metricClusterName),
    24  				Check: resource.ComposeTestCheckFunc(
    25  					resource.TestCheckResourceAttr("circonus_metric_cluster.nomad-job1", "description", `Metric Cluster Description`),
    26  					resource.TestCheckResourceAttrSet("circonus_metric_cluster.nomad-job1", "id"),
    27  					resource.TestCheckResourceAttr("circonus_metric_cluster.nomad-job1", "name", metricClusterName),
    28  					resource.TestCheckResourceAttr("circonus_metric_cluster.nomad-job1", "query.236803225.definition", "*`nomad-jobname`memory`rss"),
    29  					resource.TestCheckResourceAttr("circonus_metric_cluster.nomad-job1", "query.236803225.type", "average"),
    30  					resource.TestCheckResourceAttr("circonus_metric_cluster.nomad-job1", "tags.2087084518", "author:terraform"),
    31  					resource.TestCheckResourceAttr("circonus_metric_cluster.nomad-job1", "tags.3354173695", "source:nomad"),
    32  				),
    33  			},
    34  		},
    35  	})
    36  }
    37  
    38  func testAccCheckDestroyCirconusMetricCluster(s *terraform.State) error {
    39  	ctxt := testAccProvider.Meta().(*providerContext)
    40  
    41  	for _, rs := range s.RootModule().Resources {
    42  		if rs.Type != "circonus_metric_cluster" {
    43  			continue
    44  		}
    45  
    46  		cid := rs.Primary.ID
    47  		exists, err := checkMetricClusterExists(ctxt, api.CIDType(&cid))
    48  		switch {
    49  		case !exists:
    50  			// noop
    51  		case exists:
    52  			return fmt.Errorf("metric cluster still exists after destroy")
    53  		case err != nil:
    54  			return fmt.Errorf("Error checking metric cluster: %v", err)
    55  		}
    56  	}
    57  
    58  	return nil
    59  }
    60  
    61  func checkMetricClusterExists(c *providerContext, metricClusterCID api.CIDType) (bool, error) {
    62  	cmc, err := c.client.FetchMetricCluster(metricClusterCID, "")
    63  	if err != nil {
    64  		if strings.Contains(err.Error(), defaultCirconus404ErrorString) {
    65  			return false, nil
    66  		}
    67  
    68  		return false, err
    69  	}
    70  
    71  	if api.CIDType(&cmc.CID) == metricClusterCID {
    72  		return true, nil
    73  	}
    74  
    75  	return false, nil
    76  }
    77  
    78  const testAccCirconusMetricClusterConfigFmt = `
    79  resource "circonus_metric_cluster" "nomad-job1" {
    80    description = <<EOF
    81  Metric Cluster Description
    82  EOF
    83    name = "%s"
    84  
    85    query {
    86      definition = "*` + "`" + `nomad-jobname` + "`" + `memory` + "`" + `rss"
    87      type = "average"
    88    }
    89  
    90    tags = [
    91      "author:terraform",
    92      "source:nomad",
    93    ]
    94  }
    95  `