github.com/vtorhonen/terraform@v0.9.0-beta2.0.20170307220345-5d894e4ffda7/builtin/providers/librato/resource_librato_space_chart_test.go (about)

     1  package librato
     2  
     3  import (
     4  	"fmt"
     5  	"strconv"
     6  	"testing"
     7  
     8  	"github.com/hashicorp/terraform/helper/resource"
     9  	"github.com/hashicorp/terraform/terraform"
    10  	"github.com/henrikhodne/go-librato/librato"
    11  )
    12  
    13  func TestAccLibratoSpaceChart_Basic(t *testing.T) {
    14  	var spaceChart librato.SpaceChart
    15  
    16  	resource.Test(t, resource.TestCase{
    17  		PreCheck:     func() { testAccPreCheck(t) },
    18  		Providers:    testAccProviders,
    19  		CheckDestroy: testAccCheckLibratoSpaceChartDestroy,
    20  		Steps: []resource.TestStep{
    21  			resource.TestStep{
    22  				Config: testAccCheckLibratoSpaceChartConfig_basic,
    23  				Check: resource.ComposeTestCheckFunc(
    24  					testAccCheckLibratoSpaceChartExists("librato_space_chart.foobar", &spaceChart),
    25  					testAccCheckLibratoSpaceChartName(&spaceChart, "Foo Bar"),
    26  					resource.TestCheckResourceAttr(
    27  						"librato_space_chart.foobar", "name", "Foo Bar"),
    28  				),
    29  			},
    30  		},
    31  	})
    32  }
    33  
    34  func TestAccLibratoSpaceChart_Full(t *testing.T) {
    35  	var spaceChart librato.SpaceChart
    36  
    37  	resource.Test(t, resource.TestCase{
    38  		PreCheck:     func() { testAccPreCheck(t) },
    39  		Providers:    testAccProviders,
    40  		CheckDestroy: testAccCheckLibratoSpaceChartDestroy,
    41  		Steps: []resource.TestStep{
    42  			resource.TestStep{
    43  				Config: testAccCheckLibratoSpaceChartConfig_full,
    44  				Check: resource.ComposeTestCheckFunc(
    45  					testAccCheckLibratoSpaceChartExists("librato_space_chart.foobar", &spaceChart),
    46  					testAccCheckLibratoSpaceChartName(&spaceChart, "Foo Bar"),
    47  					resource.TestCheckResourceAttr(
    48  						"librato_space_chart.foobar", "name", "Foo Bar"),
    49  				),
    50  			},
    51  		},
    52  	})
    53  }
    54  
    55  func TestAccLibratoSpaceChart_Updated(t *testing.T) {
    56  	var spaceChart librato.SpaceChart
    57  
    58  	resource.Test(t, resource.TestCase{
    59  		PreCheck:     func() { testAccPreCheck(t) },
    60  		Providers:    testAccProviders,
    61  		CheckDestroy: testAccCheckLibratoSpaceChartDestroy,
    62  		Steps: []resource.TestStep{
    63  			resource.TestStep{
    64  				Config: testAccCheckLibratoSpaceChartConfig_basic,
    65  				Check: resource.ComposeTestCheckFunc(
    66  					testAccCheckLibratoSpaceChartExists("librato_space_chart.foobar", &spaceChart),
    67  					testAccCheckLibratoSpaceChartName(&spaceChart, "Foo Bar"),
    68  					resource.TestCheckResourceAttr(
    69  						"librato_space_chart.foobar", "name", "Foo Bar"),
    70  				),
    71  			},
    72  			resource.TestStep{
    73  				Config: testAccCheckLibratoSpaceChartConfig_new_value,
    74  				Check: resource.ComposeTestCheckFunc(
    75  					testAccCheckLibratoSpaceChartExists("librato_space_chart.foobar", &spaceChart),
    76  					testAccCheckLibratoSpaceChartName(&spaceChart, "Bar Baz"),
    77  					resource.TestCheckResourceAttr(
    78  						"librato_space_chart.foobar", "name", "Bar Baz"),
    79  				),
    80  			},
    81  		},
    82  	})
    83  }
    84  
    85  func testAccCheckLibratoSpaceChartDestroy(s *terraform.State) error {
    86  	client := testAccProvider.Meta().(*librato.Client)
    87  
    88  	for _, rs := range s.RootModule().Resources {
    89  		if rs.Type != "librato_space_chart" {
    90  			continue
    91  		}
    92  
    93  		id, err := strconv.ParseUint(rs.Primary.ID, 10, 0)
    94  		if err != nil {
    95  			return fmt.Errorf("ID not a number")
    96  		}
    97  
    98  		spaceID, err := strconv.ParseUint(rs.Primary.Attributes["space_id"], 10, 0)
    99  		if err != nil {
   100  			return fmt.Errorf("Space ID not a number")
   101  		}
   102  
   103  		_, _, err = client.Spaces.GetChart(uint(spaceID), uint(id))
   104  
   105  		if err == nil {
   106  			return fmt.Errorf("Space Chart still exists")
   107  		}
   108  	}
   109  
   110  	return nil
   111  }
   112  
   113  func testAccCheckLibratoSpaceChartName(spaceChart *librato.SpaceChart, name string) resource.TestCheckFunc {
   114  	return func(s *terraform.State) error {
   115  
   116  		if spaceChart.Name == nil || *spaceChart.Name != name {
   117  			return fmt.Errorf("Bad name: %s", *spaceChart.Name)
   118  		}
   119  
   120  		return nil
   121  	}
   122  }
   123  
   124  func testAccCheckLibratoSpaceChartExists(n string, spaceChart *librato.SpaceChart) resource.TestCheckFunc {
   125  	return func(s *terraform.State) error {
   126  		rs, ok := s.RootModule().Resources[n]
   127  
   128  		if !ok {
   129  			return fmt.Errorf("Not found: %s", n)
   130  		}
   131  
   132  		if rs.Primary.ID == "" {
   133  			return fmt.Errorf("No Space Chart ID is set")
   134  		}
   135  
   136  		client := testAccProvider.Meta().(*librato.Client)
   137  
   138  		id, err := strconv.ParseUint(rs.Primary.ID, 10, 0)
   139  		if err != nil {
   140  			return fmt.Errorf("ID not a number")
   141  		}
   142  
   143  		spaceID, err := strconv.ParseUint(rs.Primary.Attributes["space_id"], 10, 0)
   144  		if err != nil {
   145  			return fmt.Errorf("Space ID not a number")
   146  		}
   147  
   148  		foundSpaceChart, _, err := client.Spaces.GetChart(uint(spaceID), uint(id))
   149  
   150  		if err != nil {
   151  			return err
   152  		}
   153  
   154  		if foundSpaceChart.ID == nil || *foundSpaceChart.ID != uint(id) {
   155  			return fmt.Errorf("Space not found")
   156  		}
   157  
   158  		*spaceChart = *foundSpaceChart
   159  
   160  		return nil
   161  	}
   162  }
   163  
   164  const testAccCheckLibratoSpaceChartConfig_basic = `
   165  resource "librato_space" "foobar" {
   166      name = "Foo Bar"
   167  }
   168  
   169  resource "librato_space_chart" "foobar" {
   170      space_id = "${librato_space.foobar.id}"
   171      name = "Foo Bar"
   172      type = "line"
   173  }`
   174  
   175  const testAccCheckLibratoSpaceChartConfig_new_value = `
   176  resource "librato_space" "foobar" {
   177      name = "Foo Bar"
   178  }
   179  
   180  resource "librato_space_chart" "foobar" {
   181      space_id = "${librato_space.foobar.id}"
   182      name = "Bar Baz"
   183      type = "line"
   184  }`
   185  
   186  const testAccCheckLibratoSpaceChartConfig_full = `
   187  resource "librato_space" "foobar" {
   188      name = "Foo Bar"
   189  }
   190  
   191  resource "librato_space" "barbaz" {
   192      name = "Bar Baz"
   193  }
   194  
   195  resource "librato_space_chart" "foobar" {
   196      space_id = "${librato_space.foobar.id}"
   197      name = "Foo Bar"
   198      type = "line"
   199      min = 0
   200      max = 100
   201      label = "Percent"
   202      related_space = "${librato_space.barbaz.id}"
   203  
   204      # Minimal metric stream
   205      stream {
   206          metric = "librato.cpu.percent.idle"
   207          source = "*"
   208      }
   209  
   210      # Minimal composite stream
   211      stream {
   212          composite = "s(\"cpu\", \"*\")"
   213      }
   214  
   215      # Full metric stream
   216      stream {
   217          metric = "librato.cpu.percent.idle"
   218          source = "*"
   219          group_function = "average"
   220          summary_function = "max"
   221          name = "CPU usage"
   222          color = "#990000"
   223          units_short = "%"
   224          units_long = "percent"
   225          min = 0
   226          max = 100
   227          transform_function = "x * 100"
   228          period = 60
   229      }
   230  }`