github.com/richardbowden/terraform@v0.6.12-0.20160901200758-30ea22c25211/builtin/provisioners/chef/linux_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/terraform"
    10  )
    11  
    12  func TestResourceProvider_linuxInstallChefClient(t *testing.T) {
    13  	cases := map[string]struct {
    14  		Config   *terraform.ResourceConfig
    15  		Commands map[string]bool
    16  	}{
    17  		"Sudo": {
    18  			Config: testConfig(t, map[string]interface{}{
    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  
    26  			Commands: map[string]bool{
    27  				"sudo curl -LO https://www.chef.io/chef/install.sh": true,
    28  				"sudo bash ./install.sh -v \"\"":                    true,
    29  				"sudo rm -f install.sh":                             true,
    30  			},
    31  		},
    32  
    33  		"NoSudo": {
    34  			Config: testConfig(t, map[string]interface{}{
    35  				"node_name":              "nodename1",
    36  				"prevent_sudo":           true,
    37  				"run_list":               []interface{}{"cookbook::recipe"},
    38  				"server_url":             "https://chef.local",
    39  				"validation_client_name": "validator",
    40  				"validation_key_path":    "validator.pem",
    41  				"secret_key_path":        "encrypted_data_bag_secret",
    42  			}),
    43  
    44  			Commands: map[string]bool{
    45  				"curl -LO https://www.chef.io/chef/install.sh": true,
    46  				"bash ./install.sh -v \"\"":                    true,
    47  				"rm -f install.sh":                             true,
    48  			},
    49  		},
    50  
    51  		"HTTPProxy": {
    52  			Config: testConfig(t, map[string]interface{}{
    53  				"http_proxy":             "http://proxy.local",
    54  				"node_name":              "nodename1",
    55  				"prevent_sudo":           true,
    56  				"run_list":               []interface{}{"cookbook::recipe"},
    57  				"server_url":             "https://chef.local",
    58  				"validation_client_name": "validator",
    59  				"validation_key_path":    "validator.pem",
    60  			}),
    61  
    62  			Commands: map[string]bool{
    63  				"http_proxy='http://proxy.local' curl -LO https://www.chef.io/chef/install.sh": true,
    64  				"http_proxy='http://proxy.local' bash ./install.sh -v \"\"":                    true,
    65  				"http_proxy='http://proxy.local' rm -f install.sh":                             true,
    66  			},
    67  		},
    68  
    69  		"HTTPSProxy": {
    70  			Config: testConfig(t, map[string]interface{}{
    71  				"https_proxy":            "https://proxy.local",
    72  				"node_name":              "nodename1",
    73  				"prevent_sudo":           true,
    74  				"run_list":               []interface{}{"cookbook::recipe"},
    75  				"server_url":             "https://chef.local",
    76  				"validation_client_name": "validator",
    77  				"validation_key_path":    "validator.pem",
    78  			}),
    79  
    80  			Commands: map[string]bool{
    81  				"https_proxy='https://proxy.local' curl -LO https://www.chef.io/chef/install.sh": true,
    82  				"https_proxy='https://proxy.local' bash ./install.sh -v \"\"":                    true,
    83  				"https_proxy='https://proxy.local' rm -f install.sh":                             true,
    84  			},
    85  		},
    86  
    87  		"NoProxy": {
    88  			Config: testConfig(t, map[string]interface{}{
    89  				"http_proxy":             "http://proxy.local",
    90  				"no_proxy":               []interface{}{"http://local.local", "http://local.org"},
    91  				"node_name":              "nodename1",
    92  				"prevent_sudo":           true,
    93  				"run_list":               []interface{}{"cookbook::recipe"},
    94  				"server_url":             "https://chef.local",
    95  				"validation_client_name": "validator",
    96  				"validation_key_path":    "validator.pem",
    97  			}),
    98  
    99  			Commands: map[string]bool{
   100  				"http_proxy='http://proxy.local' no_proxy='http://local.local,http://local.org' " +
   101  					"curl -LO https://www.chef.io/chef/install.sh": true,
   102  				"http_proxy='http://proxy.local' no_proxy='http://local.local,http://local.org' " +
   103  					"bash ./install.sh -v \"\"": true,
   104  				"http_proxy='http://proxy.local' no_proxy='http://local.local,http://local.org' " +
   105  					"rm -f install.sh": true,
   106  			},
   107  		},
   108  
   109  		"Version": {
   110  			Config: testConfig(t, map[string]interface{}{
   111  				"node_name":              "nodename1",
   112  				"prevent_sudo":           true,
   113  				"run_list":               []interface{}{"cookbook::recipe"},
   114  				"server_url":             "https://chef.local",
   115  				"validation_client_name": "validator",
   116  				"validation_key_path":    "validator.pem",
   117  				"version":                "11.18.6",
   118  			}),
   119  
   120  			Commands: map[string]bool{
   121  				"curl -LO https://www.chef.io/chef/install.sh": true,
   122  				"bash ./install.sh -v \"11.18.6\"":             true,
   123  				"rm -f install.sh":                             true,
   124  			},
   125  		},
   126  	}
   127  
   128  	r := new(ResourceProvisioner)
   129  	o := new(terraform.MockUIOutput)
   130  	c := new(communicator.MockCommunicator)
   131  
   132  	for k, tc := range cases {
   133  		c.Commands = tc.Commands
   134  
   135  		p, err := r.decodeConfig(tc.Config)
   136  		if err != nil {
   137  			t.Fatalf("Error: %v", err)
   138  		}
   139  
   140  		p.useSudo = !p.PreventSudo
   141  
   142  		err = p.linuxInstallChefClient(o, c)
   143  		if err != nil {
   144  			t.Fatalf("Test %q failed: %v", k, err)
   145  		}
   146  	}
   147  }
   148  
   149  func TestResourceProvider_linuxCreateConfigFiles(t *testing.T) {
   150  	cases := map[string]struct {
   151  		Config   *terraform.ResourceConfig
   152  		Commands map[string]bool
   153  		Uploads  map[string]string
   154  	}{
   155  		"Sudo": {
   156  			Config: testConfig(t, map[string]interface{}{
   157  				"ohai_hints":             []interface{}{"test-fixtures/ohaihint.json"},
   158  				"node_name":              "nodename1",
   159  				"run_list":               []interface{}{"cookbook::recipe"},
   160  				"secret_key_path":        "test-fixtures/encrypted_data_bag_secret",
   161  				"server_url":             "https://chef.local",
   162  				"validation_client_name": "validator",
   163  				"validation_key_path":    "test-fixtures/validator.pem",
   164  			}),
   165  
   166  			Commands: map[string]bool{
   167  				"sudo mkdir -p " + linuxConfDir:                                          true,
   168  				"sudo chmod 777 " + linuxConfDir:                                         true,
   169  				"sudo " + fmt.Sprintf(chmod, linuxConfDir, 666):                          true,
   170  				"sudo mkdir -p " + path.Join(linuxConfDir, "ohai/hints"):                 true,
   171  				"sudo chmod 777 " + path.Join(linuxConfDir, "ohai/hints"):                true,
   172  				"sudo " + fmt.Sprintf(chmod, path.Join(linuxConfDir, "ohai/hints"), 666): true,
   173  				"sudo chmod 755 " + path.Join(linuxConfDir, "ohai/hints"):                true,
   174  				"sudo " + fmt.Sprintf(chmod, path.Join(linuxConfDir, "ohai/hints"), 600): true,
   175  				"sudo chown -R root.root " + path.Join(linuxConfDir, "ohai/hints"):       true,
   176  				"sudo chmod 755 " + linuxConfDir:                                         true,
   177  				"sudo " + fmt.Sprintf(chmod, linuxConfDir, 600):                          true,
   178  				"sudo chown -R root.root " + linuxConfDir:                                true,
   179  			},
   180  
   181  			Uploads: map[string]string{
   182  				linuxConfDir + "/client.rb":                 defaultLinuxClientConf,
   183  				linuxConfDir + "/encrypted_data_bag_secret": "SECRET-KEY-FILE",
   184  				linuxConfDir + "/first-boot.json":           `{"run_list":["cookbook::recipe"]}`,
   185  				linuxConfDir + "/ohai/hints/ohaihint.json":  "OHAI-HINT-FILE",
   186  				linuxConfDir + "/validation.pem":            "VALIDATOR-PEM-FILE",
   187  			},
   188  		},
   189  
   190  		"NoSudo": {
   191  			Config: testConfig(t, map[string]interface{}{
   192  				"node_name":              "nodename1",
   193  				"prevent_sudo":           true,
   194  				"run_list":               []interface{}{"cookbook::recipe"},
   195  				"secret_key_path":        "test-fixtures/encrypted_data_bag_secret",
   196  				"server_url":             "https://chef.local",
   197  				"validation_client_name": "validator",
   198  				"validation_key_path":    "test-fixtures/validator.pem",
   199  			}),
   200  
   201  			Commands: map[string]bool{
   202  				"mkdir -p " + linuxConfDir: true,
   203  			},
   204  
   205  			Uploads: map[string]string{
   206  				linuxConfDir + "/client.rb":                 defaultLinuxClientConf,
   207  				linuxConfDir + "/encrypted_data_bag_secret": "SECRET-KEY-FILE",
   208  				linuxConfDir + "/first-boot.json":           `{"run_list":["cookbook::recipe"]}`,
   209  				linuxConfDir + "/validation.pem":            "VALIDATOR-PEM-FILE",
   210  			},
   211  		},
   212  
   213  		"Proxy": {
   214  			Config: testConfig(t, map[string]interface{}{
   215  				"http_proxy":             "http://proxy.local",
   216  				"https_proxy":            "https://proxy.local",
   217  				"no_proxy":               []interface{}{"http://local.local", "https://local.local"},
   218  				"node_name":              "nodename1",
   219  				"prevent_sudo":           true,
   220  				"run_list":               []interface{}{"cookbook::recipe"},
   221  				"secret_key_path":        "test-fixtures/encrypted_data_bag_secret",
   222  				"server_url":             "https://chef.local",
   223  				"ssl_verify_mode":        "verify_none",
   224  				"validation_client_name": "validator",
   225  				"validation_key_path":    "test-fixtures/validator.pem",
   226  			}),
   227  
   228  			Commands: map[string]bool{
   229  				"mkdir -p " + linuxConfDir: true,
   230  			},
   231  
   232  			Uploads: map[string]string{
   233  				linuxConfDir + "/client.rb":                 proxyLinuxClientConf,
   234  				linuxConfDir + "/encrypted_data_bag_secret": "SECRET-KEY-FILE",
   235  				linuxConfDir + "/first-boot.json":           `{"run_list":["cookbook::recipe"]}`,
   236  				linuxConfDir + "/validation.pem":            "VALIDATOR-PEM-FILE",
   237  			},
   238  		},
   239  
   240  		"Attributes": {
   241  			Config: testConfig(t, map[string]interface{}{
   242  				"attributes": []map[string]interface{}{
   243  					map[string]interface{}{
   244  						"key1": []map[string]interface{}{
   245  							map[string]interface{}{
   246  								"subkey1": []map[string]interface{}{
   247  									map[string]interface{}{
   248  										"subkey2a": []interface{}{
   249  											"val1", "val2", "val3",
   250  										},
   251  										"subkey2b": []map[string]interface{}{
   252  											map[string]interface{}{
   253  												"subkey3": "value3",
   254  											},
   255  										},
   256  									},
   257  								},
   258  							},
   259  						},
   260  						"key2": "value2",
   261  					},
   262  				},
   263  				"node_name":              "nodename1",
   264  				"prevent_sudo":           true,
   265  				"run_list":               []interface{}{"cookbook::recipe"},
   266  				"secret_key_path":        "test-fixtures/encrypted_data_bag_secret",
   267  				"server_url":             "https://chef.local",
   268  				"validation_client_name": "validator",
   269  				"validation_key_path":    "test-fixtures/validator.pem",
   270  			}),
   271  
   272  			Commands: map[string]bool{
   273  				"mkdir -p " + linuxConfDir: true,
   274  			},
   275  
   276  			Uploads: map[string]string{
   277  				linuxConfDir + "/client.rb":                 defaultLinuxClientConf,
   278  				linuxConfDir + "/encrypted_data_bag_secret": "SECRET-KEY-FILE",
   279  				linuxConfDir + "/validation.pem":            "VALIDATOR-PEM-FILE",
   280  				linuxConfDir + "/first-boot.json": `{"key1":{"subkey1":{"subkey2a":["val1","val2","val3"],` +
   281  					`"subkey2b":{"subkey3":"value3"}}},"key2":"value2","run_list":["cookbook::recipe"]}`,
   282  			},
   283  		},
   284  
   285  		"Attributes JSON": {
   286  			Config: testConfig(t, map[string]interface{}{
   287  				"attributes_json": `{"key1":{"subkey1":{"subkey2a":["val1","val2","val3"],` +
   288  					`"subkey2b":{"subkey3":"value3"}}},"key2":"value2"}`,
   289  				"node_name":              "nodename1",
   290  				"prevent_sudo":           true,
   291  				"run_list":               []interface{}{"cookbook::recipe"},
   292  				"secret_key_path":        "test-fixtures/encrypted_data_bag_secret",
   293  				"server_url":             "https://chef.local",
   294  				"validation_client_name": "validator",
   295  				"validation_key_path":    "test-fixtures/validator.pem",
   296  			}),
   297  
   298  			Commands: map[string]bool{
   299  				"mkdir -p " + linuxConfDir: true,
   300  			},
   301  
   302  			Uploads: map[string]string{
   303  				linuxConfDir + "/client.rb":                 defaultLinuxClientConf,
   304  				linuxConfDir + "/encrypted_data_bag_secret": "SECRET-KEY-FILE",
   305  				linuxConfDir + "/validation.pem":            "VALIDATOR-PEM-FILE",
   306  				linuxConfDir + "/first-boot.json": `{"key1":{"subkey1":{"subkey2a":["val1","val2","val3"],` +
   307  					`"subkey2b":{"subkey3":"value3"}}},"key2":"value2","run_list":["cookbook::recipe"]}`,
   308  			},
   309  		},
   310  	}
   311  
   312  	r := new(ResourceProvisioner)
   313  	o := new(terraform.MockUIOutput)
   314  	c := new(communicator.MockCommunicator)
   315  
   316  	for k, tc := range cases {
   317  		c.Commands = tc.Commands
   318  		c.Uploads = tc.Uploads
   319  
   320  		p, err := r.decodeConfig(tc.Config)
   321  		if err != nil {
   322  			t.Fatalf("Error: %v", err)
   323  		}
   324  
   325  		p.useSudo = !p.PreventSudo
   326  
   327  		err = p.linuxCreateConfigFiles(o, c)
   328  		if err != nil {
   329  			t.Fatalf("Test %q failed: %v", k, err)
   330  		}
   331  	}
   332  }
   333  
   334  const defaultLinuxClientConf = `log_location            STDOUT
   335  chef_server_url         "https://chef.local"
   336  validation_client_name  "validator"
   337  node_name               "nodename1"`
   338  
   339  const proxyLinuxClientConf = `log_location            STDOUT
   340  chef_server_url         "https://chef.local"
   341  validation_client_name  "validator"
   342  node_name               "nodename1"
   343  
   344  http_proxy          "http://proxy.local"
   345  ENV['http_proxy'] = "http://proxy.local"
   346  ENV['HTTP_PROXY'] = "http://proxy.local"
   347  
   348  https_proxy          "https://proxy.local"
   349  ENV['https_proxy'] = "https://proxy.local"
   350  ENV['HTTPS_PROXY'] = "https://proxy.local"
   351  
   352  no_proxy          "http://local.local,https://local.local"
   353  ENV['no_proxy'] = "http://local.local,https://local.local"
   354  
   355  ssl_verify_mode  :verify_none`