github.com/richardbowden/terraform@v0.6.12-0.20160901200758-30ea22c25211/builtin/provisioners/chef/resource_provisioner_test.go (about)

     1  package chef
     2  
     3  import (
     4  	"fmt"
     5  	"path"
     6  	"testing"
     7  
     8  	"github.com/hashicorp/terraform/communicator"
     9  	"github.com/hashicorp/terraform/config"
    10  	"github.com/hashicorp/terraform/terraform"
    11  )
    12  
    13  func TestResourceProvisioner_impl(t *testing.T) {
    14  	var _ terraform.ResourceProvisioner = new(ResourceProvisioner)
    15  }
    16  
    17  func TestResourceProvider_Validate_good(t *testing.T) {
    18  	c := testConfig(t, map[string]interface{}{
    19  		"environment":            "_default",
    20  		"node_name":              "nodename1",
    21  		"run_list":               []interface{}{"cookbook::recipe"},
    22  		"server_url":             "https://chef.local",
    23  		"validation_client_name": "validator",
    24  		"validation_key":         "contentsofsomevalidator.pem",
    25  	})
    26  	r := new(ResourceProvisioner)
    27  	warn, errs := r.Validate(c)
    28  	if len(warn) > 0 {
    29  		t.Fatalf("Warnings: %v", warn)
    30  	}
    31  	if len(errs) > 0 {
    32  		t.Fatalf("Errors: %v", errs)
    33  	}
    34  }
    35  
    36  func TestResourceProvider_Validate_bad(t *testing.T) {
    37  	c := testConfig(t, map[string]interface{}{
    38  		"invalid": "nope",
    39  	})
    40  	p := new(ResourceProvisioner)
    41  	warn, errs := p.Validate(c)
    42  	if len(warn) > 0 {
    43  		t.Fatalf("Warnings: %v", warn)
    44  	}
    45  	if len(errs) == 0 {
    46  		t.Fatalf("Should have errors")
    47  	}
    48  }
    49  
    50  func testConfig(t *testing.T, c map[string]interface{}) *terraform.ResourceConfig {
    51  	r, err := config.NewRawConfig(c)
    52  	if err != nil {
    53  		t.Fatalf("bad: %s", err)
    54  	}
    55  
    56  	return terraform.NewResourceConfig(r)
    57  }
    58  
    59  func TestResourceProvider_runChefClient(t *testing.T) {
    60  	cases := map[string]struct {
    61  		Config   *terraform.ResourceConfig
    62  		ChefCmd  string
    63  		ConfDir  string
    64  		Commands map[string]bool
    65  	}{
    66  		"Sudo": {
    67  			Config: testConfig(t, map[string]interface{}{
    68  				"node_name":              "nodename1",
    69  				"run_list":               []interface{}{"cookbook::recipe"},
    70  				"server_url":             "https://chef.local",
    71  				"validation_client_name": "validator",
    72  				"validation_key_path":    "test-fixtures/validator.pem",
    73  			}),
    74  
    75  			ChefCmd: linuxChefCmd,
    76  
    77  			ConfDir: linuxConfDir,
    78  
    79  			Commands: map[string]bool{
    80  				fmt.Sprintf(`sudo %s -j %q -E "_default"`,
    81  					linuxChefCmd,
    82  					path.Join(linuxConfDir, "first-boot.json")): true,
    83  			},
    84  		},
    85  
    86  		"NoSudo": {
    87  			Config: testConfig(t, map[string]interface{}{
    88  				"node_name":              "nodename1",
    89  				"prevent_sudo":           true,
    90  				"run_list":               []interface{}{"cookbook::recipe"},
    91  				"server_url":             "https://chef.local",
    92  				"validation_client_name": "validator",
    93  				"validation_key_path":    "test-fixtures/validator.pem",
    94  			}),
    95  
    96  			ChefCmd: linuxChefCmd,
    97  
    98  			ConfDir: linuxConfDir,
    99  
   100  			Commands: map[string]bool{
   101  				fmt.Sprintf(`%s -j %q -E "_default"`,
   102  					linuxChefCmd,
   103  					path.Join(linuxConfDir, "first-boot.json")): true,
   104  			},
   105  		},
   106  
   107  		"Environment": {
   108  			Config: testConfig(t, map[string]interface{}{
   109  				"environment":            "production",
   110  				"node_name":              "nodename1",
   111  				"prevent_sudo":           true,
   112  				"run_list":               []interface{}{"cookbook::recipe"},
   113  				"server_url":             "https://chef.local",
   114  				"validation_client_name": "validator",
   115  				"validation_key_path":    "test-fixtures/validator.pem",
   116  			}),
   117  
   118  			ChefCmd: windowsChefCmd,
   119  
   120  			ConfDir: windowsConfDir,
   121  
   122  			Commands: map[string]bool{
   123  				fmt.Sprintf(`%s -j %q -E "production"`,
   124  					windowsChefCmd,
   125  					path.Join(windowsConfDir, "first-boot.json")): true,
   126  			},
   127  		},
   128  	}
   129  
   130  	r := new(ResourceProvisioner)
   131  	o := new(terraform.MockUIOutput)
   132  	c := new(communicator.MockCommunicator)
   133  
   134  	for k, tc := range cases {
   135  		c.Commands = tc.Commands
   136  
   137  		p, err := r.decodeConfig(tc.Config)
   138  		if err != nil {
   139  			t.Fatalf("Error: %v", err)
   140  		}
   141  
   142  		p.runChefClient = p.runChefClientFunc(tc.ChefCmd, tc.ConfDir)
   143  		p.useSudo = !p.PreventSudo
   144  
   145  		err = p.runChefClient(o, c)
   146  		if err != nil {
   147  			t.Fatalf("Test %q failed: %v", k, err)
   148  		}
   149  	}
   150  }
   151  
   152  func TestResourceProvider_fetchChefCertificates(t *testing.T) {
   153  	cases := map[string]struct {
   154  		Config   *terraform.ResourceConfig
   155  		KnifeCmd string
   156  		ConfDir  string
   157  		Commands map[string]bool
   158  	}{
   159  		"Sudo": {
   160  			Config: testConfig(t, map[string]interface{}{
   161  				"fetch_chef_certificates": true,
   162  				"node_name":               "nodename1",
   163  				"run_list":                []interface{}{"cookbook::recipe"},
   164  				"server_url":              "https://chef.local",
   165  				"validation_client_name":  "validator",
   166  				"validation_key_path":     "test-fixtures/validator.pem",
   167  			}),
   168  
   169  			KnifeCmd: linuxKnifeCmd,
   170  
   171  			ConfDir: linuxConfDir,
   172  
   173  			Commands: map[string]bool{
   174  				fmt.Sprintf(`sudo %s ssl fetch -c %s`,
   175  					linuxKnifeCmd,
   176  					path.Join(linuxConfDir, "client.rb")): true,
   177  			},
   178  		},
   179  
   180  		"NoSudo": {
   181  			Config: testConfig(t, map[string]interface{}{
   182  				"fetch_chef_certificates": true,
   183  				"node_name":               "nodename1",
   184  				"prevent_sudo":            true,
   185  				"run_list":                []interface{}{"cookbook::recipe"},
   186  				"server_url":              "https://chef.local",
   187  				"validation_client_name":  "validator",
   188  				"validation_key_path":     "test-fixtures/validator.pem",
   189  			}),
   190  
   191  			KnifeCmd: windowsKnifeCmd,
   192  
   193  			ConfDir: windowsConfDir,
   194  
   195  			Commands: map[string]bool{
   196  				fmt.Sprintf(`%s ssl fetch -c %s`,
   197  					windowsKnifeCmd,
   198  					path.Join(windowsConfDir, "client.rb")): true,
   199  			},
   200  		},
   201  	}
   202  
   203  	r := new(ResourceProvisioner)
   204  	o := new(terraform.MockUIOutput)
   205  	c := new(communicator.MockCommunicator)
   206  
   207  	for k, tc := range cases {
   208  		c.Commands = tc.Commands
   209  
   210  		p, err := r.decodeConfig(tc.Config)
   211  		if err != nil {
   212  			t.Fatalf("Error: %v", err)
   213  		}
   214  
   215  		p.fetchChefCertificates = p.fetchChefCertificatesFunc(tc.KnifeCmd, tc.ConfDir)
   216  		p.useSudo = !p.PreventSudo
   217  
   218  		err = p.fetchChefCertificates(o, c)
   219  		if err != nil {
   220  			t.Fatalf("Test %q failed: %v", k, err)
   221  		}
   222  	}
   223  }