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

     1  package circonus
     2  
     3  import (
     4  	"fmt"
     5  	"testing"
     6  
     7  	"github.com/hashicorp/terraform/helper/acctest"
     8  	"github.com/hashicorp/terraform/helper/resource"
     9  	"github.com/hashicorp/terraform/terraform"
    10  )
    11  
    12  func TestAccCirconusMetric_basic(t *testing.T) {
    13  	metricAvgName := fmt.Sprintf("Average Ping Time - %s", acctest.RandString(5))
    14  	metricMaxName := fmt.Sprintf("Maximum Ping Time - %s", acctest.RandString(5))
    15  	metricMinName := fmt.Sprintf("Minimum Ping Time - %s", acctest.RandString(5))
    16  
    17  	resource.Test(t, resource.TestCase{
    18  		PreCheck:     func() { testAccPreCheck(t) },
    19  		Providers:    testAccProviders,
    20  		CheckDestroy: testAccCheckDestroyMetric,
    21  		Steps: []resource.TestStep{
    22  			{
    23  				Config: fmt.Sprintf(testAccCirconusMetricConfigFmt, metricAvgName, metricMaxName, metricMinName),
    24  				Check: resource.ComposeTestCheckFunc(
    25  					resource.TestCheckResourceAttr("circonus_metric.icmp_ping_average", "name", metricAvgName),
    26  					resource.TestCheckResourceAttr("circonus_metric.icmp_ping_average", "active", "false"),
    27  					resource.TestCheckResourceAttr("circonus_metric.icmp_ping_average", "tags.#", "2"),
    28  					resource.TestCheckResourceAttr("circonus_metric.icmp_ping_average", "tags.2087084518", "author:terraform"),
    29  					resource.TestCheckResourceAttr("circonus_metric.icmp_ping_average", "tags.3241999189", "source:circonus"),
    30  
    31  					resource.TestCheckResourceAttr("circonus_metric.icmp_ping_average", "type", "numeric"),
    32  					resource.TestCheckResourceAttr("circonus_metric.icmp_ping_average", "unit", "seconds"),
    33  
    34  					resource.TestCheckResourceAttr("circonus_metric.icmp_ping_maximum", "name", metricMaxName),
    35  					resource.TestCheckResourceAttr("circonus_metric.icmp_ping_maximum", "active", "true"),
    36  					resource.TestCheckResourceAttr("circonus_metric.icmp_ping_maximum", "tags.#", "2"),
    37  					resource.TestCheckResourceAttr("circonus_metric.icmp_ping_maximum", "tags.2087084518", "author:terraform"),
    38  					resource.TestCheckResourceAttr("circonus_metric.icmp_ping_maximum", "tags.3241999189", "source:circonus"),
    39  					resource.TestCheckResourceAttr("circonus_metric.icmp_ping_maximum", "type", "numeric"),
    40  					resource.TestCheckResourceAttr("circonus_metric.icmp_ping_maximum", "unit", "seconds"),
    41  
    42  					resource.TestCheckResourceAttr("circonus_metric.icmp_ping_minimum", "name", metricMinName),
    43  					resource.TestCheckResourceAttr("circonus_metric.icmp_ping_minimum", "active", "true"),
    44  					resource.TestCheckResourceAttr("circonus_metric.icmp_ping_minimum", "tags.#", "0"),
    45  					resource.TestCheckResourceAttr("circonus_metric.icmp_ping_minimum", "type", "numeric"),
    46  					resource.TestCheckResourceAttr("circonus_metric.icmp_ping_minimum", "unit", ""),
    47  				),
    48  			},
    49  		},
    50  	})
    51  }
    52  
    53  func TestAccCirconusMetric_tagsets(t *testing.T) {
    54  	metricName := fmt.Sprintf("foo - %s", acctest.RandString(5))
    55  
    56  	resource.Test(t, resource.TestCase{
    57  		PreCheck:     func() { testAccPreCheck(t) },
    58  		Providers:    testAccProviders,
    59  		CheckDestroy: testAccCheckDestroyMetric,
    60  		Steps: []resource.TestStep{
    61  			{
    62  				Config: fmt.Sprintf(testAccCirconusMetricTagsFmt0, metricName),
    63  				Check: resource.ComposeTestCheckFunc(
    64  					resource.TestCheckResourceAttr("circonus_metric.t", "name", metricName),
    65  					resource.TestCheckResourceAttr("circonus_metric.t", "type", "numeric"),
    66  					resource.TestCheckResourceAttr("circonus_metric.t", "tags.#", "0"),
    67  				),
    68  			},
    69  			{
    70  				Config: fmt.Sprintf(testAccCirconusMetricTagsFmt1, metricName),
    71  				Check: resource.ComposeTestCheckFunc(
    72  					resource.TestCheckResourceAttr("circonus_metric.t", "name", metricName),
    73  					resource.TestCheckResourceAttr("circonus_metric.t", "type", "numeric"),
    74  					resource.TestCheckResourceAttr("circonus_metric.t", "tags.#", "1"),
    75  					resource.TestCheckResourceAttr("circonus_metric.t", "tags.1750285118", "foo:bar"),
    76  				),
    77  			},
    78  			{
    79  				Config: fmt.Sprintf(testAccCirconusMetricTagsFmt2, metricName),
    80  				Check: resource.ComposeTestCheckFunc(
    81  					resource.TestCheckResourceAttr("circonus_metric.t", "name", metricName),
    82  					resource.TestCheckResourceAttr("circonus_metric.t", "type", "numeric"),
    83  					resource.TestCheckResourceAttr("circonus_metric.t", "tags.#", "2"),
    84  					resource.TestCheckResourceAttr("circonus_metric.t", "tags.1750285118", "foo:bar"),
    85  					resource.TestCheckResourceAttr("circonus_metric.t", "tags.2693443894", "foo:baz"),
    86  				),
    87  			},
    88  			{
    89  				Config: fmt.Sprintf(testAccCirconusMetricTagsFmt3, metricName),
    90  				Check: resource.ComposeTestCheckFunc(
    91  					resource.TestCheckResourceAttr("circonus_metric.t", "name", metricName),
    92  					resource.TestCheckResourceAttr("circonus_metric.t", "type", "numeric"),
    93  					resource.TestCheckResourceAttr("circonus_metric.t", "tags.#", "3"),
    94  					resource.TestCheckResourceAttr("circonus_metric.t", "tags.1750285118", "foo:bar"),
    95  					resource.TestCheckResourceAttr("circonus_metric.t", "tags.2693443894", "foo:baz"),
    96  					resource.TestCheckResourceAttr("circonus_metric.t", "tags.1937518738", "foo:bur"),
    97  				),
    98  			},
    99  			{
   100  				Config: fmt.Sprintf(testAccCirconusMetricTagsFmt4, metricName),
   101  				Check: resource.ComposeTestCheckFunc(
   102  					resource.TestCheckResourceAttr("circonus_metric.t", "name", metricName),
   103  					resource.TestCheckResourceAttr("circonus_metric.t", "type", "numeric"),
   104  					resource.TestCheckResourceAttr("circonus_metric.t", "tags.#", "4"),
   105  					resource.TestCheckResourceAttr("circonus_metric.t", "tags.1750285118", "foo:bar"),
   106  					resource.TestCheckResourceAttr("circonus_metric.t", "tags.2693443894", "foo:baz"),
   107  					resource.TestCheckResourceAttr("circonus_metric.t", "tags.1937518738", "foo:bur"),
   108  					resource.TestCheckResourceAttr("circonus_metric.t", "tags.2110890696", "foo:baz2"),
   109  				),
   110  			},
   111  			{
   112  				Config: fmt.Sprintf(testAccCirconusMetricTagsFmt5, metricName),
   113  				Check: resource.ComposeTestCheckFunc(
   114  					resource.TestCheckResourceAttr("circonus_metric.t", "name", metricName),
   115  					resource.TestCheckResourceAttr("circonus_metric.t", "type", "numeric"),
   116  					resource.TestCheckResourceAttr("circonus_metric.t", "tags.#", "3"),
   117  					resource.TestCheckResourceAttr("circonus_metric.t", "tags.1750285118", "foo:bar"),
   118  					resource.TestCheckResourceAttr("circonus_metric.t", "tags.1937518738", "foo:bur"),
   119  					resource.TestCheckResourceAttr("circonus_metric.t", "tags.2110890696", "foo:baz2"),
   120  				),
   121  			},
   122  		},
   123  	})
   124  }
   125  
   126  func testAccCheckDestroyMetric(s *terraform.State) error {
   127  	for _, rs := range s.RootModule().Resources {
   128  		if rs.Type != "circonus_metric" {
   129  			continue
   130  		}
   131  
   132  		id := rs.Primary.ID
   133  		exists := id == ""
   134  		switch {
   135  		case !exists:
   136  			// noop
   137  		case exists:
   138  			return fmt.Errorf("metric still exists after destroy")
   139  		}
   140  	}
   141  
   142  	return nil
   143  }
   144  
   145  const testAccCirconusMetricConfigFmt = `
   146  resource "circonus_metric" "icmp_ping_average" {
   147    name = "%s"
   148    active = false
   149    type = "numeric"
   150    unit = "seconds"
   151  
   152    tags = [
   153      "author:terraform",
   154      "source:circonus",
   155    ]
   156  }
   157  
   158  resource "circonus_metric" "icmp_ping_maximum" {
   159    name = "%s"
   160    active = true
   161    type = "numeric"
   162    unit = "seconds"
   163  
   164    tags = [
   165      "source:circonus",
   166      "author:terraform",
   167    ]
   168  }
   169  
   170  resource "circonus_metric" "icmp_ping_minimum" {
   171    name = "%s"
   172    type = "numeric"
   173  }
   174  `
   175  
   176  const testAccCirconusMetricTagsFmt0 = `
   177  resource "circonus_metric" "t" {
   178    name = "%s"
   179  # tags = [
   180  #    "foo:bar",
   181  #    "foo:baz",
   182  #    "foo:bur",
   183  #    "foo:baz2"
   184  # ]
   185    type = "numeric"
   186  }
   187  `
   188  
   189  const testAccCirconusMetricTagsFmt1 = `
   190  resource "circonus_metric" "t" {
   191    name = "%s"
   192    tags = [
   193      "foo:bar",
   194  #    "foo:baz",
   195  #    "foo:bur",
   196  #    "foo:baz2"
   197    ]
   198    type = "numeric"
   199  }
   200  `
   201  
   202  const testAccCirconusMetricTagsFmt2 = `
   203  resource "circonus_metric" "t" {
   204    name = "%s"
   205    tags = [
   206      "foo:bar",
   207      "foo:baz",
   208  #    "foo:bur",
   209  #    "foo:baz2"
   210    ]
   211    type = "numeric"
   212  }
   213  `
   214  
   215  const testAccCirconusMetricTagsFmt3 = `
   216  resource "circonus_metric" "t" {
   217    name = "%s"
   218    tags = [
   219      "foo:bar",
   220      "foo:baz",
   221      "foo:bur",
   222  #    "foo:baz2"
   223    ]
   224    type = "numeric"
   225  }
   226  `
   227  
   228  const testAccCirconusMetricTagsFmt4 = `
   229  resource "circonus_metric" "t" {
   230    name = "%s"
   231    tags = [
   232      "foo:bar",
   233      "foo:baz",
   234      "foo:bur",
   235      "foo:baz2"
   236    ]
   237    type = "numeric"
   238  }
   239  `
   240  
   241  const testAccCirconusMetricTagsFmt5 = `
   242  resource "circonus_metric" "t" {
   243    name = "%s"
   244    tags = [
   245      "foo:bar",
   246  #    "foo:baz",
   247      "foo:bur",
   248      "foo:baz2"
   249    ]
   250    type = "numeric"
   251  }
   252  `