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