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

     1  package chef
     2  
     3  import (
     4  	"testing"
     5  
     6  	"github.com/hashicorp/terraform/communicator"
     7  	"github.com/hashicorp/terraform/terraform"
     8  )
     9  
    10  func TestResourceProvider_sshInstallChefClient(t *testing.T) {
    11  	cases := map[string]struct {
    12  		Config   *terraform.ResourceConfig
    13  		Commands map[string]bool
    14  	}{
    15  		"Sudo": {
    16  			Config: testConfig(t, map[string]interface{}{
    17  				"node_name":              "nodename1",
    18  				"run_list":               []interface{}{"cookbook::recipe"},
    19  				"server_url":             "https://chef.local",
    20  				"validation_client_name": "validator",
    21  				"validation_key_path":    "validator.pem",
    22  			}),
    23  
    24  			Commands: map[string]bool{
    25  				"sudo curl -LO https://www.chef.io/chef/install.sh": true,
    26  				"sudo bash ./install.sh -v ":                        true,
    27  				"sudo rm -f install.sh":                             true,
    28  			},
    29  		},
    30  
    31  		"NoSudo": {
    32  			Config: testConfig(t, map[string]interface{}{
    33  				"node_name":              "nodename1",
    34  				"prevent_sudo":           true,
    35  				"run_list":               []interface{}{"cookbook::recipe"},
    36  				"server_url":             "https://chef.local",
    37  				"validation_client_name": "validator",
    38  				"validation_key_path":    "validator.pem",
    39  			}),
    40  
    41  			Commands: map[string]bool{
    42  				"curl -LO https://www.chef.io/chef/install.sh": true,
    43  				"bash ./install.sh -v ":                        true,
    44  				"rm -f install.sh":                             true,
    45  			},
    46  		},
    47  
    48  		"HTTPProxy": {
    49  			Config: testConfig(t, map[string]interface{}{
    50  				"http_proxy":             "http://proxy.local",
    51  				"node_name":              "nodename1",
    52  				"prevent_sudo":           true,
    53  				"run_list":               []interface{}{"cookbook::recipe"},
    54  				"server_url":             "https://chef.local",
    55  				"validation_client_name": "validator",
    56  				"validation_key_path":    "validator.pem",
    57  			}),
    58  
    59  			Commands: map[string]bool{
    60  				"proxy_http='http://proxy.local' curl -LO https://www.chef.io/chef/install.sh": true,
    61  				"proxy_http='http://proxy.local' bash ./install.sh -v ":                        true,
    62  				"proxy_http='http://proxy.local' rm -f install.sh":                             true,
    63  			},
    64  		},
    65  
    66  		"NOProxy": {
    67  			Config: testConfig(t, map[string]interface{}{
    68  				"http_proxy":             "http://proxy.local",
    69  				"no_proxy":               []interface{}{"http://local.local", "http://local.org"},
    70  				"node_name":              "nodename1",
    71  				"prevent_sudo":           true,
    72  				"run_list":               []interface{}{"cookbook::recipe"},
    73  				"server_url":             "https://chef.local",
    74  				"validation_client_name": "validator",
    75  				"validation_key_path":    "validator.pem",
    76  			}),
    77  
    78  			Commands: map[string]bool{
    79  				"proxy_http='http://proxy.local' no_proxy='http://local.local,http://local.org' " +
    80  					"curl -LO https://www.chef.io/chef/install.sh": true,
    81  				"proxy_http='http://proxy.local' no_proxy='http://local.local,http://local.org' " +
    82  					"bash ./install.sh -v ": true,
    83  				"proxy_http='http://proxy.local' no_proxy='http://local.local,http://local.org' " +
    84  					"rm -f install.sh": true,
    85  			},
    86  		},
    87  
    88  		"Version": {
    89  			Config: testConfig(t, map[string]interface{}{
    90  				"node_name":              "nodename1",
    91  				"prevent_sudo":           true,
    92  				"run_list":               []interface{}{"cookbook::recipe"},
    93  				"server_url":             "https://chef.local",
    94  				"validation_client_name": "validator",
    95  				"validation_key_path":    "validator.pem",
    96  				"version":                "11.18.6",
    97  			}),
    98  
    99  			Commands: map[string]bool{
   100  				"curl -LO https://www.chef.io/chef/install.sh": true,
   101  				"bash ./install.sh -v 11.18.6":                 true,
   102  				"rm -f install.sh":                             true,
   103  			},
   104  		},
   105  	}
   106  
   107  	r := new(ResourceProvisioner)
   108  	o := new(terraform.MockUIOutput)
   109  	c := new(communicator.MockCommunicator)
   110  
   111  	for k, tc := range cases {
   112  		c.Commands = tc.Commands
   113  
   114  		p, err := r.decodeConfig(tc.Config)
   115  		if err != nil {
   116  			t.Fatalf("Error: %v", err)
   117  		}
   118  
   119  		p.useSudo = !p.PreventSudo
   120  
   121  		err = p.sshInstallChefClient(o, c)
   122  		if err != nil {
   123  			t.Fatalf("Test %q failed: %v", k, err)
   124  		}
   125  	}
   126  }
   127  
   128  func TestResourceProvider_sshCreateConfigFiles(t *testing.T) {
   129  	cases := map[string]struct {
   130  		Config   *terraform.ResourceConfig
   131  		Commands map[string]bool
   132  		Uploads  map[string]string
   133  	}{
   134  		"Sudo": {
   135  			Config: testConfig(t, map[string]interface{}{
   136  				"node_name":              "nodename1",
   137  				"run_list":               []interface{}{"cookbook::recipe"},
   138  				"server_url":             "https://chef.local",
   139  				"validation_client_name": "validator",
   140  				"validation_key_path":    "test-fixtures/validator.pem",
   141  			}),
   142  
   143  			Commands: map[string]bool{
   144  				"sudo mkdir -p " + linuxConfDir:           true,
   145  				"sudo chmod 777 " + linuxConfDir:          true,
   146  				"sudo chmod 755 " + linuxConfDir:          true,
   147  				"sudo chown -R root.root " + linuxConfDir: true,
   148  			},
   149  
   150  			Uploads: map[string]string{
   151  				"/etc/chef/validation.pem":  "VALIDATOR-PEM-FILE",
   152  				"/etc/chef/client.rb":       defaultSSHClientConf,
   153  				"/etc/chef/first-boot.json": `{"run_list":["cookbook::recipe"]}`,
   154  			},
   155  		},
   156  
   157  		"NoSudo": {
   158  			Config: testConfig(t, map[string]interface{}{
   159  				"node_name":              "nodename1",
   160  				"prevent_sudo":           true,
   161  				"run_list":               []interface{}{"cookbook::recipe"},
   162  				"server_url":             "https://chef.local",
   163  				"validation_client_name": "validator",
   164  				"validation_key_path":    "test-fixtures/validator.pem",
   165  			}),
   166  
   167  			Commands: map[string]bool{
   168  				"mkdir -p " + linuxConfDir: true,
   169  			},
   170  
   171  			Uploads: map[string]string{
   172  				"/etc/chef/validation.pem":  "VALIDATOR-PEM-FILE",
   173  				"/etc/chef/client.rb":       defaultSSHClientConf,
   174  				"/etc/chef/first-boot.json": `{"run_list":["cookbook::recipe"]}`,
   175  			},
   176  		},
   177  
   178  		"Proxy": {
   179  			Config: testConfig(t, map[string]interface{}{
   180  				"http_proxy":             "http://proxy.local",
   181  				"https_proxy":            "https://proxy.local",
   182  				"no_proxy":               []interface{}{"http://local.local", "https://local.local"},
   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  			Commands: map[string]bool{
   192  				"mkdir -p " + linuxConfDir: true,
   193  			},
   194  
   195  			Uploads: map[string]string{
   196  				"/etc/chef/validation.pem":  "VALIDATOR-PEM-FILE",
   197  				"/etc/chef/client.rb":       proxySSHClientConf,
   198  				"/etc/chef/first-boot.json": `{"run_list":["cookbook::recipe"]}`,
   199  			},
   200  		},
   201  
   202  		"Attributes": {
   203  			Config: testConfig(t, map[string]interface{}{
   204  				"attributes": []map[string]interface{}{
   205  					map[string]interface{}{
   206  						"key1": []map[string]interface{}{
   207  							map[string]interface{}{
   208  								"subkey1": []map[string]interface{}{
   209  									map[string]interface{}{
   210  										"subkey2a": []interface{}{
   211  											"val1", "val2", "val3",
   212  										},
   213  										"subkey2b": []map[string]interface{}{
   214  											map[string]interface{}{
   215  												"subkey3": "value3",
   216  											},
   217  										},
   218  									},
   219  								},
   220  							},
   221  						},
   222  						"key2": "value2",
   223  					},
   224  				},
   225  				"node_name":              "nodename1",
   226  				"prevent_sudo":           true,
   227  				"run_list":               []interface{}{"cookbook::recipe"},
   228  				"server_url":             "https://chef.local",
   229  				"validation_client_name": "validator",
   230  				"validation_key_path":    "test-fixtures/validator.pem",
   231  			}),
   232  
   233  			Commands: map[string]bool{
   234  				"mkdir -p " + linuxConfDir: true,
   235  			},
   236  
   237  			Uploads: map[string]string{
   238  				"/etc/chef/validation.pem": "VALIDATOR-PEM-FILE",
   239  				"/etc/chef/client.rb":      defaultSSHClientConf,
   240  				"/etc/chef/first-boot.json": `{"key1":{"subkey1":{"subkey2a":["val1","val2","val3"],` +
   241  					`"subkey2b":{"subkey3":"value3"}}},"key2":"value2","run_list":["cookbook::recipe"]}`,
   242  			},
   243  		},
   244  	}
   245  
   246  	r := new(ResourceProvisioner)
   247  	o := new(terraform.MockUIOutput)
   248  	c := new(communicator.MockCommunicator)
   249  
   250  	for k, tc := range cases {
   251  		c.Commands = tc.Commands
   252  		c.Uploads = tc.Uploads
   253  
   254  		p, err := r.decodeConfig(tc.Config)
   255  		if err != nil {
   256  			t.Fatalf("Error: %v", err)
   257  		}
   258  
   259  		p.useSudo = !p.PreventSudo
   260  
   261  		err = p.sshCreateConfigFiles(o, c)
   262  		if err != nil {
   263  			t.Fatalf("Test %q failed: %v", k, err)
   264  		}
   265  	}
   266  }
   267  
   268  const defaultSSHClientConf = `log_location            STDOUT
   269  chef_server_url         "https://chef.local"
   270  validation_client_name  "validator"
   271  node_name               "nodename1"`
   272  
   273  const proxySSHClientConf = `log_location            STDOUT
   274  chef_server_url         "https://chef.local"
   275  validation_client_name  "validator"
   276  node_name               "nodename1"
   277  
   278  
   279  http_proxy          "http://proxy.local"
   280  ENV['http_proxy'] = "http://proxy.local"
   281  ENV['HTTP_PROXY'] = "http://proxy.local"
   282  
   283  
   284  
   285  https_proxy          "https://proxy.local"
   286  ENV['https_proxy'] = "https://proxy.local"
   287  ENV['HTTPS_PROXY'] = "https://proxy.local"
   288  
   289  
   290  no_proxy "http://local.local,https://local.local"`