github.com/leeprovoost/terraform@v0.6.10-0.20160119085442-96f3f76118e7/builtin/providers/chef/resource_role_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 TestAccRole_basic(t *testing.T) { 15 var role chefc.Role 16 17 resource.Test(t, resource.TestCase{ 18 PreCheck: func() { testAccPreCheck(t) }, 19 Providers: testAccProviders, 20 CheckDestroy: testAccRoleCheckDestroy(&role), 21 Steps: []resource.TestStep{ 22 resource.TestStep{ 23 Config: testAccRoleConfig_basic, 24 Check: resource.ComposeTestCheckFunc( 25 testAccRoleCheckExists("chef_role.test", &role), 26 func(s *terraform.State) error { 27 28 if expected := "terraform-acc-test-basic"; role.Name != expected { 29 return fmt.Errorf("wrong name; expected %v, got %v", expected, role.Name) 30 } 31 if expected := "Terraform Acceptance Tests"; role.Description != expected { 32 return fmt.Errorf("wrong description; expected %v, got %v", expected, role.Description) 33 } 34 35 expectedRunList := chefc.RunList{ 36 "recipe[terraform@1.0.0]", 37 "recipe[consul]", 38 "role[foo]", 39 } 40 if !reflect.DeepEqual(role.RunList, expectedRunList) { 41 return fmt.Errorf("wrong runlist; expected %#v, got %#v", expectedRunList, role.RunList) 42 } 43 44 var expectedAttributes interface{} 45 expectedAttributes = map[string]interface{}{ 46 "terraform_acc_test": true, 47 } 48 if !reflect.DeepEqual(role.DefaultAttributes, expectedAttributes) { 49 return fmt.Errorf("wrong default attributes; expected %#v, got %#v", expectedAttributes, role.DefaultAttributes) 50 } 51 if !reflect.DeepEqual(role.OverrideAttributes, expectedAttributes) { 52 return fmt.Errorf("wrong override attributes; expected %#v, got %#v", expectedAttributes, role.OverrideAttributes) 53 } 54 55 return nil 56 }, 57 ), 58 }, 59 }, 60 }) 61 } 62 63 func testAccRoleCheckExists(rn string, role *chefc.Role) resource.TestCheckFunc { 64 return func(s *terraform.State) error { 65 rs, ok := s.RootModule().Resources[rn] 66 if !ok { 67 return fmt.Errorf("resource not found: %s", rn) 68 } 69 70 if rs.Primary.ID == "" { 71 return fmt.Errorf("role id not set") 72 } 73 74 client := testAccProvider.Meta().(*chefc.Client) 75 gotRole, err := client.Roles.Get(rs.Primary.ID) 76 if err != nil { 77 return fmt.Errorf("error getting role: %s", err) 78 } 79 80 *role = *gotRole 81 82 return nil 83 } 84 } 85 86 func testAccRoleCheckDestroy(role *chefc.Role) resource.TestCheckFunc { 87 return func(s *terraform.State) error { 88 client := testAccProvider.Meta().(*chefc.Client) 89 _, err := client.Roles.Get(role.Name) 90 if err == nil { 91 return fmt.Errorf("role still exists") 92 } 93 if _, ok := err.(*chefc.ErrorResponse); !ok { 94 // A more specific check is tricky because Chef Server can return 95 // a few different error codes in this case depending on which 96 // part of its stack catches the error. 97 return fmt.Errorf("got something other than an HTTP error (%v) when getting role", err) 98 } 99 100 return nil 101 } 102 } 103 104 const testAccRoleConfig_basic = ` 105 resource "chef_role" "test" { 106 name = "terraform-acc-test-basic" 107 description = "Terraform Acceptance Tests" 108 default_attributes_json = <<EOT 109 { 110 "terraform_acc_test": true 111 } 112 EOT 113 override_attributes_json = <<EOT 114 { 115 "terraform_acc_test": true 116 } 117 EOT 118 run_list = ["terraform@1.0.0", "recipe[consul]", "role[foo]"] 119 } 120 `