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

     1  package circonus
     2  
     3  import (
     4  	"fmt"
     5  	"testing"
     6  
     7  	"github.com/hashicorp/terraform/helper/resource"
     8  	"github.com/hashicorp/terraform/terraform"
     9  )
    10  
    11  func TestAccDataSourceCirconusAccount(t *testing.T) {
    12  	resource.Test(t, resource.TestCase{
    13  		PreCheck:  func() { testAccPreCheck(t) },
    14  		Providers: testAccProviders,
    15  		Steps: []resource.TestStep{
    16  			resource.TestStep{
    17  				Config: testAccDataSourceCirconusAccountCurrentConfig,
    18  				Check: resource.ComposeTestCheckFunc(
    19  					testAccDataSourceCirconusAccountCheck("data.circonus_account.by_current", "/account/3081"),
    20  				),
    21  			},
    22  		},
    23  	})
    24  
    25  	resource.Test(t, resource.TestCase{
    26  		PreCheck:  func() { testAccPreCheck(t) },
    27  		Providers: testAccProviders,
    28  		Steps: []resource.TestStep{
    29  			resource.TestStep{
    30  				Config: testAccDataSourceCirconusAccountIDConfig,
    31  				Check: resource.ComposeTestCheckFunc(
    32  					testAccDataSourceCirconusAccountCheck("data.circonus_account.by_id", "/account/3081"),
    33  				),
    34  			},
    35  		},
    36  	})
    37  }
    38  
    39  func testAccDataSourceCirconusAccountCheck(name, cid string) resource.TestCheckFunc {
    40  	return func(s *terraform.State) error {
    41  		rs, ok := s.RootModule().Resources[name]
    42  		if !ok {
    43  			return fmt.Errorf("root module has no resource called %s", name)
    44  		}
    45  
    46  		attr := rs.Primary.Attributes
    47  
    48  		if attr[accountIDAttr] != cid {
    49  			return fmt.Errorf("bad %s %s", accountIDAttr, attr[accountIDAttr])
    50  		}
    51  
    52  		return nil
    53  	}
    54  }
    55  
    56  const testAccDataSourceCirconusAccountCurrentConfig = `
    57  data "circonus_account" "by_current" {
    58    current = true
    59  }
    60  `
    61  
    62  const testAccDataSourceCirconusAccountIDConfig = `
    63  data "circonus_account" "by_id" {
    64    id = "/account/3081"
    65  }
    66  `