github.com/minamijoyo/terraform@v0.7.8-0.20161029001309-18b3736ba44b/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  	"github.com/zorkian/go-datadog-api"
    12  )
    13  
    14  const config1 = `
    15  resource "datadog_timeboard" "acceptance_test" {
    16    title = "Acceptance Test Timeboard"
    17    description = "Created using the Datadog prodivider 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 prodivider 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        stacked = true
    46      }
    47      request {
    48        q = "avg:redis.mem.rss{$host}"
    49      }
    50      request {
    51        q = "avg:redis.mem.rss{$host}"
    52        type = "bars"
    53        style {
    54          palette = "warm"
    55        }
    56      }
    57    }
    58    template_variable {
    59      name = "host"
    60      prefix = "host"
    61    }
    62  }
    63  `
    64  
    65  func TestAccDatadogTimeboard_update(t *testing.T) {
    66  
    67  	step1 := resource.TestStep{
    68  		Config: config1,
    69  		Check: resource.ComposeTestCheckFunc(
    70  			checkExists,
    71  			resource.TestCheckResourceAttr("datadog_timeboard.acceptance_test", "title", "Acceptance Test Timeboard"),
    72  			resource.TestCheckResourceAttr("datadog_timeboard.acceptance_test", "description", "Created using the Datadog prodivider in Terraform"),
    73  			resource.TestCheckResourceAttr("datadog_timeboard.acceptance_test", "read_only", "true"),
    74  			resource.TestCheckResourceAttr("datadog_timeboard.acceptance_test", "graph.0.title", "Top System CPU by Docker container"),
    75  			resource.TestCheckResourceAttr("datadog_timeboard.acceptance_test", "graph.0.viz", "toplist"),
    76  			resource.TestCheckResourceAttr("datadog_timeboard.acceptance_test", "graph.0.request.0.q", "top(avg:docker.cpu.system{*} by {container_name}, 10, 'mean', 'desc')"),
    77  		),
    78  	}
    79  
    80  	step2 := resource.TestStep{
    81  		Config: config2,
    82  		Check: resource.ComposeTestCheckFunc(
    83  			checkExists,
    84  			resource.TestCheckResourceAttr("datadog_timeboard.acceptance_test", "title", "Acceptance Test Timeboard"),
    85  			resource.TestCheckResourceAttr("datadog_timeboard.acceptance_test", "description", "Created using the Datadog prodivider in Terraform"),
    86  			resource.TestCheckResourceAttr("datadog_timeboard.acceptance_test", "graph.0.title", "Redis latency (ms)"),
    87  			resource.TestCheckResourceAttr("datadog_timeboard.acceptance_test", "graph.0.viz", "timeseries"),
    88  			resource.TestCheckResourceAttr("datadog_timeboard.acceptance_test", "graph.0.request.0.q", "avg:redis.info.latency_ms{$host}"),
    89  			resource.TestCheckResourceAttr("datadog_timeboard.acceptance_test", "graph.1.title", "Redis memory usage"),
    90  			resource.TestCheckResourceAttr("datadog_timeboard.acceptance_test", "graph.1.viz", "timeseries"),
    91  			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}"),
    92  			resource.TestCheckResourceAttr("datadog_timeboard.acceptance_test", "graph.1.request.0.stacked", "true"),
    93  			resource.TestCheckResourceAttr("datadog_timeboard.acceptance_test", "graph.1.request.1.q", "avg:redis.mem.rss{$host}"),
    94  			resource.TestCheckResourceAttr("datadog_timeboard.acceptance_test", "template_variable.0.name", "host"),
    95  			resource.TestCheckResourceAttr("datadog_timeboard.acceptance_test", "template_variable.0.prefix", "host"),
    96  			resource.TestCheckResourceAttr("datadog_timeboard.acceptance_test", "graph.1.request.2.type", "bars"),
    97  			resource.TestCheckResourceAttr("datadog_timeboard.acceptance_test", "graph.1.request.2.style.palette", "warm"),
    98  		),
    99  	}
   100  
   101  	resource.Test(t, resource.TestCase{
   102  		PreCheck:     func() { testAccPreCheck(t) },
   103  		Providers:    testAccProviders,
   104  		CheckDestroy: checkDestroy,
   105  		Steps:        []resource.TestStep{step1, step2},
   106  	})
   107  }
   108  
   109  func checkExists(s *terraform.State) error {
   110  	client := testAccProvider.Meta().(*datadog.Client)
   111  	for _, r := range s.RootModule().Resources {
   112  		i, _ := strconv.Atoi(r.Primary.ID)
   113  		if _, err := client.GetDashboard(i); err != nil {
   114  			return fmt.Errorf("Received an error retrieving monitor %s", err)
   115  		}
   116  	}
   117  	return nil
   118  }
   119  
   120  func checkDestroy(s *terraform.State) error {
   121  	client := testAccProvider.Meta().(*datadog.Client)
   122  	for _, r := range s.RootModule().Resources {
   123  		i, _ := strconv.Atoi(r.Primary.ID)
   124  		if _, err := client.GetDashboard(i); err != nil {
   125  			if strings.Contains(err.Error(), "404 Not Found") {
   126  				continue
   127  			}
   128  			return fmt.Errorf("Received an error retrieving timeboard %s", err)
   129  		}
   130  		return fmt.Errorf("Timeboard still exists")
   131  	}
   132  	return nil
   133  }