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