github.com/danp/terraform@v0.9.5-0.20170426144147-39d740081351/builtin/providers/circonus/resource_circonus_graph_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 TestAccCirconusGraph_basic(t *testing.T) {
    15  	graphName := fmt.Sprintf("Test Graph - %s", acctest.RandString(5))
    16  	checkName := fmt.Sprintf("ICMP Ping check - %s", acctest.RandString(5))
    17  
    18  	resource.Test(t, resource.TestCase{
    19  		PreCheck:     func() { testAccPreCheck(t) },
    20  		Providers:    testAccProviders,
    21  		CheckDestroy: testAccCheckDestroyCirconusGraph,
    22  		Steps: []resource.TestStep{
    23  			{
    24  				Config: fmt.Sprintf(testAccCirconusGraphConfigFmt, checkName, graphName),
    25  				Check: resource.ComposeTestCheckFunc(
    26  					resource.TestCheckResourceAttr("circonus_graph.mixed-points", "name", graphName),
    27  					resource.TestCheckResourceAttr("circonus_graph.mixed-points", "description", "Terraform Test: mixed graph"),
    28  					resource.TestCheckResourceAttr("circonus_graph.mixed-points", "notes", "test notes"),
    29  					resource.TestCheckResourceAttr("circonus_graph.mixed-points", "graph_style", "line"),
    30  					resource.TestCheckResourceAttr("circonus_graph.mixed-points", "left.%", "1"),
    31  					resource.TestCheckResourceAttr("circonus_graph.mixed-points", "left.max", "11"),
    32  					resource.TestCheckResourceAttr("circonus_graph.mixed-points", "right.%", "3"),
    33  					resource.TestCheckResourceAttr("circonus_graph.mixed-points", "right.logarithmic", "10"),
    34  					resource.TestCheckResourceAttr("circonus_graph.mixed-points", "right.max", "20"),
    35  					resource.TestCheckResourceAttr("circonus_graph.mixed-points", "right.min", "-1"),
    36  
    37  					resource.TestCheckResourceAttr("circonus_graph.mixed-points", "line_style", "stepped"),
    38  
    39  					resource.TestCheckResourceAttr("circonus_graph.mixed-points", "metric.#", "2"),
    40  
    41  					resource.TestCheckResourceAttr("circonus_graph.mixed-points", "metric.0.caql", ""),
    42  					resource.TestCheckResourceAttrSet("circonus_graph.mixed-points", "metric.0.check"),
    43  					resource.TestCheckResourceAttr("circonus_graph.mixed-points", "metric.0.metric_name", "maximum"),
    44  					resource.TestCheckResourceAttr("circonus_graph.mixed-points", "metric.0.metric_type", "numeric"),
    45  					resource.TestCheckResourceAttr("circonus_graph.mixed-points", "metric.0.name", "Maximum Latency"),
    46  					resource.TestCheckResourceAttr("circonus_graph.mixed-points", "metric.0.axis", "left"),
    47  					resource.TestCheckResourceAttr("circonus_graph.mixed-points", "metric.0.color", "#657aa6"),
    48  					resource.TestCheckResourceAttr("circonus_graph.mixed-points", "metric.0.function", "gauge"),
    49  					resource.TestCheckResourceAttr("circonus_graph.mixed-points", "metric.0.active", "true"),
    50  
    51  					resource.TestCheckResourceAttr("circonus_graph.mixed-points", "metric.1.caql", ""),
    52  					resource.TestCheckResourceAttrSet("circonus_graph.mixed-points", "metric.1.check"),
    53  					resource.TestCheckResourceAttr("circonus_graph.mixed-points", "metric.1.metric_name", "minimum"),
    54  					resource.TestCheckResourceAttr("circonus_graph.mixed-points", "metric.1.metric_type", "numeric"),
    55  					resource.TestCheckResourceAttr("circonus_graph.mixed-points", "metric.1.name", "Minimum Latency"),
    56  					resource.TestCheckResourceAttr("circonus_graph.mixed-points", "metric.1.axis", "right"),
    57  					resource.TestCheckResourceAttr("circonus_graph.mixed-points", "metric.1.color", "#657aa6"),
    58  					resource.TestCheckResourceAttr("circonus_graph.mixed-points", "metric.1.function", "gauge"),
    59  					resource.TestCheckResourceAttr("circonus_graph.mixed-points", "metric.1.active", "true"),
    60  
    61  					resource.TestCheckResourceAttr("circonus_graph.mixed-points", "tags.#", "2"),
    62  					resource.TestCheckResourceAttr("circonus_graph.mixed-points", "tags.2087084518", "author:terraform"),
    63  					resource.TestCheckResourceAttr("circonus_graph.mixed-points", "tags.1401442048", "lifecycle:unittest"),
    64  				),
    65  			},
    66  		},
    67  	})
    68  }
    69  
    70  func testAccCheckDestroyCirconusGraph(s *terraform.State) error {
    71  	ctxt := testAccProvider.Meta().(*providerContext)
    72  
    73  	for _, rs := range s.RootModule().Resources {
    74  		if rs.Type != "circonus_graph" {
    75  			continue
    76  		}
    77  
    78  		cid := rs.Primary.ID
    79  		exists, err := checkGraphExists(ctxt, api.CIDType(&cid))
    80  		switch {
    81  		case !exists:
    82  			// noop
    83  		case exists:
    84  			return fmt.Errorf("graph still exists after destroy")
    85  		case err != nil:
    86  			return fmt.Errorf("Error checking graph %s", err)
    87  		}
    88  	}
    89  
    90  	return nil
    91  }
    92  
    93  func checkGraphExists(c *providerContext, graphID api.CIDType) (bool, error) {
    94  	g, err := c.client.FetchGraph(graphID)
    95  	if err != nil {
    96  		if strings.Contains(err.Error(), defaultCirconus404ErrorString) {
    97  			return false, nil
    98  		}
    99  
   100  		return false, err
   101  	}
   102  
   103  	if api.CIDType(&g.CID) == graphID {
   104  		return true, nil
   105  	}
   106  
   107  	return false, nil
   108  }
   109  
   110  const testAccCirconusGraphConfigFmt = `
   111  variable "test_tags" {
   112    type = "list"
   113    default = [ "author:terraform", "lifecycle:unittest" ]
   114  }
   115  
   116  resource "circonus_check" "api_latency" {
   117    active = true
   118    name = "%s"
   119    period = "60s"
   120  
   121    collector {
   122      id = "/broker/1"
   123    }
   124  
   125    icmp_ping {
   126      count = 5
   127    }
   128  
   129    metric {
   130      name = "maximum"
   131      tags = [ "${var.test_tags}" ]
   132      type = "numeric"
   133      unit = "seconds"
   134    }
   135  
   136    metric {
   137      name = "minimum"
   138      tags = [ "${var.test_tags}" ]
   139      type = "numeric"
   140      unit = "seconds"
   141    }
   142  
   143    tags = [ "${var.test_tags}" ]
   144    target = "api.circonus.com"
   145  }
   146  
   147  resource "circonus_graph" "mixed-points" {
   148    name = "%s"
   149    description = "Terraform Test: mixed graph"
   150    notes = "test notes"
   151    graph_style = "line"
   152    line_style = "stepped"
   153  
   154    metric {
   155      # caql = "" # conflicts with metric_name/check
   156      check = "${circonus_check.api_latency.checks[0]}"
   157      metric_name = "maximum"
   158      metric_type = "numeric"
   159      name = "Maximum Latency"
   160      axis = "left" # right
   161      color = "#657aa6"
   162      function = "gauge"
   163      active = true
   164    }
   165  
   166    metric {
   167      # caql = "" # conflicts with metric_name/check
   168      check = "${circonus_check.api_latency.checks[0]}"
   169      metric_name = "minimum"
   170      metric_type = "numeric"
   171      name = "Minimum Latency"
   172      axis = "right" # left
   173      color = "#657aa6"
   174      function = "gauge"
   175      active = true
   176    }
   177  
   178    // metric_cluster {
   179    //   active = true
   180    //   aggregate = "average"
   181    //   axis = "left" # right
   182    //   color = "#657aa6"
   183    //   group = "${circonus_check.api_latency.checks[0]}"
   184    //   name = "Metrics Used"
   185    // }
   186  
   187    left {
   188      max = 11
   189    }
   190  
   191    right {
   192      logarithmic = 10
   193      max = 20
   194      min = -1
   195    }
   196  
   197    tags = [ "${var.test_tags}" ]
   198  }
   199  `