github.com/mohanarpit/terraform@v0.6.16-0.20160909104007-291f29853544/builtin/providers/grafana/resource_data_source_test.go (about)

     1  package grafana
     2  
     3  import (
     4  	"fmt"
     5  	"regexp"
     6  	"strconv"
     7  	"testing"
     8  
     9  	gapi "github.com/apparentlymart/go-grafana-api"
    10  
    11  	"github.com/hashicorp/terraform/helper/resource"
    12  	"github.com/hashicorp/terraform/terraform"
    13  )
    14  
    15  func TestAccDataSource_basic(t *testing.T) {
    16  	var dataSource gapi.DataSource
    17  
    18  	resource.Test(t, resource.TestCase{
    19  		PreCheck:     func() { testAccPreCheck(t) },
    20  		Providers:    testAccProviders,
    21  		CheckDestroy: testAccDataSourceCheckDestroy(&dataSource),
    22  		Steps: []resource.TestStep{
    23  			resource.TestStep{
    24  				Config: testAccDataSourceConfig_basic,
    25  				Check: resource.ComposeTestCheckFunc(
    26  					testAccDataSourceCheckExists("grafana_data_source.test", &dataSource),
    27  					resource.TestCheckResourceAttr(
    28  						"grafana_data_source.test", "type", "influxdb",
    29  					),
    30  					resource.TestMatchResourceAttr(
    31  						"grafana_data_source.test", "id", regexp.MustCompile(`\d+`),
    32  					),
    33  				),
    34  			},
    35  		},
    36  	})
    37  }
    38  
    39  func testAccDataSourceCheckExists(rn string, dataSource *gapi.DataSource) resource.TestCheckFunc {
    40  	return func(s *terraform.State) error {
    41  		rs, ok := s.RootModule().Resources[rn]
    42  		if !ok {
    43  			return fmt.Errorf("resource not found: %s", rn)
    44  		}
    45  
    46  		if rs.Primary.ID == "" {
    47  			return fmt.Errorf("resource id not set")
    48  		}
    49  
    50  		id, err := strconv.ParseInt(rs.Primary.ID, 10, 64)
    51  		if err != nil {
    52  			return fmt.Errorf("resource id is malformed")
    53  		}
    54  
    55  		client := testAccProvider.Meta().(*gapi.Client)
    56  		gotDataSource, err := client.DataSource(id)
    57  		if err != nil {
    58  			return fmt.Errorf("error getting data source: %s", err)
    59  		}
    60  
    61  		*dataSource = *gotDataSource
    62  
    63  		return nil
    64  	}
    65  }
    66  
    67  func testAccDataSourceCheckDestroy(dataSource *gapi.DataSource) resource.TestCheckFunc {
    68  	return func(s *terraform.State) error {
    69  		client := testAccProvider.Meta().(*gapi.Client)
    70  		_, err := client.DataSource(dataSource.Id)
    71  		if err == nil {
    72  			return fmt.Errorf("data source still exists")
    73  		}
    74  		return nil
    75  	}
    76  }
    77  
    78  const testAccDataSourceConfig_basic = `
    79  resource "grafana_data_source" "test" {
    80      type = "influxdb"
    81      name = "terraform-acc-test"
    82      database_name = "terraform-acc-test"
    83      url = "http://terraform-acc-test.invalid/"
    84      username = "terraform_user"
    85      password = "terraform_password"
    86  }
    87  `