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

     1  package chef
     2  
     3  import (
     4  	"fmt"
     5  	"testing"
     6  
     7  	chefc "github.com/go-chef/chef"
     8  
     9  	"github.com/hashicorp/terraform/helper/resource"
    10  	"github.com/hashicorp/terraform/terraform"
    11  )
    12  
    13  func TestAccDataBag_basic(t *testing.T) {
    14  	var dataBagName string
    15  	resource.Test(t, resource.TestCase{
    16  		PreCheck:     func() { testAccPreCheck(t) },
    17  		Providers:    testAccProviders,
    18  		CheckDestroy: testAccDataBagCheckDestroy(dataBagName),
    19  		Steps: []resource.TestStep{
    20  			resource.TestStep{
    21  				Config: testAccDataBagConfig_basic,
    22  				Check:  testAccDataBagCheckExists("chef_data_bag.test", &dataBagName),
    23  			},
    24  		},
    25  	})
    26  }
    27  
    28  func testAccDataBagCheckExists(rn string, name *string) resource.TestCheckFunc {
    29  	return func(s *terraform.State) error {
    30  		rs, ok := s.RootModule().Resources[rn]
    31  		if !ok {
    32  			return fmt.Errorf("resource not found: %s", rn)
    33  		}
    34  
    35  		if rs.Primary.ID == "" {
    36  			return fmt.Errorf("data bag id not set")
    37  		}
    38  
    39  		client := testAccProvider.Meta().(*chefc.Client)
    40  		_, err := client.DataBags.ListItems(rs.Primary.ID)
    41  		if err != nil {
    42  			return fmt.Errorf("error getting data bag: %s", err)
    43  		}
    44  
    45  		*name = rs.Primary.ID
    46  
    47  		return nil
    48  	}
    49  }
    50  
    51  func testAccDataBagCheckDestroy(name string) resource.TestCheckFunc {
    52  	return func(s *terraform.State) error {
    53  		client := testAccProvider.Meta().(*chefc.Client)
    54  		result, err := client.DataBags.ListItems(name)
    55  		if err == nil && len(*result) != 0 {
    56  			return fmt.Errorf("data bag still exists")
    57  		}
    58  		if _, ok := err.(*chefc.ErrorResponse); err != nil && !ok {
    59  			return fmt.Errorf("got something other than an HTTP error (%v) when getting data bag", err)
    60  		}
    61  
    62  		return nil
    63  	}
    64  }
    65  
    66  const testAccDataBagConfig_basic = `
    67  resource "chef_data_bag" "test" {
    68    name = "terraform-acc-test-basic"
    69  }
    70  `