github.com/vtorhonen/terraform@v0.9.0-beta2.0.20170307220345-5d894e4ffda7/builtin/providers/chef/resource_data_bag_item_test.go (about) 1 package chef 2 3 import ( 4 "fmt" 5 "reflect" 6 "testing" 7 8 chefc "github.com/go-chef/chef" 9 10 "github.com/hashicorp/terraform/helper/resource" 11 "github.com/hashicorp/terraform/terraform" 12 ) 13 14 func TestAccDataBagItem_basic(t *testing.T) { 15 var dataBagItemName string 16 resource.Test(t, resource.TestCase{ 17 PreCheck: func() { testAccPreCheck(t) }, 18 Providers: testAccProviders, 19 CheckDestroy: testAccDataBagItemCheckDestroy(dataBagItemName), 20 Steps: []resource.TestStep{ 21 resource.TestStep{ 22 Config: testAccDataBagItemConfig_basic, 23 Check: testAccDataBagItemCheck( 24 "chef_data_bag_item.test", &dataBagItemName, 25 ), 26 }, 27 }, 28 }) 29 } 30 31 func testAccDataBagItemCheck(rn string, name *string) resource.TestCheckFunc { 32 return func(s *terraform.State) error { 33 rs, ok := s.RootModule().Resources[rn] 34 if !ok { 35 return fmt.Errorf("resource not found: %s", rn) 36 } 37 38 if rs.Primary.ID == "" { 39 return fmt.Errorf("data bag item id not set") 40 } 41 42 client := testAccProvider.Meta().(*chefc.Client) 43 content, err := client.DataBags.GetItem("terraform-acc-test-bag-item-basic", rs.Primary.ID) 44 if err != nil { 45 return fmt.Errorf("error getting data bag item: %s", err) 46 } 47 48 expectedContent := map[string]interface{}{ 49 "id": "terraform_acc_test", 50 "something_else": true, 51 } 52 if !reflect.DeepEqual(content, expectedContent) { 53 return fmt.Errorf("wrong content: expected %#v, got %#v", expectedContent, content) 54 } 55 56 if expected := "terraform_acc_test"; rs.Primary.Attributes["id"] != expected { 57 return fmt.Errorf("wrong id; expected %#v, got %#v", expected, rs.Primary.Attributes["id"]) 58 } 59 60 *name = rs.Primary.ID 61 62 return nil 63 } 64 } 65 66 func testAccDataBagItemCheckDestroy(name string) resource.TestCheckFunc { 67 return func(s *terraform.State) error { 68 client := testAccProvider.Meta().(*chefc.Client) 69 _, err := client.DataBags.GetItem("terraform-acc-test-bag-item-basic", name) 70 if err == nil { 71 return fmt.Errorf("data bag item still exists") 72 } 73 if _, ok := err.(*chefc.ErrorResponse); err != nil && !ok { 74 return fmt.Errorf("got something other than an HTTP error (%v) when getting data bag item", err) 75 } 76 77 return nil 78 } 79 } 80 81 const testAccDataBagItemConfig_basic = ` 82 resource "chef_data_bag" "test" { 83 name = "terraform-acc-test-bag-item-basic" 84 } 85 resource "chef_data_bag_item" "test" { 86 data_bag_name = "terraform-acc-test-bag-item-basic" 87 depends_on = ["chef_data_bag.test"] 88 content_json = <<EOT 89 { 90 "id": "terraform_acc_test", 91 "something_else": true 92 } 93 EOT 94 } 95 `