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