github.com/danp/terraform@v0.9.5-0.20170426144147-39d740081351/builtin/providers/influxdb/resource_database_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 TestAccInfluxDBDatabase(t *testing.T) {
    13  	resource.Test(t, resource.TestCase{
    14  		Providers: testAccProviders,
    15  		Steps: []resource.TestStep{
    16  			resource.TestStep{
    17  				Config: testAccDatabaseConfig,
    18  				Check: resource.ComposeTestCheckFunc(
    19  					testAccCheckDatabaseExists("influxdb_database.test"),
    20  					resource.TestCheckResourceAttr(
    21  						"influxdb_database.test", "name", "terraform-test",
    22  					),
    23  				),
    24  			},
    25  		},
    26  	})
    27  }
    28  
    29  func testAccCheckDatabaseExists(n string) resource.TestCheckFunc {
    30  	return func(s *terraform.State) error {
    31  		rs, ok := s.RootModule().Resources[n]
    32  		if !ok {
    33  			return fmt.Errorf("Not found: %s", n)
    34  		}
    35  
    36  		if rs.Primary.ID == "" {
    37  			return fmt.Errorf("No database id set")
    38  		}
    39  
    40  		conn := testAccProvider.Meta().(*client.Client)
    41  
    42  		query := client.Query{
    43  			Command: "SHOW DATABASES",
    44  		}
    45  
    46  		resp, err := conn.Query(query)
    47  		if err != nil {
    48  			return err
    49  		}
    50  
    51  		if resp.Err != nil {
    52  			return resp.Err
    53  		}
    54  
    55  		for _, result := range resp.Results[0].Series[0].Values {
    56  			if result[0] == rs.Primary.Attributes["name"] {
    57  				return nil
    58  			}
    59  		}
    60  
    61  		return fmt.Errorf("Database %q does not exist", rs.Primary.Attributes["name"])
    62  	}
    63  }
    64  
    65  var testAccDatabaseConfig = `
    66  
    67  resource "influxdb_database" "test" {
    68      name = "terraform-test"
    69  }
    70  
    71  `