github.com/danp/terraform@v0.9.5-0.20170426144147-39d740081351/builtin/providers/circonus/resource_circonus_rule_set_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 TestAccCirconusRuleSet_basic(t *testing.T) { 15 checkName := fmt.Sprintf("ICMP Ping check - %s", acctest.RandString(5)) 16 contactGroupName := fmt.Sprintf("ops-staging-sev3 - %s", acctest.RandString(5)) 17 18 resource.Test(t, resource.TestCase{ 19 PreCheck: func() { testAccPreCheck(t) }, 20 Providers: testAccProviders, 21 CheckDestroy: testAccCheckDestroyCirconusRuleSet, 22 Steps: []resource.TestStep{ 23 { 24 Config: fmt.Sprintf(testAccCirconusRuleSetConfigFmt, contactGroupName, checkName), 25 Check: resource.ComposeTestCheckFunc( 26 resource.TestCheckResourceAttrSet("circonus_rule_set.icmp-latency-alarm", "check"), 27 resource.TestCheckResourceAttr("circonus_rule_set.icmp-latency-alarm", "metric_name", "maximum"), 28 resource.TestCheckResourceAttr("circonus_rule_set.icmp-latency-alarm", "metric_type", "numeric"), 29 resource.TestCheckResourceAttr("circonus_rule_set.icmp-latency-alarm", "notes", "Simple check to create notifications based on ICMP performance."), 30 resource.TestCheckResourceAttr("circonus_rule_set.icmp-latency-alarm", "link", "https://wiki.example.org/playbook/what-to-do-when-high-latency-strikes"), 31 // resource.TestCheckResourceAttr("circonus_rule_set.icmp-latency-alarm", "parent", "some check ID"), 32 resource.TestCheckResourceAttr("circonus_rule_set.icmp-latency-alarm", "if.#", "4"), 33 34 resource.TestCheckResourceAttr("circonus_rule_set.icmp-latency-alarm", "if.0.value.#", "1"), 35 resource.TestCheckResourceAttr("circonus_rule_set.icmp-latency-alarm", "if.0.value.360613670.absent", "70s"), 36 resource.TestCheckResourceAttr("circonus_rule_set.icmp-latency-alarm", "if.0.value.360613670.over.#", "0"), 37 resource.TestCheckResourceAttr("circonus_rule_set.icmp-latency-alarm", "if.0.then.#", "1"), 38 // Computed: 39 // resource.TestCheckResourceAttr("circonus_rule_set.icmp-latency-alarm", "if.0.then.<computed>.notify.#", "1"), 40 // resource.TestCheckResourceAttrSet("circonus_rule_set.icmp-latency-alarm", "if.0.then.<computed>.notify.0"), 41 // resource.TestCheckResourceAttr("circonus_rule_set.icmp-latency-alarm", "if.0.then.<computed>.severity", "1"), 42 43 resource.TestCheckResourceAttr("circonus_rule_set.icmp-latency-alarm", "if.1.value.#", "1"), 44 resource.TestCheckResourceAttr("circonus_rule_set.icmp-latency-alarm", "if.1.value.2300199732.over.#", "1"), 45 resource.TestCheckResourceAttr("circonus_rule_set.icmp-latency-alarm", "if.1.value.2300199732.over.689776960.last", "120s"), 46 resource.TestCheckResourceAttr("circonus_rule_set.icmp-latency-alarm", "if.1.value.2300199732.over.689776960.using", "average"), 47 resource.TestCheckResourceAttr("circonus_rule_set.icmp-latency-alarm", "if.1.value.2300199732.min_value", "2"), 48 resource.TestCheckResourceAttr("circonus_rule_set.icmp-latency-alarm", "if.1.then.#", "1"), 49 // Computed: 50 // resource.TestCheckResourceAttr("circonus_rule_set.icmp-latency-alarm", "if.1.then.<computed>.notify.#", "1"), 51 // resource.TestCheckResourceAttrSet("circonus_rule_set.icmp-latency-alarm", "if.1.then.<computed>.notify.0"), 52 // resource.TestCheckResourceAttr("circonus_rule_set.icmp-latency-alarm", "if.1.then.<computed>.severity", "2"), 53 54 resource.TestCheckResourceAttr("circonus_rule_set.icmp-latency-alarm", "if.2.value.#", "1"), 55 resource.TestCheckResourceAttr("circonus_rule_set.icmp-latency-alarm", "if.2.value.2842654150.over.#", "1"), 56 resource.TestCheckResourceAttr("circonus_rule_set.icmp-latency-alarm", "if.2.value.2842654150.over.999877839.last", "180s"), 57 resource.TestCheckResourceAttr("circonus_rule_set.icmp-latency-alarm", "if.2.value.2842654150.over.999877839.using", "average"), 58 resource.TestCheckResourceAttr("circonus_rule_set.icmp-latency-alarm", "if.2.value.2842654150.max_value", "300"), 59 resource.TestCheckResourceAttr("circonus_rule_set.icmp-latency-alarm", "if.2.then.#", "1"), 60 // Computed: 61 // resource.TestCheckResourceAttr("circonus_rule_set.icmp-latency-alarm", "if.2.then.<computed>.notify.#", "1"), 62 // resource.TestCheckResourceAttrSet("circonus_rule_set.icmp-latency-alarm", "if.2.then.<computed>.notify.0"), 63 // resource.TestCheckResourceAttr("circonus_rule_set.icmp-latency-alarm", "if.2.then.<computed>.severity", "3"), 64 65 resource.TestCheckResourceAttr("circonus_rule_set.icmp-latency-alarm", "if.3.value.#", "1"), 66 resource.TestCheckResourceAttr("circonus_rule_set.icmp-latency-alarm", "if.3.value.803690187.over.#", "0"), 67 resource.TestCheckResourceAttr("circonus_rule_set.icmp-latency-alarm", "if.3.value.803690187.max_value", "400"), 68 // Computed: 69 // resource.TestCheckResourceAttr("circonus_rule_set.icmp-latency-alarm", "if.3.then.<computed>.notify.#", "1"), 70 // resource.TestCheckResourceAttrSet("circonus_rule_set.icmp-latency-alarm", "if.3.then.<computed>.notify.0"), 71 // resource.TestCheckResourceAttr("circonus_rule_set.icmp-latency-alarm", "if.3.then.<computed>.after", "2400s"), 72 // resource.TestCheckResourceAttr("circonus_rule_set.icmp-latency-alarm", "if.3.then.<computed>.severity", "4"), 73 74 resource.TestCheckResourceAttr("circonus_rule_set.icmp-latency-alarm", "tags.#", "2"), 75 resource.TestCheckResourceAttr("circonus_rule_set.icmp-latency-alarm", "tags.2087084518", "author:terraform"), 76 resource.TestCheckResourceAttr("circonus_rule_set.icmp-latency-alarm", "tags.1401442048", "lifecycle:unittest"), 77 ), 78 }, 79 }, 80 }) 81 } 82 83 func testAccCheckDestroyCirconusRuleSet(s *terraform.State) error { 84 ctxt := testAccProvider.Meta().(*providerContext) 85 86 for _, rs := range s.RootModule().Resources { 87 if rs.Type != "circonus_rule_set" { 88 continue 89 } 90 91 cid := rs.Primary.ID 92 exists, err := checkRuleSetExists(ctxt, api.CIDType(&cid)) 93 switch { 94 case !exists: 95 // noop 96 case exists: 97 return fmt.Errorf("rule set still exists after destroy") 98 case err != nil: 99 return fmt.Errorf("Error checking rule set: %v", err) 100 } 101 } 102 103 return nil 104 } 105 106 func checkRuleSetExists(c *providerContext, ruleSetCID api.CIDType) (bool, error) { 107 rs, err := c.client.FetchRuleSet(ruleSetCID) 108 if err != nil { 109 if strings.Contains(err.Error(), defaultCirconus404ErrorString) { 110 return false, nil 111 } 112 113 return false, err 114 } 115 116 if api.CIDType(&rs.CID) == ruleSetCID { 117 return true, nil 118 } 119 120 return false, nil 121 } 122 123 const testAccCirconusRuleSetConfigFmt = ` 124 variable "test_tags" { 125 type = "list" 126 default = [ "author:terraform", "lifecycle:unittest" ] 127 } 128 129 resource "circonus_contact_group" "test-trigger" { 130 name = "%s" 131 tags = [ "${var.test_tags}" ] 132 } 133 134 resource "circonus_check" "api_latency" { 135 active = true 136 name = "%s" 137 period = "60s" 138 139 collector { 140 id = "/broker/1" 141 } 142 143 icmp_ping { 144 count = 1 145 } 146 147 metric { 148 name = "maximum" 149 tags = [ "${var.test_tags}" ] 150 type = "numeric" 151 unit = "seconds" 152 } 153 154 tags = [ "${var.test_tags}" ] 155 target = "api.circonus.com" 156 } 157 158 resource "circonus_rule_set" "icmp-latency-alarm" { 159 check = "${circonus_check.api_latency.checks[0]}" 160 metric_name = "maximum" 161 // metric_name = "${circonus_check.api_latency.metric["maximum"].name}" 162 // metric_type = "${circonus_check.api_latency.metric["maximum"].type}" 163 notes = <<EOF 164 Simple check to create notifications based on ICMP performance. 165 EOF 166 link = "https://wiki.example.org/playbook/what-to-do-when-high-latency-strikes" 167 # parent = "${check cid}" 168 169 if { 170 value { 171 absent = "70s" 172 } 173 174 then { 175 notify = [ "${circonus_contact_group.test-trigger.id}" ] 176 severity = 1 177 } 178 } 179 180 if { 181 value { 182 over { 183 last = "120s" 184 using = "average" 185 } 186 187 min_value = 2 188 } 189 190 then { 191 notify = [ "${circonus_contact_group.test-trigger.id}" ] 192 severity = 2 193 } 194 } 195 196 if { 197 value { 198 over { 199 last = "180s" 200 using = "average" 201 } 202 203 max_value = 300 204 } 205 206 then { 207 notify = [ "${circonus_contact_group.test-trigger.id}" ] 208 severity = 3 209 } 210 } 211 212 if { 213 value { 214 max_value = 400 215 } 216 217 then { 218 notify = [ "${circonus_contact_group.test-trigger.id}" ] 219 after = "2400s" 220 severity = 4 221 } 222 } 223 224 tags = [ "${var.test_tags}" ] 225 } 226 `