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