github.com/richardmarshall/terraform@v0.9.5-0.20170429023105-15704cc6ee35/builtin/providers/datadog/resource_datadog_timeboard_test.go (about) 1 package datadog 2 3 import ( 4 "fmt" 5 "strconv" 6 "strings" 7 "testing" 8 9 "github.com/hashicorp/terraform/helper/resource" 10 "github.com/hashicorp/terraform/terraform" 11 "gopkg.in/zorkian/go-datadog-api.v2" 12 ) 13 14 const config1 = ` 15 resource "datadog_timeboard" "acceptance_test" { 16 title = "Acceptance Test Timeboard" 17 description = "Created using the Datadog provider in Terraform" 18 read_only = true 19 graph { 20 title = "Top System CPU by Docker container" 21 viz = "toplist" 22 request { 23 q = "top(avg:docker.cpu.system{*} by {container_name}, 10, 'mean', 'desc')" 24 } 25 } 26 } 27 ` 28 29 const config2 = ` 30 resource "datadog_timeboard" "acceptance_test" { 31 title = "Acceptance Test Timeboard" 32 description = "Created using the Datadog provider in Terraform" 33 graph { 34 title = "Redis latency (ms)" 35 viz = "timeseries" 36 request { 37 q = "avg:redis.info.latency_ms{$host}" 38 } 39 } 40 graph { 41 title = "Redis memory usage" 42 viz = "timeseries" 43 request { 44 q = "avg:redis.mem.used{$host} - avg:redis.mem.lua{$host}, avg:redis.mem.lua{$host}" 45 aggregator = "sum" 46 stacked = true 47 } 48 request { 49 q = "avg:redis.mem.rss{$host}" 50 } 51 request { 52 q = "avg:redis.mem.rss{$host}" 53 type = "bars" 54 style { 55 palette = "warm" 56 } 57 aggregator = "max" 58 } 59 } 60 template_variable { 61 name = "host" 62 prefix = "host" 63 } 64 } 65 ` 66 67 const config3 = ` 68 resource "datadog_timeboard" "acceptance_test" { 69 title = "Acceptance Test Timeboard" 70 description = "Created using the Datadog provider in Terraform" 71 graph { 72 title = "Redis latency (ms)" 73 viz = "timeseries" 74 request { 75 q = "avg:redis.info.latency_ms{$host}" 76 } 77 events = ["sources:capistrano"] 78 79 marker { 80 label = "High Latency" 81 type = "error solid" 82 value = "y > 100" 83 } 84 yaxis { 85 max = "50" 86 scale = "sqrt" 87 } 88 } 89 graph { 90 title = "ELB Requests" 91 viz = "query_value" 92 request { 93 q = "sum:aws.elb.request_count{*}.as_count()" 94 type = "line" 95 aggregator = "min" 96 conditional_format { 97 comparator = ">" 98 value = "1000" 99 palette = "white_on_red" 100 } 101 conditional_format { 102 comparator = "<=" 103 value = "1000" 104 palette = "white_on_green" 105 } 106 } 107 custom_unit = "hits" 108 precision = "*" 109 text_align = "left" 110 } 111 template_variable { 112 name = "host" 113 prefix = "host" 114 } 115 } 116 ` 117 118 func TestAccDatadogTimeboard_update(t *testing.T) { 119 120 step1 := resource.TestStep{ 121 Config: config1, 122 Check: resource.ComposeTestCheckFunc( 123 checkExists, 124 resource.TestCheckResourceAttr("datadog_timeboard.acceptance_test", "title", "Acceptance Test Timeboard"), 125 resource.TestCheckResourceAttr("datadog_timeboard.acceptance_test", "description", "Created using the Datadog provider in Terraform"), 126 resource.TestCheckResourceAttr("datadog_timeboard.acceptance_test", "read_only", "true"), 127 resource.TestCheckResourceAttr("datadog_timeboard.acceptance_test", "graph.0.title", "Top System CPU by Docker container"), 128 resource.TestCheckResourceAttr("datadog_timeboard.acceptance_test", "graph.0.viz", "toplist"), 129 resource.TestCheckResourceAttr("datadog_timeboard.acceptance_test", "graph.0.request.0.q", "top(avg:docker.cpu.system{*} by {container_name}, 10, 'mean', 'desc')"), 130 ), 131 } 132 133 step2 := resource.TestStep{ 134 Config: config2, 135 Check: resource.ComposeTestCheckFunc( 136 checkExists, 137 resource.TestCheckResourceAttr("datadog_timeboard.acceptance_test", "title", "Acceptance Test Timeboard"), 138 resource.TestCheckResourceAttr("datadog_timeboard.acceptance_test", "description", "Created using the Datadog provider in Terraform"), 139 resource.TestCheckResourceAttr("datadog_timeboard.acceptance_test", "graph.0.title", "Redis latency (ms)"), 140 resource.TestCheckResourceAttr("datadog_timeboard.acceptance_test", "graph.0.viz", "timeseries"), 141 resource.TestCheckResourceAttr("datadog_timeboard.acceptance_test", "graph.0.request.0.q", "avg:redis.info.latency_ms{$host}"), 142 resource.TestCheckResourceAttr("datadog_timeboard.acceptance_test", "graph.1.title", "Redis memory usage"), 143 resource.TestCheckResourceAttr("datadog_timeboard.acceptance_test", "graph.1.viz", "timeseries"), 144 resource.TestCheckResourceAttr("datadog_timeboard.acceptance_test", "graph.1.request.0.q", "avg:redis.mem.used{$host} - avg:redis.mem.lua{$host}, avg:redis.mem.lua{$host}"), 145 resource.TestCheckResourceAttr("datadog_timeboard.acceptance_test", "graph.1.request.0.aggregator", "sum"), 146 resource.TestCheckResourceAttr("datadog_timeboard.acceptance_test", "graph.1.request.0.stacked", "true"), 147 resource.TestCheckResourceAttr("datadog_timeboard.acceptance_test", "graph.1.request.1.q", "avg:redis.mem.rss{$host}"), 148 resource.TestCheckResourceAttr("datadog_timeboard.acceptance_test", "template_variable.0.name", "host"), 149 resource.TestCheckResourceAttr("datadog_timeboard.acceptance_test", "template_variable.0.prefix", "host"), 150 resource.TestCheckResourceAttr("datadog_timeboard.acceptance_test", "graph.1.request.2.type", "bars"), 151 resource.TestCheckResourceAttr("datadog_timeboard.acceptance_test", "graph.1.request.2.q", "avg:redis.mem.rss{$host}"), 152 resource.TestCheckResourceAttr("datadog_timeboard.acceptance_test", "graph.1.request.2.aggregator", "max"), 153 resource.TestCheckResourceAttr("datadog_timeboard.acceptance_test", "graph.1.request.2.style.palette", "warm"), 154 ), 155 } 156 157 step3 := resource.TestStep{ 158 Config: config3, 159 Check: resource.ComposeTestCheckFunc( 160 checkExists, 161 resource.TestCheckResourceAttr("datadog_timeboard.acceptance_test", "title", "Acceptance Test Timeboard"), 162 resource.TestCheckResourceAttr("datadog_timeboard.acceptance_test", "description", "Created using the Datadog provider in Terraform"), 163 resource.TestCheckResourceAttr("datadog_timeboard.acceptance_test", "graph.0.title", "Redis latency (ms)"), 164 resource.TestCheckResourceAttr("datadog_timeboard.acceptance_test", "graph.0.viz", "timeseries"), 165 resource.TestCheckResourceAttr("datadog_timeboard.acceptance_test", "graph.0.request.0.q", "avg:redis.info.latency_ms{$host}"), 166 resource.TestCheckResourceAttr("datadog_timeboard.acceptance_test", "graph.0.events.#", "1"), 167 resource.TestCheckResourceAttr("datadog_timeboard.acceptance_test", "graph.0.marker.0.label", "High Latency"), 168 resource.TestCheckResourceAttr("datadog_timeboard.acceptance_test", "graph.0.marker.0.type", "error solid"), 169 resource.TestCheckResourceAttr("datadog_timeboard.acceptance_test", "graph.0.marker.0.value", "y > 100"), 170 resource.TestCheckResourceAttr("datadog_timeboard.acceptance_test", "graph.0.yaxis.max", "50"), 171 resource.TestCheckResourceAttr("datadog_timeboard.acceptance_test", "graph.0.yaxis.scale", "sqrt"), 172 resource.TestCheckResourceAttr("datadog_timeboard.acceptance_test", "graph.1.title", "ELB Requests"), 173 resource.TestCheckResourceAttr("datadog_timeboard.acceptance_test", "graph.1.viz", "query_value"), 174 resource.TestCheckResourceAttr("datadog_timeboard.acceptance_test", "graph.1.request.0.q", "sum:aws.elb.request_count{*}.as_count()"), 175 resource.TestCheckResourceAttr("datadog_timeboard.acceptance_test", "graph.1.request.0.aggregator", "min"), 176 resource.TestCheckResourceAttr("datadog_timeboard.acceptance_test", "graph.1.request.0.type", "line"), 177 resource.TestCheckResourceAttr("datadog_timeboard.acceptance_test", "graph.1.request.0.conditional_format.0.comparator", ">"), 178 resource.TestCheckResourceAttr("datadog_timeboard.acceptance_test", "graph.1.request.0.conditional_format.0.value", "1000"), 179 resource.TestCheckResourceAttr("datadog_timeboard.acceptance_test", "graph.1.request.0.conditional_format.0.palette", "white_on_red"), 180 resource.TestCheckResourceAttr("datadog_timeboard.acceptance_test", "graph.1.request.0.conditional_format.1.comparator", "<="), 181 resource.TestCheckResourceAttr("datadog_timeboard.acceptance_test", "graph.1.request.0.conditional_format.1.value", "1000"), 182 resource.TestCheckResourceAttr("datadog_timeboard.acceptance_test", "graph.1.request.0.conditional_format.1.palette", "white_on_green"), 183 resource.TestCheckResourceAttr("datadog_timeboard.acceptance_test", "graph.1.custom_unit", "hits"), 184 resource.TestCheckResourceAttr("datadog_timeboard.acceptance_test", "graph.1.precision", "*"), 185 resource.TestCheckResourceAttr("datadog_timeboard.acceptance_test", "graph.1.text_align", "left"), 186 resource.TestCheckResourceAttr("datadog_timeboard.acceptance_test", "template_variable.0.name", "host"), 187 resource.TestCheckResourceAttr("datadog_timeboard.acceptance_test", "template_variable.0.prefix", "host"), 188 ), 189 } 190 191 resource.Test(t, resource.TestCase{ 192 PreCheck: func() { testAccPreCheck(t) }, 193 Providers: testAccProviders, 194 CheckDestroy: checkDestroy, 195 Steps: []resource.TestStep{step1, step2, step3}, 196 }) 197 } 198 199 func checkExists(s *terraform.State) error { 200 client := testAccProvider.Meta().(*datadog.Client) 201 for _, r := range s.RootModule().Resources { 202 i, _ := strconv.Atoi(r.Primary.ID) 203 if _, err := client.GetDashboard(i); err != nil { 204 return fmt.Errorf("Received an error retrieving monitor %s", err) 205 } 206 } 207 return nil 208 } 209 210 func checkDestroy(s *terraform.State) error { 211 client := testAccProvider.Meta().(*datadog.Client) 212 for _, r := range s.RootModule().Resources { 213 i, _ := strconv.Atoi(r.Primary.ID) 214 if _, err := client.GetDashboard(i); err != nil { 215 if strings.Contains(err.Error(), "404 Not Found") { 216 continue 217 } 218 return fmt.Errorf("Received an error retrieving timeboard %s", err) 219 } 220 return fmt.Errorf("Timeboard still exists") 221 } 222 return nil 223 } 224 225 func TestValidateAggregatorMethod(t *testing.T) { 226 validMethods := []string{ 227 "average", 228 "max", 229 "min", 230 "sum", 231 } 232 for _, v := range validMethods { 233 _, errors := validateAggregatorMethod(v, "request") 234 if len(errors) != 0 { 235 t.Fatalf("%q should be a valid aggregator method: %q", v, errors) 236 } 237 } 238 239 invalidMethods := []string{ 240 "avg", 241 "suM", 242 "m", 243 "foo", 244 } 245 for _, v := range invalidMethods { 246 _, errors := validateAggregatorMethod(v, "request") 247 if len(errors) == 0 { 248 t.Fatalf("%q should be an invalid aggregator method", v) 249 } 250 } 251 252 }