github.com/bendemaree/terraform@v0.5.4-0.20150613200311-f50d97d6eee6/builtin/provisioners/chef/resource_provisioner_test.go (about)

     1  package chef
     2  
     3  import (
     4  	"testing"
     5  
     6  	"github.com/hashicorp/terraform/communicator"
     7  	"github.com/hashicorp/terraform/config"
     8  	"github.com/hashicorp/terraform/terraform"
     9  )
    10  
    11  func TestResourceProvisioner_impl(t *testing.T) {
    12  	var _ terraform.ResourceProvisioner = new(ResourceProvisioner)
    13  }
    14  
    15  func TestResourceProvider_Validate_good(t *testing.T) {
    16  	c := testConfig(t, map[string]interface{}{
    17  		"attributes":             []interface{}{"key1 { subkey1 = value1 }"},
    18  		"environment":            "_default",
    19  		"node_name":              "nodename1",
    20  		"run_list":               []interface{}{"cookbook::recipe"},
    21  		"server_url":             "https://chef.local",
    22  		"validation_client_name": "validator",
    23  		"validation_key_path":    "validator.pem",
    24  	})
    25  	r := new(ResourceProvisioner)
    26  	warn, errs := r.Validate(c)
    27  	if len(warn) > 0 {
    28  		t.Fatalf("Warnings: %v", warn)
    29  	}
    30  	if len(errs) > 0 {
    31  		t.Fatalf("Errors: %v", errs)
    32  	}
    33  }
    34  
    35  func TestResourceProvider_Validate_bad(t *testing.T) {
    36  	c := testConfig(t, map[string]interface{}{
    37  		"invalid": "nope",
    38  	})
    39  	p := new(ResourceProvisioner)
    40  	warn, errs := p.Validate(c)
    41  	if len(warn) > 0 {
    42  		t.Fatalf("Warnings: %v", warn)
    43  	}
    44  	if len(errs) == 0 {
    45  		t.Fatalf("Should have errors")
    46  	}
    47  }
    48  
    49  func testConfig(t *testing.T, c map[string]interface{}) *terraform.ResourceConfig {
    50  	r, err := config.NewRawConfig(c)
    51  	if err != nil {
    52  		t.Fatalf("bad: %s", err)
    53  	}
    54  
    55  	return terraform.NewResourceConfig(r)
    56  }
    57  
    58  func TestResourceProvider_runChefClient(t *testing.T) {
    59  	cases := map[string]struct {
    60  		Config   *terraform.ResourceConfig
    61  		ConfDir  string
    62  		Commands map[string]bool
    63  	}{
    64  		"Sudo": {
    65  			Config: testConfig(t, map[string]interface{}{
    66  				"node_name":              "nodename1",
    67  				"run_list":               []interface{}{"cookbook::recipe"},
    68  				"server_url":             "https://chef.local",
    69  				"validation_client_name": "validator",
    70  				"validation_key_path":    "test-fixtures/validator.pem",
    71  			}),
    72  
    73  			ConfDir: linuxConfDir,
    74  
    75  			Commands: map[string]bool{
    76  				`sudo chef-client -j "/etc/chef/first-boot.json" -E "_default"`: true,
    77  			},
    78  		},
    79  
    80  		"NoSudo": {
    81  			Config: testConfig(t, map[string]interface{}{
    82  				"node_name":              "nodename1",
    83  				"prevent_sudo":           true,
    84  				"run_list":               []interface{}{"cookbook::recipe"},
    85  				"server_url":             "https://chef.local",
    86  				"validation_client_name": "validator",
    87  				"validation_key_path":    "test-fixtures/validator.pem",
    88  			}),
    89  
    90  			ConfDir: linuxConfDir,
    91  
    92  			Commands: map[string]bool{
    93  				`chef-client -j "/etc/chef/first-boot.json" -E "_default"`: true,
    94  			},
    95  		},
    96  
    97  		"Environment": {
    98  			Config: testConfig(t, map[string]interface{}{
    99  				"environment":            "production",
   100  				"node_name":              "nodename1",
   101  				"prevent_sudo":           true, // Needs to be set for ALL WinRM tests!
   102  				"run_list":               []interface{}{"cookbook::recipe"},
   103  				"server_url":             "https://chef.local",
   104  				"validation_client_name": "validator",
   105  				"validation_key_path":    "test-fixtures/validator.pem",
   106  			}),
   107  
   108  			ConfDir: windowsConfDir,
   109  
   110  			Commands: map[string]bool{
   111  				`chef-client -j "C:/chef/first-boot.json" -E "production"`: true,
   112  			},
   113  		},
   114  	}
   115  
   116  	r := new(ResourceProvisioner)
   117  	o := new(terraform.MockUIOutput)
   118  	c := new(communicator.MockCommunicator)
   119  
   120  	for k, tc := range cases {
   121  		c.Commands = tc.Commands
   122  
   123  		p, err := r.decodeConfig(tc.Config)
   124  		if err != nil {
   125  			t.Fatalf("Error: %v", err)
   126  		}
   127  
   128  		p.runChefClient = p.runChefClientFunc(tc.ConfDir)
   129  		p.useSudo = !p.PreventSudo
   130  
   131  		err = p.runChefClient(o, c)
   132  		if err != nil {
   133  			t.Fatalf("Test %q failed: %v", k, err)
   134  		}
   135  	}
   136  }