github.com/danp/terraform@v0.9.5-0.20170426144147-39d740081351/builtin/providers/influxdb/continuous_query_test.go (about)

     1  package influxdb
     2  
     3  import (
     4  	"fmt"
     5  	"testing"
     6  
     7  	"github.com/hashicorp/terraform/helper/resource"
     8  	"github.com/hashicorp/terraform/terraform"
     9  	"github.com/influxdata/influxdb/client"
    10  )
    11  
    12  func TestAccInfluxDBContiuousQuery(t *testing.T) {
    13  	resource.Test(t, resource.TestCase{
    14  		Providers: testAccProviders,
    15  		Steps: []resource.TestStep{
    16  			resource.TestStep{
    17  				Config: testAccContiuousQueryConfig,
    18  				Check: resource.ComposeTestCheckFunc(
    19  					testAccCheckContiuousQueryExists("influxdb_continuous_query.minnie"),
    20  					resource.TestCheckResourceAttr(
    21  						"influxdb_continuous_query.minnie", "name", "minnie",
    22  					),
    23  					resource.TestCheckResourceAttr(
    24  						"influxdb_continuous_query.minnie", "database", "terraform-test",
    25  					),
    26  					resource.TestCheckResourceAttr(
    27  						"influxdb_continuous_query.minnie", "query", "SELECT min(mouse) INTO min_mouse FROM zoo GROUP BY time(30m)",
    28  					),
    29  				),
    30  			},
    31  		},
    32  	})
    33  }
    34  
    35  func testAccCheckContiuousQueryExists(n string) resource.TestCheckFunc {
    36  	return func(s *terraform.State) error {
    37  		rs, ok := s.RootModule().Resources[n]
    38  		if !ok {
    39  			return fmt.Errorf("Not found: %s", n)
    40  		}
    41  
    42  		if rs.Primary.ID == "" {
    43  			return fmt.Errorf("No ContiuousQuery id set")
    44  		}
    45  
    46  		conn := testAccProvider.Meta().(*client.Client)
    47  
    48  		query := client.Query{
    49  			Command: "SHOW CONTINUOUS QUERIES",
    50  		}
    51  
    52  		resp, err := conn.Query(query)
    53  		if err != nil {
    54  			return err
    55  		}
    56  
    57  		if resp.Err != nil {
    58  			return resp.Err
    59  		}
    60  
    61  		for _, series := range resp.Results[0].Series {
    62  			if series.Name == rs.Primary.Attributes["database"] {
    63  				for _, result := range series.Values {
    64  					if result[0].(string) == rs.Primary.Attributes["name"] {
    65  						return nil
    66  					}
    67  				}
    68  			}
    69  		}
    70  
    71  		return fmt.Errorf("ContiuousQuery %q does not exist", rs.Primary.Attributes["name"])
    72  	}
    73  }
    74  
    75  var testAccContiuousQueryConfig = `
    76  
    77  resource "influxdb_database" "test" {
    78      name = "terraform-test"
    79  }
    80  
    81  resource "influxdb_continuous_query" "minnie" {
    82      name = "minnie"
    83      database = "${influxdb_database.test.name}"
    84      query = "SELECT min(mouse) INTO min_mouse FROM zoo GROUP BY time(30m)"
    85  }
    86  
    87  `